2

I have just started to use Drupal and I'm trying to add programmatically field in the .install file.

 $field = array(
   'field_name' => 'title',
   'cardinality' => 1,
   'type' => 'text',
   'settings'    => array(
     'max_length' => 1024,
   ),
 );

 field_create_field($field);


 $instance = array(
   'field_name'  => 'title',
   'label' => $t('Title'),
   'type' => 'text',
   'widget' => array(
     'type'    => 'text_textfield',
     'weight' => 1.0,
    ),
   'display' => array(
     'default' => array(
       'label' => $t('label'),
       'type' => 'text',
      ),
    ),
 );

$instance['entity_type'] = 'node';
$instance['bundle'] = 'mytype';
field_create_instance($instance);

The field is added correctly but it is disabled in the Manage Display. How can I change the region column from 'disabled' to 'content'? I tried to add 'region' => 'content' in the instance array but it is not working and I cannot find a good example or documentation about the problem.

Thanks.

2

I couldn't find any concrete documentation either. However, I'm going out on a limb here and not 100% certain but, I think you just need to specify a correct formatter and the settings since it works that way in the UI.

$instance = array(
   'field_name'  => 'title',
   'label' => $t('Title'),
   'type' => 'text',
   'widget' => array(
     'type'    => 'text_textfield',
     'weight' => 1.0,
    ),
   'display' => array(
     'default' => array(
       'label' => $t('label'),
       'type' => 'text', // <-- invalid formatter
      ),
    ),
 );

text is not a valid formatter type. It should either be text_default or text_plain or text_trimmed or text_summary_or_trimmed or hidden (which hides the field from display).

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.