summaryrefslogtreecommitdiffstats
path: root/src/Plugin/Linkit/Substitution/Media.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Plugin/Linkit/Substitution/Media.php')
-rw-r--r--src/Plugin/Linkit/Substitution/Media.php99
1 files changed, 99 insertions, 0 deletions
diff --git a/src/Plugin/Linkit/Substitution/Media.php b/src/Plugin/Linkit/Substitution/Media.php
new file mode 100644
index 0000000..07124b8
--- /dev/null
+++ b/src/Plugin/Linkit/Substitution/Media.php
@@ -0,0 +1,99 @@
+<?php
+
+namespace Drupal\linkit\Plugin\Linkit\Substitution;
+
+use Drupal\Core\Entity\EntityInterface;
+use Drupal\Core\Entity\EntityTypeInterface;
+use Drupal\Core\Entity\EntityTypeManager;
+use Drupal\Core\GeneratedUrl;
+use Drupal\linkit\SubstitutionInterface;
+use Drupal\views\Plugin\views\PluginBase;
+use Symfony\Component\DependencyInjection\ContainerInterface;
+
+/**
+ * A substitution plugin for the URL to a file.
+ *
+ * @Substitution(
+ * id = "media",
+ * label = @Translation("Direct URL to media file entity"),
+ * )
+ */
+class Media extends PluginBase implements SubstitutionInterface {
+
+ /**
+ * The entity type manager.
+ *
+ * @var \Drupal\Core\Entity\EntityTypeManager
+ */
+ 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\EntityTypeManager $entity_type_manager
+ * The entity type manager.
+ */
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManager $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);
+ }
+
+ $source_field = $media_bundle->getTypeConfiguration()['source_field'];
+ /** @var \Drupal\file\FileInterface $file */
+ $file = $entity->{$source_field}->entity;
+ $url->setGeneratedUrl(file_create_url($file->getFileUri()));
+ $url->addCacheableDependency($entity);
+ return $url;
+ }
+
+ /**
+ * Checks if this substitution plugin is applicable for the given entity type.
+ *
+ * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
+ * An entity type object.
+ *
+ * @return bool
+ * If the plugin is applicable.
+ */
+ public static function isApplicable(EntityTypeInterface $entity_type) {
+ return $entity_type->entityClassImplements('Drupal\media_entity\MediaInterface');
+ }
+
+}