0

hook _menu 实现

<?php
    function my_module_menu() {
      $items = array();
      $items['my_module/form'] = array(
        'title' => t('My form'),
        'page callback' => 'my_module_form',
        'access arguments' => array('access content'),
        'description' => t('My form'),
        'type' => MENU_CALLBACK,
      );
      return $items;
    }

    function my_module_form() {
      return drupal_get_form('my_module_my_form');
    }
    // created the custom fields using form api
    function my_module_my_form($form_state) {
      $form['name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name'),
         );
      $form['phone']= array(
        '#type' => 'textfield',
        '#title' => t('Phone Number'),   
        '#maxlength' => 10, 
        '#size' => 10,
        );
      $form['email']= array(
      '#type' => 'textfield',
      '#title' => 'Email',
      '#rules' => array(
        'email',
        ),
       );

      // New form field added to permit entry of year of birth.
      // The data entered into this field will be validated with
      // the default validation function.
      $form['address'] = array(
        '#type' => 'textfield',
        '#title' => "address",
         );
     $form['choice'] = array(
    '#type' => 'radios',
    '#title' => t('Gender'),
    '#options' => array(
    t('Male'),
    t('Female'),
    t('Transgender'),
    )
    );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
        '#submit' => array('my_module_my_form_submit'),
      );
      return $form;
    }
    Done some validation for the form fields 
    function my_module_my_form_validate($form, &$form_state) {
      $regex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'; 
      $phone = $form_state['values']['phone'];
      $name = $form_state['values']['name'];
      $email = $form_state['values']['email'];
      $address = $form_state['values']['address'];
      $choice = $form_state['values']['choice'];

      if (!$name) {
            form_set_error('name', 'Please enter your name.');
        } 
      if (!$phone) {
            form_set_error('phone', 'Please enter your Phone Number.');
        }
      if (!$email) {
            form_set_error('email', 'Please enter your Email.');
        }
    else if (!preg_match($regex, $email)) {
         form_set_error('Email', 'Enter correct Email');
    } 
      if (!$address) {
            form_set_error('address', 'Please enter your Address.');
        }
        if (!isset($choice) )
        {
            form_set_error('choice', 'Please enter your Gender.');
        }
       if (!preg_match('/^[1-9][0-9]*$/',$phone)) {
            form_set_error('phone', 'Enter correct phone number');
      }  

    }
    function my_module_my_form_submit($form,&$form_state) {
        drupal_set_message(t('The form has been submitted.'));
    }

?> 我是 drupal 的新手,我需要知道如何使用 Form Api 在 drupal 中实现节点保存

  1. 这些字段在提交中验证我需要将表单结果保存在 Employee 类型的内容页面中
  2. 我创建了员工内容类型,字段如下

姓名电话电子邮件地址性别我想将表单值保存在员工类型的内容中,其中员工是已经创建的内容类型请帮助我,我是drupal的新手

4

1 回答 1

0

我是drupal的新手,我需要知道如何使用Form Api在drupal中实现节点保存

只需按原样使用 node_save() 即可。您必须构建节点对象并调用 node_save($new_node); 与此类似

$node = new stdClass(); // Create a new node object
$node->type = "article";// node's content type
node_object_prepare($node); // Set some default values
$node->title    = "Node title";
$node->language = LANGUAGE_NONE; 
$node->uid = 1; 
$node->body[$node->language][0]['value'] = 'body text';
node_save($node);

您可以使用“devel”模块检查节点对象结构(在节点页面上提供“devel”选项卡)。此外,您可以阅读更详细的指南,例如 http://fooninja.net/2011/04/13/guide-to-programmatic-node-creation-in-drupal-7/

于 2013-10-30T07:54:13.787 回答