简体   繁体   English

从drupal 6迁移到drupal 7 form_alter挂钩会引发错误-注意:未定义索引:sport_utils_form_alter()中的按钮

[英]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 . 我正在从Drupal 6升级到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. 我不知道是否需要用在Drupal 7中可用的其他东西替换$form['buttons']

Any suggestions? 有什么建议么?

In Drupal 7 the form buttons are grouped under $form['actions'] . 在Drupal 7中,表单按钮被分组在$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>';
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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