Editor.php 3.87 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\editor\Entity\Editor.
6 7
 */

8
namespace Drupal\editor\Entity;
9 10

use Drupal\Core\Config\Entity\ConfigEntityBase;
11
use Drupal\editor\EditorInterface;
12 13 14 15

/**
 * Defines the configured text editor entity.
 *
16
 * @ConfigEntityType(
17
 *   id = "editor",
18
 *   label = @Translation("Text Editor"),
19
 *   entity_keys = {
20
 *     "id" = "format"
21 22 23
 *   }
 * )
 */
24
class Editor extends ConfigEntityBase implements EditorInterface {
25 26 27 28 29 30

  /**
   * The machine name of the text format with which this configured text editor
   * is associated.
   *
   * @var string
31 32
   *
   * @see getFilterFormat()
33
   */
34
  protected $format;
35 36 37 38 39 40

  /**
   * The name (plugin ID) of the text editor.
   *
   * @var string
   */
41
  protected $editor;
42 43

  /**
44
   * The structured array of text editor plugin-specific settings.
45 46 47
   *
   * @var array
   */
48
  protected $settings = array();
49

50
  /**
51
   * The structured array of image upload settings.
52 53 54
   *
   * @var array
   */
55
  protected $image_upload = array();
56

57
  /**
58 59 60 61 62 63
   * The filter format this text editor is associated with.
   *
   * @var \Drupal\filter\FilterFormatInterface
   */
  protected $filterFormat;

64 65 66 67 68
  /**
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $editorPluginManager;

69 70
  /**
   * {@inheritdoc}
71 72 73 74 75 76
   */
  public function id() {
    return $this->format;
  }

  /**
77
   * {@inheritdoc}
78 79 80 81
   */
  public function __construct(array $values, $entity_type) {
    parent::__construct($values, $entity_type);

82
    $plugin = $this->editorPluginManager()->createInstance($this->editor);
83 84 85

    // Initialize settings, merging module-provided defaults.
    $default_settings = $plugin->getDefaultSettings();
86
    $default_settings += \Drupal::moduleHandler()->invokeAll('editor_default_settings', array($this->editor));
87
    \Drupal::moduleHandler()->alter('editor_default_settings', $default_settings, $this->editor);
88 89 90
    $this->settings += $default_settings;
  }

91 92 93 94 95 96 97
  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this->getFilterFormat()->label();
  }

98 99 100 101 102 103 104
  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    parent::calculateDependencies();
    // Create a dependency on the associated FilterFormat.
    $this->addDependency('entity', $this->getFilterFormat()->getConfigDependencyName());
105
    // @todo use EntityWithPluginBagsInterface so configuration between config
106 107 108 109 110 111
    //   entity and dependency on provider is managed automatically.
    $definition = $this->editorPluginManager()->createInstance($this->editor)->getPluginDefinition();
    $this->addDependency('module', $definition['provider']);
    return $this->dependencies;
  }

112 113 114 115 116 117 118
  /**
   * {@inheritdoc}
   */
  public function hasAssociatedFilterFormat() {
    return $this->format !== NULL;
  }

119 120 121 122 123
  /**
   * {@inheritdoc}
   */
  public function getFilterFormat() {
    if (!$this->filterFormat) {
124
      $this->filterFormat = \Drupal::entityManager()->getStorage('filter_format')->load($this->format);
125 126 127 128
    }
    return $this->filterFormat;
  }

129 130 131 132 133 134 135 136 137 138 139 140 141
  /**
   * Returns the editor plugin manager.
   *
   * @return \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected function editorPluginManager() {
    if (!$this->editorPluginManager) {
      $this->editorPluginManager = \Drupal::service('plugin.manager.editor');
    }

    return $this->editorPluginManager;
  }

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  /**
   * {@inheritdoc}
   */
  public function getEditor() {
    return $this->editor;
  }

  /**
   * {@inheritdoc}
   */
  public function getSettings() {
    return $this->settings;
  }

  /**
   * {@inheritdoc}
   */
  public function setSettings(array $settings) {
    $this->settings = $settings;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function getImageUploadSettings() {
    return $this->image_upload;
  }

  /**
   * {@inheritdoc}
   */
  public function setImageUploadSettings(array $image_upload_settings) {
    $this->image_upload = $image_upload_settings;
    return $this;
  }

179
}