3 namespace Drupal\embed\Entity;
5 use Drupal\Core\Config\Entity\ConfigEntityBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8 use Drupal\embed\EmbedButtonInterface;
11 * Defines the EmbedButton entity.
14 * id = "embed_button",
15 * label = @Translation("Embed button"),
18 * "add" = "Drupal\embed\Form\EmbedButtonForm",
19 * "edit" = "Drupal\embed\Form\EmbedButtonForm",
20 * "delete" = "Drupal\Core\Entity\EntityDeleteForm",
22 * "list_builder" = "Drupal\embed\EmbedButtonListBuilder",
24 * admin_permission = "administer embed buttons",
25 * config_prefix = "button",
31 * "edit-form" = "/admin/config/content/embed/button/manage/{embed_button}",
32 * "delete-form" = "/admin/config/content/embed/button/manage/{embed_button}/delete",
33 * "collection" = "/admin/config/content/embed",
44 class EmbedButton extends ConfigEntityBase implements EmbedButtonInterface {
46 use StringTranslationTrait;
56 * Label of EmbedButton.
63 * The embed type plugin ID.
70 * Embed type settings.
72 * An array of key/value pairs.
76 public $type_settings = [];
79 * UUID of the button's icon file.
88 public function getTypeId() {
89 return $this->type_id;
95 public function getTypeLabel() {
96 if ($definition = $this->embedTypeManager()->getDefinition($this->getTypeId(), FALSE)) {
97 return $definition['label'];
100 return $this->t('Unknown');
107 public function getTypePlugin() {
108 if ($plugin_id = $this->getTypeId()) {
109 return $this->embedTypeManager()->createInstance($plugin_id, $this->getTypeSettings());
116 public function getIconFile() {
117 if ($this->icon_uuid) {
118 return $this->entityManager()->loadEntityByUuid('file', $this->icon_uuid);
125 public function getIconUrl() {
126 if ($image = $this->getIconFile()) {
127 return file_create_url($image->getFileUri());
130 return $this->getTypePlugin()->getDefaultIconUrl();
137 public function calculateDependencies() {
138 parent::calculateDependencies();
140 // Add the file icon entity as dependency if an UUID was specified.
141 if ($this->icon_uuid && $file_icon = $this->entityManager()->loadEntityByUuid('file', $this->icon_uuid)) {
142 $this->addDependency($file_icon->getConfigDependencyKey(), $file_icon->getConfigDependencyName());
145 // Gather the dependencies of the embed type plugin.
146 $plugin = $this->getTypePlugin();
147 $this->calculatePluginDependencies($plugin);
149 return $this->dependencies;
153 * Gets the embed type plugin manager.
155 * @return \Drupal\embed\EmbedType\EmbedTypeManager
156 * The embed type plugin manager.
158 protected function embedTypeManager() {
159 return \Drupal::service('plugin.manager.embed.type');
163 * Gets the file usage service.
165 * @return \Drupal\file\FileUsage\FileUsageInterface
166 * The file usage service.
168 protected function fileUsage() {
169 return \Drupal::service('file.usage');
175 public function postSave(EntityStorageInterface $storage, $update = TRUE) {
176 parent::postSave($storage, $update);
178 $icon_file = $this->getIconFile();
179 if (isset($this->original) && $old_icon_file = $this->original->getIconFile()) {
180 /** @var \Drupal\file\FileInterface $old_icon_file */
181 if (!$icon_file || $icon_file->uuid() != $old_icon_file->uuid()) {
182 $this->fileUsage()->delete($old_icon_file, 'embed', $this->getEntityTypeId(), $this->id());
187 $usage = $this->fileUsage()->listUsage($icon_file);
188 if (empty($usage['embed'][$this->getEntityTypeId()][$this->id()])) {
189 $this->fileUsage()->add($icon_file, 'embed', $this->getEntityTypeId(), $this->id());
197 public static function postDelete(EntityStorageInterface $storage, array $entities) {
198 parent::postDelete($storage, $entities);
200 // Remove file usage for any button icons.
201 foreach ($entities as $entity) {
202 /** @var \Drupal\embed\EmbedButtonInterface $entity */
203 if ($icon_file = $entity->getIconFile()) {
204 \Drupal::service('file.usage')->delete($icon_file, 'embed', $entity->getEntityTypeId(), $entity->id());
212 public function getTypeSetting($key, $default = NULL) {
213 if (isset($this->type_settings[$key])) {
214 return $this->type_settings[$key];
224 public function getTypeSettings() {
225 return $this->type_settings;