【问题标题】:Drupal 7 hook_form_alter and submitting the formDrupal 7 hook_form_alter 并提交表单
【发布时间】:2016-06-13 20:57:06
【问题描述】:

我添加了一些自定义字段,以便在有人注册到网站但我想更改表单上的一个字段时获得更多信息。我创建了一个具有 hook_form_alter 函数的模块,以便我可以更改注册表单的 field_first_name。这是代码

function user_registration_form_alter(&$form, &$form_state, $form_id){
if ($form_id == 'user_register_form'){

    $form['field_first_name'] = array(
        '#type' => 'textfield',
        '#title' => t('First Name'),
        '#description' => t('Enter your first name.'),
        '#maxlength' => 255,
        '#required' => TRUE,
        );



}
if (isset($form['actions']['submit'])) {
 $form['actions']['submit']['#submit'][] ='user_registration_form_submit';
   }

}

我还创建了一个处理表单提交的函数。

function user_registration_form_submit(&$form, &$form_state)
{


$fe_id = db_insert('field_revision_field_first_name')
    ->fields(array(
        'field_first_name_value' => $form_state['value']['field_first_name'],
        ))->execute();
    drupal_set_message(t('your form entry has been added'));
}

我的问题是,当我提交表单并检查用户详细信息时。我发现数据库中不存在名字详细信息,或者当我以管理员身份登录并单击“人员”链接时。我发现除了我要更改的名字字段外,所有信息都已提交。我也试过不使用表单提交功能提交表单,但还是不行。

如果我添加 form_submit 函数,我会收到以下错误消息

注意:未定义的索引:user_registration_form_submit() 中的值(/var/www/html/lite/sites/all/modules/user_regestration/user_registration.module 的第 37 行)。

PDOException: SQLSTATE[HY000]: 一般错误: 1364 Field 'entity_id' 没有默认值:INSERT INTO {field_revision_field_first_name}(field_first_name_value)值 (:db_insert_placeholder_0);数组( [:db_insert_placeholder_0] => )在 user_registration_form_submit()(第 38 行 /var/www/html/lite/sites/all/modules/user_regestration/user_registration.module)。

这是我的代码的第 37 和 38 行

'field_first_name_value' => $form_state['value']['field_first_name'],
))->execute();

我先在本地主机上创建模块,然后再将其推送到实时网站

【问题讨论】:

  • 我在第 37 行打错字了。它应该是“values”而不是“value”,但现在我收到以下错误......“PDOException: SQLSTATE[ HY000]:一般错误:1364 字段 'entity_id' 没有默认值:INSERT INTO {field_revision_field_first_name} (field_first_name_value) VALUES (:db_insert_placeholder_0); Array ([:db_insert_placeholder_0] => john) in user_registration_form_submit()(第 38 行/var/www/html/lite/sites/all/modules/user_regestration/user_registration.module)‌​"

标签: php mysql forms drupal


【解决方案1】:

首先,它是价值而不是价值。

您为什么不使用提供此功能的模块,例如 profile2

用户不是在你的提交处理程序中创建的,所以你要么保存它:

function foo_user_register_form_submit($form, &$form_state){
    $edit = array(
          'name' => $form_state['values']['name'], 
          'pass' => user_password(),
          'mail' => $form_state['values']['mail'],
          'init' => $form_state['values']['mail'], 
          'status' => 1, 
          'access' => REQUEST_TIME,
          'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_first_name']))),
    );
    user_save(drupal_anonymous_user(), $edit);
}

更多详情请见docs

或者您可以尝试在 form_alter 中添加自定义提交处理程序,该处理程序将在创建用户后触发:

$form['#submit'][] = 'your_custom_submit_handler_function';

在那里应该已经创建了用户。所以:

function your_custom_submit_handler_function(&$form, &$form_state)
    {
        $fe_id = db_insert('field_revision_field_first_name')
        ->fields(array(
            'field_first_name_value' => $form_state['values']['field_first_name'],
            ))->execute();
        drupal_set_message(t('your form entry has been added'));
    }

但请记住,这种自定义插入可能需要更多 代码,这里只添加修订条目..

【讨论】:

    【解决方案2】:

    您没有传入 entity_id,并且您的数据库架构没有定义默认值。数据库需要一个,所以它失败了,插入失败 - 这是你的第 37 和 38 行。它与你的名字部分完全无关。

    基本上,Drupal(技术上是 MySQL,或任何您的数据库后端)不知道您尝试将该字段与该字段关联的什么实体。

    请查看您实际将信息放入的表,并确保架构具有默认设置

    类似:

    db_insert('field_revision_field_first_name')
            ->fields(array(
                'field_first_name_value' => $form_state['values']['field_first_name'],
    'entity_id' => 1,
                ))->execute();
    

    可能会更适合您。可能还有其他必填字段。我建议查看您的数据库架构。

    您也不应该为此定义自定义表单 - 您可以向用户添加字段,并且还有 profile2 作为另一个海报提到的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 1970-01-01
      相关资源
      最近更新 更多