function AddItemToToolbar::apply

Same name and namespace in other branches
  1. 10 core/modules/ckeditor5/src/Plugin/ConfigAction/AddItemToToolbar.php \Drupal\ckeditor5\Plugin\ConfigAction\AddItemToToolbar::apply()
  2. main core/modules/ckeditor5/src/Plugin/ConfigAction/AddItemToToolbar.php \Drupal\ckeditor5\Plugin\ConfigAction\AddItemToToolbar::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/ckeditor5/src/Plugin/ConfigAction/AddItemToToolbar.php, line 48

Class

AddItemToToolbar
Config action plugin to add an item to the toolbar.

Namespace

Drupal\ckeditor5\Plugin\ConfigAction

Code

public function apply(string $configName, mixed $value) : void {
  // Normalize $value, which could be one of three things:
  // - A string (add one item to the toolbar, no additional options)
  // - An associative array (add one item to the toolbar, with options)
  // - An indexed array (add multiple items to the toolbar, each of which
  //   could be one of the previous two forms)
  if (is_string($value) || is_array($value) && !array_is_list($value)) {
    $value = [
      $value,
    ];
  }
  if (!is_array($value) || $value === []) {
    throw new ConfigActionException(sprintf('The %s config action requires one or more toolbar items.', $this->pluginId));
  }
  $editor = $this->configManager
    ->loadConfigEntityByName($configName);
  assert($editor instanceof EditorInterface);
  if ($editor->getEditor() !== 'ckeditor5') {
    throw new ConfigActionException(sprintf('The %s config action only works with editors that use CKEditor 5.', $this->pluginId));
  }
  $changed = FALSE;
  foreach ($value as $item) {
    if (!is_string($item) && !is_array($item)) {
      throw new ConfigActionException(sprintf('The %s config action requires each toolbar item to be a string or an array of options.', $this->pluginId));
    }
    $changed = $this->applySingle($editor, $item) || $changed;
  }
  if ($changed) {
    $editor->save();
  }
}

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