18 replies [Last post]
jrgoolsby's picture
Offline
Joined: 08/21/2008
Juice: 32
Was this information Helpful?

I have searched for days and I just cant seem to understand / figure out what needs to be
done.

I simply want to rearrange or move the attribute drop downs (and labels) to a horizontal layout as opposed to vertical.

Through CSS I can get the buttons and attributes to align like desired, but the labels for the attribute drop downs are still in their original space. Since no class or id tag is passed to the label (only a for tage) when they are generated if I use CSS for labels they do not move independently.

I imagine that it can be done via hook_alter_form, but the documentation just seems lacking as it does not reference if you want to work with the add to cart section. It seems more if you want to simply edit one (or only a few forms) not for every product that you have listed.

Can someone help me either work with hook_alter_form and get the correct format to edit all of my product's attributes and buttons

OR

help me simply remove the labels from displaying when the node is generated?

I assume that the 2nd would be the easiest option, but obviously I only want the labels not to show up on the product pages (attributes) and not for every page with a form.

Any help is greatly appreciated! Thanks in advance.

jrgoolsby's picture
Offline
Joined: 08/21/2008
Juice: 32
Re: Add to Cart / hook_alter_form / rearrange

Also, the drop down attributes default to "Please Select" even though they are selected as a default. Any suggestions on this matter?

Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15438
Re: Re: Add to Cart / hook_alter_form / rearrange

I'm pretty sure the "Please select" is coming from them being required attributes... UC wants to make sure the customer makes a conscious choice, so it'll ignore the default and add that "Please select" option.

jrgoolsby's picture
Offline
Joined: 08/21/2008
Juice: 32
Re: Re: Re: Add to Cart / hook_alter_form / rearrange

OK, thanks that cleared up that issue. Such a simple solution sometimes.

Any suggestions for the 1st posting?

TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Add to Cart / hook_alter_form / rearrange

This will remove the labels ...

<?php
/**
* Implementation of hook_form_alter()
*/
function mymodule_form_alter($form_id, &$form) {
  if (
substr($form_id, 0, 27) == 'uc_product_add_to_cart_form') {
   
$form['qty']['#title'] = "";
    foreach (
$form['attributes'] as &$element) {
      if (
is_array($element)) {
       
$element['#title'] = "";
      }
    }
  }
}
?>

A better way to attack the problem, one I haven't tried, might be to override the default theme Drupal uses for the select box. This is defined in theme_select. See the FAQ for how to override a theme function. Basically, you can make Drupal output exactly the markup you want to see when a select box appears - that includes adding a CSS class or id to the label tag.

EDIT: Looking at the Drupal source, it's actually theme_form_element that creates the label tags, so that's the function you'll want to override.

<tr>.
jrgoolsby's picture
Offline
Joined: 08/21/2008
Juice: 32
Re: Re: Add to Cart / hook_alter_form / rearrange

Thanks. Do I need to stick this in a custom module? or right in the product node?

TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Re: Add to Cart / hook_alter_form / rearrange

Custom module. I explained in more detail how to write a custom module to do a hook_form_alter in this post: http://www.ubercart.org/comment/8749/Re-Removing-SKU-product-edit-page

<tr>.
jrgoolsby's picture
Offline
Joined: 08/21/2008
Juice: 32
Re: Re: Re: Re: Add to Cart / hook_alter_form / rearrange

Alright will do, thanks again.

The code snippet looks pretty straight forward. I didn't even think about substring for the uc_product_add_to_cart_form.

Through this I should be able to separate individual items within the form and pass them back to the page correct?
Any examples how to pass specific divs for each element? echo or such...

jrgoolsby's picture
Offline
Joined: 08/21/2008
Juice: 32
Re: Re: Re: Re: Re: Add to Cart / hook_alter_form / rearrange

