Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

In Drupal 7, parameters controlling how to render the various "parts" of an entity view in a given view mode were scattered in many separate locations:
- in $instance['display'] for each individual (Field API) field in the bundle
- in the 'field_bundle_settings_[entity_type]_[bundle]' variable for the 'extra fields' in the bundle
- in other places for contrib additions that are not one of the two above (most notably field_group module)

In Drupal 8, all those parameters ("options") for all those parts ("components") are centralized in one single EntityViewDisplay configuration object for each entity bundle and view mode. Note that there are similar EntityFormDisplay objects on the form side.

Related change records:
- Entity & field rendering hooks receive an additional EntityViewDisplay parameter for impact on functions and hooks on the rendering side.
- field formatter output is generated by EntityViewDisplayInterface::build() / buildMultiple.

API changes:

  • New EntityViewDisplay config entity, holding the display settings for all the components of an entity in a view mode.The EntityViewDisplay object associated with a bundle and view mode can be accessed with the entity_get_display() function.
    The setComponent() and removeComponent() methods can be used to assign display options for a given component.

    Drupal 8:

    entity_get_display('node', 'article', 'default')
      // Set 'component_1' to be visible, with weight 1.
      ->setComponent('component_1', array(
        'weight' => 1,
      ))
      // Set 'component_2' to be hidden.
      ->removeComponent('component_2')
      ->save();
    

Impact on Field API display options

  • Display options for a field are not specified in the $instance definition structure anymore.
    The $instance['display'] entry no longer exists, $field and $instance definitions structures are purely about data, not about how render it.
    Instead, display options are specified in the EntityViewDisplay object associated to the bundle and view mode.
    Drupal 7: Drupal 8:
    field_create_instance(array(
      'field_name' => 'some_field',
      'entity_type' => 'node',
      'bundle' => 'article',
      'widget' => array(
        'type' => 'some_widget',
      ),
      'display' => array(
        'default' => array(
          'type' => 'some_formatter',
          'settings' => array(
            'foo' => 'bar',
          ),
          'weight' => 1,
        ),
        'teaser' => array(
          'type' => 'hidden',
        ),
      )
    );
    
    entity_create('field_config', array(
      'field_name' => 'some_field',
      'entity_type' => 'node',
      'bundle' => 'article',
      'widget' => array(
        'type' => 'some_widget',
      ),
    );
    entity_get_display('node', 'article', 'default')
      ->setComponent('some_field', array(
        'type' => 'some_formatter',
        'settings' => array(
          'foo' => 'bar',
        ),
        'weight' => 1,
      ))
      ->save();
    // Not strictly needed, fields 
    // are hidden by default.
    entity_get_display('node', 'article', 'teaser')
      ->removeComponent('some_field')
      ->save();
    
  • In Drupal 7, if $instance['display']['default'] was not specified, field_create_instance($instance) implicitly set the field to be displayed in the 'default' view mode, using the default formatter for the field type.
    In Drupal 8, field_create_instance() does not assign any display options, and all fields are considered hidden by default.
    Display options need to be explicitly assigned to the EntityViewDisplay objects :
    Drupal 7: Drupal 8:
    field_create_instance(array(
      'field_name' => 'some_field',
      'entity_type' => 'node',
      'bundle' => 'article',
    );
    // The field is displayed in the 'default' view mode, 
    // using the default formatter for the field type.
    
    entity_create('field_config', array(
      'field_name' => 'some_field',
      'entity_type' => 'node',
      'bundle' => 'article',
    );
    // Set the field to be displayed in the 'default' view mode, 
    // using the default formatter for the field type.
    entity_get_display('node', 'article', 'teaser')
      ->setComponent('some_field')
      ->save();
    
  • hook_field_display_alter() and hook_field_extra_fields_display_alter() are replaced by hook_entity_view_display_alter()
  • Drupal 7:

// Called once per field & per entity in entity_view_multiple():
function hook_field_display_alter(array &$display_properties, array $context) {
  if ($context['entity_type'] == 'node' && $context['view_mode'] == 'teaser' && $context['field']['field_name'] == 'my_field') {
    $display_properties['type'] = 'hidden';
  }
}

Drupal 8:

// Called once per bundle in entity_view_multiple():
function hook_entity_view_display_alter(EntityViewDisplayInterface $entity_display, array $context) {
  if ($context['entity_type'] == 'node' && $context['view_mode'] == 'teaser') {
    $entity_display->removeComponent('my_field');
  }
}
Impacts: 
Module developers
Updates Done (doc team, etc.)
Online documentation: 
Not done
Theming guide: 
Not done
Module developer documentation: 
Not done
Examples project: 
Not done
Coder Review: 
Not done
Coder Upgrade: 
Not done
Other: 
Other updates done

Comments

smiletrl’s picture

This probably needs updated, since entity_create('field_instance', $instance_definition) is a preferable way of creating new instances in D8.