7

Trying to convert a module from Drupal 6 to Drupal 7 which defines its own node type in hook_node_info.

Even though hook_node_info defines has_title and has_body to TRUE, how I can get these fields to appear on the node form? Previously, I used hook_form():

function mymodule_form(&$node, $form_state) { 
  $type = node_get_types('type', $node);

  if ($type->has_title) {
    $form['title'] = array(
        '#type' => 'textfield',
        '#title' => check_plain($type->title_label),
        '#required' => TRUE,
        '#default_value' => $node->title,
        '#weight' => -25,
    );
  }

  if ($type->has_body) {
    $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
    $form['body_field']['body']['#rows'] = 5;
    $form['body_field']['body']['#description'] = 'Enter a short message to present to the user.';
  }

  return $form;
}

In D6, this worked fine for me. In Drupal 7, hook_form has changed, and I am not quite following the documentation of it.

How can I utilize the Body and Title Field in core and assign them to this node type when the module is activated?

Furthermore, how can I define a new Field type to mimic Body (Long text with summary) to something of my choosing?

4

To add the body field to a content type, you use node_add_body_field(). To add a field that is not the body field, you use field_create_field(), if the field doesn't exist, and field_create_instance() to assign an instance of the field to an entity (e.g. a node or a user object).

As example of how to use those functions, you can look at standard_install(), which is the installation hook of the standard profile, that contains the following code:

  $field = array(
    'field_name' => 'field_' . $vocabulary->machine_name, 
    'type' => 'taxonomy_term_reference',
    'cardinality' => FIELD_CARDINALITY_UNLIMITED, 
    'settings' => array(
      'allowed_values' => array(
        array(
          'vocabulary' => $vocabulary->machine_name, 
          'parent' => 0,
        ),
      ),
    ),
  );
  field_create_field($field);

  $instance = array(
    'field_name' => 'field_' . $vocabulary->machine_name, 
    'entity_type' => 'node', 
    'label' => 'Tags', 
    'bundle' => 'article', 
    'description' => $vocabulary->help, 
    'widget' => array(
      'type' => 'taxonomy_autocomplete', 
      'weight' => -4,
    ), 
    'display' => array(
      'default' => array(
        'type' => 'taxonomy_term_reference_link', 
        'weight' => 10,
      ), 
      'teaser' => array(
        'type' => 'taxonomy_term_reference_link', 
        'weight' => 10,
      ),
    ),
  );
  field_create_instance($instance);

The code is adding a taxonomy term field to the content type "Article."

1

This is a great document on Converting your module from Drupal 6.x to 7.x

In there is states

node body field instances are now handled exclusively by field API

When defining a node type or creating one with node_type_save(), the 'has_body' and 'body_label' keys have been deprecated.

To add a body field, you can instead call node_add_body_field($type); which will add the field instance to that node type. See blog.install and forum.install for examples.

Check out forum.install & blog.install

So the place where you want to do this is in mymodule.install, and either inside the mymodule_install() or mymodule_enable().

  • also look at drupal.org/update/modules/6/7#body_teaser_fields – iStryker Jul 15 '11 at 15:45
  • to mimic a field like the body, take the code from node_add_body_field and put in it a private function – iStryker Jul 15 '11 at 16:17
  • There is no simple Field API to create and assign a field to a node type? – Kevin Jul 15 '11 at 17:37
  • I don't think there is a simpler way, such as inside mymodule_form(). Drupal 7 uses fields instances, and from my understanding, they have to be added to the database before you can use them. Thats why the forum and blog module uses hook_install() & hook_enable(). Side Note: You can add them afterwards to the type using field_create_instance() & field_create_field() in a private function, but why would you. – iStryker Jul 15 '11 at 18:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.