Chris@0: entityTypeManager()->getDefinition($values['targetEntityType'])->entityClassImplements(FieldableEntityInterface::class)) { Chris@0: throw new \InvalidArgumentException('EntityDisplay entities can only handle fieldable entity types.'); Chris@0: } Chris@0: Chris@0: $this->renderer = \Drupal::service('renderer'); Chris@0: Chris@0: // A plugin manager and a context type needs to be set by extending classes. Chris@0: if (!isset($this->pluginManager)) { Chris@0: throw new \RuntimeException('Missing plugin manager.'); Chris@0: } Chris@0: if (!isset($this->displayContext)) { Chris@0: throw new \RuntimeException('Missing display context type.'); Chris@0: } Chris@0: Chris@0: parent::__construct($values, $entity_type); Chris@0: Chris@0: $this->originalMode = $this->mode; Chris@0: Chris@0: $this->init(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Initializes the display. Chris@0: * Chris@0: * This fills in default options for components: Chris@0: * - that are not explicitly known as either "visible" or "hidden" in the Chris@0: * display, Chris@0: * - or that are not supposed to be configurable. Chris@0: */ Chris@0: protected function init() { Chris@0: // Only populate defaults for "official" view modes and form modes. Chris@0: if ($this->mode !== static::CUSTOM_MODE) { Chris@0: $default_region = $this->getDefaultRegion(); Chris@0: // Fill in defaults for extra fields. Chris@0: $context = $this->displayContext == 'view' ? 'display' : $this->displayContext; Chris@0: $extra_fields = \Drupal::entityManager()->getExtraFields($this->targetEntityType, $this->bundle); Chris@0: $extra_fields = isset($extra_fields[$context]) ? $extra_fields[$context] : []; Chris@0: foreach ($extra_fields as $name => $definition) { Chris@0: if (!isset($this->content[$name]) && !isset($this->hidden[$name])) { Chris@0: // Extra fields are visible by default unless they explicitly say so. Chris@0: if (!isset($definition['visible']) || $definition['visible'] == TRUE) { Chris@17: $this->setComponent($name, [ Chris@17: 'weight' => $definition['weight'], Chris@17: ]); Chris@0: } Chris@0: else { Chris@17: $this->removeComponent($name); Chris@0: } Chris@0: } Chris@0: // Ensure extra fields have a 'region'. Chris@0: if (isset($this->content[$name])) { Chris@0: $this->content[$name] += ['region' => $default_region]; Chris@0: } Chris@0: } Chris@0: Chris@0: // Fill in defaults for fields. Chris@0: $fields = $this->getFieldDefinitions(); Chris@0: foreach ($fields as $name => $definition) { Chris@0: if (!$definition->isDisplayConfigurable($this->displayContext) || (!isset($this->content[$name]) && !isset($this->hidden[$name]))) { Chris@0: $options = $definition->getDisplayOptions($this->displayContext); Chris@0: Chris@0: // @todo Remove handling of 'type' in https://www.drupal.org/node/2799641. Chris@0: if (!isset($options['region']) && !empty($options['type']) && $options['type'] === 'hidden') { Chris@0: $options['region'] = 'hidden'; Chris@0: @trigger_error("Specifying 'type' => 'hidden' is deprecated, use 'region' => 'hidden' instead.", E_USER_DEPRECATED); Chris@0: } Chris@0: Chris@0: if (!empty($options['region']) && $options['region'] === 'hidden') { Chris@14: $this->removeComponent($name); Chris@0: } Chris@0: elseif ($options) { Chris@0: $options += ['region' => $default_region]; Chris@14: $this->setComponent($name, $options); Chris@0: } Chris@0: // Note: (base) fields that do not specify display options are not Chris@0: // tracked in the display at all, in order to avoid cluttering the Chris@0: // configuration that gets saved back. Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTargetEntityTypeId() { Chris@0: return $this->targetEntityType; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getMode() { Chris@0: return $this->get('mode'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getOriginalMode() { Chris@0: return $this->get('originalMode'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTargetBundle() { Chris@0: return $this->bundle; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setTargetBundle($bundle) { Chris@0: $this->set('bundle', $bundle); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function id() { Chris@0: return $this->targetEntityType . '.' . $this->bundle . '.' . $this->mode; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@14: public function preSave(EntityStorageInterface $storage) { Chris@0: // Ensure that a region is set on each component. Chris@0: foreach ($this->getComponents() as $name => $component) { Chris@0: $this->handleHiddenType($name, $component); Chris@0: // Ensure that a region is set. Chris@0: if (isset($this->content[$name]) && !isset($component['region'])) { Chris@0: // Directly set the component to bypass other changes in setComponent(). Chris@0: $this->content[$name]['region'] = $this->getDefaultRegion(); Chris@0: } Chris@0: } Chris@0: Chris@0: ksort($this->content); Chris@0: ksort($this->hidden); Chris@14: parent::preSave($storage); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Handles a component type of 'hidden'. Chris@0: * Chris@0: * @deprecated This method exists only for backwards compatibility. Chris@0: * Chris@0: * @todo Remove this in https://www.drupal.org/node/2799641. Chris@0: * Chris@0: * @param string $name Chris@0: * The name of the component. Chris@0: * @param array $component Chris@0: * The component array. Chris@0: */ Chris@0: protected function handleHiddenType($name, array $component) { Chris@0: if (!isset($component['region']) && isset($component['type']) && $component['type'] === 'hidden') { Chris@0: $this->removeComponent($name); Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function calculateDependencies() { Chris@0: parent::calculateDependencies(); Chris@18: $target_entity_type = $this->entityTypeManager()->getDefinition($this->targetEntityType); Chris@0: Chris@0: // Create dependency on the bundle. Chris@0: $bundle_config_dependency = $target_entity_type->getBundleConfigDependency($this->bundle); Chris@0: $this->addDependency($bundle_config_dependency['type'], $bundle_config_dependency['name']); Chris@0: Chris@0: // If field.module is enabled, add dependencies on 'field_config' entities Chris@0: // for both displayed and hidden fields. We intentionally leave out base Chris@0: // field overrides, since the field still exists without them. Chris@0: if (\Drupal::moduleHandler()->moduleExists('field')) { Chris@0: $components = $this->content + $this->hidden; Chris@18: $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($this->targetEntityType, $this->bundle); Chris@0: foreach (array_intersect_key($field_definitions, $components) as $field_name => $field_definition) { Chris@0: if ($field_definition instanceof ConfigEntityInterface && $field_definition->getEntityTypeId() == 'field_config') { Chris@0: $this->addDependency('config', $field_definition->getConfigDependencyName()); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // Depend on configured modes. Chris@0: if ($this->mode != 'default') { Chris@18: $mode_entity = $this->entityTypeManager()->getStorage('entity_' . $this->displayContext . '_mode')->load($target_entity_type->id() . '.' . $this->mode); Chris@0: $this->addDependency('config', $mode_entity->getConfigDependencyName()); Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function toArray() { Chris@0: $properties = parent::toArray(); Chris@0: // Do not store options for fields whose display is not set to be Chris@0: // configurable. Chris@0: foreach ($this->getFieldDefinitions() as $field_name => $definition) { Chris@0: if (!$definition->isDisplayConfigurable($this->displayContext)) { Chris@0: unset($properties['content'][$field_name]); Chris@0: unset($properties['hidden'][$field_name]); Chris@0: } Chris@0: } Chris@0: Chris@0: return $properties; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function createCopy($mode) { Chris@0: $display = $this->createDuplicate(); Chris@0: $display->mode = $display->originalMode = $mode; Chris@0: return $display; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getComponents() { Chris@0: return $this->content; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getComponent($name) { Chris@0: return isset($this->content[$name]) ? $this->content[$name] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setComponent($name, array $options = []) { Chris@0: // If no weight specified, make sure the field sinks at the bottom. Chris@0: if (!isset($options['weight'])) { Chris@0: $max = $this->getHighestWeight(); Chris@0: $options['weight'] = isset($max) ? $max + 1 : 0; Chris@0: } Chris@0: Chris@0: // For a field, fill in default options. Chris@0: if ($field_definition = $this->getFieldDefinition($name)) { Chris@0: $options = $this->pluginManager->prepareConfiguration($field_definition->getType(), $options); Chris@0: } Chris@0: Chris@0: // Ensure we always have an empty settings and array. Chris@0: $options += ['settings' => [], 'third_party_settings' => []]; Chris@0: Chris@0: $this->content[$name] = $options; Chris@0: unset($this->hidden[$name]); Chris@0: unset($this->plugins[$name]); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function removeComponent($name) { Chris@0: $this->hidden[$name] = TRUE; Chris@0: unset($this->content[$name]); Chris@0: unset($this->plugins[$name]); Chris@0: Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getHighestWeight() { Chris@0: $weights = []; Chris@0: Chris@0: // Collect weights for the components in the display. Chris@0: foreach ($this->content as $options) { Chris@0: if (isset($options['weight'])) { Chris@0: $weights[] = $options['weight']; Chris@0: } Chris@0: } Chris@0: Chris@0: // Let other modules feedback about their own additions. Chris@0: $weights = array_merge($weights, \Drupal::moduleHandler()->invokeAll('field_info_max_weight', [$this->targetEntityType, $this->bundle, $this->displayContext, $this->mode])); Chris@0: Chris@0: return $weights ? max($weights) : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the field definition of a field. Chris@0: */ Chris@0: protected function getFieldDefinition($field_name) { Chris@0: $definitions = $this->getFieldDefinitions(); Chris@0: return isset($definitions[$field_name]) ? $definitions[$field_name] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the definitions of the fields that are candidate for display. Chris@0: */ Chris@0: protected function getFieldDefinitions() { Chris@0: if (!isset($this->fieldDefinitions)) { Chris@0: $definitions = \Drupal::entityManager()->getFieldDefinitions($this->targetEntityType, $this->bundle); Chris@0: // For "official" view modes and form modes, ignore fields whose Chris@0: // definition states they should not be displayed. Chris@0: if ($this->mode !== static::CUSTOM_MODE) { Chris@0: $definitions = array_filter($definitions, [$this, 'fieldHasDisplayOptions']); Chris@0: } Chris@0: $this->fieldDefinitions = $definitions; Chris@0: } Chris@0: Chris@0: return $this->fieldDefinitions; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Determines if a field has options for a given display. Chris@0: * Chris@12: * @param \Drupal\Core\Field\FieldDefinitionInterface $definition Chris@0: * A field definition. Chris@0: * @return array|null Chris@0: */ Chris@0: private function fieldHasDisplayOptions(FieldDefinitionInterface $definition) { Chris@0: // The display only cares about fields that specify display options. Chris@0: // Discard base fields that are not rendered through formatters / widgets. Chris@0: return $definition->getDisplayOptions($this->displayContext); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function onDependencyRemoval(array $dependencies) { Chris@0: $changed = parent::onDependencyRemoval($dependencies); Chris@0: foreach ($dependencies['config'] as $entity) { Chris@0: if ($entity->getEntityTypeId() == 'field_config') { Chris@0: // Remove components for fields that are being deleted. Chris@0: $this->removeComponent($entity->getName()); Chris@0: unset($this->hidden[$entity->getName()]); Chris@0: $changed = TRUE; Chris@0: } Chris@0: } Chris@0: foreach ($this->getComponents() as $name => $component) { Chris@0: if ($renderer = $this->getRenderer($name)) { Chris@0: if (in_array($renderer->getPluginDefinition()['provider'], $dependencies['module'])) { Chris@0: // Revert to the defaults if the plugin that supplies the widget or Chris@0: // formatter depends on a module that is being uninstalled. Chris@0: $this->setComponent($name); Chris@0: $changed = TRUE; Chris@0: } Chris@0: Chris@0: // Give this component the opportunity to react on dependency removal. Chris@0: $component_removed_dependencies = $this->getPluginRemovedDependencies($renderer->calculateDependencies(), $dependencies); Chris@0: if ($component_removed_dependencies) { Chris@0: if ($renderer->onDependencyRemoval($component_removed_dependencies)) { Chris@0: // Update component settings to reflect changes. Chris@0: $component['settings'] = $renderer->getSettings(); Chris@0: $component['third_party_settings'] = []; Chris@0: foreach ($renderer->getThirdPartyProviders() as $module) { Chris@0: $component['third_party_settings'][$module] = $renderer->getThirdPartySettings($module); Chris@0: } Chris@0: $this->setComponent($name, $component); Chris@0: $changed = TRUE; Chris@0: } Chris@0: // If there are unresolved deleted dependencies left, disable this Chris@0: // component to avoid the removal of the entire display entity. Chris@0: if ($this->getPluginRemovedDependencies($renderer->calculateDependencies(), $dependencies)) { Chris@0: $this->removeComponent($name); Chris@0: $arguments = [ Chris@0: '@display' => (string) $this->getEntityType()->getLabel(), Chris@0: '@id' => $this->id(), Chris@0: '@name' => $name, Chris@0: ]; Chris@0: $this->getLogger()->warning("@display '@id': Component '@name' was disabled because its settings depend on removed dependencies.", $arguments); Chris@0: $changed = TRUE; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: return $changed; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the plugin dependencies being removed. Chris@0: * Chris@0: * The function recursively computes the intersection between all plugin Chris@0: * dependencies and all removed dependencies. Chris@0: * Chris@0: * Note: The two arguments do not have the same structure. Chris@0: * Chris@0: * @param array[] $plugin_dependencies Chris@0: * A list of dependencies having the same structure as the return value of Chris@0: * ConfigEntityInterface::calculateDependencies(). Chris@0: * @param array[] $removed_dependencies Chris@0: * A list of dependencies having the same structure as the input argument of Chris@0: * ConfigEntityInterface::onDependencyRemoval(). Chris@0: * Chris@0: * @return array Chris@0: * A recursively computed intersection. Chris@0: * Chris@0: * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::calculateDependencies() Chris@0: * @see \Drupal\Core\Config\Entity\ConfigEntityInterface::onDependencyRemoval() Chris@0: */ Chris@0: protected function getPluginRemovedDependencies(array $plugin_dependencies, array $removed_dependencies) { Chris@0: $intersect = []; Chris@0: foreach ($plugin_dependencies as $type => $dependencies) { Chris@0: if ($removed_dependencies[$type]) { Chris@0: // Config and content entities have the dependency names as keys while Chris@0: // module and theme dependencies are indexed arrays of dependency names. Chris@0: // @see \Drupal\Core\Config\ConfigManager::callOnDependencyRemoval() Chris@0: if (in_array($type, ['config', 'content'])) { Chris@0: $removed = array_intersect_key($removed_dependencies[$type], array_flip($dependencies)); Chris@0: } Chris@0: else { Chris@0: $removed = array_values(array_intersect($removed_dependencies[$type], $dependencies)); Chris@0: } Chris@0: if ($removed) { Chris@0: $intersect[$type] = $removed; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $intersect; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the default region. Chris@0: * Chris@0: * @return string Chris@0: * The default region for this display. Chris@0: */ Chris@0: protected function getDefaultRegion() { Chris@0: return 'content'; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function __sleep() { Chris@0: // Only store the definition, not external objects or derived data. Chris@0: $keys = array_keys($this->toArray()); Chris@0: // In addition, we need to keep the entity type and the "is new" status. Chris@0: $keys[] = 'entityTypeId'; Chris@0: $keys[] = 'enforceIsNew'; Chris@0: // Keep track of the serialized keys, to avoid calling toArray() again in Chris@0: // __wakeup(). Because of the way __sleep() works, the data has to be Chris@0: // present in the object to be included in the serialized values. Chris@0: $keys[] = '_serializedKeys'; Chris@0: $this->_serializedKeys = $keys; Chris@0: return $keys; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function __wakeup() { Chris@0: // Determine what were the properties from toArray() that were saved in Chris@0: // __sleep(). Chris@0: $keys = $this->_serializedKeys; Chris@0: unset($this->_serializedKeys); Chris@0: $values = array_intersect_key(get_object_vars($this), array_flip($keys)); Chris@0: // Run those values through the __construct(), as if they came from a Chris@0: // regular entity load. Chris@0: $this->__construct($values, $this->entityTypeId); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Provides the 'system' channel logger service. Chris@0: * Chris@0: * @return \Psr\Log\LoggerInterface Chris@0: * The 'system' channel logger. Chris@0: */ Chris@0: protected function getLogger() { Chris@0: return \Drupal::logger('system'); Chris@0: } Chris@0: Chris@0: }