简体   繁体   中英

migrating from drupal 6 to drupal 7 form_alter hook is throwing an error - Notice: Undefined index: buttons in sport_utils_form_alter()

I'm upgrading from Drupal 6 to Drupal 7 . I enabled this module after updating it.

Here's my function:

function sport_utils_form_alter($form, $form_state, $form_id) {
    if (strpos($form_id, '_node_form') !== FALSE) {
        $form['#validate'][] = 'byu_sport_utils_verify_valid_author';
        $form['#validate'][] = 'byu_sport_utils_remove_first_line_break';

        $form['top_buttons'] = $form['buttons'];
        $form['top_buttons']['#weight'] = -500;
        $form['top_buttons']['#prefix'] = $form['buttons']['#prefix'] = '<div class="button-bar">';
        $form['top_buttons']['#suffix'] = $form['buttons']['#suffix'] = '</div><div class="clear"></div>';
    }
}

It's throwing an error on this line:

$form['top_buttons'] = $form['buttons'];

I can't find out if I need to replace $form['buttons'] with something else that works in Drupal 7.

Any suggestions?

In Drupal 7 the form buttons are grouped under $form['actions'] . So you need to modify your code to support that like below.

function sport_utils_form_alter($form, $form_state, $form_id) {
    if (strpos($form_id, '_node_form') !== FALSE) {
      $form['#validate'][] = 'byu_sport_utils_verify_valid_author';
      $form['#validate'][] = 'byu_sport_utils_remove_first_line_break';

      /**
        * Copy the action buttons (submit, preview, etc ..)
        * and place them at the top of the form
        */
      if(!empty($form['actions'])) {
        $actions = element_children($form['actions'], TRUE);
        foreach($actions as $name) {
          $form['top_buttons']["$name-top"] = $form['actions'][$name];
        }
      }

      $form['top_buttons']['#weight'] = -500;
      $form['top_buttons']['#prefix'] = $form['actions']['#prefix'] = '<div class="button-bar">';
      $form['top_buttons']['#suffix'] = $form['actions']['#suffix'] = '</div><div class="clear"></div>';
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM