You are here

protected static function CodeMirrorPluginTrait::buildCodeMirrorSettingsForm in The CodeMirror Editor 8

Returns a form to configure settings for the CodeMirror plugin.

Parameters

array $settings: The plugin settings.

array $keys: If provided only specified form elements will be rendered.

Return value

array The form definition for the plugin settings.

4 calls to CodeMirrorPluginTrait::buildCodeMirrorSettingsForm()
CodeMirrorEditor::buildConfigurationForm in src/Plugin/Editor/CodeMirrorEditor.php
Form constructor.
CodeMirrorEditor::settingsForm in src/Plugin/Filter/CodeMirrorEditor.php
Generates a filter's settings form.
CodemirrorEditorFormatter::settingsForm in src/Plugin/Field/FieldFormatter/CodemirrorEditorFormatter.php
Returns a form to configure settings for the formatter.
CodeMirrorEditorWidget::settingsForm in src/Plugin/Field/FieldWidget/CodeMirrorEditorWidget.php
Returns a form to configure settings for the widget.

File

src/CodeMirrorPluginTrait.php, line 66

Class

CodeMirrorPluginTrait
Provides a helper methods to for CodeMirror plugins.

Namespace

Drupal\codemirror_editor

Code

protected static function buildCodeMirrorSettingsForm(array $settings, array $keys = NULL) {
  $form = [];
  if (!$keys || in_array('mode', $keys)) {
    $definitions = \Drupal::service('plugin.manager.codemirror_mode')
      ->getDefinitions();
    $enabled_modes = \Drupal::config('codemirror_editor.settings')
      ->get('language_modes');
    $options = [];
    foreach ($definitions as $mode => $definition) {
      if (in_array($mode, $enabled_modes)) {
        ksort($definition['mime_types']);
        foreach ($definition['mime_types'] as $mime_type) {
          $options[$definition['label']][$mime_type] = $mime_type;
        }
      }
    }
    $form['mode'] = [
      '#type' => 'select',
      '#title' => t('Language mode'),
      '#default_value' => $settings['mode'],
      '#options' => $options,
    ];
  }
  if (!$keys || in_array('toolbar', $keys)) {
    $form['toolbar'] = [
      '#title' => t('Load toolbar'),
      '#type' => 'checkbox',
      '#default_value' => $settings['toolbar'],
    ];
  }
  if (!$keys || in_array('buttons', $keys)) {
    $form['buttons'] = [
      '#type' => 'select',
      '#multiple' => TRUE,
      '#title' => t('Toolbar buttons'),
      '#default_value' => $settings['buttons'],
      '#options' => array_combine(static::getAvailableButtons(), static::getAvailableButtons()),
      '#value_callback' => [
        static::class,
        'setButtonsValue',
      ],
      '#states' => [
        'visible' => [
          ':input[name$="[settings_edit_form][settings][toolbar]"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
      '#description' => t('Buttons that will be available inside the toolbar.'),
    ];
  }
  if (!$keys || in_array('lineWrapping', $keys)) {
    $form['lineWrapping'] = [
      '#title' => t('Line wrapping'),
      '#type' => 'checkbox',
      '#default_value' => $settings['lineWrapping'],
    ];
  }
  if (!$keys || in_array('lineNumbers', $keys)) {
    $form['lineNumbers'] = [
      '#title' => t('Line numbers'),
      '#type' => 'checkbox',
      '#default_value' => $settings['lineNumbers'],
    ];
  }
  if (!$keys || in_array('foldGutter', $keys)) {
    $form['foldGutter'] = [
      '#title' => t('Fold gutter'),
      '#type' => 'checkbox',
      '#default_value' => $settings['foldGutter'],
    ];
  }
  if (!$keys || in_array('autoCloseTags', $keys)) {
    $form['autoCloseTags'] = [
      '#title' => t('Auto close tags'),
      '#type' => 'checkbox',
      '#default_value' => $settings['autoCloseTags'],
    ];
  }
  if (!$keys || in_array('styleActiveLine', $keys)) {
    $form['styleActiveLine'] = [
      '#title' => t('Style active line'),
      '#type' => 'checkbox',
      '#default_value' => $settings['styleActiveLine'],
    ];
  }
  return $form;
}