DAMAsset.php in Media: Acquia DAM 8
File
src/Plugin/Linkit/Substitution/DAMAsset.php
View source
<?php
namespace Drupal\media_acquiadam\Plugin\Linkit\Substitution;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\GeneratedUrl;
use Drupal\file\FileInterface;
use Drupal\linkit\Plugin\Linkit\Substitution\Media;
use Drupal\media\MediaInterface;
use Drupal\media_acquiadam\Plugin\media\Source\AcquiadamAsset;
use Symfony\Component\DependencyInjection\ContainerInterface;
class DAMAsset extends Media {
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
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'));
}
public function getUrl(EntityInterface $entity) {
if ($entity instanceof MediaInterface) {
$source = $entity
->getSource();
if (!empty($source) && $source instanceof AcquiadamAsset) {
$fid = $source
->getMetadata($entity, 'file');
if (!empty($fid)) {
$file = $this->entityTypeManager
->getStorage('file')
->load($fid);
if (!empty($file) && $file instanceof FileInterface) {
$url = new GeneratedUrl();
$url
->setGeneratedUrl(file_create_url($file
->getFileUri()));
$url
->addCacheableDependency($entity);
return $url;
}
}
}
}
return parent::getUrl($entity);
}
}
Classes
Name |
Description |
DAMAsset |
A substitution plugin for the URL to a file. |