You are here

public function CKEditor::settingsForm in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/ckeditor/src/Plugin/Editor/CKEditor.php \Drupal\ckeditor\Plugin\Editor\CKEditor::settingsForm()

Returns a settings form to configure this text editor.

If the editor's behavior depends on extensive options and/or external data, then the implementing module can choose to provide a separate, global configuration page rather than per-text-format settings. In that case, this form should provide a link to the separate settings page.

Parameters

array $form: An empty form array to be populated with a configuration form, if any.

\Drupal\Core\Form\FormStateInterface $form_state: The state of the entire filter administration form.

\Drupal\editor\Entity\Editor $editor: A configured text editor object.

Return value

array A render array for the settings form.

Overrides EditorBase::settingsForm

File

core/modules/ckeditor/src/Plugin/Editor/CKEditor.php, line 145
Contains \Drupal\ckeditor\Plugin\Editor\CKEditor.

Class

CKEditor
Defines a CKEditor-based text editor for Drupal.

Namespace

Drupal\ckeditor\Plugin\Editor

Code

public function settingsForm(array $form, FormStateInterface $form_state, EditorEntity $editor) {
  $settings = $editor
    ->getSettings();
  $ckeditor_settings_toolbar = array(
    '#theme' => 'ckeditor_settings_toolbar',
    '#editor' => $editor,
    '#plugins' => $this->ckeditorPluginManager
      ->getButtons(),
  );
  $form['toolbar'] = array(
    '#type' => 'container',
    '#attached' => array(
      'library' => array(
        'ckeditor/drupal.ckeditor.admin',
      ),
      'drupalSettings' => [
        'ckeditor' => [
          'toolbarAdmin' => (string) $this->renderer
            ->renderPlain($ckeditor_settings_toolbar),
        ],
      ],
    ),
    '#attributes' => array(
      'class' => array(
        'ckeditor-toolbar-configuration',
      ),
    ),
  );
  $form['toolbar']['button_groups'] = array(
    '#type' => 'textarea',
    '#title' => t('Toolbar buttons'),
    '#default_value' => json_encode($settings['toolbar']['rows']),
    '#attributes' => array(
      'class' => array(
        'ckeditor-toolbar-textarea',
      ),
    ),
  );

  // CKEditor plugin settings, if any.
  $form['plugin_settings'] = array(
    '#type' => 'vertical_tabs',
    '#title' => t('CKEditor plugin settings'),
    '#attributes' => array(
      'id' => 'ckeditor-plugin-settings',
    ),
  );
  $this->ckeditorPluginManager
    ->injectPluginSettingsForm($form, $form_state, $editor);
  if (count(Element::children($form['plugins'])) === 0) {
    unset($form['plugins']);
    unset($form['plugin_settings']);
  }

  // Hidden CKEditor instance. We need a hidden CKEditor instance with all
  // plugins enabled, so we can retrieve CKEditor's per-feature metadata (on
  // which tags, attributes, styles and classes are enabled). This metadata is
  // necessary for certain filters' (e.g. the html_filter filter) settings to
  // be updated accordingly.
  // Get a list of all external plugins and their corresponding files.
  $plugins = array_keys($this->ckeditorPluginManager
    ->getDefinitions());
  $all_external_plugins = array();
  foreach ($plugins as $plugin_id) {
    $plugin = $this->ckeditorPluginManager
      ->createInstance($plugin_id);
    if (!$plugin
      ->isInternal()) {
      $all_external_plugins[$plugin_id] = $plugin
        ->getFile();
    }
  }

  // Get a list of all buttons that are provided by all plugins.
  $all_buttons = array_reduce($this->ckeditorPluginManager
    ->getButtons(), function ($result, $item) {
    return array_merge($result, array_keys($item));
  }, array());

  // Build a fake Editor object, which we'll use to generate JavaScript
  // settings for this fake Editor instance.
  $fake_editor = entity_create('editor', array(
    'format' => $editor
      ->id(),
    'editor' => 'ckeditor',
    'settings' => array(
      // Single toolbar row, single button group, all existing buttons.
      'toolbar' => array(
        'rows' => array(
          0 => array(
            0 => array(
              'name' => 'All existing buttons',
              'items' => $all_buttons,
            ),
          ),
        ),
      ),
      'plugins' => $settings['plugins'],
    ),
  ));
  $config = $this
    ->getJSSettings($fake_editor);

  // Remove the ACF configuration that is generated based on filter settings,
  // because otherwise we cannot retrieve per-feature metadata.
  unset($config['allowedContent']);
  $form['hidden_ckeditor'] = array(
    '#markup' => '<div id="ckeditor-hidden" class="hidden"></div>',
    '#attached' => array(
      'drupalSettings' => [
        'ckeditor' => [
          'hiddenCKEditorConfig' => $config,
        ],
      ],
    ),
  );
  return $form;
}