You are here

public function BUEditor::settingsForm in BUEditor 8

File

src/Plugin/Editor/BUEditor.php, line 38

Class

BUEditor
Defines BUEditor as an Editor plugin.

Namespace

Drupal\bueditor\Plugin\Editor

Code

public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
  $settings = $editor
    ->getSettings();
  $bueditor_editors = [];
  foreach (\Drupal::entityTypeManager()
    ->getStorage('bueditor_editor')
    ->loadMultiple() as $bueditor_editor) {
    $bueditor_editors[$bueditor_editor
      ->id()] = $bueditor_editor
      ->label();
  }

  // Default editor
  $form['default_editor'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('BUEditor Editor'),
    '#options' => $bueditor_editors,
    '#default_value' => $settings['default_editor'],
    '#description' => $this
      ->t('Select the default editor for the authorized roles. Editors can be configured at <a href=":url">BUEditor admin page</a>.', [
      ':url' => Url::fromRoute('bueditor.admin')
        ->toString(),
    ]),
    '#empty_option' => '- ' . $this
      ->t('Select an editor') . ' -',
  ];

  // Roles editors
  $role_ids = [];
  if ($format_form = $form_state
    ->getCompleteForm()) {
    if (isset($format_form['roles']['#value'])) {
      $role_ids = $format_form['roles']['#value'];
    }
    elseif (isset($format_form['roles']['#default_value'])) {
      $role_ids = $format_form['roles']['#default_value'];
    }
  }
  elseif ($format = $editor
    ->getFilterFormat()) {
    $role_ids = array_keys(filter_get_roles_by_format($format));
  }
  if (count($role_ids) > 1) {
    $form['roles_editors'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Role specific editors'),
    ];
    $roles = user_roles();
    foreach ($role_ids as $role_id) {
      $form['roles_editors'][$role_id] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Editor for %role', [
          '%role' => $roles[$role_id]
            ->label(),
        ]),
        '#options' => $bueditor_editors,
        '#default_value' => isset($settings['roles_editors'][$role_id]) ? $settings['roles_editors'][$role_id] : '',
        '#empty_option' => '- ' . $this
          ->t('Use the default') . ' -',
      ];
    }
  }
  return $form;
}