1

I have created a content type in Drupal 7 using the following code.

function module_node_info() {
  return array(
    'module' => array(
      'name' => t('module'),
      'base' => 'module',
      'module' => 'module',
      'description' => t("You can add a content for module from here."),
    ),
  );
}

This seems to be working fine: I see the content type and I can add fields. When I create fields for this content type through code, the fields show up when I click on 'add content' but they are not showing in my content type. When I add nodes of that specific content type, the nodes are not showing and the fields are empty when I edit them.

I miss something, but I have no idea what that could be. I use the following code to create the fields.

function module_form($node, &$form_state) {
  $form = array();

  $form['module_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Enter the name of your module'),
    '#required' => TRUE, 
    '#description' => t('This name will be visible on the website'),
  );

  return $form;
}

2 Answers 2

1

The second piece of code creates a field in the Form API. This means that you do that when you create a custom form with several fields in it and then you have to handle its submission in your custom way. If this is what you want to do, fine. In this case you would need to add your custom form in your hook_menu().

Instead, from what I can understand with your question, you would like to add a field of the Field API to tour custom node type. For this matter you would need to use the field_create_field() and field_create_instance() functions. The excellent examples module provides very neat examples of how to accomplish that.

0

Adding it the form isn't enough, you need to register your custom fields in the .module and create your fields in the module.install. You'll also need to create the widget display.

Tutorial here: http://clikfocus.com/blog/how-set-custom-field-type-using-drupal-7-fields-api

And review the schema API here: https://api.drupal.org/api/drupal/includes%21database%21schema.inc/group/schemaapi/7

2
  • Can I create the same effect with this link: api.drupal.org/api/drupal/modules!field!field.api.php/group/… I have all here that I need? Thanks.
    – Appel
    Commented Feb 22, 2014 at 22:35
  • @dmd That is only one part of the field API: the one that allows to define new datatypes (e.g. (integer, string, date) for the fields. It doesn't allow to define new fields.
    – avpaderno
    Commented Feb 23, 2014 at 16:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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