1a417d77a180bbd949440adbf82b85f54997d23f
[yaffs-website] / web / core / modules / editor / src / Entity / Editor.php
1 <?php
2
3 namespace Drupal\editor\Entity;
4
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\editor\EditorInterface;
7
8 /**
9  * Defines the configured text editor entity.
10  *
11  * @ConfigEntityType(
12  *   id = "editor",
13  *   label = @Translation("Text Editor"),
14  *   handlers = {
15  *     "access" = "Drupal\editor\EditorAccessControlHandler",
16  *   },
17  *   entity_keys = {
18  *     "id" = "format"
19  *   },
20  *   config_export = {
21  *     "format",
22  *     "editor",
23  *     "settings",
24  *     "image_upload",
25  *   }
26  * )
27  */
28 class Editor extends ConfigEntityBase implements EditorInterface {
29
30   /**
31    * The machine name of the text format with which this configured text editor
32    * is associated.
33    *
34    * @var string
35    *
36    * @see getFilterFormat()
37    */
38   protected $format;
39
40   /**
41    * The name (plugin ID) of the text editor.
42    *
43    * @var string
44    */
45   protected $editor;
46
47   /**
48    * The structured array of text editor plugin-specific settings.
49    *
50    * @var array
51    */
52   protected $settings = [];
53
54   /**
55    * The structured array of image upload settings.
56    *
57    * @var array
58    */
59   protected $image_upload = [];
60
61   /**
62    * The filter format this text editor is associated with.
63    *
64    * @var \Drupal\filter\FilterFormatInterface
65    */
66   protected $filterFormat;
67
68   /**
69    * @var \Drupal\Component\Plugin\PluginManagerInterface
70    */
71   protected $editorPluginManager;
72
73   /**
74    * {@inheritdoc}
75    */
76   public function id() {
77     return $this->format;
78   }
79
80   /**
81    * {@inheritdoc}
82    */
83   public function __construct(array $values, $entity_type) {
84     parent::__construct($values, $entity_type);
85
86     $plugin = $this->editorPluginManager()->createInstance($this->editor);
87     $this->settings += $plugin->getDefaultSettings();
88   }
89
90   /**
91    * {@inheritdoc}
92    */
93   public function label() {
94     return $this->getFilterFormat()->label();
95   }
96
97   /**
98    * {@inheritdoc}
99    */
100   public function calculateDependencies() {
101     parent::calculateDependencies();
102     // Create a dependency on the associated FilterFormat.
103     $this->addDependency('config', $this->getFilterFormat()->getConfigDependencyName());
104     // @todo use EntityWithPluginCollectionInterface so configuration between
105     //   config entity and dependency on provider is managed automatically.
106     $definition = $this->editorPluginManager()->createInstance($this->editor)->getPluginDefinition();
107     $this->addDependency('module', $definition['provider']);
108     return $this;
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public function hasAssociatedFilterFormat() {
115     return $this->format !== NULL;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function getFilterFormat() {
122     if (!$this->filterFormat) {
123       $this->filterFormat = \Drupal::entityManager()->getStorage('filter_format')->load($this->format);
124     }
125     return $this->filterFormat;
126   }
127
128   /**
129    * Returns the editor plugin manager.
130    *
131    * @return \Drupal\Component\Plugin\PluginManagerInterface
132    */
133   protected function editorPluginManager() {
134     if (!$this->editorPluginManager) {
135       $this->editorPluginManager = \Drupal::service('plugin.manager.editor');
136     }
137
138     return $this->editorPluginManager;
139   }
140
141   /**
142    * {@inheritdoc}
143    */
144   public function getEditor() {
145     return $this->editor;
146   }
147
148   /**
149    * {@inheritdoc}
150    */
151   public function setEditor($editor) {
152     $this->editor = $editor;
153     return $this;
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   public function getSettings() {
160     return $this->settings;
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   public function setSettings(array $settings) {
167     $this->settings = $settings;
168     return $this;
169   }
170
171   /**
172    * {@inheritdoc}
173    */
174   public function getImageUploadSettings() {
175     return $this->image_upload;
176   }
177
178   /**
179    * {@inheritdoc}
180    */
181   public function setImageUploadSettings(array $image_upload_settings) {
182     $this->image_upload = $image_upload_settings;
183     return $this;
184   }
185
186 }