You are here

class N1ED in N1ED - Visual editor as CKEditor plugin with Bootstrap support 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/CKEditorPlugin/n1ed.php \Drupal\n1ed\Plugin\CKEditorPlugin\n1ed

Defines plugin.

Plugin annotation


@CKEditorPlugin(
  id = "N1EDEco",
  label = @Translation("N1ED"),
  module = "n1ed"
)

Hierarchy

Expanded class hierarchy of N1ED

2 string references to 'N1ED'
n1ed.info.yml in ./n1ed.info.yml
n1ed.info.yml
n1ed_plugin in ./n1ed.module

File

src/Plugin/CKEditorPlugin/N1ED.php, line 22

Namespace

Drupal\n1ed\Plugin\CKEditorPlugin
View source
class N1ED extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, CKEditorPluginContextualInterface {

  /**
   * {@inheritdoc}
   */
  public function getButtons() {
    $buttons = [];
    $buttonsManual = json_decode(file_get_contents(__DIR__ . '/buttonsManual.json'), TRUE);
    for ($i = 0; $i < count($buttonsManual); $i++) {
      $button = $buttonsManual[$i];
      $buttons[$button['name']] = [
        'image' => 'https://n1ed.com/cdn/buttons/' . $button['name'] . '.png',
        'label' => isset($button['hint']) ? $button['hint'] : null,
      ];
    }
    $buttonsToolbar = json_decode(file_get_contents(__DIR__ . '/buttonsToolbar.json'), TRUE);
    for ($line = 0; $line < count($buttonsToolbar); $line++) {
      for ($i = 0; $i < count($buttonsToolbar[$line]); $i++) {
        $button = $buttonsToolbar[$line][$i];
        if (isset($button['name'])) {
          $buttons[$button['name']] = [
            'image' => 'https://n1ed.com/cdn/buttons/' . $button['name'] . '.png',
            'label' => isset($button['hint']) ? $button['hint'] : null,
          ];
        }
      }
    }
    return $buttons;
  }