It looks like it is throwing an error on: foreach ($form['attributes'] as $element) {

Its giving me :
warning: Invalid argument supplied for foreach() in /test/sites/all/modules/custom/my_module.module on line 8.

<?php
/**
* Implementation of hook_form_alter()
*/
function my_module_form_alter($form_id, &$form) {
  if (
substr($form_id, 0, 27) == 'uc_product_add_to_cart_form') {
   
$form['qty']['#title'] = "";
    foreach (
$form['attributes'] as &$element) {
      if (
is_array($element)) {
       
$element['#title'] = "";
      }
    }
  }
}

?>

EDIT:
I believe that the form id uses hypens instead of dashes
(substr($form_id, 0, 27) == 'uc-product-add-to-cart-form'
but still no luck...

Thanks in advance.

TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Re: Re: Re: Re: Add to Cart / hook_alter_form / rearrang

That's actually code extracted from a live module, so I know it works. I'll bet you have PHP 4, since I don't think PHP 4 supports the declaration of $element as a reference ... ?

<tr>.
jrgoolsby's picture
Offline
Joined: 08/21/2008
Juice: 32
Re: Re: Re: Re: Re: Re: Re: Add to Cart / hook_alter_form / rear

Actually i believe we have PHP version 5...

TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Re: Re: Re: Re: Re: Re: Add to Cart / hook_alter_form /

You can try this alternate implementation which doesn't use the reference. If this doesn't work, then I would suspect you have something wrong with how you made it into a module.

<?php
/**
* Implementation of hook_form_alter()
*/
function mymodule_form_alter($form_id, &$form) {
  if (
substr($form_id, 0, 27) == 'uc_product_add_to_cart_form') {
   
$form['qty']['#title'] = "";
    foreach (
$form['attributes'] as $key => $value) {
      if (
is_array($value)) {
       
$form['attributes'][$key]['#title'] = "";
      }
    }
  }
}
?>
<tr>.
jrgoolsby's picture
Offline
Joined: 08/21/2008
Juice: 32
still having issues... this

still having issues...

this is what is in my my_module.info file:

; $Id$
name = My Module
description = My customizations to Ubercart
package = Custom

Module is enabled via admin panel.
Caching is disabled.

I get this error when i use this in my my_module.module:
Error:
warning: Invalid argument supplied for foreach() in public_html/test/sites/all/modules/custom/my_module.module on line 8

<?php
/**
* Implementation of hook_form_alter()
*/
function my_module_form_alter($form_id, &$form) {
  if (
substr($form_id, 0, 27) == 'uc_product_add_to_cart_form') {
   
$form['qty']['#title'] = "";
    foreach (
$form['attributes'] as $key => $value) {
      if (
is_array($value)) {
       
$form['attributes'][$key]['#title'] = "";
      }
    }
  }
}
?>

And no error when I use this in my my_module.module

<?php
/**
* Implementation of hook_form_alter()
*/
function my_module_form_alter($form_id, &$form) {
  if (
substr($form_id, 0, 27) == 'uc-product-add-to-cart-form') {
   
$form['qty']['#title'] = "";
    foreach (
$form['attributes'] as $key => $value) {
      if (
is_array($value)) {
       
$form['attributes'][$key]['#title'] = "";
      }
    }
  }
}
?>

but alas neither removes the labels....

from source:

<input type="hidden" name="form_id" id="edit-uc-product-add-to-cart-form-67" value="uc_product_add_to_cart_form_67"  />
TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: still having issues...

In my_module.module, do you have the <?php tag at the beginning? And NO ?> tag at the end? Anything else in there?

Check to make sure you're using real ASCII apostrophes and quotation marks in your code, not "smart" quotes.

The $form_id in my code, using underscores and not hyphens, is correct.

<tr>.
jrgoolsby's picture
Offline
Joined: 08/21/2008
Juice: 32
Re: Re: still having issues...

Everything is identical to as you say.

no extra spaces... only the code nothing else.
I try it with both versions of the code (the one for PHP 4 and PHP 5) and both result in the same error.

Does it matter what drupal version I am using? I am on 5, is this only for 6?

TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Re: still having issues...

Please zip up your module code and attach it to your post, so I can try it and see what's wrong.

This is Drupal 5.

<tr>.
jrgoolsby's picture
Offline
Joined: 08/21/2008
Juice: 32
Re: Re: Re: Re: still having issues...

Attached. Thanks again for all your help.

AttachmentSize
custom.zip 517 bytes
TR
TR's picture
Online
Bug FinderFAQ ModeratorGetting busy with the Ubercode.
Joined: 11/05/2007
Juice: 3424
Re: Re: Re: Re: Re: still having issues...

Interesting problem... here's the solution.

Attributes are added to the uc_product_add_to_cart_form by uc_attributes_form_alter(). If my_module is loaded BEFORE uc_attributes, my_module_form_alter() will not find any $form['attributes'] to alter! To fix this, we need to ensure my_module gets loaded AFTER uc_attributes. This can be done by putting the following code in my_module.install (keep the <?php tag at the beginning, delete the ?> tag at the end). You will have to disable then uninstall my_module (http://www.ubercart.org/faq/5259) then re-enable it in order to force the install() function to run.

<?php
/**
* Implementation of hook_install()
*/
function my_module_install() {
 
// Attributes are added to the uc_product_add_to_cart_form by
  // uc_attributes_form_alter(),  so we need to ensure this module gets
  // loaded AFTER uc_attributes
 
$sql    = "SELECT weight FROM {system} WHERE name = 'uc_attributes'";
 
$weight = (int) db_result(db_query($sql));
 
$sql    = "UPDATE {system} SET weight = %d WHERE name = 'my_module'";
 
db_query($sql, $weight+1);
}
?>
<tr>.
kerunt's picture
Offline
Bug Finder
Joined: 05/05/2008
Juice: 158
TR wrote: <?php   
TR wrote:
<?php
   
foreach ($form['attributes'] as &$element)
?>

Whoa, what do we have here? A variable reference in a loop? That's something I didn't realize could be done - I like!