View source
<?php
namespace Drupal\bueditor;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\editor\Entity\Editor;
use Drupal\bueditor\Entity\BUEditorEditor;
class BUEditorPluginManager extends DefaultPluginManager {
protected $hooks;
public $instances;
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
parent::__construct('Plugin/BUEditorPlugin', $namespaces, $module_handler, 'Drupal\\bueditor\\BUEditorPluginInterface', 'Drupal\\bueditor\\Annotation\\BUEditorPlugin');
$this
->alterInfo('bueditor_plugin_info');
$this
->setCacheBackend($cache_backend, 'bueditor_plugins');
}
protected function findDefinitions() {
$definitions = parent::findDefinitions();
uasort($definitions, [
'Drupal\\Component\\Utility\\SortArray',
'sortByWeightElement',
]);
return $definitions;
}
public function getInstance(array $options) {
if (isset($options['id']) && ($id = $options['id'])) {
return isset($this->instances[$id]) ? $this->instances[$id] : $this
->createInstance($id);
}
}
public function getInstances() {
if (!isset($this->instances)) {
$this->instances = [];
foreach ($this
->getDefinitions() as $id => $def) {
$this->instances[$id] = $this
->createInstance($id);
}
}
return $this->instances;
}
public function getHooks() {
if (!isset($this->hooks)) {
$this->hooks = get_class_methods('Drupal\\bueditor\\BUEditorPluginInterface');
}
return $this->hooks;
}
public function invokeAll($hook, &$a = NULL, $b = NULL, $c = NULL) {
$ret = [];
if (in_array($hook, $this
->getHooks())) {
foreach ($this
->getInstances() as $plugin => $instance) {
$ret[$plugin] = $instance
->{$hook}($a, $b, $c);
}
}
return $ret;
}
public function getButtons() {
$buttons = [];
foreach ($this
->getButtonGroups() as $plugin => $group) {
$buttons = array_merge($buttons, $group['buttons']);
}
return $buttons;
}
public function getButtonGroups() {
$plugin_buttons = [];
$definitions = $this
->getDefinitions();
foreach ($this
->invokeAll('getButtons') as $plugin => $buttons) {
if ($buttons) {
foreach ($buttons as $bid => $label) {
$buttons[$bid] = (is_array($label) ? $label : [
'label' => $label,
]) + [
'id' => $bid,
];
}
$plugin_buttons[$plugin] = [
'label' => $definitions[$plugin]['label'],
'buttons' => $buttons,
];
}
}
return $plugin_buttons;
}
public function alterEditorJS(array &$js, BUEditorEditor $bueditor_editor, Editor $editor = NULL) {
return $this
->invokeAll('alterEditorJS', $js, $bueditor_editor, $editor);
}
public function alterToolbarWidget(array &$widget) {
return $this
->invokeAll('alterToolbarWidget', $widget);
}
public function alterEditorForm(array &$form, FormStateInterface $form_state, BUEditorEditor $bueditor_editor) {
return $this
->invokeAll('alterEditorForm', $form, $form_state, $bueditor_editor);
}
public function validateEditorForm(array &$form, FormStateInterface $form_state, BUEditorEditor $bueditor_editor) {
return $this
->invokeAll('validateEditorForm', $form, $form_state, $bueditor_editor);
}
}