function EntityDisplayBase::init

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityDisplayBase.php \Drupal\Core\Entity\EntityDisplayBase::init()
  2. 8.9.x core/lib/Drupal/Core/Entity/EntityDisplayBase.php \Drupal\Core\Entity\EntityDisplayBase::init()
  3. 11.x core/lib/Drupal/Core/Entity/EntityDisplayBase.php \Drupal\Core\Entity\EntityDisplayBase::init()

Initializes the display.

This fills in default options for components:

  • that are not explicitly known as either "visible" or "hidden" in the display,
  • or that are not supposed to be configurable.
1 call to EntityDisplayBase::init()
EntityDisplayBase::__construct in core/lib/Drupal/Core/Entity/EntityDisplayBase.php
Constructs an Entity object.

File

core/lib/Drupal/Core/Entity/EntityDisplayBase.php, line 166

Class

EntityDisplayBase
Provides a common base class for entity view and form displays.

Namespace

Drupal\Core\Entity

Code

protected function init() {
    // Only populate defaults for "official" view modes and form modes.
    if (!$this->initialized && $this->mode !== static::CUSTOM_MODE) {
        $this->initialized = TRUE;
        $default_region = $this->getDefaultRegion();
        // Fill in defaults for extra fields.
        $context = $this->displayContext == 'view' ? 'display' : $this->displayContext;
        $extra_fields = \Drupal::service('entity_field.manager')->getExtraFields($this->targetEntityType, $this->bundle);
        $extra_fields = $extra_fields[$context] ?? [];
        foreach ($extra_fields as $name => $definition) {
            if (!isset($this->content[$name]) && !isset($this->hidden[$name])) {
                // Extra fields are visible by default unless they explicitly say so.
                if (!isset($definition['visible']) || $definition['visible'] == TRUE) {
                    $this->setComponent($name, [
                        'weight' => $definition['weight'],
                    ]);
                }
                else {
                    $this->removeComponent($name);
                }
            }
            // Ensure extra fields have a 'region'.
            if (isset($this->content[$name])) {
                $this->content[$name] += [
                    'region' => $default_region,
                ];
            }
        }
        // Fill in defaults for fields.
        $fields = $this->getFieldDefinitions();
        foreach ($fields as $name => $definition) {
            if (!$definition->isDisplayConfigurable($this->displayContext) || !isset($this->content[$name]) && !isset($this->hidden[$name])) {
                $options = $definition->getDisplayOptions($this->displayContext);
                if (!empty($options['region']) && $options['region'] === 'hidden') {
                    $this->removeComponent($name);
                }
                elseif ($options) {
                    $options += [
                        'region' => $default_region,
                    ];
                    $this->setComponent($name, $options);
                }
                // Note: (base) fields that do not specify display options are not
                // tracked in the display at all, in order to avoid cluttering the
                // configuration that gets saved back.
            }
        }
    }
}

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