5ebbe7ac4760d5b5bb4bcb76234960b01a72e85a
[yaffs-website] / web / modules / contrib / embed / src / EmbedCKEditorPluginBase.php
1 <?php
2
3 namespace Drupal\embed;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Entity\Query\QueryInterface;
7 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
8 use Drupal\ckeditor\CKEditorPluginBase;
9 use Drupal\editor\Entity\Editor;
10 use Drupal\embed\Entity\EmbedButton;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 abstract class EmbedCKEditorPluginBase extends CKEditorPluginBase implements ContainerFactoryPluginInterface {
14
15   /**
16    * The embed button query.
17    *
18    * @var \Drupal\Core\Entity\Query\QueryInterface
19    */
20   protected $embedButtonQuery;
21
22   /**
23    * Constructs a Drupal\entity_embed\Plugin\CKEditorPlugin\DrupalEntity object.
24    *
25    * @param array $configuration
26    *   A configuration array containing information about the plugin instance.
27    * @param string $plugin_id
28    *   The plugin_id for the plugin instance.
29    * @param mixed $plugin_definition
30    *   The plugin implementation definition.
31    * @param \Drupal\Core\Entity\Query\QueryInterface $embed_button_query
32    *   The entity query object for embed button.
33    */
34   public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryInterface $embed_button_query) {
35     parent::__construct($configuration, $plugin_id, $plugin_definition);
36     $this->embedButtonQuery = $embed_button_query;
37     if (!empty($plugin_definition['embed_type_id'])) {
38       $this->embedButtonQuery->condition('type_id', $plugin_definition['embed_type_id']);
39     }
40   }
41
42   /**
43    * {@inheritdoc}
44    */
45   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
46     return new static(
47       $configuration,
48       $plugin_id,
49       $plugin_definition,
50       $container->get('entity.query')->get('embed_button')
51     );
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function getButtons() {
58     $buttons = [];
59
60     if ($ids = $this->embedButtonQuery->execute()) {
61       $embed_buttons = EmbedButton::loadMultiple($ids);
62       foreach ($embed_buttons as $embed_button) {
63         $buttons[$embed_button->id()] = $this->getButton($embed_button);
64       }
65     }
66
67     return $buttons;
68   }
69
70   protected function getButton(EmbedButtonInterface $embed_button) {
71     return [
72       'id' => $embed_button->id(),
73       'name' => Html::escape($embed_button->label()),
74       'label' => Html::escape($embed_button->label()),
75       'image' => $embed_button->getIconUrl(),
76     ];
77   }
78
79   /**
80    * {@inheritdoc}
81    */
82   public function getLibraries(Editor $editor) {
83     return [
84       'embed/embed',
85     ];
86   }
87
88 }