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

The $field and $instance arrays from Drupal 7 are configuration entities in Drupal 8. Additionally, the only code that interacts with these objects directly should be code that changes or stores their configuration, or reacts to the configuration being changed.

Formatters, widgets, and other code that acts on fields without changing or storing their configuration, instead receive a $field_definition object that implements FieldDefinitionInterface.

  • Some settings, like the field type and whether it's required, can be retrieved directly from this interface.
  • Other configurable settings that relate to how a field's data is stored (like the field's cardinality, whether it is revisionable, and whether it supports multiple values), should be retrieved from the field's storage definition by calling the getFieldStorageDefinition() method.

The data returned via methods of these interfaces might be internally stored in the $field configuration entity, or in the $instance configuration entity, or represent a nonconfigurable, code-defined, field for which there is no configuration entity at all. Formatters, widgets, etc. have no reason to know or care about any of those internal details.

Code example:

Drupal 7:

function mymodule_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  ...
  $field_type = $field['type'];
  $required = $instance['required'];
  $cardinality = $field['cardinality'];
  $multiple = $cardinality > 1 || $cardinality == FIELD_CARDINALITY_UNLIMITED;
  ...
}

Drupal 8:

abstract class MyWidget extends WidgetBase {
  ...
  public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state) {
    ...
    $type = $this->fieldDefinition->getType();
    $required = $this->fieldDefinition->isRequired();
    $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
    $multiple  =  $this->fieldDefinition->getFieldStorageDefinition()->isMultiple();
    ...
  }
}

See Field widgets are now plugins for more detail about the widget example used above.

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

mradcliffe’s picture

The title of #2143263: Remove "Field" prefix from FieldDefinitionInterface methods reflects that the method getFieldSettings() changed to getSettings().