class AddNavigationBlock

@internal This API is experimental.

Hierarchy

Expanded class hierarchy of AddNavigationBlock

2 string references to 'AddNavigationBlock'
AddNavigationBlockConfigActionTest::testActionOnlySupportsNavigationConfig in core/modules/navigation/tests/src/Kernel/ConfigAction/AddNavigationBlockConfigActionTest.php
Checks invalid config exception.
AddNavigationBlockConfigActionTest::testAddBlockToNavigation in core/modules/navigation/tests/src/Kernel/ConfigAction/AddNavigationBlockConfigActionTest.php
Tests add item logic.

File

core/modules/navigation/src/Plugin/ConfigAction/AddNavigationBlock.php, line 24

Namespace

Drupal\navigation\Plugin\ConfigAction
View source
final class AddNavigationBlock implements ConfigActionPluginInterface, ContainerFactoryPluginInterface {
    public function __construct(SectionStorageManagerInterface $sectionStorageManager, UuidInterface $uuidGenerator) {
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) : static {
        return new static($container->get(SectionStorageManagerInterface::class), $container->get(UuidInterface::class));
    }
    
    /**
     * {@inheritdoc}
     */
    public function apply(string $configName, mixed $value) : void {
        if ($configName !== 'navigation.block_layout') {
            throw new ConfigActionException('addNavigationBlock can only be executed for the navigation.block_layout config.');
        }
        // Load the navigation section storage.
        $navigation_storage = $this->sectionStorageManager
            ->load('navigation', [
            'navigation' => new Context(new ContextDefinition('string'), 'navigation'),
        ]);
        if (!$navigation_storage instanceof SectionStorageInterface) {
            throw new ConfigActionException('Unable to load Navigation Layout storage.');
        }
        $section = $navigation_storage->getSection(0);
        // Create the component from the recipe values.
        $delta = $value['delta'] ?? 0;
        // Weight is set to 0 because it is irrelevant now. It will be adjusted to
        // its final value in insertComponent() or appendComponent().
        $component = [
            'uuid' => $this->uuidGenerator
                ->generate(),
            'region' => $section->getDefaultRegion(),
            'weight' => 0,
            'configuration' => $value['configuration'] ?? [],
            'additional' => $value['additional'] ?? [],
        ];
        // Insert the new component in Navigation.
        $new_component = SectionComponent::fromArray($component);
        try {
            $section->insertComponent($delta, $new_component);
        } catch (\OutOfBoundsException) {
            $section->appendComponent($new_component);
        }
        $navigation_storage->save();
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AddNavigationBlock::apply public function Applies the config action. Overrides ConfigActionPluginInterface::apply
AddNavigationBlock::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
AddNavigationBlock::__construct public function

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