function FieldStorageCreationTrait::configureEntityViewDisplay

Same name and namespace in other branches
  1. 11.x core/modules/field_ui/src/Form/FieldStorageCreationTrait.php \Drupal\field_ui\Form\FieldStorageCreationTrait::configureEntityViewDisplay()

Configures the field for the default view mode.

Parameters

string $field_name: The field name.

array[] $formatter_settings: (optional) An array of settings, keyed by view mode. Only the 'type' key of the inner array is used, and the value should be the plugin ID of a formatter. Defaults to an empty array.

Throws

\Drupal\Core\Entity\EntityStorageException

2 calls to FieldStorageCreationTrait::configureEntityViewDisplay()
FieldConfigEditForm::save in core/modules/field_ui/src/Form/FieldConfigEditForm.php
Form submission handler for the 'save' action.
FieldStorageReuseForm::reuseCallback in core/modules/field_ui/src/Form/FieldStorageReuseForm.php
Callback function to handle re-using an existing field.

File

core/modules/field_ui/src/Form/FieldStorageCreationTrait.php, line 51

Class

FieldStorageCreationTrait
Provides common functionality for adding or re-using a field.

Namespace

Drupal\field_ui\Form

Code

protected function configureEntityViewDisplay(string $field_name, array $formatter_settings = []) {
  // For a new field, only $mode = 'default' should be set. Use the
  // preconfigured or default formatter and settings. The field stays hidden
  // for other view modes until it is explicitly configured.
  foreach ($formatter_settings as $mode => $options) {
    $view_display = $this->entityDisplayRepository
      ->getViewDisplay($this->entityTypeId, $this->bundle, $mode);
    if ($view_display->status()) {
      $view_display->setComponent($field_name, $options)
        ->save();
    }
  }
  if (empty($formatter_settings)) {
    $this->entityDisplayRepository
      ->getViewDisplay($this->entityTypeId, $this->bundle)
      ->setComponent($field_name, [])
      ->save();
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.