You are here

class Media in Linkit 8.5

A substitution plugin for the URL to a file.

Plugin annotation


@Substitution(
  id = "media",
  label = @Translation("Direct URL to media file entity"),
)

Hierarchy

Expanded class hierarchy of Media

1 file declares its use of Media
SubstitutionPluginTest.php in tests/src/Kernel/SubstitutionPluginTest.php
2 string references to 'Media'
editor.editor.format_1.yml in tests/fixtures/update/editor.editor.format_1.yml
tests/fixtures/update/editor.editor.format_1.yml
editor.editor.format_2.yml in tests/fixtures/update/editor.editor.format_2.yml
tests/fixtures/update/editor.editor.format_2.yml

File

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

Namespace

Drupal\linkit\Plugin\Linkit\Substitution
View source
class Media extends PluginBase implements SubstitutionInterface, ContainerFactoryPluginInterface {

  /**
   * {@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'));
  }

  /**
   * {@inheritdoc}
   */
  public function getUrl(EntityInterface $entity) {
    $url = new GeneratedUrl();
    if (!$entity instanceof MediaInterface) {
      return $url;
    }
    $source_field = $entity
      ->getSource()
      ->getSourceFieldDefinition($entity
      ->get('bundle')->entity);
    if ($source_field && $entity
      ->hasField($source_field
      ->getName()) && $entity
      ->get($source_field
      ->getName())->entity instanceof FileInterface) {

      /** @var \Drupal\file\FileInterface $file */
      $file = $entity
        ->get($source_field
        ->getName())->entity;
      $url
        ->setGeneratedUrl(file_create_url($file
        ->getFileUri()));
      $url
        ->addCacheableDependency($entity);
      return $url;
    }

    // If available, fall back to the canonical URL if the bundle doesn't have
    // a file source field.
    if ($entity
      ->getEntityType()
      ->getLinkTemplate('canonical') != $entity
      ->getEntityType()
      ->getLinkTemplate('edit-form')) {
      return $entity
        ->toUrl('canonical')
        ->toString(TRUE);
    }
    return $url;
  }

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

}

Members

Namesort descending Modifiers Type Description Overrides
Media::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
Media::getUrl public function Get the URL associated with a given entity. Overrides SubstitutionInterface::getUrl
Media::isApplicable public static function Checks if this substitution plugin is applicable for the given entity type. Overrides SubstitutionInterface::isApplicable
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.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92