如何使用带有表单Api的自定义模块在drupal中添加内容

时间:2013-10-29 14:48:55

标签: php forms drupal drupal-7

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.'));
    }

&GT?; 我是drupal的新手,我需要知道如何使用Form Api

在drupal中实现节点保存
  1. 字段在提交中验证我需要将表单结果保存在Employee
  2. 类型的内容页面中
  3. 我创建了员工内容类型 以及字段如下
  4. 名 电话 电子邮件 地址 性别 我想将表单值保存在员工类型已经创建的内容类型的内容中 PLZ帮助我,我是drupal的新手

1 个答案:

答案 0 :(得分: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/