W3cubDocs

/Drupal 8

protected function EntityDisplayBase::init

protected 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.

File

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

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->mode !== static::CUSTOM_MODE) {
    // Fill in defaults for extra fields.
    $context = $this->displayContext == 'view' ? 'display' : $this->displayContext;
    $extra_fields = \Drupal::entityManager()->getExtraFields($this->targetEntityType, $this->bundle);
    $extra_fields = isset($extra_fields[$context]) ? $extra_fields[$context] : array();
    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->content[$name] = array(
            'weight' => $definition['weight']
          );
        }
        else {
          $this->hidden[$name] = TRUE;
        }
      }
    }

    // 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['type']) && $options['type'] == 'hidden') {
          $this->hidden[$name] = TRUE;
        }
        elseif ($options) {
          $this->content[$name] = $this->pluginManager->prepareConfiguration($definition->getType(), $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.
      }
    }
  }
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!EntityDisplayBase.php/function/EntityDisplayBase::init/8.1.x