Commit 1084887a authored by slashrsm's avatar slashrsm Committed by anon

Issue #2930171 by slashrsm, anon: Support core Media entity

parent 0a644829
......@@ -6,4 +6,3 @@ core: 8.x
configure: entity.linkit_profile.collection
test_dependencies:
- imce
- media_entity
\ No newline at end of file
......@@ -69,14 +69,14 @@ class Media extends PluginBase implements SubstitutionInterface, ContainerFactor
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());
/** @var \Drupal\media\MediaTypeInterface $media_type */
$media_type = $entity->get('bundle')->entity;
// Default to the canonical URL if the bundle doesn't have a source field.
if (empty($media_bundle->getTypeConfiguration()['source_field'])) {
if (empty($media_type->getSource()->getConfiguration()['source_field'])) {
return $entity->toUrl('canonical')->toString(TRUE);
}
$source_field = $media_bundle->getTypeConfiguration()['source_field'];
$source_field = $media_type->getSource()->getConfiguration()['source_field'];
/** @var \Drupal\file\FileInterface $file */
$file = $entity->{$source_field}->entity;
$url->setGeneratedUrl(file_create_url($file->getFileUri()));
......@@ -94,7 +94,7 @@ class Media extends PluginBase implements SubstitutionInterface, ContainerFactor
* If the plugin is applicable.
*/
public static function isApplicable(EntityTypeInterface $entity_type) {
return $entity_type->entityClassImplements('Drupal\media_entity\MediaInterface');
return $entity_type->entityClassImplements('Drupal\media\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:linkit
- drupal:field
- drupal:text
- media_entity: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',
];
}
}
......@@ -4,7 +4,6 @@ namespace Drupal\Tests\linkit\Kernel\Matchers;
use Drupal\file\Entity\File;
use Drupal\Tests\linkit\Kernel\LinkitKernelTestBase;
use Drupal\user\Entity\User;
/**
* Tests file matcher.
......
......@@ -3,14 +3,13 @@
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;
use Drupal\media\Entity\Media;
use Drupal\media\Entity\MediaType;
use Drupal\Core\DependencyInjection\ContainerBuilder;
/**
* Tests the substitution plugins.
......@@ -43,10 +42,9 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
public static $modules = [
'file',
'entity_test',
// 'media_entity',
'media',
'image',
'field',
// 'linkit_media_test',
];
/**
......@@ -59,11 +57,43 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
$this->installEntitySchema('file');
$this->installEntitySchema('entity_test');
// $this->installEntitySchema('media');
// $this->installEntitySchema('media_bundle');
$this->installEntitySchema('media');
$this->installEntitySchema('media_type');
$this->installEntitySchema('field_storage_config');
$this->installEntitySchema('field_config');
$this->installSchema('file', ['file_usage']);
unset($GLOBALS['config']['system.file']);
\Drupal::configFactory()->getEditable('system.file')->set('default_scheme', 'public')->save();
}
/**
* {@inheritdoc}
*/
public function register(ContainerBuilder $container) {
parent::register($container);
$container->register('stream_wrapper.public', 'Drupal\Core\StreamWrapper\PublicStream')
->addTag('stream_wrapper', ['scheme' => 'public']);
}
/**
* {@inheritdoc}
*/
protected function setUpFilesystem() {
$public_file_directory = $this->siteDirectory . '/files';
require_once 'core/includes/file.inc';
mkdir($this->siteDirectory, 0775);
mkdir($this->siteDirectory . '/files', 0775);
mkdir($this->siteDirectory . '/files/config/' . CONFIG_SYNC_DIRECTORY, 0775, TRUE);
$this->setSetting('file_public_path', $public_file_directory);
$GLOBALS['config_directories'] = [
CONFIG_SYNC_DIRECTORY => $this->siteDirectory . '/files/config/sync',
];
}
/**
......@@ -107,30 +137,22 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
/**
* Test the media substitution.
*/
/*
public function testMediaSubstitution() {
// Set up media bundle and fields.
MediaBundle::create([
$media_type = MediaType::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',
],
'description' => 'Test type.',
'source' => 'file',
]);
$media_type->save();
$source_field = $media_type->getSource()->createSourceField($media_type);
$source_field->getFieldStorageDefinition()->save();
$source_field->save();
$media_type->set('source_configuration', [
'source_field' => $source_field->getName(),
])->save();
$file = File::create([
'uid' => 1,
'filename' => 'druplicon.txt',
......@@ -139,9 +161,10 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
'status' => FILE_STATUS_PERMANENT,
]);
$file->save();
$media = Media::create([
'bundle' => 'test',
'field_media_file' => ['target_id' => $file->id()],
$source_field->getName() => ['target_id' => $file->id()],
]);
$media->save();
......@@ -154,6 +177,5 @@ class SubstitutionPluginTest extends LinkitKernelTestBase {
$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