diff --git a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php index a138c3b..cfb0ac2 100644 --- a/core/lib/Drupal/Core/Entity/EntityDisplayBase.php +++ b/core/lib/Drupal/Core/Entity/EntityDisplayBase.php @@ -22,6 +22,10 @@ use ThirdPartySettingsTrait; + /** + * The 'mode' for runtime EntityDisplay objects used to render entities with + * arbitrary display options rather than a configured view mode or form mode. + */ const CUSTOM_MODE = '_custom'; /** @@ -215,39 +219,42 @@ public function toArray() { * - or that are not supposed to be configurable. */ protected function init() { - // 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; + // 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); + // 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; + 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. } - 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. } } } @@ -347,6 +354,8 @@ protected function getFieldDefinitions() { if (!isset($this->fieldDefinitions)) { $definitions = \Drupal::entityManager()->getFieldDefinitions($this->targetEntityType, $this->bundle); + // For "official" view modes and form modes, ignore fields whose + // definition states they should not be displayed. if ($this->mode !== static::CUSTOM_MODE) { $definitions = array_filter($definitions, array($this, 'fieldHasDisplayOptions')); }