function AddNavigationBlock::apply

Applies the config action.

Parameters

string $configName: The name of the config to apply the action to.

mixed $value: The value for the action to use.

Overrides ConfigActionPluginInterface::apply

File

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

Class

AddNavigationBlock
@internal This API is experimental.

Namespace

Drupal\navigation\Plugin\ConfigAction

Code

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();
}

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