Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\editor\Entity\Editor.
Angie Byron
committed
*/
namespace Drupal\editor\Entity;
Angie Byron
committed
use Drupal\Core\Config\Entity\ConfigEntityBase;
use Drupal\editor\EditorInterface;
Angie Byron
committed
/**
* Defines the configured text editor entity.
*
Alex Pott
committed
* @ConfigEntityType(
Angie Byron
committed
* id = "editor",
* label = @Translation("Text Editor"),
Angie Byron
committed
* entity_keys = {
Dries Buytaert
committed
* "id" = "format"
Angie Byron
committed
* }
* )
*/
class Editor extends ConfigEntityBase implements EditorInterface {
Angie Byron
committed
/**
* The machine name of the text format with which this configured text editor
* is associated.
*
* @var string
Angie Byron
committed
*
* @see getFilterFormat()
Angie Byron
committed
*/
Angie Byron
committed
protected $format;
Angie Byron
committed
/**
* The name (plugin ID) of the text editor.
*
* @var string
*/
Angie Byron
committed
protected $editor;
Angie Byron
committed
/**
Angie Byron
committed
* The structured array of text editor plugin-specific settings.
Angie Byron
committed
*
* @var array
*/
Angie Byron
committed
protected $settings = array();
Angie Byron
committed
/**
Angie Byron
committed
* The structured array of image upload settings.
*
* @var array
*/
Angie Byron
committed
protected $image_upload = array();
Angie Byron
committed
/**
* The filter format this text editor is associated with.
*
* @var \Drupal\filter\FilterFormatInterface
*/
protected $filterFormat;
/**
* @var \Drupal\Component\Plugin\PluginManagerInterface
*/
protected $editorPluginManager;
/**
* {@inheritdoc}
Angie Byron
committed
*/
public function id() {
return $this->format;
}
/**
* {@inheritdoc}
Angie Byron
committed
*/
public function __construct(array $values, $entity_type) {
parent::__construct($values, $entity_type);
$plugin = $this->editorPluginManager()->createInstance($this->editor);
Angie Byron
committed
// Initialize settings, merging module-provided defaults.
$default_settings = $plugin->getDefaultSettings();
$default_settings += \Drupal::moduleHandler()->invokeAll('editor_default_settings', array($this->editor));
\Drupal::moduleHandler()->alter('editor_default_settings', $default_settings, $this->editor);
Angie Byron
committed
$this->settings += $default_settings;
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
parent::calculateDependencies();
// Create a dependency on the associated FilterFormat.
$this->addDependency('entity', $this->getFilterFormat()->getConfigDependencyName());
// @todo use EntityWithPluginBagInterface so configuration between config
// entity and dependency on provider is managed automatically.
$definition = $this->editorPluginManager()->createInstance($this->editor)->getPluginDefinition();
$this->addDependency('module', $definition['provider']);
return $this->dependencies;
}
/**
* {@inheritdoc}
*/
public function hasAssociatedFilterFormat() {
return $this->format !== NULL;
}
/**
* {@inheritdoc}
*/
public function getFilterFormat() {
if (!$this->filterFormat) {
catch
committed
$this->filterFormat = \Drupal::entityManager()->getStorage('filter_format')->load($this->format);
}
return $this->filterFormat;
}
/**
* 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;
}
Angie Byron
committed
135
136
137
138
139
140
141
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
179
180
181
182
183
184
185
186
187
188
/**
* {@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;
}
/**
* {@inheritdoc}
*/
public function toArray() {
$properties = parent::toArray();
$names = array(
'format',
'editor',
'settings',
'image_upload',
);
foreach ($names as $name) {
$properties[$name] = $this->get($name);
}
return $properties;
}