diff --git a/src/Plugin/Linkit/Substitution/Media.php b/src/Plugin/Linkit/Substitution/Media.php new file mode 100644 index 0000000000000000000000000000000000000000..07124b87b4a24dde4fe8fabc9ddb7f310012f8b0 --- /dev/null +++ b/src/Plugin/Linkit/Substitution/Media.php @@ -0,0 +1,99 @@ +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 0000000000000000000000000000000000000000..2e13885b5ac350c26e297a1d7edb96962630a3d6 --- /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 0000000000000000000000000000000000000000..ce29e8fcd5c02ce464239f12d06d66fb3e089338 --- /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 0000000000000000000000000000000000000000..fa6ee46aa1fe261d57272e16ee680e120f1a326c --- /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 0000000000000000000000000000000000000000..a14b5cb501c3b61334a64e871ec83f6a19c64eb0 --- /dev/null +++ b/tests/linkit_media_test/src/Plugin/MediaEntity/Type/TestType.php @@ -0,0 +1,27 @@ + 'field_media_file', + ]; + } + +} diff --git a/tests/src/Kernel/SubstitutionPluginTest.php b/tests/src/Kernel/SubstitutionPluginTest.php index 2ec483e9f4899820f216ed1c4869732accd683e5..12a182df330f822989af651eaec16c0b870634eb 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.'); + } + }