X-Git-Url: https://yaffs.net/gitweb/?a=blobdiff_plain;f=web%2Fmodules%2Fcontrib%2Fembed%2Fsrc%2FEntity%2FEmbedButton.php;fp=web%2Fmodules%2Fcontrib%2Fembed%2Fsrc%2FEntity%2FEmbedButton.php;h=9188b95b5aafb56b48bc05f64ceac96d7ab455aa;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/modules/contrib/embed/src/Entity/EmbedButton.php b/web/modules/contrib/embed/src/Entity/EmbedButton.php new file mode 100644 index 000000000..9188b95b5 --- /dev/null +++ b/web/modules/contrib/embed/src/Entity/EmbedButton.php @@ -0,0 +1,228 @@ +type_id; + } + + /** + * {@inheritdoc} + */ + public function getTypeLabel() { + if ($definition = $this->embedTypeManager()->getDefinition($this->getTypeId(), FALSE)) { + return $definition['label']; + } + else { + return $this->t('Unknown'); + } + } + + /** + * {@inheritdoc} + */ + public function getTypePlugin() { + if ($plugin_id = $this->getTypeId()) { + return $this->embedTypeManager()->createInstance($plugin_id, $this->getTypeSettings()); + } + } + + /** + * {@inheritdoc} + */ + public function getIconFile() { + if ($this->icon_uuid) { + return $this->entityManager()->loadEntityByUuid('file', $this->icon_uuid); + } + } + + /** + * {@inheritdoc} + */ + public function getIconUrl() { + if ($image = $this->getIconFile()) { + return file_create_url($image->getFileUri()); + } + else { + return $this->getTypePlugin()->getDefaultIconUrl(); + } + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + parent::calculateDependencies(); + + // Add the file icon entity as dependency if an UUID was specified. + if ($this->icon_uuid && $file_icon = $this->entityManager()->loadEntityByUuid('file', $this->icon_uuid)) { + $this->addDependency($file_icon->getConfigDependencyKey(), $file_icon->getConfigDependencyName()); + } + + // Gather the dependencies of the embed type plugin. + $plugin = $this->getTypePlugin(); + $this->calculatePluginDependencies($plugin); + + return $this->dependencies; + } + + /** + * Gets the embed type plugin manager. + * + * @return \Drupal\embed\EmbedType\EmbedTypeManager + * The embed type plugin manager. + */ + protected function embedTypeManager() { + return \Drupal::service('plugin.manager.embed.type'); + } + + /** + * Gets the file usage service. + * + * @return \Drupal\file\FileUsage\FileUsageInterface + * The file usage service. + */ + protected function fileUsage() { + return \Drupal::service('file.usage'); + } + + /** + * {@inheritdoc} + */ + public function postSave(EntityStorageInterface $storage, $update = TRUE) { + parent::postSave($storage, $update); + + $icon_file = $this->getIconFile(); + if (isset($this->original) && $old_icon_file = $this->original->getIconFile()) { + /** @var \Drupal\file\FileInterface $old_icon_file */ + if (!$icon_file || $icon_file->uuid() != $old_icon_file->uuid()) { + $this->fileUsage()->delete($old_icon_file, 'embed', $this->getEntityTypeId(), $this->id()); + } + } + + if ($icon_file) { + $usage = $this->fileUsage()->listUsage($icon_file); + if (empty($usage['embed'][$this->getEntityTypeId()][$this->id()])) { + $this->fileUsage()->add($icon_file, 'embed', $this->getEntityTypeId(), $this->id()); + } + } + } + + /** + * {@inheritdoc} + */ + public static function postDelete(EntityStorageInterface $storage, array $entities) { + parent::postDelete($storage, $entities); + + // Remove file usage for any button icons. + foreach ($entities as $entity) { + /** @var \Drupal\embed\EmbedButtonInterface $entity */ + if ($icon_file = $entity->getIconFile()) { + \Drupal::service('file.usage')->delete($icon_file, 'embed', $entity->getEntityTypeId(), $entity->id()); + } + } + } + + /** + * {@inheritdoc} + */ + public function getTypeSetting($key, $default = NULL) { + if (isset($this->type_settings[$key])) { + return $this->type_settings[$key]; + } + else { + return $default; + } + } + + /** + * {@inheritdoc} + */ + public function getTypeSettings() { + return $this->type_settings; + } + +}