Commit e595d05a authored by acbramley's avatar acbramley Committed by anon

Issue #2866525 by acbramley, realityloop, anon: Direct link to file of linked media entity

parent db2cb264
<?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');
}
}
media_entity.bundle.type.test_type:
type: mapping
label: 'Test type configuration'
mapping:
source_field:
type: string
label: 'Field with source information'
# 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
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
<?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',
];
}
}
......@@ -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.');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment