summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorZombienaute2017-05-29 23:27:56 +0200
committerEmil Stjerneman2017-05-29 23:27:56 +0200
commite595d05a622f5ab75758dce5f11ae5208b991f91 (patch)
tree160eaf61286c7cf9b91503dde6011c1fd555e08a
parentdb2cb264184ca629364730a59671faef74f3e010 (diff)
Issue #2866525 by acbramley, realityloop, anon: Direct link to file of linked media entity
-rw-r--r--src/Plugin/Linkit/Substitution/Media.php99
-rw-r--r--tests/linkit_media_test/config/schema/linkit_media_test.schema.yml7
-rw-r--r--tests/linkit_media_test/config/schema/linkit_test.schema.yml8
-rw-r--r--tests/linkit_media_test/linkit_media_test.info.yml10
-rw-r--r--tests/linkit_media_test/src/Plugin/MediaEntity/Type/TestType.php27
-rw-r--r--tests/src/Kernel/SubstitutionPluginTest.php66
6 files changed, 217 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');
+ }
+
+}
diff --git a/tests/linkit_media_test/config/schema/linkit_media_test.schema.yml b/tests/linkit_media_test/config/schema/linkit_media_test.schema.yml
new file mode 100644
index 0000000..2e13885
--- /dev/null
+++ b/tests/linkit_media_test/config/schema/linkit_media_test.schema.yml
@@ -0,0 +1,7 @@
+media_entity.bundle.type.test_type:
+ type: mapping
+ label: 'Test type configuration'
+ mapping:
+ source_field:
+ type: string
+ label: 'Field with source information'
diff --git a/tests/linkit_media_test/config/schema/linkit_test.schema.yml b/tests/linkit_media_test/config/schema/linkit_test.schema.yml
new file mode 100644
index 0000000..ce29e8f
--- /dev/null
+++ b/tests/linkit_media_test/config/schema/linkit_test.schema.yml
@@ -0,0 +1,8 @@
+# Schema for the configuration files of the Linkit test module.
+
+# Plugin \Drupal\linkit_test\Plugin\Linkit\Matcher\ConfigurableDummyMatcher
+linkit.matcher.configurable_dummy_matcher:
+ type: linkit.matcher
+ mapping:
+ dummy_setting:
+ type: boolean
diff --git a/tests/linkit_media_test/linkit_media_test.info.yml b/tests/linkit_media_test/linkit_media_test.info.yml
new file mode 100644
index 0000000..fa6ee46
--- /dev/null
+++ b/tests/linkit_media_test/linkit_media_test.info.yml
@@ -0,0 +1,10 @@
+name: 'Linkit media test module'
+description: 'Support module for Linkit media testing.'
+package: Testing
+type: module
+core: 8.x
+dependencies:
+ - linkit
+ - field
+ - text
+ - media_entity
diff --git a/tests/linkit_media_test/src/Plugin/MediaEntity/Type/TestType.php b/tests/linkit_media_test/src/Plugin/MediaEntity/Type/TestType.php
new file mode 100644
index 0000000..a14b5cb
--- /dev/null
+++ b/tests/linkit_media_test/src/Plugin/MediaEntity/Type/TestType.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Drupal\linkit_media_test\Plugin\MediaEntity\Type;
+
+use Drupal\media_entity\Plugin\MediaEntity\Type\Generic;
+
+/**
+ * Provides generic media type.
+ *
+ * @MediaType(
+ * id = "test_type",
+ * label = @Translation("Test type"),
+ * description = @Translation("Test media type.")
+ * )
+ */
+class TestType extends Generic {
+
+ /**
+ * {@inheritdoc}
+ */
+ public function defaultConfiguration() {
+ return [
+ 'source_field' => 'field_media_file',
+ ];
+ }
+
+}
diff --git a/tests/src/Kernel/SubstitutionPluginTest.php b/tests/src/Kernel/SubstitutionPluginTest.php
index 2ec483e..12a182d 100644
--- a/tests/src/Kernel/SubstitutionPluginTest.php
+++ b/tests/src/Kernel/SubstitutionPluginTest.php
@@ -3,14 +3,21 @@
namespace Drupal\Tests\linkit\Kernel;
use Drupal\entity_test\Entity\EntityTest;
+use Drupal\field\Entity\FieldConfig;
+use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\linkit\Plugin\Linkit\Substitution\Canonical as CanonicalSubstitutionPlugin;
use Drupal\linkit\Plugin\Linkit\Substitution\File as FileSubstitutionPlugin;
+use Drupal\linkit\Plugin\Linkit\Substitution\Media as MediaSubstitutionPlugin;
+use Drupal\media_entity\Entity\Media;
+use Drupal\media_entity\Entity\MediaBundle;
/**
* Tests the substitution plugins.
*
* @group linkit
+ *
+ * @requires module media_entity
*/
class SubstitutionPluginTest extends LinkitKernelTestBase {
@@ -36,6 +43,10 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
public static $modules = [
'file',
'entity_test',
+ 'media_entity',
+ 'image',
+ 'field',
+ 'linkit_media_test',
];
/**
@@ -48,6 +59,11 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
$this->installEntitySchema('file');
$this->installEntitySchema('entity_test');
+ $this->installEntitySchema('media');
+ $this->installEntitySchema('media_bundle');
+ $this->installEntitySchema('field_storage_config');
+ $this->installEntitySchema('field_config');
+ $this->installSchema('file', ['file_usage']);
}
/**
@@ -88,4 +104,54 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
$this->assertFalse(CanonicalSubstitutionPlugin::isApplicable($entity_type), 'The entity type File is not applicable the canonical substitution.');
}
+ /**
+ * Test the media substitution.
+ */
+ public function testMediaSubstitution() {
+ // Set up media bundle and fields.
+ MediaBundle::create([
+ 'label' => 'test',
+ 'id' => 'test',
+ 'description' => 'test bundle.',
+ 'type' => 'test_type',
+ ])->save();
+ FieldStorageConfig::create([
+ 'field_name' => 'field_media_file',
+ 'entity_type' => 'media',
+ 'type' => 'file',
+ 'settings' => [],
+ ])->save();
+ FieldConfig::create([
+ 'entity_type' => 'media',
+ 'bundle' => 'test',
+ 'field_name' => 'field_media_file',
+ 'label' => 'Media field',
+ 'settings' => [
+ 'file_extensions' => 'txt',
+ ],
+ ])->save();
+ $file = File::create([
+ 'uid' => 1,
+ 'filename' => 'druplicon.txt',
+ 'uri' => 'public://druplicon.txt',
+ 'filemime' => 'text/plain',
+ 'status' => FILE_STATUS_PERMANENT,
+ ]);
+ $file->save();
+ $media = Media::create([
+ 'bundle' => 'test',
+ 'field_media_file' => ['target_id' => $file->id()],
+ ]);
+ $media->save();
+
+ $media_substitution = $this->substitutionManager->createInstance('media');
+ $this->assertEquals($GLOBALS['base_url'] . '/' . $this->siteDirectory . '/files/druplicon.txt', $media_substitution->getUrl($media)->getGeneratedUrl());
+
+ $entity_type = $this->entityTypeManager->getDefinition('media');
+ $this->assertTrue(MediaSubstitutionPlugin::isApplicable($entity_type), 'The entity type Media is applicable the media substitution.');
+
+ $entity_type = $this->entityTypeManager->getDefinition('file');
+ $this->assertFalse(MediaSubstitutionPlugin::isApplicable($entity_type), 'The entity type File is not applicable the media substitution.');
+ }
+
}