You are here

function ckeditor_form_embed_button_add_form_validate in Embed 8

CKEditor-validation callback for new embed buttons.

Checks to make sure that when adding a new embed button, its ID will not conflict with any existing CKEditor buttons.

1 string reference to 'ckeditor_form_embed_button_add_form_validate'
ckeditor_form_embed_button_add_form_alter in ./embed.module
Implements hook_form_FORM_ID_alter() on behalf of ckeditor.module.

File

./embed.module, line 20

Code

function ckeditor_form_embed_button_add_form_validate(array &$form, FormStateInterface $form_state) {

  /** @var \Drupal\ckeditor\CKEditorPluginManager $ckeditor_plugin_manager */
  $ckeditor_plugin_manager = \Drupal::service('plugin.manager.ckeditor.plugin');

  // Get a list of all buttons that are provided by all plugins.
  $button_ids = array_reduce($ckeditor_plugin_manager
    ->getButtons(), function ($result, $item) {
    return array_merge($result, array_keys($item));
  }, []);

  // Ensure that button ID is unique.
  // @todo Should this do a case-insensitive comparison?
  $button_id = $form_state
    ->getValue('id');
  if (in_array($button_id, $button_ids)) {
    $form_state
      ->setErrorByName('id', t('A CKEditor button with ID %id already exists.', [
      '%id' => $button_id,
    ]));
  }
}