3 use Drupal\Core\Form\FormStateInterface;
6 * Implements hook_form_FORM_ID_alter() on behalf of ckeditor.module.
8 function ckeditor_form_embed_button_add_form_alter(array &$form, FormStateInterface $form_state) {
9 $form['#validate'][] = 'ckeditor_form_embed_button_add_form_validate';
13 * CKEditor-validation callback for new embed buttons.
15 * Checks to make sure that when adding a new embed button, its ID will not
16 * conflict with any existing CKEditor buttons.
18 function ckeditor_form_embed_button_add_form_validate(array &$form, FormStateInterface $form_state) {
19 /** @var \Drupal\ckeditor\CKEditorPluginManager $ckeditor_plugin_manager */
20 $ckeditor_plugin_manager = \Drupal::service('plugin.manager.ckeditor.plugin');
22 // Get a list of all buttons that are provided by all plugins.
23 $button_ids = array_reduce($ckeditor_plugin_manager->getButtons(), function ($result, $item) {
24 return array_merge($result, array_keys($item));
27 // Ensure that button ID is unique.
28 // @todo Should this do a case-insensitive comparison?
29 $button_id = $form_state->getValue('id');
30 if (in_array($button_id, $button_ids)) {
31 $form_state->setErrorByName('id', t('A CKEditor button with ID %id already exists.', ['%id' => $button_id]));