View source
<?php
namespace Drupal\page_manager\Plugin\SectionStorage;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\RefinableCacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\SharedTempStoreFactory;
use Drupal\Core\Url;
use Drupal\layout_builder\Entity\SampleEntityGeneratorInterface;
use Drupal\layout_builder\Plugin\SectionStorage\SectionStorageBase;
use Drupal\page_manager\Entity\PageVariant;
use Drupal\page_manager\Plugin\DisplayVariant\LayoutBuilderDisplayVariant;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\RouteCollection;
class PageManagerSectionStorage extends SectionStorageBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
protected $sampleEntityGenerator;
protected $tempstore;
protected $entityTypeBundleInfo;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, SampleEntityGeneratorInterface $sample_entity_generator, SharedTempStoreFactory $tempstore, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->sampleEntityGenerator = $sample_entity_generator;
$this->tempstore = $tempstore;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
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('layout_builder.sample_entity_generator'), $container
->get('tempstore.shared'), $container
->get('entity_type.bundle.info'));
}
protected function getSectionList() {
return $this
->getContextValue('entity')
->getVariantPlugin();
}
protected function getPageVariant() {
return $this
->getContextValue('entity');
}
public function getStorageId() {
return $this
->getContextValue('entity')
->id();
}
public function getRedirectUrl() {
return Url::fromUri($this
->getPageVariant()
->getPage()
->getPath());
}
public function getLayoutBuilderUrl($rel = 'view') {
return Url::fromRoute("layout_builder.page_manager.view", [
'page_variant' => $this
->getPageVariant()
->id(),
]);
}
public function buildRoutes(RouteCollection $collection) {
$path = '/admin/structure/page_manager/{page_variant}/layout';
$options['parameters']['page_variant']['type'] = 'entity:page_variant';
$options['_admin_route'] = FALSE;
$this
->buildLayoutRoutes($collection, $this
->getPluginDefinition(), $path, [], [], $options, '', 'page_variant');
}
public function deriveContextsFromRoute($value, $definition, $name, array $defaults) {
$entity = $this
->extractEntityFromRoute($value, $defaults);
if (!$entity) {
$entity = $this->tempstore
->get('page_manager.layout_builder')
->get($value);
}
return [
'entity' => EntityContext::fromEntity($entity),
];
}
private function extractEntityFromRoute($value, array $defaults) {
if (!empty($value)) {
return PageVariant::load($value);
}
return PageVariant::load($defaults['page_variant']);
}
public function label() {
return $this
->getPageVariant()
->label();
}
public function save() {
$page_variant = $this
->getPageVariant();
return $page_variant
->save();
}
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) {
return $this
->isLayoutBuilderEnabled();
}
public function isLayoutBuilderEnabled() {
return $this
->getContextValue('entity')
->getVariantPlugin() instanceof LayoutBuilderDisplayVariant;
}
public function getSectionListFromId($id) {
}
public function extractIdFromRoute($value, $definition, $name, array $defaults) {
}
public function getContextsDuringPreview() {
$contexts = $this
->getPageVariant()
->getContexts() + $this
->getPageVariant()
->getStaticContexts();
foreach ($contexts as $name => $context) {
if (!$context
->hasContextValue()) {
$data_type = $context
->getContextDefinition()
->getDataType();
if (strpos($data_type, 'entity:') === 0) {
list(, $entity_type_id) = explode(':', $data_type, 2);
$bundle = $entity_type_id;
if ($this->entityTypeManager
->getDefinition($entity_type_id)
->hasKey('bundle')) {
$bundle = key($this->entityTypeBundleInfo
->getBundleInfo($entity_type_id));
}
$sample = $this->sampleEntityGenerator
->get($entity_type_id, $bundle);
$contexts[$name] = Context::createFromContext($context, $sample);
}
}
}
return $contexts;
}
}