class MediaDownload in Media Entity Download 8.2
Same name and namespace in other branches
- 8 src/Plugin/Linkit/Substitution/MediaDownload.php \Drupal\media_entity_download\Plugin\Linkit\Substitution\MediaDownload
A substitution plugin for a direct download link to a file.
Plugin annotation
@Substitution(
id = "media_download",
label = @Translation("Direct download URL for media item (Forcing a Save as... browser dialog)"),
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\media_entity_download\Plugin\Linkit\Substitution\MediaDownload implements ContainerFactoryPluginInterface, SubstitutionInterface
Expanded class hierarchy of MediaDownload
File
- src/
Plugin/ Linkit/ Substitution/ MediaDownload.php, line 24
Namespace
Drupal\media_entity_download\Plugin\Linkit\SubstitutionView source
class MediaDownload extends PluginBase implements SubstitutionInterface, ContainerFactoryPluginInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a Media object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
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;
}
/**
* {@inheritdoc}
*/
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'));
}
/**
* Get the URL associated with a given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to get a URL for.
*
* @return \Drupal\Core\GeneratedUrl
* A url to replace.
*/
public function getUrl(EntityInterface $entity) {
$url = new GeneratedUrl();
/** @var \Drupal\media\Entity\MediaType $media_bundle */
$media_bundle = $this->entityTypeManager
->getStorage('media_type')
->load($entity
->bundle());
// Default to the canonical URL if the bundle doesn't have a source field.
if (empty($media_bundle
->getSource()
->getConfiguration()['source_field'])) {
return $entity
->toUrl('canonical')
->toString(TRUE);
}
// @todo: Discover if we can support file field deltas at some point via the suggestion matcher.
$url_object = Url::fromRoute('media_entity_download.download', [
'media' => $entity
->id(),
], [
'query' => [
$this
->getContentDisposition() => NULL,
],
]);
$url
->setGeneratedUrl($url_object
->toString());
$url
->addCacheableDependency($entity);
return $url;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(EntityTypeInterface $entity_type) {
return $entity_type
->entityClassImplements('Drupal\\media\\MediaInterface');
}
/**
* Get the disposition header for downloads.
*
* @return string
* Either ResponseHeaderBag::DISPOSITION_INLINE or
* ResponseHeaderBag::DISPOSITION_ATTACHMENT.
*/
protected function getContentDisposition() {
return ResponseHeaderBag::DISPOSITION_ATTACHMENT;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
MediaDownload:: |
protected | property | The entity type manager. | |
MediaDownload:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
MediaDownload:: |
protected | function | Get the disposition header for downloads. | 1 |
MediaDownload:: |
public | function |
Get the URL associated with a given entity. Overrides SubstitutionInterface:: |
|
MediaDownload:: |
public static | function |
Checks if this substitution plugin is applicable for the given entity type. Overrides SubstitutionInterface:: |
|
MediaDownload:: |
public | function |
Constructs a Media object. Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. |