Skip to main content

Introducing the Drupal Extrafield Settings Module

Apr 20 '17

Drupal’s Extra Fields are a powerful tool for developers working with Entities. These non-editable fields allow developers to expose properties of their entities, and even properties of entities created by other modules, to the Field UI allowing site-builders to place the properties on the content creation form or in view modes on the Manage Display tab. There are times however when it’s helpful to allow more control of how these pseudo-fields are displayed when the content is rendered. We decided to create the Extrafield Settings module to give developers the ability to create settings forms for their extra fields that work in much the same way as formatter settings created with hook_field_formatter_settings_form()

To create a settings form, a developer only needs to define a `settings callback` property in their hook_field_extra_fields() definition that will return an array of renderable Form API components. These settings are stored with the extra field’s visibility setting and are easily accessed when the entity content is rendered allowing extra fields to react to the varying needs of different view modes. For example, a generated button or link could be configured to display different text, or in a different color in separate view modes, or markup can be added to wrap a property and adjusted in a streamlined and intuitive way. Check out the sample below for an example of adding some helpful options to a link:

/**
 * Implements hook_field_extra_fields().
 */
function hook_field_extra_fields() {
  $extra['entity_type']['bundle'] = array(
    'display' => array(
      'field_name' => array(
        'label' => 'Field Name',
        'weight' => 0,
        //
        // Below are the callbacks for extrafield_settings
        'callback' => 'example_field_name_render',
        'settings callback' => 'example_field_name_settings_form'
      ),
    ),
  );
  return $extra;
}
 
/**
 * Settings callback for example field.
 */
function example_field_name_settings_form($settings) {
  return array(
    'link_text' => array(
      '#type' => 'textfield',
      '#title' => isset($settings['link_text']) ? $settings['link_text'] : t('Click here'),
      '#description' => t('Link text for my custom link.'),
    ),
    'classes' => array(
      '#type' => 'textfield',
      '#title' => isset($settings['classes']) ? $settings['classes'] : '',
      '#description' => t('Classes to add to my custom link.'),
    ),
  );
}
 
/**
 * Render callback for example field.
 */
function example_field_name_render($entity, $type, $view_mode, $settings) {
  $text = isset($settings['link_text']) ? $settings['link_text'] : t('Click here');
  $classes = isset($settings['classes']) ? $settings['classes'] : '';
 
  return array(
    '#theme' => 'link',
    '#title' => $text,
    '#path' => entity_uri($type, $entity),
    '#options' => array(
      'attributes' => array('class' => $classes),
      'html' => FALSE,
    ),
  );
}

 

As shown above, Extrafield Settings also adds support for an optional callback that will be executed when an extra field is visible on an entity being rendered, and supplied with information about the entity and display settings for the field. If you want to retain complete control over rendering your field, skip the callback and grab the field settings with field_info_extra_fields(). To learn more and download the Extrafield Settings module check out the project page on drupal.org. Drupal 8 version coming soon!