You are here

class PageManagerSectionStorage in Page Manager 8.4

Defines the 'page_manager' section storage type.

Plugin annotation


@SectionStorage(
  id = "page_manager",
  context_definitions = {
    "entity" = @ContextDefinition("entity:page_variant"),
  },
)

Hierarchy

Expanded class hierarchy of PageManagerSectionStorage

File

src/Plugin/SectionStorage/PageManagerSectionStorage.php, line 32

Namespace

Drupal\page_manager\Plugin\SectionStorage
View source
class PageManagerSectionStorage extends SectionStorageBase implements ContainerFactoryPluginInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The sample entity generator.
   *
   * @var \Drupal\layout_builder\Entity\SampleEntityGeneratorInterface
   */
  protected $sampleEntityGenerator;

  /**
   * The tempstore factory.
   *
   * @var \Drupal\Core\TempStore\SharedTempStoreFactory
   */
  protected $tempstore;

  /**
   * The entity bundle info.
   *
   * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
   */
  protected $entityTypeBundleInfo;

  /**
   * PageManagerSectionStorage constructor.
   *
   * @param array $configuration
   *   The plugin configuration, i.e. an array with configuration values keyed
   *   by configuration option name. The special key 'context' may be used to
   *   initialize the defined contexts by setting it to an array of context
   *   values keyed by context names.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\layout_builder\Entity\SampleEntityGeneratorInterface $sample_entity_generator
   *   The sample entity generator.
   * @param \Drupal\Core\TempStore\SharedTempStoreFactory $tempstore
   *   The tempstore factory.
   * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
   *   The entity bundle information.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getSectionList() {
    return $this
      ->getContextValue('entity')
      ->getVariantPlugin();
  }

  /**
   * Gets the page variant entity.
   *
   * @return \Drupal\page_manager\Entity\PageVariant
   *   The page variant entity.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  protected function getPageVariant() {
    return $this
      ->getContextValue('entity');
  }

  /**
   * {@inheritdoc}
   */
  public function getStorageId() {
    return $this
      ->getContextValue('entity')
      ->id();
  }

  /**
   * {@inheritdoc}
   */
  public function getRedirectUrl() {
    return Url::fromUri($this
      ->getPageVariant()
      ->getPage()
      ->getPath());
  }

  /**
   * {@inheritdoc}
   */
  public function getLayoutBuilderUrl($rel = 'view') {
    return Url::fromRoute("layout_builder.page_manager.view", [
      'page_variant' => $this
        ->getPageVariant()
        ->id(),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  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');
  }

  /**
   * {@inheritdoc}
   */
  public function deriveContextsFromRoute($value, $definition, $name, array $defaults) {

    // Try to load from defaults.
    $entity = $this
      ->extractEntityFromRoute($value, $defaults);

    // Otherwise try the tempstore.
    if (!$entity) {
      $entity = $this->tempstore
        ->get('page_manager.layout_builder')
        ->get($value);
    }
    return [
      'entity' => EntityContext::fromEntity($entity),
    ];
  }

  /**
   * {@inheritdoc}
   */
  private function extractEntityFromRoute($value, array $defaults) {
    if (!empty($value)) {
      return PageVariant::load($value);
    }
    return PageVariant::load($defaults['page_variant']);
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this
      ->getPageVariant()
      ->label();
  }

  /**
   * {@inheritdoc}
   */
  public function save() {
    $page_variant = $this
      ->getPageVariant();
    return $page_variant
      ->save();
  }

  /**
   * {@inheritdoc}
   */
  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();
  }

  /**
   * {@inheritdoc}
   */
  public function isApplicable(RefinableCacheableDependencyInterface $cacheability) {
    return $this
      ->isLayoutBuilderEnabled();
  }

  /**
   * Determines if Layout Builder is enabled.
   *
   * @return bool
   *   TRUE if Layout Builder is enabled, FALSE otherwise.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function isLayoutBuilderEnabled() {
    return $this
      ->getContextValue('entity')
      ->getVariantPlugin() instanceof LayoutBuilderDisplayVariant;
  }

  /**
   * {@inheritdoc}
   */
  public function getSectionListFromId($id) {

    // @todo
    // This is deprecated and can be removed before Drupal 9.0.0.
  }

  /**
   * {@inheritdoc}
   */
  public function extractIdFromRoute($value, $definition, $name, array $defaults) {

    // @todo
    // This is deprecated and can be removed before Drupal 9.0.0.
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContextAwarePluginBase::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginBase::$contexts Deprecated private property Data objects representing the contexts passed in the plugin configuration.
ContextAwarePluginBase::contextHandler protected function Wraps the context handler.
ContextAwarePluginBase::createContextFromConfiguration protected function Overrides ContextAwarePluginBase::createContextFromConfiguration
ContextAwarePluginBase::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyInterface::getCacheContexts 9
ContextAwarePluginBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyInterface::getCacheMaxAge 7
ContextAwarePluginBase::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyInterface::getCacheTags 4
ContextAwarePluginBase::getContext public function This code is identical to the Component in order to pick up a different Context class. Overrides ContextAwarePluginBase::getContext
ContextAwarePluginBase::getContextDefinition public function Overrides ContextAwarePluginBase::getContextDefinition
ContextAwarePluginBase::getContextDefinitions public function Overrides ContextAwarePluginBase::getContextDefinitions
ContextAwarePluginBase::getContextMapping public function Gets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::getContextMapping
ContextAwarePluginBase::getContexts public function Gets the defined contexts. Overrides ContextAwarePluginInterface::getContexts
ContextAwarePluginBase::getContextValue public function Gets the value for a defined context. Overrides ContextAwarePluginInterface::getContextValue
ContextAwarePluginBase::getContextValues public function Gets the values for all defined contexts. Overrides ContextAwarePluginInterface::getContextValues
ContextAwarePluginBase::setContext public function Set a context on this plugin. Overrides ContextAwarePluginBase::setContext
ContextAwarePluginBase::setContextMapping public function Sets a mapping of the expected assignment names to their context names. Overrides ContextAwarePluginInterface::setContextMapping
ContextAwarePluginBase::setContextValue public function Sets the value for a defined context. Overrides ContextAwarePluginBase::setContextValue
ContextAwarePluginBase::validateContexts public function Validates the set values for the defined contexts. Overrides ContextAwarePluginInterface::validateContexts
ContextAwarePluginBase::__get public function Implements magic __get() method.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
LayoutBuilderRoutesTrait::buildLayoutRoutes protected function Builds the layout routes for the given values.
PageManagerSectionStorage::$entityTypeBundleInfo protected property The entity bundle info.
PageManagerSectionStorage::$entityTypeManager protected property The entity type manager.
PageManagerSectionStorage::$sampleEntityGenerator protected property The sample entity generator.
PageManagerSectionStorage::$tempstore protected property The tempstore factory.
PageManagerSectionStorage::access public function Overrides \Drupal\Core\Access\AccessibleInterface::access(). Overrides SectionStorageInterface::access
PageManagerSectionStorage::buildRoutes public function Provides the routes needed for Layout Builder UI. Overrides SectionStorageInterface::buildRoutes
PageManagerSectionStorage::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
PageManagerSectionStorage::deriveContextsFromRoute public function Derives the available plugin contexts from route values. Overrides SectionStorageInterface::deriveContextsFromRoute
PageManagerSectionStorage::extractEntityFromRoute private function
PageManagerSectionStorage::extractIdFromRoute public function Configures the plugin based on route values. Overrides SectionStorageInterface::extractIdFromRoute
PageManagerSectionStorage::getContextsDuringPreview public function Gets contexts for use during preview. Overrides SectionStorageBase::getContextsDuringPreview
PageManagerSectionStorage::getLayoutBuilderUrl public function Gets the URL used to display the Layout Builder UI. Overrides SectionStorageInterface::getLayoutBuilderUrl
PageManagerSectionStorage::getPageVariant protected function Gets the page variant entity.
PageManagerSectionStorage::getRedirectUrl public function Gets the URL used when redirecting away from the Layout Builder UI. Overrides SectionStorageInterface::getRedirectUrl
PageManagerSectionStorage::getSectionList protected function Gets the section list. Overrides SectionStorageBase::getSectionList
PageManagerSectionStorage::getSectionListFromId public function Derives the section list from the storage ID. Overrides SectionStorageInterface::getSectionListFromId
PageManagerSectionStorage::getStorageId public function Returns an identifier for this storage. Overrides SectionStorageInterface::getStorageId
PageManagerSectionStorage::isApplicable public function Determines if this section storage is applicable for the current contexts. Overrides SectionStorageInterface::isApplicable
PageManagerSectionStorage::isLayoutBuilderEnabled public function Determines if Layout Builder is enabled.
PageManagerSectionStorage::label public function Gets the label for the object using the sections. Overrides SectionStorageInterface::label
PageManagerSectionStorage::save public function Saves the sections. Overrides SectionStorageInterface::save
PageManagerSectionStorage::__construct public function PageManagerSectionStorage constructor. Overrides ContextAwarePluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
SectionStorageBase::appendSection public function Appends a new section to the end of the list. Overrides SectionListInterface::appendSection
SectionStorageBase::count public function
SectionStorageBase::getSection public function Gets a domain object for the layout section. Overrides SectionListInterface::getSection
SectionStorageBase::getSections public function Gets the layout sections. Overrides SectionListInterface::getSections 1
SectionStorageBase::getStorageType public function Returns the type of this storage. Overrides SectionStorageInterface::getStorageType
SectionStorageBase::getTempstoreKey public function Gets a string suitable for use as a tempstore key. Overrides TempStoreIdentifierInterface::getTempstoreKey 1
SectionStorageBase::insertSection public function Inserts a new section at a given delta. Overrides SectionListInterface::insertSection
SectionStorageBase::removeAllSections public function Removes all of the sections. Overrides SectionListInterface::removeAllSections
SectionStorageBase::removeSection public function Removes the section at the given delta. Overrides SectionListInterface::removeSection
SectionStorageBase::setSectionList Deprecated public function Sets the section list on the storage.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
TypedDataTrait::$typedDataManager protected property The typed data manager used for creating the data types.
TypedDataTrait::getTypedDataManager public function Gets the typed data manager. 2
TypedDataTrait::setTypedDataManager public function Sets the typed data manager. 2