View source
<?php
namespace Drupal\layout_builder\Plugin\SectionStorage;
use Drupal\Component\Plugin\Context\ContextInterface as ComponentContextInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\field_ui\FieldUI;
use Drupal\layout_builder\DefaultsSectionStorageInterface;
use Drupal\layout_builder\Entity\SampleEntityGeneratorInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RouteCollection;
class DefaultsSectionStorage extends SectionStorageBase implements ContainerFactoryPluginInterface, DefaultsSectionStorageInterface {
protected $entityTypeManager;
protected $entityTypeBundleInfo;
protected $sampleEntityGenerator;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, SampleEntityGeneratorInterface $sample_entity_generator) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
$this->sampleEntityGenerator = $sample_entity_generator;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('layout_builder.sample_entity_generator'));
}
protected function getSectionList() {
return $this
->getContextValue('display');
}
protected function getDisplay() {
return $this
->getSectionList();
}
public function getStorageId() {
return $this
->getDisplay()
->id();
}
public function getRedirectUrl() {
return Url::fromRoute("entity.entity_view_display.{$this->getDisplay()->getTargetEntityTypeId()}.view_mode", $this
->getRouteParameters());
}
public function getLayoutBuilderUrl($rel = 'view') {
return Url::fromRoute("layout_builder.{$this->getStorageType()}.{$this->getDisplay()->getTargetEntityTypeId()}.{$rel}", $this
->getRouteParameters());
}
protected function getRouteParameters() {
$display = $this
->getDisplay();
$entity_type = $this->entityTypeManager
->getDefinition($display
->getTargetEntityTypeId());
$route_parameters = FieldUI::getRouteBundleParameter($entity_type, $display
->getTargetBundle());
$route_parameters['view_mode_name'] = $display
->getMode();
return $route_parameters;
}
public function buildRoutes(RouteCollection $collection) {
foreach ($this
->getEntityTypes() as $entity_type_id => $entity_type) {
if (!($entity_route = $collection
->get($entity_type
->get('field_ui_base_route')))) {
continue;
}
$path = $entity_route
->getPath() . '/display/{view_mode_name}/layout';
$defaults = [];
$defaults['entity_type_id'] = $entity_type_id;
if (strpos($path, '{bundle}') === FALSE) {
if (!$entity_type
->hasKey('bundle')) {
$defaults['bundle'] = $entity_type_id;
}
else {
$defaults['bundle_key'] = $entity_type
->getBundleEntityType();
}
}
$requirements = [];
$requirements['_field_ui_view_mode_access'] = 'administer ' . $entity_type_id . ' display';
$options = $entity_route
->getOptions();
$options['_admin_route'] = FALSE;
$this
->buildLayoutRoutes($collection, $this
->getPluginDefinition(), $path, $defaults, $requirements, $options, $entity_type_id, 'entity_view_display');
if (isset($defaults['bundle_key'])) {
$collection
->get("layout_builder.defaults.{$entity_type_id}.view")
->setOption('_field_ui', TRUE)
->setDefault('bundle', '');
}
$route_names = [
"entity.entity_view_display.{$entity_type_id}.default",
"entity.entity_view_display.{$entity_type_id}.view_mode",
];
foreach ($route_names as $route_name) {
if (!($route = $collection
->get($route_name))) {
continue;
}
$route
->addDefaults([
'section_storage_type' => $this
->getStorageType(),
'section_storage' => '',
] + $defaults);
$parameters['section_storage']['layout_builder_tempstore'] = TRUE;
$parameters = NestedArray::mergeDeep($parameters, $route
->getOption('parameters') ?: []);
$route
->setOption('parameters', $parameters);
}
}
}
protected function getEntityTypes() {
return array_filter($this->entityTypeManager
->getDefinitions(), function (EntityTypeInterface $entity_type) {
return $entity_type
->entityClassImplements(FieldableEntityInterface::class) && $entity_type
->hasHandlerClass('form', 'layout_builder') && $entity_type
->hasViewBuilderClass() && $entity_type
->get('field_ui_base_route');
});
}
public function extractIdFromRoute($value, $definition, $name, array $defaults) {
@trigger_error('\\Drupal\\layout_builder\\SectionStorageInterface::extractIdFromRoute() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. \\Drupal\\layout_builder\\SectionStorageInterface::deriveContextsFromRoute() should be used instead. See https://www.drupal.org/node/3016262.', E_USER_DEPRECATED);
if (is_string($value) && strpos($value, '.') !== FALSE) {
return $value;
}
if (empty($defaults['bundle']) && isset($defaults['bundle_key']) && !empty($defaults[$defaults['bundle_key']])) {
$defaults['bundle'] = $defaults[$defaults['bundle_key']];
}
if (!empty($defaults['entity_type_id']) && !empty($defaults['bundle']) && !empty($defaults['view_mode_name'])) {
return $defaults['entity_type_id'] . '.' . $defaults['bundle'] . '.' . $defaults['view_mode_name'];
}
}
public function getSectionListFromId($id) {
@trigger_error('\\Drupal\\layout_builder\\SectionStorageInterface::getSectionListFromId() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. The section list should be derived from context. See https://www.drupal.org/node/3016262.', E_USER_DEPRECATED);
if (strpos($id, '.') === FALSE) {
throw new \InvalidArgumentException(sprintf('The "%s" ID for the "%s" section storage type is invalid', $id, $this
->getStorageType()));
}
$storage = $this->entityTypeManager
->getStorage('entity_view_display');
if (!($display = $storage
->load($id))) {
list($entity_type_id, $bundle, $view_mode) = explode('.', $id, 3);
$display = $storage
->create([
'targetEntityType' => $entity_type_id,
'bundle' => $bundle,
'mode' => $view_mode,
'status' => TRUE,
]);
}
return $display;
}
public function getContextsDuringPreview() {
$contexts = parent::getContextsDuringPreview();
$display = $this
->getDisplay();
$entity = $this->sampleEntityGenerator
->get($display
->getTargetEntityTypeId(), $display
->getTargetBundle());
$contexts['layout_builder.entity'] = EntityContext::fromEntity($entity);
return $contexts;
}
public function deriveContextsFromRoute($value, $definition, $name, array $defaults) {
$contexts = [];
if ($entity = $this
->extractEntityFromRoute($value, $defaults)) {
$contexts['display'] = EntityContext::fromEntity($entity);
}
return $contexts;
}
private function extractEntityFromRoute($value, array $defaults) {
if (empty($defaults['bundle']) && isset($defaults['bundle_key']) && !empty($defaults[$defaults['bundle_key']])) {
$defaults['bundle'] = $defaults[$defaults['bundle_key']];
}
if (is_string($value) && strpos($value, '.') !== FALSE) {
list($entity_type_id, $bundle, $view_mode) = explode('.', $value, 3);
}
elseif (!empty($defaults['entity_type_id']) && !empty($defaults['bundle']) && !empty($defaults['view_mode_name'])) {
$entity_type_id = $defaults['entity_type_id'];
$bundle = $defaults['bundle'];
$view_mode = $defaults['view_mode_name'];
$value = "{$entity_type_id}.{$bundle}.{$view_mode}";
}
else {
return NULL;
}
$storage = $this->entityTypeManager
->getStorage('entity_view_display');
if (!($display = $storage
->load($value))) {
$display = $storage
->create([
'targetEntityType' => $entity_type_id,
'bundle' => $bundle,
'mode' => $view_mode,
'status' => TRUE,
]);
}
return $display;
}
public function label() {
return $this
->getDisplay()
->label();
}
public function save() {
return $this
->getDisplay()
->save();
}
public function isOverridable() {
return $this
->getDisplay()
->isOverridable();
}
public function setOverridable($overridable = TRUE) {
$this
->getDisplay()
->setOverridable($overridable);
return $this;
}
public function setThirdPartySetting($module, $key, $value) {
$this
->getDisplay()
->setThirdPartySetting($module, $key, $value);
return $this;
}
public function isLayoutBuilderEnabled() {
return $this
->getDisplay()
->isLayoutBuilderEnabled();
}
public function enableLayoutBuilder() {
$this
->getDisplay()
->enableLayoutBuilder();
return $this;
}
public function disableLayoutBuilder() {
$this
->getDisplay()
->disableLayoutBuilder();
return $this;
}
public function getThirdPartySetting($module, $key, $default = NULL) {
return $this
->getDisplay()
->getThirdPartySetting($module, $key, $default);
}
public function getThirdPartySettings($module) {
return $this
->getDisplay()
->getThirdPartySettings($module);
}
public function unsetThirdPartySetting($module, $key) {
$this
->getDisplay()
->unsetThirdPartySetting($module, $key);
return $this;
}
public function getThirdPartyProviders() {
return $this
->getDisplay()
->getThirdPartyProviders();
}
public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
$result = AccessResult::allowedIf($this
->isLayoutBuilderEnabled())
->addCacheableDependency($this);
return $return_as_object ? $result : $result
->isAllowed();
}
public function isApplicable(RefinableCacheableDependencyInterface $cacheability) {
$cacheability
->addCacheableDependency($this);
return $this
->isLayoutBuilderEnabled();
}
public function setContext($name, ComponentContextInterface $context) {
if ($name === 'display') {
$this
->setContextValue('view_mode', $context
->getContextValue()
->getMode());
}
parent::setContext($name, $context);
}
}