Commit 3941fd02 authored by catch's avatar catch
Browse files

Issue #3271057 by Wim Leers, xjm, lauriii: Move Media Library CKEditor 4...

Issue #3271057 by Wim Leers, xjm, lauriii: Move Media Library CKEditor 4 integrations from Media into CKEditor

(cherry picked from commit 13051c34)
parent 8db9a652
Loading
Loading
Loading
Loading
+73 −0
Original line number Diff line number Diff line
@@ -6,8 +6,11 @@
 */

use Drupal\Core\Url;
use Drupal\Component\Serialization\Json;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\editor\Entity\Editor;

/**
@@ -130,3 +133,73 @@ function ckeditor_library_info_alter(&$libraries, $extension) {
    $libraries['drupal.ckeditor']['drupalSettings']['ckeditor']['timestamp'] = $query_string;
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ckeditor_form_filter_format_edit_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  // Add an additional validate callback so we can ensure the media_embed filter
  // is enabled when the DrupalMediaLibrary button is enabled.
  $form['#validate'][] = 'ckeditor_filter_format_edit_form_validate';
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function ckeditor_form_filter_format_add_form_alter(array &$form, FormStateInterface $form_state, $form_id) {
  // Add an additional validate callback so we can ensure the media_embed filter
  // is enabled when the DrupalMediaLibrary button is enabled.
  $form['#validate'][] = 'ckeditor_filter_format_edit_form_validate';
}

/**
 * Validate callback to ensure the DrupalMediaLibrary button can work correctly.
 */
function ckeditor_filter_format_edit_form_validate($form, FormStateInterface $form_state) {
  if ($form_state->getTriggeringElement()['#name'] !== 'op') {
    return;
  }

  // The "DrupalMediaLibrary" button is for the CKEditor text editor.
  if ($form_state->getValue(['editor', 'editor']) !== 'ckeditor') {
    return;
  }

  $button_group_path = [
    'editor',
    'settings',
    'toolbar',
    'button_groups',
  ];

  if ($button_groups = $form_state->getValue($button_group_path)) {
    $buttons = [];
    $button_groups = Json::decode($button_groups);

    foreach ($button_groups as $button_row) {
      foreach ($button_row as $button_group) {
        $buttons = array_merge($buttons, array_values($button_group['items']));
      }
    }

    $get_filter_label = function ($filter_plugin_id) use ($form) {
      return (string) $form['filters']['order'][$filter_plugin_id]['filter']['#markup'];
    };

    if (in_array('DrupalMediaLibrary', $buttons, TRUE)) {
      $media_embed_enabled = $form_state->getValue([
        'filters',
        'media_embed',
        'status',
      ]);

      if (!$media_embed_enabled) {
        $error_message = new TranslatableMarkup('The %media-embed-filter-label filter must be enabled to use the %drupal-media-library-button button.', [
          '%media-embed-filter-label' => $get_filter_label('media_embed'),
          '%drupal-media-library-button' => new TranslatableMarkup('Insert from Media Library'),
        ]);
        $form_state->setErrorByName('filters', $error_message);
      }
    }
  }
}
Loading