0

我在一个自定义模块中创建了一个表单,在一个 drupal 7 项目中,我需要将值插入到一个名为“玩家”的自定义内容类型中

这是我的表格:

function custom_module_reg_form($form, $form_state){
  $form['first_name'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('First Name')),
  );

  $form['last_name'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('Last Name')),
  );
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('Email Address')),
  );
  $form['state'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('State')),
  );
  $form['zip_code'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('Zip Code')),
  );
  $form['phone_number'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('Phone Number')),
  );
  $form['password'] = array(
    '#type' => 'password',
    '#attributes' => array('placeholder' => t('Password')),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Register',
  );

  return $form;
}

这是提交功能,但出现错误:

function custom_module_reg_form_submit($form, $form_state){
     $first_name = $form_state['values']['first_name'];
      $last_name = $form_state['values']['last_name'];
      $email_address = $form_state['values']['email_address'];
      $state = $form_state['values']['state'];
      $zip_code = $form_state['values']['zip_code'];
      $phone_number = $form_state['values']['phone_number'];
      $password = encrypt($form_state['values']['password']);

      $nid = db_insert('players')->fields(array(
        'first_name' => $first_name,
        'last_name' => $last_name,
        'email_address' => $email_address,
        'state' => $state,
        'zip_code' => $zip_code,
        'phone_number' => $phone_number,
        'password' => $password,
        'created' => REQUEST_TIME
      ))->execute();

      // Save new node
      $node = new stdClass();
      // Set node title
      $node->title = $email_address;
      // set node type ex: article etc
      $node->type = "players";
      // set node language
      $node->language = LANGUAGE_NONE;
      //(1 or 0): published or not
      $node->status = 0;
      //(1 or 0): promoted to front page
      $node->promote = 0;
      node_object_prepare($node);
      node_save($node);
}

我正在关注一个示例,我在日志中看到该表不正确,但我找不到其他任何给出示例的地方。我究竟做错了什么?为以编程方式从表单插入创建自定义表是否更好?谢谢,请告诉我。

4

1 回答 1

1

在 Drupal 7 中以编程方式创建节点有两种常用方法:不使用或使用 Entity API 贡献模块。

没有实体 API

$node = new stdClass();
$node->type = "players";
$node->uid = $user->uid; // makes sense to have the global $user the owner of the node
$node->language = LANGUAGE_NONE;
$node->status = 0;
$node->promote = 0;

$node->title = $email_address;
// NB: if you created the field in Drupal's UI -- it will be named "field_first_name", not "first_name"
$node->field_first_name[$node->language][]['value'] = $first_name;
// ...
$node = node_submit($node);
node_save($node);

使用实体 API(现在更常见)

// Use the Entity API to create a new object
$values = array(
  'type' => 'players',
  'uid' => $user->uid,
  'status' => 1,
  'promote' => 0,
);
$entity = entity_create('node', $values);

// Then create an entity_metadata_wrapper around the new entity.
$wrapper = entity_metadata_wrapper('node', $entity);

// Now assign values through the wrapper.
$wrapper->title->set($email_address);
$wrapper->field_first_name->set($first_name);
// ...

// Finally save the node.
$wrapper->save();

无论您选择哪种方式,您都不需要db_insert('players'). 它实际上是行不通的,因为 Drupal 7 没有将实体存储在单个数据库表中。

可以在此处找到有关在代码中创建节点的更多信息。

于 2019-08-14T15:24:49.757 回答