class StylesCombo in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/ckeditor/src/Plugin/CKEditorPlugin/StylesCombo.php \Drupal\ckeditor\Plugin\CKEditorPlugin\StylesCombo
Defines the "stylescombo" plugin.
Plugin annotation
@CKEditorPlugin(
  id = "stylescombo",
  label = @Translation("Styles dropdown")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, StringTranslationTrait- class \Drupal\ckeditor\CKEditorPluginBase implements CKEditorPluginButtonsInterface, CKEditorPluginInterface- class \Drupal\ckeditor\Plugin\CKEditorPlugin\StylesCombo implements CKEditorPluginConfigurableInterface
 
 
- class \Drupal\ckeditor\CKEditorPluginBase implements CKEditorPluginButtonsInterface, CKEditorPluginInterface
 
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, StringTranslationTrait
Expanded class hierarchy of StylesCombo
File
- core/modules/ ckeditor/ src/ Plugin/ CKEditorPlugin/ StylesCombo.php, line 23 
- Contains \Drupal\ckeditor\Plugin\CKEditorPlugin\StylesCombo.
Namespace
Drupal\ckeditor\Plugin\CKEditorPluginView source
class StylesCombo extends CKEditorPluginBase implements CKEditorPluginConfigurableInterface {
  /**
   * {@inheritdoc}
   */
  public function isInternal() {
    return TRUE;
  }
  /**
   * {@inheritdoc}
   */
  public function getFile() {
    // This plugin is already part of Drupal core's CKEditor build.
    return FALSE;
  }
  /**
   * {@inheritdoc}
   */
  public function getConfig(Editor $editor) {
    $config = array();
    $settings = $editor
      ->getSettings();
    if (!isset($settings['plugins']['stylescombo']['styles'])) {
      return $config;
    }
    $styles = $settings['plugins']['stylescombo']['styles'];
    $config['stylesSet'] = $this
      ->generateStylesSetSetting($styles);
    return $config;
  }
  /**
   * {@inheritdoc}
   */
  public function getButtons() {
    return array(
      'Styles' => array(
        'label' => t('Font style'),
        'image_alternative' => [
          '#type' => 'inline_template',
          '#template' => '<a href="#" role="button" aria-label="{{ styles_text }}"><span class="ckeditor-button-dropdown">{{ styles_text }}<span class="ckeditor-button-arrow"></span></span></a>',
          '#context' => [
            'styles_text' => t('Styles'),
          ],
        ],
      ),
    );
  }
  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state, Editor $editor) {
    // Defaults.
    $config = array(
      'styles' => '',
    );
    $settings = $editor
      ->getSettings();
    if (isset($settings['plugins']['stylescombo'])) {
      $config = $settings['plugins']['stylescombo'];
    }
    $form['styles'] = array(
      '#title' => t('Styles'),
      '#title_display' => 'invisible',
      '#type' => 'textarea',
      '#default_value' => $config['styles'],
      '#description' => t('A list of classes that will be provided in the "Styles" dropdown. Enter one or more classes on each line in the format: element.classA.classB|Label. Example: h1.title|Title. Advanced example: h1.fancy.title|Fancy title.<br />These styles should be available in your theme\'s CSS file.'),
      '#attached' => array(
        'library' => array(
          'ckeditor/drupal.ckeditor.stylescombo.admin',
        ),
      ),
      '#element_validate' => array(
        array(
          $this,
          'validateStylesValue',
        ),
      ),
    );
    return $form;
  }
  /**
   * #element_validate handler for the "styles" element in settingsForm().
   */
  public function validateStylesValue(array $element, FormStateInterface $form_state) {
    if ($this
      ->generateStylesSetSetting($element['#value']) === FALSE) {
      $form_state
        ->setError($element, t('The provided list of styles is syntactically incorrect.'));
    }
  }
  /**
   * Builds the "stylesSet" configuration part of the CKEditor JS settings.
   *
   * @see getConfig()
   *
   * @param string $styles
   *   The "styles" setting.
   * @return array|FALSE
   *   An array containing the "stylesSet" configuration, or FALSE when the
   *   syntax is invalid.
   */
  protected function generateStylesSetSetting($styles) {
    $styles_set = array();
    // Early-return when empty.
    $styles = trim($styles);
    if (empty($styles)) {
      return $styles_set;
    }
    $styles = str_replace(array(
      "\r\n",
      "\r",
    ), "\n", $styles);
    foreach (explode("\n", $styles) as $style) {
      $style = trim($style);
      // Ignore empty lines in between non-empty lines.
      if (empty($style)) {
        continue;
      }
      // Validate syntax: element[.class...]|label pattern expected.
      if (!preg_match('@^ *[a-zA-Z0-9]+ *(\\.[a-zA-Z0-9_-]+ *)*\\| *.+ *$@', $style)) {
        return FALSE;
      }
      // Parse.
      list($selector, $label) = explode('|', $style);
      $classes = explode('.', $selector);
      $element = array_shift($classes);
      // Build the data structure CKEditor's stylescombo plugin expects.
      // @see http://docs.cksource.com/CKEditor_3.x/Developers_Guide/Styles
      $configured_style = array(
        'name' => trim($label),
        'element' => trim($element),
      );
      if (!empty($classes)) {
        $configured_style['attributes'] = array(
          'class' => implode(' ', array_map('trim', $classes)),
        );
      }
      $styles_set[] = $configured_style;
    }
    return $styles_set;
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| CKEditorPluginBase:: | function | Returns a list of plugins this plugin requires. Overrides CKEditorPluginInterface:: | ||
| CKEditorPluginBase:: | function | Returns a list of libraries this plugin requires. Overrides CKEditorPluginInterface:: | 2 | |
| DependencySerializationTrait:: | protected | property | An array of service IDs keyed by property name used for serialization. | |
| DependencySerializationTrait:: | public | function | 1 | |
| DependencySerializationTrait:: | public | function | 2 | |
| PluginBase:: | protected | property | Configuration information passed into the plugin. | 2 | 
| PluginBase:: | protected | property | The plugin implementation definition. | |
| PluginBase:: | protected | property | The plugin_id. | |
| PluginBase:: | constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
| PluginBase:: | public | function | Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: | |
| PluginBase:: | public | function | Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: | |
| PluginBase:: | public | function | Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: | |
| PluginBase:: | public | function | Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: | |
| PluginBase:: | public | function | Constructs a Drupal\Component\Plugin\PluginBase object. | 69 | 
| StringTranslationTrait:: | protected | property | The string translation service. | |
| StringTranslationTrait:: | protected | function | Formats a string containing a count of items. | |
| StringTranslationTrait:: | protected | function | Returns the number of plurals supported by a given language. | |
| StringTranslationTrait:: | protected | function | Gets the string translation service. | |
| StringTranslationTrait:: | public | function | Sets the string translation service to use. | 2 | 
| StringTranslationTrait:: | protected | function | Translates a string to the current language or to a given language. | |
| StylesCombo:: | protected | function | Builds the "stylesSet" configuration part of the CKEditor JS settings. | |
| StylesCombo:: | public | function | Returns the buttons that this plugin provides, along with metadata. Overrides CKEditorPluginButtonsInterface:: | |
| StylesCombo:: | public | function | Returns the additions to CKEDITOR.config for a specific CKEditor instance. Overrides CKEditorPluginInterface:: | |
| StylesCombo:: | public | function | Returns the Drupal root-relative file path to the plugin JavaScript file. Overrides CKEditorPluginInterface:: | |
| StylesCombo:: | public | function | Indicates if this plugin is part of the optimized CKEditor build. Overrides CKEditorPluginBase:: | |
| StylesCombo:: | public | function | Returns a settings form to configure this CKEditor plugin. Overrides CKEditorPluginConfigurableInterface:: | |
| StylesCombo:: | public | function | #element_validate handler for the "styles" element in settingsForm(). | 
