5

I am defining a new entity following the steps related here: http://www.istos.it/en/blog/drupal-entities/drupal-entities-part-3-programming-hello-drupal-entity

My question is how I can add a field programmatically to the Entity when I define it. I have read some links but I can't get it work.

My code:

function generador_examenes_entity_info() {
  $return['question_section'] = array(
    'label' => 'Apartado',
    'controller class' => 'SectionController',
    'base table' => 'question_section',
    'uri callback' => 'section_uri',
    'fieldable' => TRUE,
    'entity_keys' => array(
      'id' => 'sid',
    ),
    'static cache' => TRUE,
    'bundles' => array(
      'question_section'=> array(
        'label' => 'Question Section',
        'admin' => array(
          'path' => 'admin/structure/question-section/manage',
          'access arguments' => array('administer'),
        ),
      ),
    ),
    'view modes' => array(
      'full' => array(
        'label' => t('Full Content'),
        'custom settings' => FALSE,
      )
    ),
    'access callback' => 'section_access',
  );
  return $return;
}

Thanks

  • You could considering use features depending on your requirements – Enxebre Sep 26 '13 at 18:32
16

You'll need to define a field and an field instance in your module's .install file. The function can either be in hook_enable() or hook_install(). The field will provide the name of the field and some default settings. The field instance can be seen as the link between the field and the entity you want to place the field on.

The format of the settings differs per field type (a text field has a maximum length, an entity reference field can define the selectable bundle(s), etc). Often the available settings are explained in the module's hook_field_info implementation. For core fields these can be found on the following page: 7 functions implement hook_field_info(). For non-core fields the settings are likely to be explained in the hook_field_info implementation as well, for example entityreference_field_info().

The process Drupal follows when registering a field is as follows:

The field is registered in the field api first, with it's default settings (the settings as defined in hook_field_info()). But your module can override them in the settings array of your field_create_field function. So that the field with your name will have by default your settings. Other values such as cardinality (how many values can the field hold) and translatability can be defined as well. See the detailed information on field_create_field() for all values.

From there the field is attached to the entity and it's bundle by means of a field instance. This instance defines which field should be attached to which entity and bundle. Here the settings can be overridden once more for this specific fields implementation on this specific entity. You can also select the widget here; the way the field is to be selected. Modules define their widgets in hook_field_widget_info(), available widgets for core fields can be found here: 7 functions implement hook_field_widget_info(). For a textfield you would use the widget text_textfield, for an image you can use image_image, for a non-core module you'll need to lookup the available widgets yourself in the hook_field_widget_info() implementation. For a full lis tof all properties you can set on field instances, see field_create_instance().

Image field example

function mymodule_enable() {
  // Check if our field is not already created.
  if (!field_info_field('my_image')) {
    $field = array(
      'field_name' => 'my_image',
      'type' => 'image',
      'locked' => TRUE, // Settings can not be changed
      'settings' => array(
        'no_ui' => TRUE, // Field is not visible in field UI and can only be instantiated programmatically.
      ),
    );
    field_create_field($field);

    // Create the instance on the bundle.
    $instance = array(
      'field_name' => 'my_image',
      'entity_type' => 'my_entity',
      'bundle' => 'my_entity_bundle', // If your entity does not have bundles, this is the same as the entity type.
      'label' => 'Featured image',
      'required' => TRUE, // Field must have a value.
      'widget' => array(
        'type' => 'image_image',
      ),
    );
    field_create_instance($instance);
  }
}

Entity reference example

function mymodule_enable() {

  // Create an entityreference field.
  $field = array(
    'field_name' => 'my_entityreference',
    'type' => 'entityreference',
    'settings' => array(
      'target_type' => 'my_other_entitytype',
    ),
    'cardinality' => 1, // Field may only have one value.
    'translatable' => FALSE,
  );
  field_create_field($field);

  $instance = array(
    'field_name' => 'my_entityreference',
    'entity_type' => 'my_entitytype',
    'bundle' => 'my_entitytype_bundle',
    'label' => 'Reference to my_other_entitytype',
    'required' => TRUE,
    'widget' => array(
      'type' => 'options_select',
    ),
  );
  field_create_instance($instance);
}
  • 2
    Perfect answer (+1):I would just add that I prefer to use hook_install to create fields (and hook_uninstall to delete them, in order to don't touch DB/use CPU when enable/disable)... and, above all, that "drush field-info types" could be very useful to know available fields, widgets and default widgets. – Pierpaolo Cira Sep 27 '16 at 9:40

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.