You are here

CkeditorTableSelection.php in CKEditor Table Selection 8

File

src/Plugin/CKEditorPlugin/CkeditorTableSelection.php
View source
<?php

namespace Drupal\ckeditor_tableselection\Plugin\CKEditorPlugin;

use Drupal\Core\Plugin\PluginBase;
use Drupal\editor\Entity\Editor;
use Drupal\ckeditor\CKEditorPluginInterface;
use Drupal\ckeditor\CKEditorPluginContextualInterface;
use Drupal\ckeditor\CKEditorPluginCssInterface;

/**
 * Defines the "Table Selection" plugin.
 *
 * @CKEditorPlugin(
 *   id = "tableselection",
 *   label = @Translation("Table Selection")
 * )
 */
class CkeditorTableSelection extends PluginBase implements CKEditorPluginInterface, CKEditorPluginContextualInterface, CKEditorPluginCssInterface {

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

  /**
   * Returns ckeditor tableselection plugin path relative to drupal root.
   *
   * @return string
   *   Relative path to the ckeditor plugin folder
   */
  private function getPluginPath() {
    return 'libraries/tableselection';
  }

  /**
   * {@inheritdoc}
   */
  public function getFile() {
    return $this
      ->getPluginPath() . '/plugin.js';
  }

  /**
   * {@inheritdoc}
   */
  public function isEnabled(Editor $editor) {
    if (!$editor
      ->hasAssociatedFilterFormat()) {
      return FALSE;
    }

    // Automatically enable this plugin if the text format associated with this
    // text editor uses the filter_align or filter_caption filter and the
    // Table button is enabled.
    $format = $editor
      ->getFilterFormat();
    if ($format
      ->filters('filter_align')->status || $format
      ->filters('filter_caption')->status) {
      $settings = $editor
        ->getSettings();
      foreach ($settings['toolbar']['rows'] as $row) {
        foreach ($row as $group) {
          foreach ($group['items'] as $button) {
            if ($button === 'Table') {
              return TRUE;
            }
          }
        }
      }
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getCssFiles(Editor $editor) {
    return [
      $this
        ->getPluginPath() . '/styles/tableselection.css',
    ];
  }

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

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

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

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

}

Classes

Namesort descending Description
CkeditorTableSelection Defines the "Table Selection" plugin.