diff --git a/core/lib/Drupal/Core/Field/FormatterPluginManager.php b/core/lib/Drupal/Core/Field/FormatterPluginManager.php index 14a3d5f..4a7b16b 100644 --- a/core/lib/Drupal/Core/Field/FormatterPluginManager.php +++ b/core/lib/Drupal/Core/Field/FormatterPluginManager.php @@ -139,24 +139,18 @@ public function getInstance(array $options) { * The display properties with defaults added. */ public function prepareConfiguration($field_type, array $configuration) { - // If no formatter is specified, use the default formatter if the field type - // provides one. + // Fill in defaults for missing properties. + $configuration += array( + 'label' => 'above', + 'settings' => array(), + ); + // If no formatter is specified, use the default formatter. if (!isset($configuration['type'])) { $field_type = $this->fieldTypeManager->getDefinition($field_type); - if (isset($field_type['default_formatter'])) { - $configuration['type'] = $field_type['default_formatter']; - } - } - - // If the field can be displayed (has a formatter), fill in the defaults - // needed to display it. - if (isset($configuration['type'])) { - $configuration += array( - 'label' => 'above', - 'settings' => array(), - ); - $configuration['settings'] += $this->getDefaultSettings($configuration['type']); + $configuration['type'] = $field_type['default_formatter']; } + // Fill in default settings values for the formatter. + $configuration['settings'] += $this->getDefaultSettings($configuration['type']); return $configuration; } diff --git a/core/lib/Drupal/Core/Field/WidgetPluginManager.php b/core/lib/Drupal/Core/Field/WidgetPluginManager.php index d523cf2..d670073 100644 --- a/core/lib/Drupal/Core/Field/WidgetPluginManager.php +++ b/core/lib/Drupal/Core/Field/WidgetPluginManager.php @@ -140,23 +140,17 @@ public function createInstance($plugin_id, array $configuration = array()) { * The display properties with defaults added. */ public function prepareConfiguration($field_type, array $configuration) { - // If no widget is specified, use the default widget if the field type - // provides one. + // Fill in defaults for missing properties. + $configuration += array( + 'settings' => array(), + ); + // If no widget is specified, use the default widget. if (!isset($configuration['type'])) { $field_type = $this->fieldTypeManager->getDefinition($field_type); - if (isset($field_type['default_widget'])) { - $configuration['type'] = $field_type['default_widget']; - } - } - - // If the field can be edited in a UI (has a widget), fill in the defaults - // needed to display that widget. - if (isset($configuration['type'])) { - $configuration += array( - 'settings' => array(), - ); - $configuration['settings'] += $this->getDefaultSettings($configuration['type']); + $configuration['type'] = $field_type['default_widget']; } + // Fill in default settings values for the widget. + $configuration['settings'] += $this->getDefaultSettings($configuration['type']); return $configuration; } diff --git a/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php b/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php index 550a4cc..261165e 100644 --- a/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php +++ b/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php @@ -199,16 +199,30 @@ public function getComponents() { * {@inheritdoc} */ public function getComponent($name) { - // We always store 'extra fields', whether they are visible or hidden. + // Until https://drupal.org/node/2144919 allows base fields to be configured + // in the UI, many base fields are also still registered as "extra fields" + // to keep appearing in the "Manage (form) display" screens. + // - Field UI still treats them as "base fields", saving only the weight + // and visibility flag in the EntityDisplay. + // - For some of them (e.g. node title), the custom rendering code has been + // removed in favor of regular widgets/formatters. Their display options + // are "upgraded" to those of a field (widget/formatter + settings) at + // runtime using hook_entity_display_alter(). + // The getComponent() / setComponent() methods handle this by treating + // components as "extra fields" if they are registered as such, *and* if + // their display options contain no 'type' entry specifying a widget or + // formatter. + // @todo Cleanup after https://drupal.org/node/2144919 is fixed. + $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, $this->displayContext); - if (isset($extra_fields[$name])) { + if (isset($extra_fields[$name]) && !isset($this->content[$name]['type'])) { // If we have explicit settings, return an array or NULL depending on // visibility. if (isset($this->content[$name])) { if ($this->content[$name]['visible']) { - $options = $this->content[$name]; - unset($options['visible']); - return $options; + return array( + 'weight' => $this->content[$name]['weight'], + ); } else { return NULL; @@ -227,8 +241,7 @@ public function getComponent($name) { return NULL; } } - - if (isset($this->content[$name])) { + elseif (isset($this->content[$name])) { return $this->content[$name]; } } @@ -243,18 +256,18 @@ public function setComponent($name, array $options = array()) { $options['weight'] = isset($max) ? $max + 1 : 0; } - if ($field_definition = $this->getFieldDefinition($name)) { - $options = $this->pluginManager->prepareConfiguration($field_definition->getFieldType(), $options); - - // Clear the persisted plugin, if any. - unset($this->plugins[$name]); - } - - // We always store 'extra fields', whether they are visible or hidden. + // See remark in getComponent(). + // @todo Cleanup after https://drupal.org/node/2144919 is fixed. $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, $this->displayContext); - if (isset($extra_fields[$name])) { + if (isset($extra_fields[$name]) && !isset($options['type'])) { $options['visible'] = TRUE; } + elseif ($field_definition = $this->getFieldDefinition($name)) { + $options = $this->pluginManager->prepareConfiguration($field_definition->getFieldType(), $options); + } + + // Clear the persisted plugin, if any. + unset($this->plugins[$name]); $this->content[$name] = $options; @@ -265,8 +278,10 @@ public function setComponent($name, array $options = array()) { * {@inheritdoc} */ public function removeComponent($name) { + // See remark in getComponent(). + // @todo Cleanup after https://drupal.org/node/2144919 is fixed. $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, $this->displayContext); - if (isset($extra_fields[$name])) { + if (isset($extra_fields[$name]) && !isset($this->content[$name]['type'])) { // 'Extra fields' are exposed in hooks and can appear at any given time. // Therefore we store extra fields that are explicitly being hidden, so // that we can differenciate with those that are simply not configured @@ -277,9 +292,10 @@ public function removeComponent($name) { } else { unset($this->content[$name]); - unset($this->plugins[$name]); } + unset($this->plugins[$name]); + return $this; }