ColorButton.php 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace Drupal\colorbutton\Plugin\CKEditorPlugin;

use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\ckeditor\CKEditorPluginConfigurableInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;

/**
 * Defines the "colorbutton" plugin.
 *
 * @CKEditorPlugin(
 *   id = "colorbutton",
15
 *   label = @Translation("CKEditor Color Button")
16 17 18
 * )
 */
class ColorButton extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface {
19 20 21 22 23

  /**
   * Get path to library folder.
   */
  public function getLibraryPath() {
24 25 26 27 28 29 30
    // @todo Remove backward compatibility conditions when we bump Drupal
    //   requirement to 8.9.0. See https://www.drupal.org/node/3099614
    if (\Drupal::hasService('library.libraries_directory_file_finder')) {
      /** @var \Drupal\Core\Asset\LibrariesDirectoryFileFinder $library_file_finder */
      $path = \Drupal::service('library.libraries_directory_file_finder')->find('colorbutton');
    }
    elseif (\Drupal::moduleHandler()->moduleExists('libraries')) {
31 32
      $path = libraries_get_path('colorbutton');
    }
33 34 35
    else {
      $path = 'libraries/colorbutton';
    }
36 37 38
    return $path;
  }

39 40 41 42 43 44 45
  /**
   * {@inheritdoc}
   */
  public function getDependencies(Editor $editor) {
    return ['panelbutton'];
  }

46 47 48 49
  /**
   * {@inheritdoc}
   */
  public function getFile() {
50
    return $this->getLibraryPath() . '/plugin.js';
51 52 53 54 55 56 57 58
  }

  /**
   * {@inheritdoc}
   */
  public function getConfig(Editor $editor) {
    $settings = $editor->getSettings();

59 60
    // enableMore can only be supported if the Color Dialog plugin is present.
    $config = [
ddrozdik's avatar
ddrozdik committed
61 62
      'colorButton_enableMore' => FALSE,
      'colorButton_enableAutomatic' => TRUE,
63
    ];
64 65 66 67 68 69

    if (!empty($settings['plugins']['colorbutton']['colors'])) {
      $config['colorButton_colors'] = $settings['plugins']['colorbutton']['colors'];
    }

    return $config;
70 71 72 73 74 75
  }

  /**
   * {@inheritdoc}
   */
  public function getButtons() {
76
    $path = $this->getLibraryPath();
77
    return [
ddrozdik's avatar
ddrozdik committed
78
      'TextColor' => [
kevinquillen's avatar
kevinquillen committed
79
        'label' => $this->t('Text Color'),
80
        'image' => $path . '/icons/textcolor.png',
ddrozdik's avatar
ddrozdik committed
81 82
      ],
      'BGColor' => [
kevinquillen's avatar
kevinquillen committed
83
        'label' => $this->t('Background Color'),
84
        'image' => $path . '/icons/bgcolor.png',
ddrozdik's avatar
ddrozdik committed
85
      ],
86 87 88 89 90 91 92 93
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
    $settings = $editor->getSettings();
94

ddrozdik's avatar
ddrozdik committed
95
    $form['colors'] = [
96 97
      '#type' => 'textarea',
      '#title' => $this->t('Text Colors'),
98
      '#description' => $this->t('Enter the hex values of all colors you would like to support (without the # symbol) separated by a comma. Leave blank to use the default colors for Color Button.'),
99
      '#default_value' => !empty($settings['plugins']['colorbutton']['colors']) ? $settings['plugins']['colorbutton']['colors'] : '',
ddrozdik's avatar
ddrozdik committed
100
    ];
101

ddrozdik's avatar
ddrozdik committed
102
    $form['colors']['#element_validate'][] = [$this, 'validateInput'];
103 104
    return $form;
  }
105 106 107 108 109 110 111

  /**
   * Ensure values entered for color hex values contain no unsafe characters.
   * @param $element
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   */
  public function validateInput(array $element, FormStateInterface $form_state) {
ddrozdik's avatar
ddrozdik committed
112 113 114 115 116 117 118
    $input = $form_state->getValue([
      'editor',
      'settings',
      'plugins',
      'colorbutton',
      'colors',
    ]);
119 120 121 122 123

    if (preg_match('/([^A-F0-9,])/i', $input)) {
      $form_state->setError($element, 'Only valid hex values are allowed (A-F, 0-9). No other symbols or letters are allowed. Please check your settings and try again.');
    }
  }
124 125

}