4

I'm trying to use hook_form_FORM_ID_alter() in my theme's template.php file to change the 'Remove' button in the Commerce add to cart form to an image button. To do this I'm using the following code:

function tempestra_form_views_form_commerce_cart_form_default_alter(&$form, &$form_state) {
  foreach($form['edit_delete'] as $item) {
    if($item['#type'] == 'submit') {
      $item['#type'] = 'image_button';
      $item['#src'] = base_path() . drupal_get_path('theme', 'tempestra') . '/images/search.png';
      unset($item['#name']);
      dpm($item);
    }
  }
}

When I look in the array printed by dpm() everything is as it should be, however the HTML for the image button isn't printed.

Thanks in advance.

3

Simple mistake, you don't change $form but only $item if you did

foreach($form['edit_delete'] as &$item) {

or

foreach($form['edit_delete'] as $key => $item) {
  ...
  $form['edit_delete'][$key] = $item;
}

it should work.

  • Yeah that now works perfectly, I hadn't thought about it that way or even realised you could use & in a foreach statement. Thank you! – splatio Jan 26 '12 at 14:33
1
function MYMODULE_form_alter(&$form, &$form_state, $form_id) { 
  if (strpos($form_id, 'commerce_cart_add_to_cart_form') !== FALSE) {
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Buy now'),
    );
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.