MediaDownload.php in Media Entity Download 8
File
src/Plugin/Linkit/Substitution/MediaDownload.php
View source
<?php
namespace Drupal\media_entity_download\Plugin\Linkit\Substitution;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\GeneratedUrl;
use Drupal\linkit\SubstitutionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
class MediaDownload extends PluginBase implements SubstitutionInterface, ContainerFactoryPluginInterface {
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) {
$url = new GeneratedUrl();
$media_bundle = $this->entityTypeManager
->getStorage('media_bundle')
->load($entity
->bundle());
if (empty($media_bundle
->getTypeConfiguration()['source_field'])) {
return $entity
->toUrl('canonical')
->toString(TRUE);
}
$url_object = Url::fromRoute('media_entity_download.download', [
'media' => $entity
->id(),
]);
$url
->setGeneratedUrl($url_object
->toString());
$url
->addCacheableDependency($entity);
return $url;
}
public static function isApplicable(EntityTypeInterface $entity_type) {
return $entity_type
->entityClassImplements('Drupal\\media_entity\\MediaInterface');
}
}
Classes
Name |
Description |
MediaDownload |
A substitution plugin for a direct download link to a file. |