diff --git a/core/modules/entity/entity.services.yml b/core/modules/entity/entity.services.yml new file mode 100644 index 0000000..81ab979 --- /dev/null +++ b/core/modules/entity/entity.services.yml @@ -0,0 +1,4 @@ +services: + plugin.manager.entity.display_component_handler: + class: Drupal\entity\Plugin\Type\DisplayComponentHandlerPluginManager + parent: default_plugin_manager diff --git a/core/modules/entity/lib/Drupal/entity/Annotation/DisplayComponent.php b/core/modules/entity/lib/Drupal/entity/Annotation/DisplayComponent.php new file mode 100644 index 0000000..9a41976 --- /dev/null +++ b/core/modules/entity/lib/Drupal/entity/Annotation/DisplayComponent.php @@ -0,0 +1,26 @@ +pluginManager = \Drupal::service('plugin.manager.field.formatter'); - - parent::__construct($values, $entity_type); - } - - /** - * {@inheritdoc} - */ - public function getRenderer($field_name) { - if (isset($this->plugins[$field_name])) { - return $this->plugins[$field_name]; - } - - // Instantiate the formatter object from the stored display properties. - if (($configuration = $this->getComponent($field_name)) && isset($configuration['type']) && ($definition = $this->getFieldDefinition($field_name))) { - $formatter = $this->pluginManager->getInstance(array( - 'field_definition' => $definition, - 'view_mode' => $this->originalMode, - // No need to prepare, defaults have been merged in setComponent(). - 'prepare' => FALSE, - 'configuration' => $configuration - )); - } - else { - $formatter = NULL; - } - - // Persist the formatter object. - $this->plugins[$field_name] = $formatter; - return $formatter; - } - } diff --git a/core/modules/entity/lib/Drupal/entity/Entity/EntityFormDisplay.php b/core/modules/entity/lib/Drupal/entity/Entity/EntityFormDisplay.php index 26de1db..cfbf885 100644 --- a/core/modules/entity/lib/Drupal/entity/Entity/EntityFormDisplay.php +++ b/core/modules/entity/lib/Drupal/entity/Entity/EntityFormDisplay.php @@ -39,41 +39,12 @@ class EntityFormDisplay extends EntityDisplayBase implements EntityFormDisplayIn * {@inheritdoc} */ public function __construct(array $values, $entity_type) { - $this->pluginManager = \Drupal::service('plugin.manager.field.widget'); - parent::__construct($values, $entity_type); } /** * {@inheritdoc} */ - public function getRenderer($field_name) { - if (isset($this->plugins[$field_name])) { - return $this->plugins[$field_name]; - } - - // Instantiate the widget object from the stored display properties. - if (($configuration = $this->getComponent($field_name)) && isset($configuration['type']) && ($definition = $this->getFieldDefinition($field_name))) { - $widget = $this->pluginManager->getInstance(array( - 'field_definition' => $definition, - 'form_mode' => $this->originalMode, - // No need to prepare, defaults have been merged in setComponent(). - 'prepare' => FALSE, - 'configuration' => $configuration - )); - } - else { - $widget = NULL; - } - - // Persist the widget object. - $this->plugins[$field_name] = $widget; - return $widget; - } - - /** - * {@inheritdoc} - */ public function serialize() { // Only store the definition, not external objects or derived data. $data = $this->getExportProperties() + array('entityType' => $this->entityType()); diff --git a/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php b/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php index afda40a..6c177b5 100644 --- a/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php +++ b/core/modules/entity/lib/Drupal/entity/EntityDisplayBase.php @@ -47,12 +47,6 @@ */ public $bundle; - /** - * A list of field definitions eligible for configuration in this display. - * - * @var \Drupal\Core\Field\FieldDefinitionInterface[] - */ - protected $fieldDefinitions; /** * View or form mode to be displayed. @@ -91,12 +85,17 @@ */ public $originalMode; - /** - * The plugin objects used for this display, keyed by field name. + /* + * The renderer objects used for this display, keyed by component name. + */ + protected $renderers = array(); + + /* + * The display component handler plugin manager. * - * @var array + * @var \Drupal\entity\Plugin\Type\DisplayComponentHandlerPluginManager */ - protected $plugins = array(); + protected $handlerManager; /** * Context in which this entity will be used (e.g. 'display', 'form'). @@ -105,12 +104,6 @@ */ protected $displayContext; - /** - * The plugin manager used by this entity type. - * - * @var \Drupal\Component\Plugin\PluginManagerBase - */ - protected $pluginManager; /** * {@inheritdoc} @@ -124,16 +117,15 @@ public function __construct(array $values, $entity_type) { // throw new \InvalidArgumentException('Missing required properties for an EntityDisplay entity.'); // } - // A plugin manager and a context type needs to be set by extending classes. - if (!isset($this->pluginManager)) { - throw new \RuntimeException('Missing plugin manager.'); - } + if (!isset($this->displayContext)) { throw new \RuntimeException('Missing display context type.'); } parent::__construct($values, $entity_type); + $this->handlerManager =\Drupal::service('plugin.manager.entity.display_component_handler'); + $this->originalMode = $this->mode; $this->init(); @@ -183,13 +175,10 @@ public function getExportProperties() { $properties[$name] = $this->get($name); } - // Do not store options for fields whose display is not set to be - // configurable. - foreach ($this->getFieldDefinitions() as $field_name => $definition) { - if (!$definition->isDisplayConfigurable($this->displayContext)) { - unset($properties['content'][$field_name]); - unset($properties['hidden'][$field_name]); - } + // Let the component handlers add missing components. + $handlers = $this->handlerManager->getDefinitions(); + foreach (array_keys($handlers) as $type) { + $properties = $this->getComponentHandler($type)->massageOut($properties); } return $properties; @@ -204,38 +193,10 @@ public function getExportProperties() { * - or that are not supposed to be configurable. */ protected function init() { - // Fill in defaults for extra fields. - $extra_fields = field_info_extra_fields($this->targetEntityType, $this->bundle, ($this->displayContext == 'view' ? 'display' : $this->displayContext)); - 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. - } + // Let the component handlers add missing components. + $handlers = $this->handlerManager->getDefinitions(); + foreach (array_keys($handlers) as $type) { + $this->getComponentHandler($type)->prepareDisplayComponents($this->content, $this->hidden); } } @@ -272,14 +233,12 @@ public function setComponent($name, array $options = array()) { $options['weight'] = isset($max) ? $max + 1 : 0; } - // For a field, fill in default options. - if ($field_definition = $this->getFieldDefinition($name)) { - $options = $this->pluginManager->prepareConfiguration($field_definition->getType(), $options); - } + // Massage in some values + $handler = $this->getComponentHandlerByElementName($name); + $options = $handler->massageIn($options); $this->content[$name] = $options; unset($this->hidden[$name]); - unset($this->plugins[$name]); return $this; } @@ -290,7 +249,6 @@ public function setComponent($name, array $options = array()) { public function removeComponent($name) { $this->hidden[$name] = TRUE; unset($this->content[$name]); - unset($this->plugins[$name]); return $this; } @@ -315,41 +273,54 @@ public function getHighestWeight() { } /** - * Returns the field definition of a field. + * {@inheritdoc} */ - protected function getFieldDefinition($field_name) { - $definitions = $this->getFieldDefinitions(); - return isset($definitions[$field_name]) ? $definitions[$field_name] : NULL; - } + public function getComponentHandlerByElementName($name) { + $handler = NULL; + $handlers = $this->handlerManager->getDefinitions(); + foreach (array_keys($handlers) as $type) { + $handler = $this->getComponentHandler($type); + if ($handler && $handler->hasElement($name)) { + continue; + } + } + return $handler; + } /** - * Returns the definitions of the fields that are candidate for display. + * {@inheritdoc} */ - protected function getFieldDefinitions() { - if (!isset($this->fieldDefinitions)) { - // @todo Replace this with \Drupal::entityManager()->getFieldDefinition() - // when it can hand the $instance objects (and then reconsider the - // $this->fieldDefinitions static cache ?) - // https://drupal.org/node/2114707 - $entity_manager = \Drupal::entityManager(); - $entity_info = $entity_manager->getDefinition($this->targetEntityType); - $definitions = array(); - if ($entity_info->isSubclassOf('\Drupal\Core\Entity\ContentEntityInterface')) { - $entity = _field_create_entity_from_ids((object) array('entity_type' => $this->targetEntityType, 'bundle' => $this->bundle, 'entity_id' => NULL)); - foreach ($entity as $field_name => $items) { - $definitions[$field_name] = $items->getFieldDefinition(); - } - } + public function getComponentHandler($type) { + $handler = $this->handlerManager->getInstance(array('type' => $type)); + if ($handler) { + $handler->setContext(array( + 'entity_type' => $this->targetEntityType, + 'bundle' => $this->bundle, + 'view_mode' => $this->originalMode, + 'display_context' => $this->displayContext, + )); + } + return $handler; + } - // The display only cares about fields that specify display options. - // Discard base fields that are not rendered through formatters / widgets. - $display_context = $this->displayContext; - $this->fieldDefinitions = array_filter($definitions, function (FieldDefinitionInterface $definition) use ($display_context) { - return $definition->getDisplayOptions($display_context); - }); + /**+ + * {@inheritdoc} + */ + public function getRenderer($name) { + if (!isset($this->content[$name])) { + return NULL; } - return $this->fieldDefinitions; + if (!array_key_exists($name, $this->renderers)) { + $options = $this->getComponent($name); + if ($handler = $this->getComponentHandlerByElementName($name)) { + $this->renderers[$name] = $handler->getRenderer($name, $options); + } + else { + $this->renderers[$name] = NULL; + } + } + return $this->renderers[$name]; } } diff --git a/core/modules/entity/lib/Drupal/entity/Plugin/DisplayComponent/ExtraFieldDisplayComponentHandler.php b/core/modules/entity/lib/Drupal/entity/Plugin/DisplayComponent/ExtraFieldDisplayComponentHandler.php new file mode 100644 index 0000000..ea26e11 --- /dev/null +++ b/core/modules/entity/lib/Drupal/entity/Plugin/DisplayComponent/ExtraFieldDisplayComponentHandler.php @@ -0,0 +1,41 @@ +context['entity_type'], $this->context['bundle'], ($this->context['display_context'] == 'view' ? 'display' : $this->context['display_context'])); + foreach ($extra_fields as $name => $definition) { + if (!isset($components[$name]) && !isset($hidden_components[$name])) { + // Extra fields are visible by default unless they explicitly say so. + if (!isset($definition['visible']) || $definition['visible'] == TRUE) { + $components[$name] = array( + 'weight' => $definition['weight'] + ); + } + else { + $hidden_components[$name] = TRUE; + } + } + } + } +} diff --git a/core/modules/entity/lib/Drupal/entity/Plugin/Type/DisplayComponentHandlerBase.php b/core/modules/entity/lib/Drupal/entity/Plugin/Type/DisplayComponentHandlerBase.php new file mode 100644 index 0000000..7f8af3d --- /dev/null +++ b/core/modules/entity/lib/Drupal/entity/Plugin/Type/DisplayComponentHandlerBase.php @@ -0,0 +1,77 @@ +context = $context; + } + + /** + * @todo + * + * @param array $components + */ + public function prepareDisplayComponents(array &$components, array &$hidden_components) { + } + + /** + * @todo + * + * @param array $components + */ + public function hasElement($name) { + return FALSE; + } + + /** + * @todo + * + * @param string $name + * @param array $options + * @return array + */ + public function massageIn($name, array $options = null) { + return $options; + } + + /** + * {@inheritdoc} + */ + public function massageOut($properties) { + return $properties; + } + + /** + * @todo + * + * @param string $name + * @param array $options + */ + public function getRenderer($name, array $options = null) { + } + +} diff --git a/core/modules/entity/lib/Drupal/entity/Plugin/Type/DisplayComponentHandlerPluginManager.php b/core/modules/entity/lib/Drupal/entity/Plugin/Type/DisplayComponentHandlerPluginManager.php new file mode 100644 index 0000000..1081065 --- /dev/null +++ b/core/modules/entity/lib/Drupal/entity/Plugin/Type/DisplayComponentHandlerPluginManager.php @@ -0,0 +1,65 @@ +alterInfo($module_handler, 'display_component_handler_info'); + $this->setCacheBackend($cache_backend, $language_manager, 'display_component_handlers'); + } + + /** + * {@inheritdoc} + */ + public function getInstance(array $options) { + $plugin_id = $options['type']; + + if (!isset($this->plugins[$plugin_id]) && !array_key_exists($plugin_id, $this->plugins)) { + $this->plugins[$plugin_id] = $this->discovery->getDefinition($plugin_id) ? $this->createInstance($plugin_id) : NULL; + } + + return $this->plugins[$plugin_id]; + } + +} diff --git a/core/modules/field/lib/Drupal/field/Plugin/DisplayComponent/FieldDisplayComponentHandler.php b/core/modules/field/lib/Drupal/field/Plugin/DisplayComponent/FieldDisplayComponentHandler.php new file mode 100644 index 0000000..ef88bd1 --- /dev/null +++ b/core/modules/field/lib/Drupal/field/Plugin/DisplayComponent/FieldDisplayComponentHandler.php @@ -0,0 +1,226 @@ +fieldInfo = $field_info; + $this->formatterPluginManager = $formatter_plugin_manager; + $this->widgetPluginManager = $widget_plugin_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static($configuration, $plugin_id, $plugin_definition, + $container->get('field.info'), + $container->get('plugin.manager.field.formatter'), + $container->get('plugin.manager.field.widget') + ); + } + + /** + * {@inheritdoc} + */ + public function massageIn($name, array $options = array()) { + $field_definition = $this->getFieldDefinition($name); + if (!isset($field_definition)) { + // The field in process of removal from display. + return $options; + } + if ($this->context['display_context'] == 'display') { + return $this->formatterPluginManager->prepareConfiguration($field_definition->getFieldType(), $options); + } + else { + return $this->widgetPluginManager->prepareConfiguration($field_definition->getFieldType(), $options); + } + } + + /** + * {@inheritdoc} + */ + public function massageOut($properties) { + // Do not store options for fields whose display is not set to be + // configurable. + foreach ($this->getFieldDefinitions() as $field_name => $definition) { + if (!$definition->isDisplayConfigurable($this->context['display_context'])) { + unset($properties['content'][$field_name]); + unset($properties['hidden'][$field_name]); + } + } + + return $properties; + } + + /** + * {@inheritdoc} + */ + public function prepareDisplayComponents(array &$components, array &$hidden_components) { + if ($this->context['display_context'] == 'display') { + $plugin_manager = $this->formatterPluginManager; + } + else { + $plugin_manager = $this->widgetPluginManager; + } + + // Fill in defaults for fields. + $fields = $this->getFieldDefinitions(); + foreach ($fields as $name => $definition) { + if (!$definition->isDisplayConfigurable($this->context['display_context']) || (!isset($components[$name]) && !isset($hidden_components[$name]))) { + $options = $definition->getDisplayOptions($this->context['display_context']); + + if (!empty($options['type']) && $options['type'] == 'hidden') { + $hidden_components[$name] = TRUE; + } + elseif ($options) { + $components[$name] = $plugin_manager->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. + } + } + } + + /** + * {@inheritdoc} + */ + public function getRenderer($name, array $options = array()) { + if (isset($options['type']) && ($definition = $this->getFieldDefinition($name))) { + if ($this->context['display_context'] == 'display') { + $plugin_manager = $this->formatterPluginManager; + $mode_key = 'view_mode'; + } + else { + $plugin_manager = $this->widgetPluginManager; + $mode_key = 'form_mode'; + } + + return $plugin_manager->getInstance(array( + 'field_definition' => $definition, + $mode_key => $this->context['view_mode'], + 'configuration' => $options, + // No need to prepare, defaults have been merged when the options were + // written in the display. + 'prepare' => FALSE, + )); + } + else { + return NULL; + } + } + + /** + * {@inheritdoc} + */ + public function hasElement($name) { + $field_defintion = $this->getFieldDefinition($name); + return isset($field_defintion); + } + + /** + * Returns the field definition of a field. + */ + protected function getFieldDefinition($field_name) { + $definitions = $this->getFieldDefinitions(); + return isset($definitions[$field_name]) ? $definitions[$field_name] : NULL; + } + + /** + * Returns the definitions of the fields that are candidate for display. + */ + protected function getFieldDefinitions() { + if (!isset($this->fieldDefinitions)) { + // @todo Replace this with \Drupal::entityManager()->getFieldDefinition() + // when it can hand the $instance objects (and then reconsider the + // $this->fieldDefinitions static cache ?) + // https://drupal.org/node/2114707 + $entity_manager = \Drupal::entityManager(); + $entity_info = $entity_manager->getDefinition($this->context['entity_type']); + $definitions = array(); + if ($entity_info->isSubclassOf('\Drupal\Core\Entity\ContentEntityInterface')) { + $entity = _field_create_entity_from_ids((object) array('entity_type' => $this->context['entity_type'], 'bundle' => $this->context['bundle'], 'entity_id' => NULL)); + foreach ($entity as $field_name => $items) { + $definitions[$field_name] = $items->getFieldDefinition(); + } + } + + // The display only cares about fields that specify display options. + // Discard base fields that are not rendered through formatters / widgets. + $display_context = $this->context['display_context']; + $this->fieldDefinitions = array_filter($definitions, function (FieldDefinitionInterface $definition) use ($display_context) { + return $definition->getDisplayOptions($display_context); + }); + } + + return $this->fieldDefinitions; + } +}