EmbedCKEditorPluginBase.php in Embed 8
File
src/EmbedCKEditorPluginBase.php
View source
<?php
namespace Drupal\embed;
use Drupal\ckeditor\CKEditorPluginBase;
use Drupal\Component\Utility\Html;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\editor\Entity\Editor;
use Drupal\embed\Entity\EmbedButton;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class EmbedCKEditorPluginBase extends CKEditorPluginBase implements ContainerFactoryPluginInterface {
protected $embedButtonQuery;
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryInterface $embed_button_query) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->embedButtonQuery = $embed_button_query;
if (!empty($plugin_definition['embed_type_id'])) {
$this->embedButtonQuery
->condition('type_id', $plugin_definition['embed_type_id']);
}
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager')
->getStorage('embed_button')
->getQuery());
}
public function getButtons() {
$buttons = [];
if ($ids = $this->embedButtonQuery
->execute()) {
$embed_buttons = EmbedButton::loadMultiple($ids);
foreach ($embed_buttons as $embed_button) {
$buttons[$embed_button
->id()] = $this
->getButton($embed_button);
}
}
return $buttons;
}
protected function getButton(EmbedButtonInterface $embed_button) {
$info = [
'id' => $embed_button
->id(),
'name' => Html::escape($embed_button
->label()),
'label' => Html::escape($embed_button
->label()),
'image' => $embed_button
->getIconUrl(),
];
$definition = $this
->getPluginDefinition();
if (!empty($definition['required_filter_plugin_id'])) {
$info['required_filter_plugin_id'] = $definition['required_filter_plugin_id'];
}
return $info;
}
public function getLibraries(Editor $editor) {
return [
'embed/embed',
];
}
}