class SimpleConfigSectionStorage

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/SimpleConfigSectionStorage.php \Drupal\layout_builder_test\Plugin\SectionStorage\SimpleConfigSectionStorage
  2. 8.9.x core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/SimpleConfigSectionStorage.php \Drupal\layout_builder_test\Plugin\SectionStorage\SimpleConfigSectionStorage
  3. 10 core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/SimpleConfigSectionStorage.php \Drupal\layout_builder_test\Plugin\SectionStorage\SimpleConfigSectionStorage

Provides section storage utilizing simple config.

Hierarchy

Expanded class hierarchy of SimpleConfigSectionStorage

1 file declares its use of SimpleConfigSectionStorage
SimpleConfigSectionListTest.php in core/modules/layout_builder/tests/src/Kernel/SimpleConfigSectionListTest.php

File

core/modules/layout_builder/tests/modules/layout_builder_test/src/Plugin/SectionStorage/SimpleConfigSectionStorage.php, line 28

Namespace

Drupal\layout_builder_test\Plugin\SectionStorage
View source
class SimpleConfigSectionStorage extends PluginBase implements SectionStorageInterface, SectionStorageLocalTaskProviderInterface, ContainerFactoryPluginInterface {
    use ContextAwarePluginTrait;
    use LayoutBuilderRoutesTrait;
    use SectionListTrait;
    
    /**
     * The config factory.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * An array of sections.
     *
     * @var \Drupal\layout_builder\Section[]|null
     */
    protected $sections;
    
    /**
     * {@inheritdoc}
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->configFactory = $config_factory;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static($configuration, $plugin_id, $plugin_definition, $container->get('config.factory'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getStorageType() {
        return $this->getPluginId();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getStorageId() {
        return $this->getContextValue('config_id');
    }
    
    /**
     * {@inheritdoc}
     */
    public function label() {
        return $this->getStorageId();
    }
    
