You are here

class BUEditor in BUEditor 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Editor/BUEditor.php \Drupal\bueditor\Plugin\Editor\BUEditor

Defines BUEditor as an Editor plugin.

Plugin annotation


@Editor(
  id = "bueditor",
  label = "BUEditor",
  supports_content_filtering = FALSE,
  supports_inline_editing = FALSE,
  is_xss_safe = TRUE,
  supported_element_types = {
    "textarea"
  }
)

Hierarchy

Expanded class hierarchy of BUEditor

3 string references to 'BUEditor'
bueditor.info.yml in ./bueditor.info.yml
bueditor.info.yml
bueditor.links.menu.yml in ./bueditor.links.menu.yml
bueditor.links.menu.yml
bueditor.routing.yml in ./bueditor.routing.yml
bueditor.routing.yml

File

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

Namespace

Drupal\bueditor\Plugin\Editor
View source
class BUEditor extends EditorBase {

  /**
   * {@inheritdoc}
   */
  public function getDefaultSettings() {
    $settings['default_editor'] = '';
    return $settings;
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsFormValidate(array $form, FormStateInterface $form_state) {
    $settings =& $form_state
      ->getValue([
      'editor',
      'settings',
    ]);

    // Remove empty role editor pairs.
    if (isset($settings['roles_editors'])) {
      $settings['roles_editors'] = array_filter($settings['roles_editors']);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getLibraries(Editor $editor) {
    $bueditor_editor = $this
      ->getBUEditorEditor($editor);
    return $bueditor_editor ? $bueditor_editor
      ->getLibraries($editor) : [];
  }

  /**
   * {@inheritdoc}
   */
  public function getJSSettings(Editor $editor) {
    $bueditor_editor = $this
      ->getBUEditorEditor($editor);
    return $bueditor_editor ? $bueditor_editor
      ->getJSSettings($editor) : [];
  }

  /**
   * Returns the selected BUEditor Editor entity for an account from editor settings.
   */
  public static function getBUEditorEditor(Editor $editor, AccountInterface $account = NULL) {
    if (!isset($account)) {
      $account = \Drupal::currentUser();
    }
    $id = static::getBUEditorEditorId($editor, $account);
    return $id ? \Drupal::entityTypeManager()
      ->getStorage('bueditor_editor')
      ->load($id) : FALSE;
  }

  /**
   * Returns the selected BUEditor Editor id for an account from editor settings.
   */
  public static function getBUEditorEditorId(Editor $editor, AccountInterface $account) {
    $settings = $editor
      ->getSettings();
    if (!empty($settings['roles_editors'])) {

      // Filter roles in two steps. May avoid a db hit by filter_get_roles_by_format().
      if ($roles_editors = array_intersect_key($settings['roles_editors'], array_flip($account
        ->getRoles()))) {
        if ($roles_editors = array_intersect_key($roles_editors, filter_get_roles_by_format($editor
          ->getFilterFormat()))) {
          return reset($roles_editors);
        }
      }
    }
    return $settings['default_editor'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BUEditor::getBUEditorEditor public static function Returns the selected BUEditor Editor entity for an account from editor settings.
BUEditor::getBUEditorEditorId public static function Returns the selected BUEditor Editor id for an account from editor settings.
BUEditor::getDefaultSettings public function Returns the default settings for this configurable text editor. Overrides EditorBase::getDefaultSettings
BUEditor::getJSSettings public function Returns JavaScript settings to be attached. Overrides EditorPluginInterface::getJSSettings
BUEditor::getLibraries public function Returns libraries to be attached. Overrides EditorPluginInterface::getLibraries
BUEditor::settingsForm public function
BUEditor::settingsFormValidate public function
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
EditorBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 3
EditorBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 1
EditorBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.