Hello,

I am trying to replace the submit button on ubercart's 7.x 3.0-beta3 uc_product_add_to_cart_form with an image and I keep getting the following error:

Notice: Undefined index: nid

I am using hook_form_alter as shown below:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  //dsm($form);
  $node = $form['nid']['#value'];
  if ($form_id == 'uc_product_add_to_cart_form_'.$node) {
    $form['actions']['submit'] = array(
      '#type' => 'image_button',
      '#value' => t('Add to Cart'),
      '#src' => drupal_get_path('module', 'mymodule') .'/images/addtoshoppingcart.gif',
    );
  }
}

The code does work except for the error. I used dsm($form) to print the $form array and $form['nid'['#value'] is present. My thought is that maybe this function is being called twice; where the first time $form['nid'['#value'] is not present. If this is the case, is there another way to obtain the node id from the form? Any insight would be greatly appreciated.

Thanks,
Paul

Comments

nevets’s picture

Not all forms are node forms, you could replace

  $node = $form['nid']['#value'];

with

if ( !empty($form['nid']) ) {
  $node = $form['nid']['#value'];
}
else {
  $node = 0;
}
pnigro’s picture

It worked!!!! Thank you so much nevets. You saved me from pulling out my hair. lol