You are here

class Wordcount in CKEditor Wordcount 8

Same name and namespace in other branches
  1. 2.x src/Plugin/CKEditorPlugin/Wordcount.php \Drupal\ckwordcount\Plugin\CKEditorPlugin\Wordcount

Defines the "wordcount" plugin.

Plugin annotation


@CKEditorPlugin(
  id = "wordcount",
  label = @Translation("Word Count & Character Count")
)

Hierarchy

Expanded class hierarchy of Wordcount

File

src/Plugin/CKEditorPlugin/Wordcount.php, line 19

Namespace

Drupal\ckwordcount\Plugin\CKEditorPlugin
View source
class Wordcount extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface, CKEditorPluginContextualInterface {

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

  /**
   * {@inheritdoc}
   */
  public function getButtons() {
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getFile() {
    return file_exists(DRUPAL_ROOT . '/libraries/wordcount/plugin.js') ? 'libraries/wordcount/plugin.js' : 'libraries/ckeditor-wordcount-plugin/wordcount/plugin.js';
  }

  /**
   * {@inheritdoc}
   */
  public function isEnabled(Editor $editor) {
    $settings = $editor
      ->getSettings();
    return isset($settings['plugins']['wordcount']) ? $settings['plugins']['wordcount']['enable'] : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getConfig(Editor $editor) {
    $settings = $editor
      ->getSettings();
    return [
      'wordcount' => [
        'showRemaining' => !empty($settings['plugins']['wordcount']['show_remaining']) ? $settings['plugins']['wordcount']['show_remaining'] : FALSE,
        'showParagraphs' => !empty($settings['plugins']['wordcount']['show_paragraphs']) ? $settings['plugins']['wordcount']['show_paragraphs'] : FALSE,
        'showWordCount' => !empty($settings['plugins']['wordcount']['show_word_count']) ? $settings['plugins']['wordcount']['show_word_count'] : FALSE,
        'showCharCount' => !empty($settings['plugins']['wordcount']['show_char_count']) ? $settings['plugins']['wordcount']['show_char_count'] : FALSE,
        'countBytesAsChars' => !empty($settings['plugins']['wordcount']['count_bytes']) ? $settings['plugins']['wordcount']['count_bytes'] : FALSE,
        'countSpacesAsChars' => !empty($settings['plugins']['wordcount']['count_spaces']) ? $settings['plugins']['wordcount']['count_spaces'] : FALSE,
        'countHTML' => !empty($settings['plugins']['wordcount']['count_html']) ? $settings['plugins']['wordcount']['count_html'] : FALSE,
        'countLineBreaks' => !empty($settings['plugins']['wordcount']['count_line_breaks']) ? $settings['plugins']['wordcount']['count_line_breaks'] : FALSE,
        'maxWordCount' => !empty($settings['plugins']['wordcount']['max_words']) ? $settings['plugins']['wordcount']['max_words'] : -1,
        'maxCharCount' => !empty($settings['plugins']['wordcount']['max_chars']) ? $settings['plugins']['wordcount']['max_chars'] : -1,
        'hardLimit' => isset($settings['plugins']['wordcount']['hard_limit']) ? $settings['plugins']['wordcount']['hard_limit'] : TRUE,
      ],
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
    $settings = $editor
      ->getSettings();
    $form['enable'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Enable the counter'),
      '#default_value' => !empty($settings['plugins']['wordcount']['enable']) ? $settings['plugins']['wordcount']['enable'] : FALSE,
    ];
    $form['show_remaining'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show remaining'),
      '#default_value' => !empty($settings['plugins']['wordcount']['show_remaining']) ? $settings['plugins']['wordcount']['show_remaining'] : FALSE,
    ];
    $form['show_paragraphs'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show the paragraphs count'),
      '#default_value' => !empty($settings['plugins']['wordcount']['show_paragraphs']) ? $settings['plugins']['wordcount']['show_paragraphs'] : FALSE,
    ];
    $form['show_word_count'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show the word count'),
      '#default_value' => !empty($settings['plugins']['wordcount']['show_word_count']) ? $settings['plugins']['wordcount']['show_word_count'] : FALSE,
    ];
    $form['show_char_count'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Show the character count'),
      '#default_value' => !empty($settings['plugins']['wordcount']['show_char_count']) ? $settings['plugins']['wordcount']['show_char_count'] : FALSE,
    ];
    $form['count_bytes'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Count bytes'),
      '#default_value' => !empty($settings['plugins']['wordcount']['count_bytes']) ? $settings['plugins']['wordcount']['count_bytes'] : FALSE,
    ];
    $form['count_spaces'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Count spaces as characters'),
      '#default_value' => !empty($settings['plugins']['wordcount']['count_spaces']) ? $settings['plugins']['wordcount']['count_spaces'] : FALSE,
    ];
    $form['count_html'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Count HTML as characters'),
      '#default_value' => !empty($settings['plugins']['wordcount']['count_html']) ? $settings['plugins']['wordcount']['count_html'] : FALSE,
    ];
    $form['count_line_breaks'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Count line breaks'),
      '#default_value' => !empty($settings['plugins']['wordcount']['count_line_breaks']) ? $settings['plugins']['wordcount']['count_line_breaks'] : FALSE,
    ];
    $form['max_words'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Maximum word limit'),
      '#description' => $this
        ->t('Enter a maximum word limit. Leave this set to -1 for unlimited.'),
      '#default_value' => !empty($settings['plugins']['wordcount']['max_words']) ? $settings['plugins']['wordcount']['max_words'] : -1,
    ];
    $form['max_chars'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Maximum character limit'),
      '#description' => $this
        ->t('Enter a maximum character limit. Leave this set to -1 for unlimited.'),
      '#default_value' => !empty($settings['plugins']['wordcount']['max_chars']) ? $settings['plugins']['wordcount']['max_chars'] : -1,
    ];
    $form['hard_limit'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Lock editor when limit is reached'),
      '#default_value' => isset($settings['plugins']['wordcount']['hard_limit']) ? $settings['plugins']['wordcount']['hard_limit'] : TRUE,
    ];
    $form['max_words']['#element_validate'][] = [
      $this,
      'isNumeric',
    ];
    $form['max_chars']['#element_validate'][] = [
      $this,
      'isNumeric',
    ];
    return $form;
  }

  /**
   * Validation function for the settings form.
   *
   * @param array $element
   *   An associative array containing the properties and children of the
   *   generic form element.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
   */
  public function isNumeric(array $element, FormStateInterface $form_state) {
    if (!is_numeric($element['#value'])) {
      $form_state
        ->setError($element, 'Value must be a number.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CKEditorPluginBase::getLibraries public function Returns a list of libraries this plugin requires. Overrides CKEditorPluginInterface::getLibraries 4
CKEditorPluginBase::isInternal public function Indicates if this plugin is part of the optimized CKEditor build. Overrides CKEditorPluginInterface::isInternal 4
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.
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.
Wordcount::getButtons public function Returns the buttons that this plugin provides, along with metadata. Overrides CKEditorPluginButtonsInterface::getButtons
Wordcount::getConfig public function Returns the additions to CKEDITOR.config for a specific CKEditor instance. Overrides CKEditorPluginInterface::getConfig
Wordcount::getDependencies public function Returns a list of plugins this plugin requires. Overrides CKEditorPluginBase::getDependencies
Wordcount::getFile public function Returns the Drupal root-relative file path to the plugin JavaScript file. Overrides CKEditorPluginInterface::getFile
Wordcount::isEnabled public function Checks if this plugin should be enabled based on the editor configuration. Overrides CKEditorPluginContextualInterface::isEnabled
Wordcount::isNumeric public function Validation function for the settings form.
Wordcount::settingsForm public function Returns a settings form to configure this CKEditor plugin. Overrides CKEditorPluginConfigurableInterface::settingsForm