You are here

public function SettingsForm::buildForm in The CodeMirror Editor 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/SettingsForm.php, line 76

Class

SettingsForm
CodeMirror editor settings form.

Namespace

Drupal\codemirror_editor\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $settings = $this
    ->config('codemirror_editor.settings')
    ->get();
  $form['cdn'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Load the library from CDN'),
    '#default_value' => $settings['cdn'],
  ];
  $form['minified'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Use minified version of the library'),
    '#default_value' => $settings['minified'],
  ];
  $codemirror_themes = static::getCodeMirrorThemes();
  $form['theme'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Theme'),
    '#options' => $codemirror_themes,
    '#default_value' => $settings['theme'],
  ];
  $form['language_modes_wrapper'] = [
    '#type' => 'details',
    '#title' => $this
      ->t('Language modes'),
    '#open' => TRUE,
  ];
  $header = [
    'label' => $this
      ->t('Mode'),
    'mime_types' => [
      'data' => $this
        ->t('Mime types'),
      'class' => [
        RESPONSIVE_PRIORITY_LOW,
      ],
    ],
    'dependencies' => $this
      ->t('Dependencies'),
    'usage' => $this
      ->t('Usage'),
  ];
  $options = [];
  $definitions = $this->modeManager
    ->getDefinitions();
  foreach ($definitions as $mode => $definition) {
    $url = Url::fromUri(sprintf('https://codemirror.net/mode/%s/index.html', $mode), [
      'attributes' => [
        'target' => '_blank',
      ],
    ]);
    $dependency_labels = [];
    foreach ($definition['dependencies'] as $dependency) {
      $dependency_labels[] = $definitions[$dependency]['label'];
    }
    $options[$mode] = [
      'label' => Link::fromTextAndUrl($definition['label'], $url),
      'mime_types' => implode(', ', $definition['mime_types']),
      'dependencies' => implode(', ', $dependency_labels),
      'usage' => implode(', ', $definition['usage']),
    ];
  }
  $form['language_modes_wrapper']['language_modes'] = [
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
    '#default_value' => array_fill_keys($settings['language_modes'], TRUE),
    '#suffix' => $this
      ->t('Language modes required by modules are always loaded.'),
  ];
  return parent::buildForm($form, $form_state);
}