0

我正在尝试创建我的第一个 drupal 模块,该模块创建一个简单的节点类型 wcNames。此节点类型有两个字段,名字和姓氏。我可以让我的模块为节点类型创建数据库表并将其添加到 node_type 表中,但是当我打开节点/添加/wc-name 字段时,名字和姓氏不会出现。下面是我的代码。请建议。

<?php
// wc_name.install file 
/*
 * hook_schema Implementation: Creates the DB and it's fields
 */
function wc_name_schema(){
    $schema['wc_name_names'] = array(
    'fields' => array(
      'nmid'              => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
      'firstname'        => array('type' => 'varchar', 'length' => 50, 'not null' => FALSE),
      'lastname'         => array('type' => 'varchar', 'length' => 50, 'not null' => FALSE),
    ),
    'primary key'        => array('nmid'),
    'unique keys'        => array(),
    'indexes' => array(
      'nmid'       => array('nmid'),
    ),
  );
    return $schema;
}

//function wc_name_install(){ } // Use this function to set variables for the module. 

function wc_name_uninstall(){ 
    //unset variables if any set 

   // Delete all wc_name nodes
  db_delete('node')->condition('type', 'wc_name')->execute();

  // Delete all wc_name tables
  db_drop_table('wc_name_names');
}
?>

<?php
// wc_name.module
/*
 * Implementation of _node_info(): define node
 */
function wc_name_node_info(){
    return array(
    'wc_name' => array(
      'name' => t('wcName'),
      'base' => 'wc_name',
      'base' => 'page',
      'description' => t("A smaple module to save names."),
      'has_title' => TRUE,
      'title_label' => t('Title'),
      'has_body' => FALSE,
    )
  );

}

function wc_name_menu() {
  $items = array();
  $items['wc_name'] = array(
    'title' => 'wcNames',
    'page callback' => 'wc_name',
//   'access arguments' => array('access wcNames'),
  );
}
?>

<?php
// wc_name_form.inc 
function wc_name_form($node, &$form_state){

    // Add fields
    $form['first_name'] = array(
    '#type' => 'textfield',
    '#title' => t('First name'),
    '#required' => FALSE,
    '#default_value' => $node->wc_name['firstname'],
  );

    $form['last_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Last name'),
    '#required' => FALSE,
    '#default_value' => $node->wc_name['lastname'],
  );
    return $form;
}
?>

我错过了什么?

4

1 回答 1

0

尝试这个:

function createTextFieldByName($type, $fieldname, $fieldlabel)
{
    $field = field_info_field($fieldname);
    if (empty($field)) {
        $field = array(
            'field_name' => $fieldname, 
            'type' => 'text', 
            'entity_types' => array('node'), 
            'translatable' => TRUE,
        );
        $field = field_create_field($field);
    }

    $instance = field_info_instance('node', $fieldname, $type);
    if (empty($instance)) {
        $instance = array(
      'field_name' => $fieldname, 
      'entity_type' => 'node', 
      'bundle' => $type, 
      'label' => $fieldlabel, 
        );
        $instance = field_create_instance($instance);
        return $instance;
    }

    return null;
}

$type =
array(
      'type' => 'MY_TYPE', 
      'name' => t('NAME'), 
      'base' => 'node_content', 
      'description' => t("DESCRIPTION"), 
      'custom' => 1, 
      'modified' => 1, 
      'locked' => 0,
);

$type = node_type_set_defaults($type);
node_type_save($type);

createTextFieldByName($type,'firstname','First name');
createTextFieldByName($type,'lastname','Last name');
于 2013-02-18T10:24:15.023 回答