    /**
     * Returns the name to be used to store in the config system.
     */
    protected function getConfigName() {
        return 'layout_builder_test.' . $this->getStorageType() . '.' . $this->getStorageId();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getSections() {
        if (is_null($this->sections)) {
            $sections = $this->configFactory
                ->get($this->getConfigName())
                ->get('sections') ?: [];
            $this->setSections(array_map([
                Section::class,
                'fromArray',
            ], $sections));
        }
        return $this->sections;
    }
    
    /**
     * {@inheritdoc}
     */
    protected function setSections(array $sections) {
        $this->sections = array_values($sections);
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function save() {
        $sections = array_map(function (Section $section) {
            return $section->toArray();
        }, $this->getSections());
        $config = $this->configFactory
            ->getEditable($this->getConfigName());
        $return = $config->get('sections') ? SAVED_UPDATED : SAVED_NEW;
        $config->set('sections', $sections)
            ->save();
        return $return;
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildRoutes(RouteCollection $collection) {
        $this->buildLayoutRoutes($collection, $this->getPluginDefinition(), 'layout-builder-test-simple-config/{id}');
    }
    
    /**
     * {@inheritdoc}
     */
    public function deriveContextsFromRoute($value, $definition, $name, array $defaults) {
        $contexts['config_id'] = new Context(new ContextDefinition('string'), $value ?: $defaults['id']);
        return $contexts;
    }
    
    /**
     * {@inheritdoc}
     */
    public function buildLocalTasks($base_plugin_definition) {
        $type = $this->getStorageType();
        $local_tasks = [];
        $local_tasks["layout_builder.{$type}.view"] = $base_plugin_definition + [
            'route_name' => "layout_builder.{$type}.view",
            'title' => $this->t('Layout'),
            'base_route' => "layout_builder.{$type}.view",
        ];
        $local_tasks["layout_builder.{$type}.view__child"] = $base_plugin_definition + [
            'route_name' => "layout_builder.{$type}.view",
            'title' => $this->t('Layout'),
            'parent_id' => "layout_builder_ui:layout_builder.{$type}.view",
        ];
        $local_tasks["layout_builder.{$type}.discard_changes"] = $base_plugin_definition + [
            'route_name' => "layout_builder.{$type}.discard_changes",
            'title' => $this->t('Discard changes'),
            'parent_id' => "layout_builder_ui:layout_builder.{$type}.view",
            'weight' => 5,
        ];
        return $local_tasks;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getLayoutBuilderUrl($rel = 'view') {
        return Url::fromRoute("layout_builder.{$this->getStorageType()}.{$rel}", [
            'id' => $this->getStorageId(),
        ]);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getRedirectUrl() {
        return $this->getLayoutBuilderUrl();
    }
    
    /**
     * {@inheritdoc}
     */
    public function access($operation, ?AccountInterface $account = NULL, $return_as_object = FALSE) {
        $result = AccessResult::allowed();
        return $return_as_object ? $result : $result->isAllowed();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getContextsDuringPreview() {
        return $this->getContexts();
    }
    
    /**
     * {@inheritdoc}
     */
    public function isApplicable(RefinableCacheableDependencyInterface $cacheability) {
        return TRUE;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ContextAwarePluginTrait::$context protected property The data objects representing the context of this plugin.
ContextAwarePluginTrait::getCacheContexts public function 10
ContextAwarePluginTrait::getCacheMaxAge public function 6
ContextAwarePluginTrait::getCacheTags public function 3
ContextAwarePluginTrait::getContext public function
ContextAwarePluginTrait::getContextDefinition public function
ContextAwarePluginTrait::getContextDefinitions public function
ContextAwarePluginTrait::getContextMapping public function 1
ContextAwarePluginTrait::getContexts public function
ContextAwarePluginTrait::getContextValue public function
ContextAwarePluginTrait::getContextValues public function
ContextAwarePluginTrait::getPluginDefinition abstract public function 1
ContextAwarePluginTrait::setContext public function 1
ContextAwarePluginTrait::setContextMapping public function
ContextAwarePluginTrait::setContextValue public function
ContextAwarePluginTrait::validateContexts public function
LayoutBuilderRoutesTrait::buildLayoutRoutes protected function Builds the layout routes for the given values.
PluginInspectionInterface::getPluginId public function Gets the plugin_id of the plugin instance. 2
SectionListTrait::addBlankSection protected function Adds a blank section to the list.
SectionListTrait::appendSection public function
SectionListTrait::count public function
SectionListTrait::getSection public function
SectionListTrait::hasBlankSection protected function Indicates if this section list contains a blank section.
SectionListTrait::hasSection protected function Indicates if there is a section at the specified delta.
SectionListTrait::insertSection public function
SectionListTrait::removeAllSections public function
SectionListTrait::removeSection public function
SectionListTrait::setSection protected function Sets the section for the given delta on the display.
SectionListTrait::__clone public function Magic method: Implements a deep clone.
SimpleConfigSectionStorage::$configFactory protected property The config factory.
SimpleConfigSectionStorage::$sections protected property An array of sections.
SimpleConfigSectionStorage::access public function Overrides \Drupal\Core\Access\AccessibleInterface::access(). Overrides SectionStorageInterface::access
SimpleConfigSectionStorage::buildLocalTasks public function Provides the local tasks dynamically for Layout Builder plugins. Overrides SectionStorageLocalTaskProviderInterface::buildLocalTasks
SimpleConfigSectionStorage::buildRoutes public function Provides the routes needed for Layout Builder UI. Overrides SectionStorageInterface::buildRoutes
SimpleConfigSectionStorage::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
SimpleConfigSectionStorage::deriveContextsFromRoute public function Derives the available plugin contexts from route values. Overrides SectionStorageInterface::deriveContextsFromRoute
SimpleConfigSectionStorage::getConfigName protected function Returns the name to be used to store in the config system.
SimpleConfigSectionStorage::getContextsDuringPreview public function Gets contexts for use during preview. Overrides SectionStorageInterface::getContextsDuringPreview
SimpleConfigSectionStorage::getLayoutBuilderUrl public function Gets the URL used to display the Layout Builder UI. Overrides SectionStorageInterface::getLayoutBuilderUrl
SimpleConfigSectionStorage::getRedirectUrl public function Gets the URL used when redirecting away from the Layout Builder UI. Overrides SectionStorageInterface::getRedirectUrl
SimpleConfigSectionStorage::getSections public function Gets the layout sections. Overrides SectionListInterface::getSections
SimpleConfigSectionStorage::getStorageId public function Returns an identifier for this storage. Overrides SectionStorageInterface::getStorageId
SimpleConfigSectionStorage::getStorageType public function Returns the type of this storage. Overrides SectionStorageInterface::getStorageType
SimpleConfigSectionStorage::isApplicable public function Determines if this section storage is applicable for the current contexts. Overrides SectionStorageInterface::isApplicable
SimpleConfigSectionStorage::label public function Gets the label for the object using the sections. Overrides SectionStorageInterface::label
SimpleConfigSectionStorage::save public function Saves the sections. Overrides SectionStorageInterface::save
SimpleConfigSectionStorage::setSections protected function Stores the information for all sections. Overrides SectionListTrait::setSections
SimpleConfigSectionStorage::__construct public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.