  /**
   * {@inheritdoc}
   */
  public function getFile() {
    $settings = \Drupal::config('n1ed.settings');
    $apiKey = $settings
      ->get('apikey') ?: 'N1D8DFLT';
    $version = $settings
      ->get('version') ?: '';
    if ($version === '') {
      $version = 'latest';
    }
    $urlCache = $settings
      ->get('urlCache') ?: '';
    if ($settings
      ->get('selfHosted')) {
      return selfHostedPath . 'N1EDEco/plugin.js';
    }
    else {
      if ($urlCache === '') {

        // Fix for: https://www.drupal.org/project/n1ed/issues/3111919
        // Do not start URL with "https:" prefix.
        // Notice about cookies: developers use it to specify debug server to use,
        // all other users will use old known cloud.n1ed.com address
        return '//' . (isset($_COOKIE["N1ED_PREFIX"]) ? $_COOKIE["N1ED_PREFIX"] . "." : "") . 'cloud.n1ed.com/cdn/' . $apiKey . '/' . $version . '/ckeditor/plugins/N1EDEco/plugin.js';
      }
      else {

        // Fix for: https://www.drupal.org/project/n1ed/issues/3111919
        // Do not start URL with "https:" prefix.
        if (strpos($urlCache, "http:") === 0) {
          $urlCache = substr($urlCache, 5);
        }
        else {
          if (strpos($urlCache, "https:") === 0) {
            $urlCache = substr($urlCache, 6);
          }
        }
        return $urlCache . $apiKey . '/' . $version . '/ckeditor/plugins/N1EDEco/plugin.js';
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getDependencies(Editor $editor) {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getLibraries(Editor $editor) {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function isInternal() {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function isEnabled(Editor $editor) {
    return TRUE;
  }

  /**
   * Returnes a value of parameter or default value.
   */
  protected function getConfigParam($settings, $name, $default, $type) {
    if (isset($settings[$name]) && is_string($settings[$name]) && strlen($settings[$name]) > 0) {
      $value = $settings[$name];
    }
    else {
      $value = $default;
    }
    if (isset($type) && $type == 'number') {
      $value = intval($value);
    }
    else {
      if (isset($type) && $type == 'boolean') {
        if ($value === 'true' || $value === 1 || $value === TRUE) {
          $value = TRUE;
        }
        else {
          if ($value === 'false' || $value === 0 || $value === FALSE) {
            $value = FALSE;
          }
        }
      }
      else {
        if (isset($type) && $type == 'json') {
          if ($value != '') {
            $value = json_decode($value);
          }
        }
      }
    }
    return $value;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfig(Editor $editor) {
    $settings = [];
    if (isset($editor
      ->getSettings()['plugins']['N1EDEco'])) {
      $settings = $editor
        ->getSettings()['plugins']['N1EDEco'];
    }
    $settings["integration"] = "drupal8";
    if (\Drupal::config('n1ed.settings')
      ->get('selfHosted')) {
      $settings["urlSelfHostedHandler"] = "/admin/config/n1ed/selfHostedHandler";
      $settings["urlSelfHostedHandler__CSRF"] = "drupal8";
      $settings["apiKey"] = \Drupal::config('n1ed.settings')
        ->get('apikey');
    }
    $settings["urlSetApiKeyAndToken"] = Url::fromRoute('n1ed.setApiKey')
      ->toString();
    $settings["urlSetApiKeyAndToken__CSRF"] = "drupal8";

    // dump(Url::fromRoute('n1ed.flmngr')->toString());
    // $settings["urlFileManager"] = Url::fromRoute('n1ed.flmngr')->toString();
    // $settings["urlFileManager__CSRF"] = "drupal8";
    $settings["Flmngr"]["urlFileManager"] = Url::fromRoute('n1ed.flmngr')
      ->toString();
    $settings["Flmngr"]["urlFileManager__CSRF"] = "drupal8";

    // Get path to /sites/SITE/files/flmngr
    $settings["Flmngr"]["urlFiles"] = parse_url(file_create_url("public://flmngr"))['path'];
    $settings["Flmngr"]["dirUploads"] = '/';
    $urlCdn = \Drupal::config('n1ed.settings')
      ->get('urlCache') ?: '';
    if ($urlCdn !== '') {
      $settings["urlCdn"] = $urlCdn;
    }
    $version = \Drupal::config('n1ed.settings')
      ->get('version') ?: '';
    if ($version !== '') {
      $settings["version"] = $version;
    }
    if (\Drupal::currentUser()
      ->hasPermission("administer n1ed configuration")) {
      $settings["token"] = \Drupal::config('n1ed.settings')
        ->get('token') ?: NULL;
    }
    return $settings;
  }

  /**
   * Adds boolean value widget to the form.
   */
  protected function addBooleanToForm(&$form, $settings, $param, $default) {
    $form[$param] = [
      '#type' => 'textfield',
      '#title' => $param,
      '#title_display' => 'invisible',
      '#default_value' => $this
        ->getConfigParam($settings, $param, $default, 'boolean') ? "true" : "false",
      '#attributes' => [
        'style' => 'display: none!important',
        'data-n1ed-eco-param-name' => $param,
        'data-n1ed-eco-param-type' => 'boolean',
        'data-n1ed-eco-param-default' => $default ? "true" : "false",
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
    $config = $this
      ->getConfig($editor);
    $form['info'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#attributes' => [
        'data-n1ed-eco-plugin' => 'N1EDEco',
        'style' => 'display: inline-block;margin-top: 13px;margin-left: 10px;',
      ],
      '#value' => '<a href="#n1ed-conf">' . t('Configure add-on') . '</a>',
    ];
    $form['#attached']['library'][] = 'n1ed/n1ed';
    $form['#attached']['drupalSettings']['n1edApiKey'] = \Drupal::config('n1ed.settings')
      ->get('apikey') ?: 'N1D8DFLT';
    $form['#attached']['drupalSettings']['useFlmngrOnFileFields'] = \Drupal::config('n1ed.settings')
      ->get('useFlmngrOnFileFields') ? 1 : 0;
    $form['#attached']['drupalSettings']['selfHostedN1ED'] = \Drupal::config('n1ed.settings')
      ->get('selfHosted') ? 1 : 0;
    $form['#attached']['drupalSettings']['n1edToken'] = \Drupal::config('n1ed.settings')
      ->get('token') ?: NULL;
    $this
      ->addBooleanToForm($form, $config, "enableN1EDEcoSystem", TRUE);
    return $form;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
N1ED::addBooleanToForm protected function Adds boolean value widget to the form.
N1ED::getButtons public function Returns the buttons that this plugin provides, along with metadata. Overrides CKEditorPluginButtonsInterface::getButtons
N1ED::getConfig public function Returns the additions to CKEDITOR.config for a specific CKEditor instance. Overrides CKEditorPluginInterface::getConfig
N1ED::getConfigParam protected function Returnes a value of parameter or default value.
N1ED::getDependencies public function Returns a list of plugins this plugin requires. Overrides CKEditorPluginBase::getDependencies
N1ED::getFile public function Returns the Drupal root-relative file path to the plugin JavaScript file. Overrides CKEditorPluginInterface::getFile
N1ED::getLibraries public function Returns a list of libraries this plugin requires. Overrides CKEditorPluginBase::getLibraries
N1ED::isEnabled public function Checks if this plugin should be enabled based on the editor configuration. Overrides CKEditorPluginContextualInterface::isEnabled
N1ED::isInternal public function Indicates if this plugin is part of the optimized CKEditor build. Overrides CKEditorPluginBase::isInternal
N1ED::settingsForm public function Returns a settings form to configure this CKEditor plugin. Overrides CKEditorPluginConfigurableInterface::settingsForm
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.