Add form elements via hook_form_alter() to uc_cart_view_form

Project: 
Ubercart
Category: 
bug report
Priority: 
normal
Status: 
fixed

The default theme of the uc_cart_view_form() does not render out all form elements, just the ones specified in the theme function. This of course can be easily changed in template.php, but does not allow contrib modules an easy way to add items to this form. To resolve this, i have replaced the specifying of each form element with drupal_render($form). This will render out all elements and allow for modules to use hook_form_alter() to add items and set weights to vary the order of how they are outputted.

AttachmentSize
uc_cart-form-render.diff497 bytes

Re: Add form elements via hook_form_alter() to uc_cart_view_form

There is a problem doing this. Take uc_donation module for example. the uc_donation module adds a table field for donation amount to the item form. This field renders correctly with the donation product in the cart, but also prints below the items with the checkout and update buttons. How is this rendering twice?

Re: Re: Add form elements via hook_form_alter() to uc_cart_view_

Perhaps it's not using drupal_render() or the form reference (&$form) properly? (Haven't looked at this module.)

Re: Re: Re: Add form elements via hook_form_alter() to uc_cart_v

Double posting after checking the module... maybe try changing this?

<?php
// Insert the & on a whim.
function uc_donation_table_alter($table_id, $op, &$args = NULL) {
?>

Re: Re: Re: Re: Add form elements via hook_form_alter() to uc_ca

Nope, didn't seem to work. I've been ever so curious what the '&' sign is for, hope you can enlighten me. The changes I've made to uc_donation are now current to download. I'm really curious how to fix this.

AttachmentSize
Picture 1.png30.76 KB

Re: Re: Add form elements via hook_form_alter() to uc_cart_view_

I found the problem. Somehow when the theme_uc_cart_view_form function is processing the form, in particular the amount field for donations, it does not add #printed to the donate_price element. TAPIr is what processes this form element so I'm not sure how to remedy the issue.

Re: Re: Re: Add form elements via hook_form_alter() to uc_cart_v

Any idea as to why TAPIr doesn't mark the custom field as #printed?

Re: Re: Re: Re: Add form elements via hook_form_alter() to uc_ca

I believe that's to be handled by the Forms API automatically... it's most likely an issue w/ variables not being passed by reference properly. I hope to solve this issue in the beta phase.

Re: Re: Re: Re: Re: Add form elements via hook_form_alter() to u

Alrighty... so... I've applied your patch and believe it'll work fine.

The other issue here is that TAPIr is using module_invoke_all() to call the table_alter functions. That won't accept/pass on arguments by reference, so I'm going to write a tapir_invoke_all() function that does. I'll post it up here when it's done... I'm also going to give this module some loving and post an update to d.o.

To get a better handle on references, just check out this code:

<?php
function func1(&$arg) {
  unset(
$arg[1]);
 
func2($arg);
}

function
func2(&$arg) {
  unset(
$arg[0]);
}

function
func3($arg) {
  unset(
$arg[2]);
}

$a = array(
 
0 => 'zero',
 
1 => 'one',
 
2 => 'two',
);

func1($a);
func3($a);

print_r($a);
?>

The result when run will be this:

Array
(
    [2] => two
)

Basically, & receives a reference to the variable, so the function will use the variable itself instead of make its own local copy. Notice that func3() doesn't use the &, so the unset() affects only the copy of $a inside that function. It's somewhat akin to pointers in other languages.

Re: Re: Re: Re: Re: Re: Add form elements via hook_form_alter()

See if this version of tapir.module fixes the issue.

AttachmentSize
tapir.zip3.25 KB

Re: Re: Re: Re: Re: Re: Add form elements via hook_form_alter()

Thanks, I learned something. Sticking out tongue

I installed the tapir.module you supplied for me and that fixed it. Thanks!