You are here

class MediaDownload in Media Entity Download 8

Same name and namespace in other branches
  1. 8.2 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"),
)

Hierarchy

Expanded class hierarchy of MediaDownload

File

src/Plugin/Linkit/Substitution/MediaDownload.php, line 23

Namespace

Drupal\media_entity_download\Plugin\Linkit\Substitution
View 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\MediaBundleInterface $media_bundle */
    $media_bundle = $this->entityTypeManager
      ->getStorage('media_bundle')
      ->load($entity
      ->bundle());

    // Default to the canonical URL if the bundle doesn't have a source field.
    if (empty($media_bundle
      ->getTypeConfiguration()['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(),
    ]);
    $url
      ->setGeneratedUrl($url_object
      ->toString());
    $url
      ->addCacheableDependency($entity);
    return $url;
  }

  /**
   * {@inheritdoc}
   */
  public static function isApplicable(EntityTypeInterface $entity_type) {
    return $entity_type
      ->entityClassImplements('Drupal\\media_entity\\MediaInterface');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MediaDownload::$entityTypeManager protected property The entity type manager.
MediaDownload::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
MediaDownload::getUrl public function Get the URL associated with a given entity. Overrides SubstitutionInterface::getUrl
MediaDownload::isApplicable public static function Checks if this substitution plugin is applicable for the given entity type. Overrides SubstitutionInterface::isApplicable
MediaDownload::__construct public function Constructs a Media object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.