diff --git a/core/composer.json b/core/composer.json
index 7452a077766a..770631ddfd27 100644
--- a/core/composer.json
+++ b/core/composer.json
@@ -133,6 +133,7 @@
"drupal/locale": "self.version",
"drupal/minimal": "self.version",
"drupal/media": "self.version",
+ "drupal/media_library": "self.version",
"drupal/menu_link_content": "self.version",
"drupal/menu_ui": "self.version",
"drupal/migrate": "self.version",
diff --git a/core/includes/bootstrap.inc b/core/includes/bootstrap.inc
index 4d28eaa03615..c2aeb55e0c52 100644
--- a/core/includes/bootstrap.inc
+++ b/core/includes/bootstrap.inc
@@ -10,6 +10,7 @@ use Drupal\Component\Utility\Html;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Config\BootstrapConfigStorageFactory;
+use Drupal\Core\Extension\Exception\UnknownExtensionException;
use Drupal\Core\Logger\RfcLogLevel;
use Drupal\Core\Test\TestDatabase;
use Drupal\Core\Session\AccountInterface;
@@ -246,7 +247,7 @@ function drupal_get_filename($type, $name, $filename = NULL) {
try {
return $extension_list->getPathname($name);
}
- catch (\InvalidArgumentException $e) {
+ catch (UnknownExtensionException $e) {
// Catch the exception. This will result in triggering an error.
}
}
diff --git a/core/includes/form.inc b/core/includes/form.inc
index d248b7b5a9a9..dacae875bdeb 100644
--- a/core/includes/form.inc
+++ b/core/includes/form.inc
@@ -251,6 +251,11 @@ function template_preprocess_details(&$variables) {
$variables['summary_attributes']['aria-pressed'] = $variables['summary_attributes']['aria-expanded'];
}
$variables['title'] = (!empty($element['#title'])) ? $element['#title'] : '';
+ // If the element title is a string, wrap it a render array so that markup
+ // will not be escaped (but XSS-filtered).
+ if (is_string($variables['title']) && $variables['title'] !== '') {
+ $variables['title'] = ['#markup' => $variables['title']];
+ }
$variables['description'] = (!empty($element['#description'])) ? $element['#description'] : '';
$variables['children'] = (isset($element['#children'])) ? $element['#children'] : '';
$variables['value'] = (isset($element['#value'])) ? $element['#value'] : '';
diff --git a/core/includes/theme.inc b/core/includes/theme.inc
index 30e5aafc6c71..5b9db30831f7 100644
--- a/core/includes/theme.inc
+++ b/core/includes/theme.inc
@@ -570,6 +570,11 @@ function template_preprocess_datetime_wrapper(&$variables) {
if (!empty($element['#title'])) {
$variables['title'] = $element['#title'];
+ // If the element title is a string, wrap it a render array so that markup
+ // will not be escaped (but XSS-filtered).
+ if (is_string($variables['title']) && $variables['title'] !== '') {
+ $variables['title'] = ['#markup' => $variables['title']];
+ }
}
// Suppress error messages.
diff --git a/core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionInterface.php b/core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionInterface.php
new file mode 100644
index 000000000000..d1ff95340c03
--- /dev/null
+++ b/core/lib/Drupal/Component/Plugin/Definition/ContextAwarePluginDefinitionInterface.php
@@ -0,0 +1,71 @@
+contextDefinitions);
+ }
+
+ /**
+ * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinitions().
+ */
+ public function getContextDefinitions() {
+ return $this->contextDefinitions;
+ }
+
+ /**
+ * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::getContextDefinition().
+ */
+ public function getContextDefinition($name) {
+ if ($this->hasContextDefinition($name)) {
+ return $this->contextDefinitions[$name];
+ }
+ throw new ContextException($this->id() . " does not define a '$name' context");
+ }
+
+ /**
+ * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::addContextDefinition().
+ */
+ public function addContextDefinition($name, ContextDefinitionInterface $definition) {
+ $this->contextDefinitions[$name] = $definition;
+ return $this;
+ }
+
+ /**
+ * Implements \Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface::removeContextDefinition().
+ */
+ public function removeContextDefinition($name) {
+ unset($this->contextDefinitions[$name]);
+ return $this;
+ }
+
+}
diff --git a/core/lib/Drupal/Core/Config/ConfigInstaller.php b/core/lib/Drupal/Core/Config/ConfigInstaller.php
index 24d735b99d59..b1ea748e7b2c 100644
--- a/core/lib/Drupal/Core/Config/ConfigInstaller.php
+++ b/core/lib/Drupal/Core/Config/ConfigInstaller.php
@@ -4,7 +4,6 @@
use Drupal\Component\Utility\Crypt;
use Drupal\Core\Config\Entity\ConfigDependencyManager;
-use Drupal\Core\Config\Entity\ConfigEntityDependency;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class ConfigInstaller implements ConfigInstallerInterface {
@@ -204,16 +203,19 @@ public function installOptionalConfig(StorageInterface $storage = NULL, $depende
$dependency_manager = new ConfigDependencyManager();
$dependency_manager->setData($config_to_create);
$config_to_create = array_merge(array_flip($dependency_manager->sortAll()), $config_to_create);
+ if (!empty($dependency)) {
+ // In order to work out dependencies we need the full config graph.
+ $dependency_manager->setData($this->getActiveStorages()->readMultiple($existing_config) + $config_to_create);
+ $dependencies = $dependency_manager->getDependentEntities(key($dependency), reset($dependency));
+ }
foreach ($config_to_create as $config_name => $data) {
// Remove configuration where its dependencies cannot be met.
$remove = !$this->validateDependencies($config_name, $data, $enabled_extensions, $all_config);
- // If $dependency is defined, remove configuration that does not have a
- // matching dependency.
+ // Remove configuration that is not dependent on $dependency, if it is
+ // defined.
if (!$remove && !empty($dependency)) {
- // Create a light weight dependency object to check dependencies.
- $config_entity = new ConfigEntityDependency($config_name, $data);
- $remove = !$config_entity->hasDependency(key($dependency), reset($dependency));
+ $remove = !isset($dependencies[$config_name]);
}
if ($remove) {
diff --git a/core/lib/Drupal/Core/EventSubscriber/OptionsRequestSubscriber.php b/core/lib/Drupal/Core/EventSubscriber/OptionsRequestSubscriber.php
index 3abc55c53a64..ee22937dcaad 100644
--- a/core/lib/Drupal/Core/EventSubscriber/OptionsRequestSubscriber.php
+++ b/core/lib/Drupal/Core/EventSubscriber/OptionsRequestSubscriber.php
@@ -46,11 +46,11 @@ public function onRequest(GetResponseEvent $event) {
// In case we don't have any routes, a 403 should be thrown by the normal
// request handling.
if (count($routes) > 0) {
- $methods = array_map(function (Route $route) {
- return $route->getMethods();
- }, $routes->all());
// Flatten and unique the available methods.
- $methods = array_unique(call_user_func_array('array_merge', $methods));
+ $methods = array_reduce($routes->all(), function ($methods, Route $route) {
+ return array_merge($methods, $route->getMethods());
+ }, []);
+ $methods = array_unique($methods);
$response = new Response('', 200, ['Allow' => implode(', ', $methods)]);
$event->setResponse($response);
}
diff --git a/core/lib/Drupal/Core/Extension/Exception/UninstalledExtensionException.php b/core/lib/Drupal/Core/Extension/Exception/UninstalledExtensionException.php
new file mode 100644
index 000000000000..f0503f1980b5
--- /dev/null
+++ b/core/lib/Drupal/Core/Extension/Exception/UninstalledExtensionException.php
@@ -0,0 +1,8 @@
+type} $extension_name does not exist.");
+ throw new UnknownExtensionException("The {$this->type} $extension_name does not exist.");
}
/**
@@ -334,7 +335,7 @@ protected function doList() {
* @return mixed[]
* An associative array of extension information.
*
- * @throws \InvalidArgumentException
+ * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException
* If there is no extension with the supplied name.
*/
public function getExtensionInfo($extension_name) {
@@ -342,7 +343,7 @@ public function getExtensionInfo($extension_name) {
if (isset($all_info[$extension_name])) {
return $all_info[$extension_name];
}
- throw new \InvalidArgumentException("The {$this->type} $extension_name does not exist or is not installed.");
+ throw new UnknownExtensionException("The {$this->type} $extension_name does not exist or is not installed.");
}
/**
@@ -505,7 +506,7 @@ public function setPathname($extension_name, $pathname) {
* The drupal-root relative filename and path of the requested extension's
* .info.yml file.
*
- * @throws \InvalidArgumentException
+ * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException
* If there is no extension with the supplied machine name.
*/
public function getPathname($extension_name) {
@@ -518,7 +519,7 @@ public function getPathname($extension_name) {
elseif (($path_names = $this->getPathnames()) && isset($path_names[$extension_name])) {
return $path_names[$extension_name];
}
- throw new \InvalidArgumentException("The {$this->type} $extension_name does not exist.");
+ throw new UnknownExtensionException("The {$this->type} $extension_name does not exist.");
}
/**
@@ -533,7 +534,7 @@ public function getPathname($extension_name) {
* @return string
* The Drupal-root-relative path to the specified extension.
*
- * @throws \InvalidArgumentException
+ * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException
* If there is no extension with the supplied name.
*/
public function getPath($extension_name) {
diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php
index a3555cd98dc6..8d43a857dd4c 100644
--- a/core/lib/Drupal/Core/Extension/ModuleHandler.php
+++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php
@@ -5,6 +5,7 @@
use Drupal\Component\Graph\Graph;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\CacheBackendInterface;
+use Drupal\Core\Extension\Exception\UnknownExtensionException;
/**
* Class that manages modules in a Drupal installation.
@@ -172,7 +173,7 @@ public function getModule($name) {
if (isset($this->moduleList[$name])) {
return $this->moduleList[$name];
}
- throw new \InvalidArgumentException(sprintf('The module %s does not exist.', $name));
+ throw new UnknownExtensionException(sprintf('The module %s does not exist.', $name));
}
/**
diff --git a/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php b/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php
index f1097e388104..abb4edde32b1 100644
--- a/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php
+++ b/core/lib/Drupal/Core/Extension/ModuleHandlerInterface.php
@@ -61,7 +61,7 @@ public function getModuleList();
* @return \Drupal\Core\Extension\Extension
* An extension object.
*
- * @throws \InvalidArgumentException
+ * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException
* Thrown when the requested module does not exist.
*/
public function getModule($name);
diff --git a/core/lib/Drupal/Core/Extension/ThemeHandler.php b/core/lib/Drupal/Core/Extension/ThemeHandler.php
index d54ff1fa1a14..4258fda9fa4f 100644
--- a/core/lib/Drupal/Core/Extension/ThemeHandler.php
+++ b/core/lib/Drupal/Core/Extension/ThemeHandler.php
@@ -3,6 +3,8 @@
namespace Drupal\Core\Extension;
use Drupal\Core\Config\ConfigFactoryInterface;
+use Drupal\Core\Extension\Exception\UninstalledExtensionException;
+use Drupal\Core\Extension\Exception\UnknownExtensionException;
use Drupal\Core\State\StateInterface;
/**
@@ -147,7 +149,7 @@ public function getDefault() {
public function setDefault($name) {
$list = $this->listInfo();
if (!isset($list[$name])) {
- throw new \InvalidArgumentException("$name theme is not installed.");
+ throw new UninstalledExtensionException("$name theme is not installed.");
}
$this->configFactory->getEditable('system.theme')
->set('default', $name)
@@ -437,7 +439,7 @@ protected function getExtensionDiscovery() {
public function getName($theme) {
$themes = $this->listInfo();
if (!isset($themes[$theme])) {
- throw new \InvalidArgumentException("Requested the name of a non-existing theme $theme");
+ throw new UnknownExtensionException("Requested the name of a non-existing theme $theme");
}
return $themes[$theme]->info['name'];
}
@@ -486,7 +488,7 @@ public function getTheme($name) {
if (isset($themes[$name])) {
return $themes[$name];
}
- throw new \InvalidArgumentException(sprintf('The theme %s does not exist.', $name));
+ throw new UnknownExtensionException(sprintf('The theme %s does not exist.', $name));
}
/**
diff --git a/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php b/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php
index 00433f0b9664..56d9e9a8a86c 100644
--- a/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php
+++ b/core/lib/Drupal/Core/Extension/ThemeHandlerInterface.php
@@ -39,8 +39,8 @@ public function install(array $theme_list, $install_dependencies = TRUE);
* @param array $theme_list
* The themes to uninstall.
*
- * @throws \InvalidArgumentException
- * Thrown when you uninstall an not installed theme.
+ * @throws \Drupal\Core\Extension\Exception\UninstalledExtensionException
+ * Thrown when you try to uninstall a theme that wasn't installed.
*
* @see hook_themes_uninstalled()
*
@@ -146,6 +146,9 @@ public function getBaseThemes(array $themes, $theme);
*
* @return string
* Returns the human readable name of the theme.
+ *
+ * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException
+ * When the specified theme does not exist.
*/
public function getName($theme);
@@ -206,7 +209,7 @@ public function themeExists($theme);
* @return \Drupal\Core\Extension\Extension
* An extension object.
*
- * @throws \InvalidArgumentException
+ * @throws \Drupal\Core\Extension\Extension\UnknownExtensionException
* Thrown when the requested theme does not exist.
*/
public function getTheme($name);
diff --git a/core/lib/Drupal/Core/Extension/ThemeInstaller.php b/core/lib/Drupal/Core/Extension/ThemeInstaller.php
index 43a5469e3f4f..2d6567fa1886 100644
--- a/core/lib/Drupal/Core/Extension/ThemeInstaller.php
+++ b/core/lib/Drupal/Core/Extension/ThemeInstaller.php
@@ -7,6 +7,7 @@
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ConfigInstallerInterface;
use Drupal\Core\Config\ConfigManagerInterface;
+use Drupal\Core\Extension\Exception\UnknownExtensionException;
use Drupal\Core\Routing\RouteBuilderInterface;
use Drupal\Core\State\StateInterface;
use Psr\Log\LoggerInterface;
@@ -111,7 +112,7 @@ public function install(array $theme_list, $install_dependencies = TRUE) {
if ($missing = array_diff_key($theme_list, $theme_data)) {
// One or more of the given themes doesn't exist.
- throw new \InvalidArgumentException('Unknown themes: ' . implode(', ', $missing) . '.');
+ throw new UnknownExtensionException('Unknown themes: ' . implode(', ', $missing) . '.');
}
// Only process themes that are not installed currently.
@@ -221,7 +222,7 @@ public function uninstall(array $theme_list) {
$list = $this->themeHandler->listInfo();
foreach ($theme_list as $key) {
if (!isset($list[$key])) {
- throw new \InvalidArgumentException("Unknown theme: $key.");
+ throw new UnknownExtensionException("Unknown theme: $key.");
}
if ($key === $theme_config->get('default')) {
throw new \InvalidArgumentException("The current default theme $key cannot be uninstalled.");
diff --git a/core/lib/Drupal/Core/Extension/ThemeInstallerInterface.php b/core/lib/Drupal/Core/Extension/ThemeInstallerInterface.php
index ad80762ed1cb..ae79b505ea18 100644
--- a/core/lib/Drupal/Core/Extension/ThemeInstallerInterface.php
+++ b/core/lib/Drupal/Core/Extension/ThemeInstallerInterface.php
@@ -22,6 +22,9 @@ interface ThemeInstallerInterface {
*
* @throws \Drupal\Core\Extension\ExtensionNameLengthException
* Thrown when the theme name is to long.
+ *
+ * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException
+ * Thrown when the theme does not exist.
*/
public function install(array $theme_list, $install_dependencies = TRUE);
@@ -34,8 +37,11 @@ public function install(array $theme_list, $install_dependencies = TRUE);
* @param array $theme_list
* The themes to uninstall.
*
+ * @throws \Drupal\Core\Extension\Exception\UnknownExtensionException
+ * Thrown when trying to uninstall a theme that was not installed.
+ *
* @throws \InvalidArgumentException
- * Thrown when you uninstall an not installed theme.
+ * Thrown when trying to uninstall the default theme or the admin theme.
*
* @see hook_themes_uninstalled()
*/
diff --git a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php
index f174d46170d5..c7517a9bdd0e 100644
--- a/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php
+++ b/core/lib/Drupal/Core/Plugin/Context/ContextHandler.php
@@ -2,6 +2,7 @@
namespace Drupal\Core\Plugin\Context;
+use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Component\Plugin\Exception\MissingValueContextException;
use Drupal\Core\Cache\CacheableDependencyInterface;
@@ -17,18 +18,37 @@ class ContextHandler implements ContextHandlerInterface {
*/
public function filterPluginDefinitionsByContexts(array $contexts, array $definitions) {
return array_filter($definitions, function ($plugin_definition) use ($contexts) {
- // If this plugin doesn't need any context, it is available to use.
- // @todo Support object-based plugin definitions in
- // https://www.drupal.org/project/drupal/issues/2961822.
- if (!is_array($plugin_definition) || !isset($plugin_definition['context'])) {
- return TRUE;
- }
+ $context_definitions = $this->getContextDefinitions($plugin_definition);
- // Check the set of contexts against the requirements.
- return $this->checkRequirements($contexts, $plugin_definition['context']);
+ if ($context_definitions) {
+ // Check the set of contexts against the requirements.
+ return $this->checkRequirements($contexts, $context_definitions);
+ }
+ // If this plugin doesn't need any context, it is available to use.
+ return TRUE;
});
}
+ /**
+ * Returns the context definitions associated with a plugin definition.
+ *
+ * @param array|\Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface $plugin_definition
+ * The plugin definition.
+ *
+ * @return \Drupal\Component\Plugin\Context\ContextDefinitionInterface[]|null
+ * The context definitions, or NULL if the plugin definition does not
+ * support contexts.
+ */
+ protected function getContextDefinitions($plugin_definition) {
+ if ($plugin_definition instanceof ContextAwarePluginDefinitionInterface) {
+ return $plugin_definition->getContextDefinitions();
+ }
+ if (is_array($plugin_definition) && isset($plugin_definition['context'])) {
+ return $plugin_definition['context'];
+ }
+ return NULL;
+ }
+
/**
* {@inheritdoc}
*/
diff --git a/core/modules/aggregator/tests/src/Functional/FeedCacheTagsTest.php b/core/modules/aggregator/tests/src/Functional/FeedCacheTagsTest.php
index dc0aab9263e7..0cf6d1d03121 100644
--- a/core/modules/aggregator/tests/src/Functional/FeedCacheTagsTest.php
+++ b/core/modules/aggregator/tests/src/Functional/FeedCacheTagsTest.php
@@ -3,7 +3,7 @@
namespace Drupal\Tests\aggregator\Functional;
use Drupal\aggregator\Entity\Feed;
-use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase;
+use Drupal\Tests\system\Functional\Entity\EntityWithUriCacheTagsTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
diff --git a/core/modules/aggregator/tests/src/Functional/ItemCacheTagsTest.php b/core/modules/aggregator/tests/src/Functional/ItemCacheTagsTest.php
index 09da3436827b..edd2ac32a92c 100644
--- a/core/modules/aggregator/tests/src/Functional/ItemCacheTagsTest.php
+++ b/core/modules/aggregator/tests/src/Functional/ItemCacheTagsTest.php
@@ -5,7 +5,7 @@
use Drupal\aggregator\Entity\Feed;
use Drupal\aggregator\Entity\Item;
use Drupal\Core\Cache\CacheBackendInterface;
-use Drupal\system\Tests\Entity\EntityCacheTagsTestBase;
+use Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install
index e3da6bde881c..c59f3fd25855 100644
--- a/core/modules/block_content/block_content.install
+++ b/core/modules/block_content/block_content.install
@@ -138,3 +138,29 @@ function block_content_update_8400() {
$definition_update_manager->uninstallFieldStorageDefinition($content_translation_status);
}
}
+
+/**
+ * Add parent entity fields to 'block_content' entities.
+ */
+function block_content_update_8600() {
+ $update_manager = \Drupal::entityDefinitionUpdateManager();
+ $parent_entity_type = BaseFieldDefinition::create('string')
+ ->setLabel(t('Parent entity type'))
+ ->setDescription(t('The parent entity type.'))
+ ->setTranslatable(FALSE)
+ ->setRevisionable(FALSE)
+ ->setDefaultValue(NULL)
+ ->setInitialValue(NULL);
+
+ $update_manager->installFieldStorageDefinition('parent_entity_type', 'block_content', 'block_content', $parent_entity_type);
+
+ $parent_entity_id = BaseFieldDefinition::create('string')
+ ->setLabel(t('Parent ID'))
+ ->setDescription(t('The parent entity ID.'))
+ ->setTranslatable(FALSE)
+ ->setRevisionable(FALSE)
+ ->setDefaultValue(NULL)
+ ->setInitialValue(NULL);
+
+ $update_manager->installFieldStorageDefinition('parent_entity_id', 'block_content', 'block_content', $parent_entity_id);
+}
diff --git a/core/modules/block_content/block_content.module b/core/modules/block_content/block_content.module
index 3adc979d9f08..53b7dadbd325 100644
--- a/core/modules/block_content/block_content.module
+++ b/core/modules/block_content/block_content.module
@@ -8,6 +8,9 @@
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
+use Drupal\Core\Database\Query\SelectInterface;
+use Drupal\Core\Database\Query\AlterableInterface;
+use Drupal\Core\Database\Query\ConditionInterface;
/**
* Implements hook_help().
@@ -105,3 +108,67 @@ function block_content_add_body_field($block_type_id, $label = 'Body') {
return $field;
}
+
+/**
+ * Implements hook_query_TAG_alter().
+ *
+ * Alters any 'entity_reference' query where the entity type is
+ * 'block_content' and the query has the tag 'block_content_access'.
+ *
+ * These queries should only return blocks with no parents unless a condition on
+ * 'entity_parent_type' or 'entity_parent_id' is explicitly set.
+ *
+ * Block_content entities that have a parent should by default not be selectable
+ * as entity reference values. A module can still
+ * create an instance of
+ * \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface
+ * that will will allow selection of blocks with parents by explicitly setting
+ * a condition on either the parent_entity_id or parent_entity_type fields.
+ *
+ * @see \Drupal\block_content\BlockContentAccessControlHandler
+ */
+function block_content_query_entity_reference_alter(AlterableInterface $query) {
+ if ($query instanceof SelectInterface && $query->getMetaData('entity_type') === 'block_content' && $query->hasTag('block_content_access')) {
+ $data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable();
+ if (array_key_exists($data_table, $query->getTables()) && !_block_content_has_parent_entity_condition($query->conditions())) {
+ // If no parent entity condition create a condition.
+ $query->isNull("$data_table.parent_entity_type");
+ }
+ }
+}
+
+/**
+ * Utility function to find nested conditions using the parent entity fields.
+ *
+ * @param array $condition
+ * The condition or condition group to check.
+ *
+ * @return bool
+ * Whether conditions contain any condition using the parent entity fields.
+ */
+function _block_content_has_parent_entity_condition(array $condition) {
+ // If this is a condition group call this function recursively for each nested
+ // condition until a condition is found that return TRUE.
+ if (isset($condition['#conjunction'])) {
+ foreach (array_filter($condition, 'is_array') as $nested_condition) {
+ if (_block_content_has_parent_entity_condition($nested_condition)) {
+ return TRUE;
+ }
+ }
+ return FALSE;
+ }
+ if (isset($condition['field'])) {
+ $field = $condition['field'];
+ if (is_object($field) && $field instanceof ConditionInterface) {
+ return _block_content_has_parent_entity_condition($field->conditions());
+ }
+ $field_parts = explode('.', $field);
+ $data_table = \Drupal::entityTypeManager()->getDefinition('block_content')->getDataTable();
+ // With nested conditions the data table may have a suffix at the end like
+ // 'block_content_field_data_2'.
+ if (strpos($field_parts[0], $data_table) === 0) {
+ return $field_parts[1] === 'parent_entity_id' || $field_parts[1] === 'parent_entity_type';
+ }
+ }
+ return FALSE;
+}
diff --git a/core/modules/block_content/block_content.post_update.php b/core/modules/block_content/block_content.post_update.php
new file mode 100644
index 000000000000..cb3e9cd0e54f
--- /dev/null
+++ b/core/modules/block_content/block_content.post_update.php
@@ -0,0 +1,46 @@
+getDefinition('block_content')
+ ->getDataTable();
+
+ \Drupal::classResolver(ConfigEntityUpdater::class)->update($sandbox, 'view', function ($view) use ($data_table) {
+ /** @var \Drupal\views\ViewEntityInterface $view */
+ if ($view->get('base_table') != $data_table) {
+ return FALSE;
+ }
+ $save_view = FALSE;
+ $displays = $view->get('display');
+ foreach ($displays as $display_name => &$display) {
+ // Update the default display and displays that have overridden filters.
+ if (!isset($display['display_options']['filters']['has_parent']) &&
+ ($display_name === 'default' || isset($display['display_options']['filters']))) {
+ $display['display_options']['filters']['has_parent'] = [
+ 'id' => 'has_parent',
+ 'plugin_id' => 'boolean_string',
+ 'table' => $data_table,
+ 'field' => 'has_parent',
+ 'value' => '0',
+ 'entity_type' => 'block_content',
+ 'entity_field' => 'parent_entity_type',
+ ];
+ $save_view = TRUE;
+ }
+ }
+ if ($save_view) {
+ $view->set('display', $displays);
+ }
+ return $save_view;
+ });
+}
diff --git a/core/modules/block_content/config/optional/views.view.block_content.yml b/core/modules/block_content/config/optional/views.view.block_content.yml
index 1be5a0417c12..b4de8c1859a3 100644
--- a/core/modules/block_content/config/optional/views.view.block_content.yml
+++ b/core/modules/block_content/config/optional/views.view.block_content.yml
@@ -431,6 +431,44 @@ display:
entity_type: block_content
entity_field: type
plugin_id: bundle
+ has_parent:
+ id: has_parent
+ table: block_content_field_data
+ field: has_parent
+ relationship: none
+ group_type: group
+ admin_label: ''
+ operator: '='
+ value: '0'
+ group: 1
+ exposed: false
+ expose:
+ operator_id: ''
+ label: ''
+ description: ''
+ use_operator: false
+ operator: ''
+ identifier: ''
+ required: false
+ remember: false
+ multiple: false
+ remember_roles:
+ authenticated: authenticated
+ is_grouped: false
+ group_info:
+ label: ''
+ description: ''
+ identifier: ''
+ optional: true
+ widget: select
+ multiple: false
+ remember: false
+ default_group: All
+ default_group_multiple: { }
+ group_items: { }
+ entity_type: block_content
+ entity_field: parent_entity_type
+ plugin_id: boolean_string
sorts: { }
title: 'Custom block library'
header: { }
diff --git a/core/modules/block_content/src/BlockContentAccessControlHandler.php b/core/modules/block_content/src/BlockContentAccessControlHandler.php
index 7079ef484951..dc5b10737168 100644
--- a/core/modules/block_content/src/BlockContentAccessControlHandler.php
+++ b/core/modules/block_content/src/BlockContentAccessControlHandler.php
@@ -19,10 +19,23 @@ class BlockContentAccessControlHandler extends EntityAccessControlHandler {
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($operation === 'view') {
- return AccessResult::allowedIf($entity->isPublished())->addCacheableDependency($entity)
+ $access = AccessResult::allowedIf($entity->isPublished())->addCacheableDependency($entity)
->orIf(AccessResult::allowedIfHasPermission($account, 'administer blocks'));
}
- return parent::checkAccess($entity, $operation, $account);
+ else {
+ $access = parent::checkAccess($entity, $operation, $account);
+ }
+ /** @var \Drupal\block_content\BlockContentInterface $entity */
+ if ($entity->hasParentEntity()) {
+ if ($parent_entity = $entity->getParentEntity()) {
+ $access = $access->andIf($parent_entity->access($operation, $account, TRUE));
+ }
+ else {
+ // The entity has a parent but it was not able to be loaded.
+ $access = $access->andIf(AccessResult::forbidden('Parent entity not available.'));
+ }
+ }
+ return $access;
}
}
diff --git a/core/modules/block_content/src/BlockContentInterface.php b/core/modules/block_content/src/BlockContentInterface.php
index 75fdc5979b38..16d427561e20 100644
--- a/core/modules/block_content/src/BlockContentInterface.php
+++ b/core/modules/block_content/src/BlockContentInterface.php
@@ -4,6 +4,7 @@
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityChangedInterface;
+use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Core\Entity\RevisionLogInterface;
@@ -83,4 +84,41 @@ public function getTheme();
*/
public function getInstances();
+ /**
+ * Sets the parent entity.
+ *
+ * @param \Drupal\Core\Entity\EntityInterface $parent_entity
+ * The parent entity.
+ *
+ * @return \Drupal\block_content\BlockContentInterface
+ * The class instance that this method is called on.
+ */
+ public function setParentEntity(EntityInterface $parent_entity);
+
+ /**
+ * Gets the parent entity.
+ *
+ * @return \Drupal\Core\Entity\EntityInterface|null
+ * The parent entity or null if none exists.
+ *
+ * @todo How to deterine parent is set but no longer exists.
+ */
+ public function getParentEntity();
+
+ /**
+ * Removes the parent entity.
+ *
+ * @return \Drupal\block_content\BlockContentInterface
+ * The class instance that this method is called on.
+ */
+ public function removeParentEntity();
+
+ /**
+ * Whether the block has a parent entity set.
+ *
+ * @return bool
+ * TRUE if a parent entity is set, otherwise FALSE.
+ */
+ public function hasParentEntity();
+
}
diff --git a/core/modules/block_content/src/BlockContentListBuilder.php b/core/modules/block_content/src/BlockContentListBuilder.php
index 7a4bdfc4c809..8467c3c3e003 100644
--- a/core/modules/block_content/src/BlockContentListBuilder.php
+++ b/core/modules/block_content/src/BlockContentListBuilder.php
@@ -28,4 +28,19 @@ public function buildRow(EntityInterface $entity) {
return $row + parent::buildRow($entity);
}
+ /**
+ * {@inheritdoc}
+ */
+ protected function getEntityIds() {
+ $query = $this->getStorage()->getQuery()
+ ->sort($this->entityType->getKey('id'))
+ ->notExists('parent_entity_type');
+
+ // Only add the pager if a limit is specified.
+ if ($this->limit) {
+ $query->pager($this->limit);
+ }
+ return $query->execute();
+ }
+
}
diff --git a/core/modules/block_content/src/BlockContentViewsData.php b/core/modules/block_content/src/BlockContentViewsData.php
index 010ede0ea59a..de3df7af6ad6 100644
--- a/core/modules/block_content/src/BlockContentViewsData.php
+++ b/core/modules/block_content/src/BlockContentViewsData.php
@@ -21,8 +21,22 @@ public function getViewsData() {
$data['block_content_field_data']['info']['field']['id'] = 'field';
$data['block_content_field_data']['info']['field']['link_to_entity default'] = TRUE;
+ $data['block_content_field_data']['has_parent'] = [
+ 'title' => $this->t('Has Parent'),
+ 'help' => $this->t('Whether the block has a parent'),
+ 'field' => ['id' => 'field'],
+ 'filter' => [
+ 'id' => 'boolean_string',
+ 'accept_null' => TRUE,
+ ],
+ 'entity field' => 'parent_entity_type',
+ 'real field' => 'parent_entity_type',
+ ];
+
$data['block_content_field_data']['type']['field']['id'] = 'field';
+ $data['block_content_field_data']['table']['wizard_id'] = 'block_content';
+
$data['block_content']['block_content_listing_empty'] = [
'title' => $this->t('Empty block library behavior'),
'help' => $this->t('Provides a link to add a new block.'),
diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php
index 7696da091e03..2a8347703633 100644
--- a/core/modules/block_content/src/Entity/BlockContent.php
+++ b/core/modules/block_content/src/Entity/BlockContent.php
@@ -3,6 +3,7 @@
namespace Drupal\block_content\Entity;
use Drupal\Core\Entity\EditorialContentEntityBase;
+use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
@@ -118,7 +119,9 @@ public function getTheme() {
*/
public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update);
- static::invalidateBlockPluginCache();
+ if (empty($this->get('parent_entity_type')->value) || (isset($this->original) && empty($this->original->get('parent_entity_type')->value))) {
+ static::invalidateBlockPluginCache();
+ }
}
/**
@@ -126,7 +129,14 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
*/
public static function postDelete(EntityStorageInterface $storage, array $entities) {
parent::postDelete($storage, $entities);
- static::invalidateBlockPluginCache();
+ /** @var \Drupal\block_content\BlockContentInterface $block */
+ foreach ($entities as $block) {
+ if (!$block->hasParentEntity()) {
+ // If any deleted blocks do not have a parent ID clear the block cache.
+ static::invalidateBlockPluginCache();
+ return;
+ }
+ }
}
/**
@@ -200,6 +210,24 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setTranslatable(TRUE)
->setRevisionable(TRUE);
+ // @todo Is there a core issue to add
+ // https://www.drupal.org/project/dynamic_entity_reference
+ $fields['parent_entity_type'] = BaseFieldDefinition::create('string')
+ ->setLabel(t('Parent entity type'))
+ ->setDescription(t('The parent entity type.'))
+ ->setTranslatable(FALSE)
+ ->setRevisionable(FALSE)
+ ->setDefaultValue(NULL)
+ ->setInitialValue(NULL);
+
+ $fields['parent_entity_id'] = BaseFieldDefinition::create('string')
+ ->setLabel(t('Parent ID'))
+ ->setDescription(t('The parent entity ID.'))
+ ->setTranslatable(FALSE)
+ ->setRevisionable(FALSE)
+ ->setDefaultValue(NULL)
+ ->setInitialValue(NULL);
+
return $fields;
}
@@ -290,4 +318,41 @@ protected static function invalidateBlockPluginCache() {
\Drupal::service('plugin.manager.block')->clearCachedDefinitions();
}
+ /**
+ * {@inheritdoc}
+ */
+ public function setParentEntity(EntityInterface $parent_entity) {
+ $this->set('parent_entity_type', $parent_entity->getEntityTypeId());
+ $this->set('parent_entity_id', $parent_entity->id());
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getParentEntity() {
+ if ($this->hasParentEntity()) {
+ return \Drupal::entityTypeManager()->getStorage($this->get('parent_entity_type')->value)->load($this->get('parent_entity_id')->value);
+ }
+ return NULL;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function removeParentEntity() {
+ $this->set('parent_entity_type', NULL);
+ $this->set('parent_entity_id', NULL);
+ return $this;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function hasParentEntity() {
+ // If either parent field value is set then the block is considered to have
+ // a parent.
+ return !empty($this->get('parent_entity_type')->value) || !empty($this->get('parent_entity_id')->value);
+ }
+
}
diff --git a/core/modules/block_content/src/Plugin/Derivative/BlockContent.php b/core/modules/block_content/src/Plugin/Derivative/BlockContent.php
index ac82a6c39caa..edf82bc46a59 100644
--- a/core/modules/block_content/src/Plugin/Derivative/BlockContent.php
+++ b/core/modules/block_content/src/Plugin/Derivative/BlockContent.php
@@ -43,10 +43,11 @@ public static function create(ContainerInterface $container, $base_plugin_id) {
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
- $block_contents = $this->blockContentStorage->loadMultiple();
+ $block_ids = $this->blockContentStorage->getQuery()->notExists('parent_entity_type')->execute();
+ $block_contents = $this->blockContentStorage->loadMultiple($block_ids);
// Reset the discovered definitions.
$this->derivatives = [];
- /** @var $block_content \Drupal\block_content\Entity\BlockContent */
+ /* @var $block_content \Drupal\block_content\Entity\BlockContent */
foreach ($block_contents as $block_content) {
$this->derivatives[$block_content->uuid()] = $base_plugin_definition;
$this->derivatives[$block_content->uuid()]['admin_label'] = $block_content->label();
diff --git a/core/modules/block_content/src/Plugin/views/wizard/BlockContent.php b/core/modules/block_content/src/Plugin/views/wizard/BlockContent.php
new file mode 100644
index 000000000000..6c4bfcf33696
--- /dev/null
+++ b/core/modules/block_content/src/Plugin/views/wizard/BlockContent.php
@@ -0,0 +1,36 @@
+ 'has_parent',
+ 'plugin_id' => 'boolean_string',
+ 'table' => $this->base_table,
+ 'field' => 'has_parent',
+ 'operator' => '=',
+ 'value' => '0',
+ 'entity_type' => $this->entityTypeId,
+ 'entity_field' => 'parent_entity_type',
+ ];
+ return $filters;
+ }
+
+}
diff --git a/core/modules/block_content/tests/modules/block_content_test/src/Plugin/EntityReferenceSelection/TestSelection.php b/core/modules/block_content/tests/modules/block_content_test/src/Plugin/EntityReferenceSelection/TestSelection.php
new file mode 100644
index 000000000000..1c3f753478c1
--- /dev/null
+++ b/core/modules/block_content/tests/modules/block_content_test/src/Plugin/EntityReferenceSelection/TestSelection.php
@@ -0,0 +1,84 @@
+testMode = $testMode;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function buildEntityQuery($match = NULL, $match_operator = 'CONTAINS') {
+ $query = parent::buildEntityQuery($match, $match_operator);
+ if (strpos($this->testMode, 'parent_entity_id') === 0) {
+ $field = 'parent_entity_id';
+ }
+ else {
+ $field = 'parent_entity_type';
+ }
+ switch ($this->testMode) {
+ case "{$field}_condition_false":
+ $query->notExists($field);
+ break;
+
+ case "{$field}_condition_group_false":
+ $group = $query->andConditionGroup()
+ ->notExists($field)
+ ->exists('type');
+ $query->condition($group);
+ break;
+
+ case "{$field}_condition_group_true":
+ $group = $query->andConditionGroup()
+ ->exists($field)
+ ->exists('type');
+ $query->condition($group);
+ break;
+
+ case "{$field}_condition_nested_group_false":
+ $query->exists('type');
+ $sub_group = $query->andConditionGroup()
+ ->notExists($field)
+ ->exists('type');
+ $group = $query->andConditionGroup()
+ ->exists('type')
+ ->condition($sub_group);
+ $query->condition($group);
+ break;
+
+ case "{$field}_condition_nested_group_true":
+ $query->exists('type');
+ $sub_group = $query->andConditionGroup()
+ ->exists($field)
+ ->exists('type');
+ $group = $query->andConditionGroup()
+ ->exists('type')
+ ->condition($sub_group);
+ $query->condition($group);
+ break;
+ }
+ return $query;
+ }
+
+}
diff --git a/core/modules/block_content/tests/modules/block_content_view_override/block_content_view_override.info.yml b/core/modules/block_content/tests/modules/block_content_view_override/block_content_view_override.info.yml
new file mode 100644
index 000000000000..3ca2d1bc375d
--- /dev/null
+++ b/core/modules/block_content/tests/modules/block_content_view_override/block_content_view_override.info.yml
@@ -0,0 +1,9 @@
+name: "Custom Block module reusable tests"
+type: module
+description: "Support module for custom block reusable testing."
+package: Testing
+version: VERSION
+core: 8.x
+dependencies:
+ - drupal:block_content
+ - drupal:views
diff --git a/core/modules/block_content/tests/modules/block_content_view_override/config/install/views.view.block_content.yml b/core/modules/block_content/tests/modules/block_content_view_override/config/install/views.view.block_content.yml
new file mode 100644
index 000000000000..1c95f9f854cb
--- /dev/null
+++ b/core/modules/block_content/tests/modules/block_content_view_override/config/install/views.view.block_content.yml
@@ -0,0 +1,602 @@
+langcode: en
+status: true
+dependencies:
+ module:
+ - block_content
+ - user
+id: block_content
+label: 'Custom block library'
+module: views
+description: 'Find and manage custom blocks.'
+tag: default
+base_table: block_content_field_data
+base_field: id
+core: 8.x
+display:
+ default:
+ display_plugin: default
+ id: default
+ display_title: Master
+ position: 0
+ display_options:
+ access:
+ type: perm
+ options:
+ perm: 'administer blocks'
+ cache:
+ type: tag
+ options: { }
+ query:
+ type: views_query
+ options:
+ disable_sql_rewrite: false
+ distinct: false
+ replica: false
+ query_comment: ''
+ query_tags: { }
+ exposed_form:
+ type: basic
+ options:
+ submit_button: Apply
+ reset_button: false
+ reset_button_label: Reset
+ exposed_sorts_label: 'Sort by'
+ expose_sort_order: true
+ sort_asc_label: Asc
+ sort_desc_label: Desc
+ pager:
+ type: mini
+ options:
+ items_per_page: 50
+ offset: 0
+ id: 0
+ total_pages: null
+ tags:
+ previous: '‹ Previous'
+ next: 'Next ›'
+ expose:
+ items_per_page: false
+ items_per_page_label: 'Items per page'
+ items_per_page_options: '5, 10, 25, 50'
+ items_per_page_options_all: false
+ items_per_page_options_all_label: '- All -'
+ offset: false
+ offset_label: Offset
+ style:
+ type: table
+ options:
+ grouping: { }
+ row_class: ''
+ default_row_class: true
+ override: true
+ sticky: false
+ caption: ''
+ summary: ''
+ description: ''
+ columns:
+ info: info
+ type: type
+ changed: changed
+ operations: operations
+ info:
+ info:
+ sortable: true
+ default_sort_order: asc
+ align: ''
+ separator: ''
+ empty_column: false
+ responsive: ''
+ type:
+ sortable: true
+ default_sort_order: asc
+ align: ''
+ separator: ''
+ empty_column: false
+ responsive: ''
+ changed:
+ sortable: true
+ default_sort_order: desc
+ align: ''
+ separator: ''
+ empty_column: false
+ responsive: ''
+ operations:
+ sortable: false
+ default_sort_order: asc
+ align: ''
+ separator: ''
+ empty_column: false
+ responsive: ''
+ default: changed
+ empty_table: true
+ row:
+ type: fields
+ fields:
+ info:
+ id: info
+ table: block_content_field_data
+ field: info
+ relationship: none
+ group_type: group
+ admin_label: ''
+ label: 'Block description'
+ exclude: false
+ alter:
+ alter_text: false
+ text: ''
+ make_link: false
+ path: ''
+ absolute: false
+ external: false
+ replace_spaces: false
+ path_case: none
+ trim_whitespace: false
+ alt: ''
+ rel: ''
+ link_class: ''
+ prefix: ''
+ suffix: ''
+ target: ''
+ nl2br: false
+ max_length: 0
+ word_boundary: true
+ ellipsis: true
+ more_link: false
+ more_link_text: ''
+ more_link_path: ''
+ strip_tags: false
+ trim: false
+ preserve_tags: ''
+ html: false
+ element_type: ''
+ element_class: ''
+ element_label_type: ''
+ element_label_class: ''
+ element_label_colon: true
+ element_wrapper_type: ''
+ element_wrapper_class: ''
+ element_default_classes: true
+ empty: ''
+ hide_empty: false
+ empty_zero: false
+ hide_alter_empty: true
+ click_sort_column: value
+ type: string
+ settings:
+ link_to_entity: true
+ group_column: value
+ group_columns: { }
+ group_rows: true
+ delta_limit: 0
+ delta_offset: 0
+ delta_reversed: false
+ delta_first_last: false
+ multi_type: separator
+ separator: ', '
+ field_api_classes: false
+ entity_type: null
+ entity_field: info
+ plugin_id: field
+ type:
+ id: type
+ table: block_content_field_data
+ field: type
+ relationship: none
+ group_type: group
+ admin_label: ''
+ label: 'Block type'
+ exclude: false
+ alter:
+ alter_text: false
+ text: ''
+ make_link: false
+ path: ''
+ absolute: false
+ external: false
+ replace_spaces: false
+ path_case: none
+ trim_whitespace: false
+ alt: ''
+ rel: ''
+ link_class: ''
+ prefix: ''
+ suffix: ''
+ target: ''
+ nl2br: false
+ max_length: 0
+ word_boundary: true
+ ellipsis: true
+ more_link: false
+ more_link_text: ''
+ more_link_path: ''
+ strip_tags: false
+ trim: false
+ preserve_tags: ''
+ html: false
+ element_type: ''
+ element_class: ''
+ element_label_type: ''
+ element_label_class: ''
+ element_label_colon: true
+ element_wrapper_type: ''
+ element_wrapper_class: ''
+ element_default_classes: true
+ empty: ''
+ hide_empty: false
+ empty_zero: false
+ hide_alter_empty: true
+ click_sort_column: target_id
+ type: entity_reference_label
+ settings:
+ link: false
+ group_column: target_id
+ group_columns: { }
+ group_rows: true
+ delta_limit: 0
+ delta_offset: 0
+ delta_reversed: false
+ delta_first_last: false
+ multi_type: separator
+ separator: ', '
+ field_api_classes: false
+ entity_type: block_content
+ entity_field: type
+ plugin_id: field
+ changed:
+ id: changed
+ table: block_content_field_data
+ field: changed
+ relationship: none
+ group_type: group
+ admin_label: ''
+ label: Updated
+ exclude: false
+ alter:
+ alter_text: false
+ text: ''
+ make_link: false
+ path: ''
+ absolute: false
+ external: false
+ replace_spaces: false
+ path_case: none
+ trim_whitespace: false
+ alt: ''
+ rel: ''
+ link_class: ''
+ prefix: ''
+ suffix: ''
+ target: ''
+ nl2br: false
+ max_length: 0
+ word_boundary: true
+ ellipsis: true
+ more_link: false
+ more_link_text: ''
+ more_link_path: ''
+ strip_tags: false
+ trim: false
+ preserve_tags: ''
+ html: false
+ element_type: ''
+ element_class: ''
+ element_label_type: ''
+ element_label_class: ''
+ element_label_colon: true
+ element_wrapper_type: ''
+ element_wrapper_class: ''
+ element_default_classes: true
+ empty: ''
+ hide_empty: false
+ empty_zero: false
+ hide_alter_empty: true
+ entity_type: block_content
+ entity_field: changed
+ type: timestamp
+ settings:
+ date_format: short
+ custom_date_format: ''
+ timezone: ''
+ plugin_id: field
+ operations:
+ id: operations
+ table: block_content
+ field: operations
+ relationship: none
+ group_type: group
+ admin_label: ''
+ label: Operations
+ exclude: false
+ alter:
+ alter_text: false
+ text: ''
+ make_link: false
+ path: ''
+ absolute: false
+ external: false
+ replace_spaces: false
+ path_case: none
+ trim_whitespace: false
+ alt: ''
+ rel: ''
+ link_class: ''
+ prefix: ''
+ suffix: ''
+ target: ''
+ nl2br: false
+ max_length: 0
+ word_boundary: true
+ ellipsis: true
+ more_link: false
+ more_link_text: ''
+ more_link_path: ''
+ strip_tags: false
+ trim: false
+ preserve_tags: ''
+ html: false
+ element_type: ''
+ element_class: ''
+ element_label_type: ''
+ element_label_class: ''
+ element_label_colon: true
+ element_wrapper_type: ''
+ element_wrapper_class: ''
+ element_default_classes: true
+ empty: ''
+ hide_empty: false
+ empty_zero: false
+ hide_alter_empty: true
+ destination: true
+ entity_type: block_content
+ plugin_id: entity_operations
+ filters:
+ info:
+ id: info
+ table: block_content_field_data
+ field: info
+ relationship: none
+ group_type: group
+ admin_label: ''
+ operator: contains
+ value: ''
+ group: 1
+ exposed: true
+ expose:
+ operator_id: info_op
+ label: 'Block description'
+ description: ''
+ use_operator: false
+ operator: info_op
+ identifier: info
+ required: false
+ remember: false
+ multiple: false
+ remember_roles:
+ authenticated: authenticated
+ anonymous: '0'
+ administrator: '0'
+ is_grouped: false
+ group_info:
+ label: ''
+ description: ''
+ identifier: ''
+ optional: true
+ widget: select
+ multiple: false
+ remember: false
+ default_group: All
+ default_group_multiple: { }
+ group_items: { }
+ entity_type: block_content
+ entity_field: info
+ plugin_id: string
+ type:
+ id: type
+ table: block_content_field_data
+ field: type
+ relationship: none
+ group_type: group
+ admin_label: ''
+ operator: in
+ value: { }
+ group: 1
+ exposed: true
+ expose:
+ operator_id: type_op
+ label: 'Block type'
+ description: ''
+ use_operator: false
+ operator: type_op
+ identifier: type
+ required: false
+ remember: false
+ multiple: false
+ remember_roles:
+ authenticated: authenticated
+ anonymous: '0'
+ administrator: '0'
+ reduce: false
+ is_grouped: false
+ group_info:
+ label: ''
+ description: ''
+ identifier: ''
+ optional: true
+ widget: select
+ multiple: false
+ remember: false
+ default_group: All
+ default_group_multiple: { }
+ group_items: { }
+ entity_type: block_content
+ entity_field: type
+ plugin_id: bundle
+ sorts: { }
+ title: 'Custom block library'
+ header: { }
+ footer: { }
+ empty:
+ area_text_custom:
+ id: area_text_custom
+ table: views
+ field: area_text_custom
+ relationship: none
+ group_type: group
+ admin_label: ''
+ empty: true
+ tokenize: false
+ content: 'There are no custom blocks available.'
+ plugin_id: text_custom
+ block_content_listing_empty:
+ admin_label: ''
+ empty: true
+ field: block_content_listing_empty
+ group_type: group
+ id: block_content_listing_empty
+ label: ''
+ relationship: none
+ table: block_content
+ plugin_id: block_content_listing_empty
+ entity_type: block_content
+ relationships: { }
+ arguments: { }
+ display_extenders: { }
+ cache_metadata:
+ contexts:
+ - 'languages:language_content'
+ - 'languages:language_interface'
+ - url
+ - url.query_args
+ - user.permissions
+ max-age: -1
+ tags: { }
+ page_1:
+ display_plugin: page
+ id: page_1
+ display_title: Page
+ position: 1
+ display_options:
+ display_extenders: { }
+ path: admin/structure/block/block-content
+ menu:
+ type: tab
+ title: 'Custom block library'
+ description: ''
+ parent: block.admin_display
+ weight: 0
+ context: '0'
+ menu_name: admin
+ cache_metadata:
+ contexts:
+ - 'languages:language_content'
+ - 'languages:language_interface'
+ - url
+ - url.query_args
+ - user.permissions
+ max-age: -1
+ tags: { }
+ page_2:
+ display_plugin: page
+ id: page_2
+ display_title: 'Page 2'
+ position: 2
+ display_options:
+ display_extenders: { }
+ path: extra-view-display
+ filters:
+ type:
+ id: type
+ table: block_content_field_data
+ field: type
+ relationship: none
+ group_type: group
+ admin_label: ''
+ operator: in
+ value: { }
+ group: 1
+ exposed: true
+ expose:
+ operator_id: type_op
+ label: 'Block type'
+ description: ''
+ use_operator: false
+ operator: type_op
+ identifier: type
+ required: false
+ remember: false
+ multiple: false
+ remember_roles:
+ authenticated: authenticated
+ anonymous: '0'
+ administrator: '0'
+ reduce: false
+ is_grouped: false
+ group_info:
+ label: ''
+ description: ''
+ identifier: ''
+ optional: true
+ widget: select
+ multiple: false
+ remember: false
+ default_group: All
+ default_group_multiple: { }
+ group_items: { }
+ entity_type: block_content
+ entity_field: type
+ plugin_id: bundle
+ info:
+ id: info
+ table: block_content_field_data
+ field: info
+ relationship: none
+ group_type: group
+ admin_label: ''
+ operator: 'contains'
+ value: block2
+ group: 1
+ exposed: false
+ expose:
+ operator_id: ''
+ label: ''
+ description: ''
+ use_operator: false
+ operator: ''
+ identifier: ''
+ required: false
+ remember: false
+ multiple: false
+ remember_roles:
+ authenticated: authenticated
+ placeholder: ''
+ is_grouped: false
+ group_info:
+ label: ''
+ description: ''
+ identifier: ''
+ optional: true
+ widget: select
+ multiple: false
+ remember: false
+ default_group: All
+ default_group_multiple: { }
+ group_items: { }
+ entity_type: block_content
+ entity_field: info
+ plugin_id: string
+ defaults:
+ filters: false
+ filter_groups: false
+ filter_groups:
+ operator: AND
+ groups:
+ 1: AND
+ cache_metadata:
+ max-age: -1
+ contexts:
+ - 'languages:language_content'
+ - 'languages:language_interface'
+ - url
+ - url.query_args
+ - user.permissions
+ tags: { }
diff --git a/core/modules/block_content/tests/src/Functional/BlockContentCacheTagsTest.php b/core/modules/block_content/tests/src/Functional/BlockContentCacheTagsTest.php
index 683500928508..ea5dd2873deb 100644
--- a/core/modules/block_content/tests/src/Functional/BlockContentCacheTagsTest.php
+++ b/core/modules/block_content/tests/src/Functional/BlockContentCacheTagsTest.php
@@ -7,7 +7,7 @@
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
-use Drupal\system\Tests\Entity\EntityCacheTagsTestBase;
+use Drupal\Tests\system\Functional\Entity\EntityCacheTagsTestBase;
use Symfony\Component\HttpFoundation\Request;
/**
diff --git a/core/modules/block_content/tests/src/Functional/BlockContentListTest.php b/core/modules/block_content/tests/src/Functional/BlockContentListTest.php
index 9a26f1c40776..12a8dcc12987 100644
--- a/core/modules/block_content/tests/src/Functional/BlockContentListTest.php
+++ b/core/modules/block_content/tests/src/Functional/BlockContentListTest.php
@@ -2,6 +2,8 @@
namespace Drupal\Tests\block_content\Functional;
+use Drupal\block_content\Entity\BlockContent;
+
/**
* Tests the listing of custom blocks.
*
@@ -104,6 +106,19 @@ public function testListing() {
// Confirm that the empty text is displayed.
$this->assertText(t('There are no custom blocks yet.'));
+
+ $block_content = BlockContent::create([
+ 'info' => 'Block with parent',
+ 'type' => 'basic',
+ ]);
+ $block_content->setParentEntity($this->loggedInUser);
+ $block_content->save();
+
+ $this->drupalGet('admin/structure/block/block-content');
+ // Confirm that the empty text is displayed.
+ $this->assertSession()->pageTextContains('There are no custom blocks yet.');
+ // Confirm the block with a parent is not on the page.
+ $this->assertSession()->pageTextNotContains('Block with parent');
}
}
diff --git a/core/modules/block_content/tests/src/Functional/BlockContentListViewsTest.php b/core/modules/block_content/tests/src/Functional/BlockContentListViewsTest.php
index 1c623be82eba..8a229e742c2a 100644
--- a/core/modules/block_content/tests/src/Functional/BlockContentListViewsTest.php
+++ b/core/modules/block_content/tests/src/Functional/BlockContentListViewsTest.php
@@ -2,6 +2,8 @@
namespace Drupal\Tests\block_content\Functional;
+use Drupal\block_content\Entity\BlockContent;
+
/**
* Tests the Views-powered listing of custom blocks.
*
@@ -112,6 +114,19 @@ public function testListing() {
// Confirm that the empty text is displayed.
$this->assertText('There are no custom blocks available.');
$this->assertLink('custom block');
+
+ $block_content = BlockContent::create([
+ 'info' => 'Block with parent',
+ 'type' => 'basic',
+ ]);
+ $block_content->setParentEntity($this->loggedInUser);
+ $block_content->save();
+
+ $this->drupalGet('admin/structure/block/block-content');
+ // Confirm that the empty text is displayed.
+ $this->assertSession()->pageTextContains('There are no custom blocks available.');
+ // Confirm the Block with parent is not on the page.
+ $this->assertSession()->pageTextNotContains('Block with parent');
}
}
diff --git a/core/modules/block_content/tests/src/Functional/Rest/BlockContentResourceTestBase.php b/core/modules/block_content/tests/src/Functional/Rest/BlockContentResourceTestBase.php
index c77585eb292d..13b2df05b1c9 100644
--- a/core/modules/block_content/tests/src/Functional/Rest/BlockContentResourceTestBase.php
+++ b/core/modules/block_content/tests/src/Functional/Rest/BlockContentResourceTestBase.php
@@ -92,6 +92,8 @@ protected function getExpectedNormalizedEntity() {
'value' => 'en',
],
],
+ 'parent_entity_type' => [],
+ 'parent_entity_id' => [],
'type' => [
[
'target_id' => 'basic',
diff --git a/core/modules/block_content/tests/src/Functional/Update/BlockContentParentEntityUpdateTest.php b/core/modules/block_content/tests/src/Functional/Update/BlockContentParentEntityUpdateTest.php
new file mode 100644
index 000000000000..7a2ab3034cf7
--- /dev/null
+++ b/core/modules/block_content/tests/src/Functional/Update/BlockContentParentEntityUpdateTest.php
@@ -0,0 +1,179 @@
+databaseDumpFiles = [
+ __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.4.0.bare.standard.php.gz',
+ ];
+ }
+
+ /**
+ * Tests adding parent entity fields to the block content entity type.
+ *
+ * @see block_content_update_8600
+ * @see block_content_post_update_add_views_parent_filter
+ */
+ public function testParentFieldsAddition() {
+ $assert_session = $this->assertSession();
+ $entity_definition_update_manager = \Drupal::entityDefinitionUpdateManager();
+
+ // Delete custom block library view.
+ View::load('block_content')->delete();
+ // Install the test module with the 'block_content' view with an extra
+ // display with overridden filters. This extra display should also have the
+ // 'has_parent' filter added so that it does not expose fields with parents
+ // This display also a filter only show blocks that contain 'block2' in the
+ // 'info' field.
+ $this->container->get('module_installer')->install(['block_content_view_override']);
+
+ // Ensure that parent entity fields are not present before updates.
+ $this->assertEmpty($entity_definition_update_manager->getFieldStorageDefinition('parent_entity_type', 'block_content'));
+ $this->assertEmpty($entity_definition_update_manager->getFieldStorageDefinition('parent_entity_id', 'block_content'));
+
+ // Ensure that 'has_parent' filter is not present before updates.
+ $view_config = \Drupal::configFactory()->get('views.view.block_content');
+ $this->assertFalse($view_config->isNew());
+ $this->assertEmpty($view_config->get('display.default.display_options.filters.has_parent'));
+ $this->assertEmpty($view_config->get('display.page_2.display_options.filters.has_parent'));
+ // Run updates.
+ $this->runUpdates();
+
+ // Ensure that 'has_parent' filter is present after updates.
+ \Drupal::configFactory()->clearStaticCache();
+ $view_config = \Drupal::configFactory()->get('views.view.block_content');
+ $this->assertNotEmpty($view_config->get('display.default.display_options.filters.has_parent'));
+ $this->assertNotEmpty($view_config->get('display.page_2.display_options.filters.has_parent'));
+
+ // Check that the 'parent_entity_type' field exists and is configured
+ // correctly.
+ $parent_type_field = $entity_definition_update_manager->getFieldStorageDefinition('parent_entity_type', 'block_content');
+ $this->assertEquals('Parent entity type', $parent_type_field->getLabel());
+ $this->assertEquals('The parent entity type.', $parent_type_field->getDescription());
+ $this->assertEquals(FALSE, $parent_type_field->isRevisionable());
+ $this->assertEquals(FALSE, $parent_type_field->isTranslatable());
+
+ // Check that the 'parent_entity_id' field exists and is configured
+ // correctly.
+ $parent_id_field = $entity_definition_update_manager->getFieldStorageDefinition('parent_entity_id', 'block_content');
+ $this->assertEquals('Parent ID', $parent_id_field->getLabel());
+ $this->assertEquals('The parent entity ID.', $parent_id_field->getDescription());
+ $this->assertEquals(FALSE, $parent_id_field->isRevisionable());
+ $this->assertEquals(FALSE, $parent_id_field->isTranslatable());
+
+ $after_block1 = BlockContent::create([
+ 'info' => 'After update block1',
+ 'type' => 'basic_block',
+ ]);
+ $after_block1->save();
+ // Add second block that will be shown with the 'info' filter on the
+ // additional view display.
+ $after_block2 = BlockContent::create([
+ 'info' => 'After update block2',
+ 'type' => 'basic_block',
+ ]);
+ $after_block2->save();
+
+ $this->assertEquals(FALSE, $after_block1->hasParentEntity());
+ $this->assertEquals(FALSE, $after_block2->hasParentEntity());
+
+ $admin_user = $this->drupalCreateUser(['administer blocks']);
+ $this->drupalLogin($admin_user);
+
+ $block_with_parent = BlockContent::create([
+ 'info' => 'block1 with parent',
+ 'type' => 'basic_block',
+ ]);
+ $block_with_parent->setParentEntity($admin_user);
+ $block_with_parent->save();
+ // Add second block that would be shown with the 'info' filter on the
+ // additional view display if the 'has_parent' filter was not added.
+ $block2_with_parent = BlockContent::create([
+ 'info' => 'block2 with parent',
+ 'type' => 'basic_block',
+ ]);
+ $block2_with_parent->setParentEntity($admin_user);
+ $block2_with_parent->save();
+ $this->assertEquals(TRUE, $block_with_parent->hasParentEntity());
+ $this->assertEquals(TRUE, $block2_with_parent->hasParentEntity());
+
+ // Ensure the Custom Block view shows the blocks without parents only.
+ $this->drupalGet('admin/structure/block/block-content');
+ $assert_session->statusCodeEquals('200');
+ $assert_session->responseContains('view-id-block_content');
+ $assert_session->pageTextContains($after_block1->label());
+ $assert_session->pageTextContains($after_block2->label());
+ $assert_session->pageTextNotContains($block_with_parent->label());
+ $assert_session->pageTextNotContains($block2_with_parent->label());
+
+ // Ensure the view's other display also only shows blocks without parent and
+ // still filters on the 'info' field.
+ $this->drupalGet('extra-view-display');
+ $assert_session->statusCodeEquals('200');
+ $assert_session->responseContains('view-id-block_content');
+ $assert_session->pageTextNotContains($after_block1->label());
+ $assert_session->pageTextContains($after_block2->label());
+ $assert_session->pageTextNotContains($block_with_parent->label());
+ $assert_session->pageTextNotContains($block2_with_parent->label());
+
+ // Ensure the Custom Block listing without Views installed shows the only
+ // blocks without parents.
+ $this->drupalGet('admin/structure/block/block-content');
+ $this->container->get('module_installer')->uninstall(['views_ui', 'views']);
+ $this->drupalGet('admin/structure/block/block-content');
+ $assert_session->statusCodeEquals('200');
+ $assert_session->responseNotContains('view-id-block_content');
+ $assert_session->pageTextContains($after_block1->label());
+ $assert_session->pageTextContains($after_block2->label());
+ $assert_session->pageTextNotContains($block_with_parent->label());
+ $assert_session->pageTextNotContains($block2_with_parent->label());
+
+ $this->drupalGet('block/' . $after_block1->id());
+ $assert_session->statusCodeEquals('200');
+
+ // Ensure the user who can access a block parent they can access edit form
+ // edit route is not accessible.
+ $this->drupalGet('block/' . $block_with_parent->id());
+ $assert_session->statusCodeEquals('200');
+
+ $this->drupalLogout();
+
+ $this->drupalLogin($this->createUser([
+ 'access user profiles',
+ 'administer blocks',
+ ]));
+ $this->drupalGet('block/' . $after_block1->id());
+ $assert_session->statusCodeEquals('200');
+
+ $this->drupalGet('block/' . $block_with_parent->id());
+ $assert_session->statusCodeEquals('403');
+
+ $this->drupalLogin($this->createUser([
+ 'administer blocks',
+ ]));
+
+ $this->drupalGet('block/' . $after_block1->id());
+ $assert_session->statusCodeEquals('200');
+
+ $this->drupalGet('user/' . $admin_user->id());
+ $assert_session->statusCodeEquals('403');
+
+ $this->drupalGet('block/' . $block_with_parent->id());
+ $assert_session->statusCodeEquals('403');
+ }
+
+}
diff --git a/core/modules/block_content/tests/src/Functional/Views/BlockContentWizardTest.php b/core/modules/block_content/tests/src/Functional/Views/BlockContentWizardTest.php
new file mode 100644
index 000000000000..e765dd6684d4
--- /dev/null
+++ b/core/modules/block_content/tests/src/Functional/Views/BlockContentWizardTest.php
@@ -0,0 +1,52 @@
+drupalLogin($this->drupalCreateUser(['administer views']));
+ $this->createBlockContentType('Basic block');
+ }
+
+ /**
+ * Tests creating a 'block_content' entity view.
+ */
+ public function testViewAddBlockContent() {
+ $view = [];
+ $view['label'] = $this->randomMachineName(16);
+ $view['id'] = strtolower($this->randomMachineName(16));
+ $view['description'] = $this->randomMachineName(16);
+ $view['page[create]'] = FALSE;
+ $view['show[wizard_key]'] = 'block_content';
+ $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit'));
+
+ $view_storage_controller = $this->container->get('entity_type.manager')->getStorage('view');
+ /** @var \Drupal\views\Entity\View $view */
+ $view = $view_storage_controller->load($view['id']);
+
+ $display_options = $view->getDisplay('default')['display_options'];
+
+ $this->assertEquals('block_content', $display_options['filters']['has_parent']['entity_type']);
+ $this->assertEquals('parent_entity_type', $display_options['filters']['has_parent']['entity_field']);
+ $this->assertEquals('boolean_string', $display_options['filters']['has_parent']['plugin_id']);
+ $this->assertEquals('0', $display_options['filters']['has_parent']['value']);
+ }
+
+}
diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentDeriverTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentDeriverTest.php
new file mode 100644
index 000000000000..dededb611a96
--- /dev/null
+++ b/core/modules/block_content/tests/src/Kernel/BlockContentDeriverTest.php
@@ -0,0 +1,73 @@
+installSchema('system', ['sequence']);
+ $this->installSchema('system', ['sequences']);
+ $this->installEntitySchema('user');
+ $this->installEntitySchema('block_content');
+ }
+
+ /**
+ * Tests that block with parents are not derived.
+ */
+ public function testBlocksWithParentsNotDerived() {
+ // Create a block content type.
+ $block_content_type = BlockContentType::create([
+ 'id' => 'spiffy',
+ 'label' => 'Mucho spiffy',
+ 'description' => "Provides a block type that increases your site's spiffiness by up to 11%",
+ ]);
+ $block_content_type->save();
+ // And a block content entity.
+ $block_content = BlockContent::create([
+ 'info' => 'Spiffy prototype',
+ 'type' => 'spiffy',
+ ]);
+ $block_content->save();
+
+ // Ensure block entity with no parent is provided as a derivative block
+ // plugin.
+ /** @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
+ $block_manager = $this->container->get('plugin.manager.block');
+ $plugin_id = 'block_content' . PluginBase::DERIVATIVE_SEPARATOR . $block_content->uuid();
+ $this->assertTrue($block_manager->hasDefinition($plugin_id));
+
+ // Set the block not to have a parent.
+ $user = User::create([
+ 'name' => 'username',
+ 'status' => 1,
+ ]);
+ $user->save();
+ $block_content->setParentEntity($user);
+ $block_content->save();
+
+ // Ensure the block content with a parent is not provided a derivative block
+ // plugin.
+ $this->assertFalse($block_manager->hasDefinition($plugin_id));
+ }
+
+}
diff --git a/core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php b/core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php
new file mode 100644
index 000000000000..d401c56ebc7d
--- /dev/null
+++ b/core/modules/block_content/tests/src/Kernel/BlockContentEntityReferenceSelectionTest.php
@@ -0,0 +1,169 @@
+installSchema('system', ['sequence']);
+ $this->installSchema('system', ['sequences']);
+ $this->installEntitySchema('user');
+ $this->installEntitySchema('block_content');
+
+ // Create a block content type.
+ $block_content_type = BlockContentType::create([
+ 'id' => 'spiffy',
+ 'label' => 'Mucho spiffy',
+ 'description' => "Provides a block type that increases your site's spiffiness by up to 11%",
+ ]);
+ $block_content_type->save();
+ $this->entityTypeManager = $this->container->get('entity_type.manager');
+ }
+
+ /**
+ * Tests that blocks with parent are not referenceable entities.
+ *
+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+ * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
+ * @throws \Exception
+ */
+ public function testReferenceableEntities() {
+ $user = User::create([
+ 'name' => 'username',
+ 'status' => 1,
+ ]);
+ $user->save();
+
+ // And block content entities with and without parents.
+ $block_content = BlockContent::create([
+ 'info' => 'Block no parent',
+ 'type' => 'spiffy',
+ ]);
+ $block_content->save();
+ $block_content_with_parent = BlockContent::create([
+ 'info' => 'Block with parent',
+ 'type' => 'spiffy',
+ ]);
+ $block_content_with_parent->setParentEntity($user);
+ $block_content_with_parent->save();
+
+ // Ensure that queries without all the tags are not altered.
+ $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
+ $this->assertCount(2, $query->execute());
+
+ $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
+ $query->addTag('block_content_access');
+ $this->assertCount(2, $query->execute());
+
+ $query = $this->entityTypeManager->getStorage('block_content')->getQuery();
+ $query->addTag('entity_query_block_content');
+ $this->assertCount(2, $query->execute());
+
+ // Use \Drupal\Core\Entity\Plugin\EntityReferenceSelection\DefaultSelection
+ // class to test that getReferenceableEntities() does not get the
+ // entity wth a parent.
+ $configuration = [
+ 'target_type' => 'block_content',
+ 'target_bundles' => ['spiffy' => 'spiffy'],
+ 'sort' => ['field' => '_none'],
+ ];
+ $selection_handler = new TestSelection($configuration, '', '', $this->container->get('entity.manager'), $this->container->get('module_handler'), \Drupal::currentUser());
+ // Setup the 3 expectation cases.
+ $both_blocks = [
+ 'spiffy' => [
+ $block_content->id() => $block_content->label(),
+ $block_content_with_parent->id() => $block_content_with_parent->label(),
+ ],
+ ];
+ $block_no_parent = ['spiffy' => [$block_content->id() => $block_content->label()]];
+ $block_with_parent = ['spiffy' => [$block_content_with_parent->id() => $block_content_with_parent->label()]];
+
+ $this->assertEquals(
+ $block_no_parent,
+ $selection_handler->getReferenceableEntities()
+ );
+
+ // Test various ways in which an EntityReferenceSelection plugin could set
+ // a condition on either the 'parent_entity_id' or 'parent_entity_type'
+ // fields. If the plugin has set a condition on either of these fields
+ // then 'block_content_query_entity_reference_alter()' will not set
+ // a parent condition.
+ foreach (['parent_entity_id', 'parent_entity_type'] as $field) {
+ $selection_handler->setTestMode("{$field}_condition_false");
+ $this->assertEquals(
+ $block_no_parent,
+ $selection_handler->getReferenceableEntities()
+ );
+
+ $selection_handler->setTestMode("{$field}_condition_group_false");
+ $this->assertEquals(
+ $block_no_parent,
+ $selection_handler->getReferenceableEntities()
+ );
+
+ $selection_handler->setTestMode("{$field}_condition_group_true");
+ $this->assertEquals(
+ $block_with_parent,
+ $selection_handler->getReferenceableEntities()
+ );
+
+ $selection_handler->setTestMode("{$field}_condition_nested_group_false");
+ $this->assertEquals(
+ $block_no_parent,
+ $selection_handler->getReferenceableEntities()
+ );
+
+ $selection_handler->setTestMode("{$field}_condition_nested_group_true");
+ $this->assertEquals(
+ $block_with_parent,
+ $selection_handler->getReferenceableEntities()
+ );
+ }
+
+ $block_content_with_parent->removeParentEntity();
+ $block_content_with_parent->save();
+ // Don't use any conditions.
+ $selection_handler->setTestMode(NULL);
+ // Ensure that the block is now returned as a referenceable entity.
+ $this->assertEquals(
+ $both_blocks,
+ $selection_handler->getReferenceableEntities()
+ );
+ }
+
+}
diff --git a/core/modules/comment/src/Tests/CommentCacheTagsTest.php b/core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php
similarity index 96%
rename from core/modules/comment/src/Tests/CommentCacheTagsTest.php
rename to core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php
index 12e0ef8451bc..0b0f4863afe7 100644
--- a/core/modules/comment/src/Tests/CommentCacheTagsTest.php
+++ b/core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php
@@ -1,14 +1,15 @@
assertNull(entity_load('config_test', 'other_module_test_unmet', TRUE), 'The optional configuration config_test.dynamic.other_module_test_unmet whose dependencies are not met is not created.');
$this->assertNull(entity_load('config_test', 'other_module_test_optional_entity_unmet', TRUE), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are not met is not created.');
$this->installModule('config_test_language');
+ $this->assertNull(entity_load('config_test', 'other_module_test_optional_entity_unmet2', TRUE), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet2 whose dependencies are not met is not created.');
$this->installModule('config_install_dependency_test');
$this->assertTrue(entity_load('config_test', 'other_module_test_unmet', TRUE), 'The optional configuration config_test.dynamic.other_module_test_unmet whose dependencies are met is now created.');
- // Although the following configuration entity's are now met it is not
- // installed because it does not have a direct dependency on the
- // config_install_dependency_test module.
- $this->assertNull(entity_load('config_test', 'other_module_test_optional_entity_unmet', TRUE), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are met is not created.');
+ // The following configuration entity's dependencies are now met. It is
+ // indirectly dependent on the config_install_dependency_test module because
+ // it has a dependency on the config_test.dynamic.dependency_for_unmet2
+ // configuration provided by that module and, therefore, should be created.
+ $this->assertTrue(entity_load('config_test', 'other_module_test_optional_entity_unmet2', TRUE), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet2 whose dependencies are met is now created.');
+
+ // The following configuration entity's dependencies are now met even though
+ // it has no direct dependency on the module. It is indirectly dependent on
+ // the config_install_dependency_test module because it has a dependency on
+ // the config_test.dynamic.other_module_test_unmet configuration that is
+ // dependent on the config_install_dependency_test module and, therefore,
+ // should be created.
+ $entity = entity_load('config_test', 'other_module_test_optional_entity_unmet', TRUE);
+ $this->assertTrue($entity, 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are met is created.');
+ $entity->delete();
+
+ // Install another module to ensure the configuration just deleted is not
+ // recreated.
+ $this->installModule('config');
+ $this->assertFalse(entity_load('config_test', 'other_module_test_optional_entity_unmet', TRUE), 'The optional configuration config_test.dynamic.other_module_test_optional_entity_unmet whose dependencies are met is not installed when an unrelated module is installed.');
}
/**
diff --git a/core/modules/content_moderation/src/Entity/Routing/EntityModerationRouteProvider.php b/core/modules/content_moderation/src/Entity/Routing/EntityModerationRouteProvider.php
index ac6af35d3b4a..1349e1b33b92 100644
--- a/core/modules/content_moderation/src/Entity/Routing/EntityModerationRouteProvider.php
+++ b/core/modules/content_moderation/src/Entity/Routing/EntityModerationRouteProvider.php
@@ -81,7 +81,7 @@ protected function getLatestVersionRoute(EntityTypeInterface $entity_type) {
'_title_callback' => '\Drupal\Core\Entity\Controller\EntityController::title',
])
// If the entity type is a node, unpublished content will be visible
- // if the user has the "view all unpublished content" permission.
+ // if the user has the "view any unpublished content" permission.
->setRequirement('_entity_access', "{$entity_type_id}.view")
->setRequirement('_content_moderation_latest_version', 'TRUE')
->setOption('_content_moderation_entity_type', $entity_type_id)
diff --git a/core/modules/content_translation/tests/src/Functional/Update/ContentTranslationUpdateTest.php b/core/modules/content_translation/tests/src/Functional/Update/ContentTranslationUpdateTest.php
index 25be0def2411..34fc579903f2 100644
--- a/core/modules/content_translation/tests/src/Functional/Update/ContentTranslationUpdateTest.php
+++ b/core/modules/content_translation/tests/src/Functional/Update/ContentTranslationUpdateTest.php
@@ -4,7 +4,7 @@
use Drupal\Core\Language\LanguageInterface;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
-use Drupal\system\Tests\Entity\EntityDefinitionTestTrait;
+use Drupal\Tests\system\Functional\Entity\Traits\EntityDefinitionTestTrait;
/**
* Tests the upgrade path for the Content Translation module.
diff --git a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php
index e9cbb574996b..44600369df67 100644
--- a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php
+++ b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php
@@ -7,6 +7,7 @@
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
+use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Lock\LockBackendInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Utility\Token;
@@ -15,7 +16,6 @@
use Drupal\rest\Plugin\ResourceBase;
use Drupal\Component\Render\PlainTextOutput;
use Drupal\Core\Entity\EntityFieldManagerInterface;
-use Drupal\Core\File\FileSystem;
use Drupal\file\Entity\File;
use Drupal\rest\Plugin\rest\resource\EntityResourceValidationTrait;
use Drupal\rest\RequestHandler;
@@ -73,7 +73,7 @@ class FileUploadResource extends ResourceBase {
/**
* The file system service.
*
- * @var \Drupal\Core\File\FileSystem
+ * @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
@@ -137,7 +137,7 @@ class FileUploadResource extends ResourceBase {
* The available serialization formats.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
- * @param \Drupal\Core\File\FileSystem $file_system
+ * @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
@@ -154,7 +154,7 @@ class FileUploadResource extends ResourceBase {
* @param \Drupal\Core\Config\Config $system_file_config
* The system file configuration.
*/
- public function __construct(array $configuration, $plugin_id, $plugin_definition, $serializer_formats, LoggerInterface $logger, FileSystem $file_system, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, AccountInterface $current_user, MimeTypeGuesserInterface $mime_type_guesser, Token $token, LockBackendInterface $lock, Config $system_file_config) {
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, $serializer_formats, LoggerInterface $logger, FileSystemInterface $file_system, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, AccountInterface $current_user, MimeTypeGuesserInterface $mime_type_guesser, Token $token, LockBackendInterface $lock, Config $system_file_config) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $serializer_formats, $logger);
$this->fileSystem = $file_system;
$this->entityTypeManager = $entity_type_manager;
diff --git a/core/modules/image/tests/src/FunctionalJavascript/QuickEditImageEditorTestTrait.php b/core/modules/image/tests/src/FunctionalJavascript/QuickEditImageEditorTestTrait.php
new file mode 100644
index 000000000000..704e4e702014
--- /dev/null
+++ b/core/modules/image/tests/src/FunctionalJavascript/QuickEditImageEditorTestTrait.php
@@ -0,0 +1,76 @@
+assertJsCondition('document.querySelector(".quickedit-image-field-info") !== null', 10000);
+
+ $quickedit_entity_toolbar = $this->getSession()->getPage()->findById('quickedit-entity-toolbar');
+ $this->assertNotNull($quickedit_entity_toolbar->find('css', 'form.quickedit-image-field-info input[name="alt"]'));
+ }
+
+ /**
+ * Simulates typing in the 'image' in-place editor 'alt' attribute text input.
+ *
+ * @param string $text
+ * The text to type.
+ */
+ protected function typeInImageEditorAltTextInput($text) {
+ $quickedit_entity_toolbar = $this->getSession()->getPage()->findById('quickedit-entity-toolbar');
+ $input = $quickedit_entity_toolbar->find('css', 'form.quickedit-image-field-info input[name="alt"]');
+ $input->setValue($text);
+ }
+
+ /**
+ * Simulates dragging and dropping an image on the 'image' in-place editor.
+ *
+ * @param string $file_uri
+ * The URI of the image file to drag and drop.
+ */
+ protected function dropImageOnImageEditor($file_uri) {
+ // Our headless browser can't drag+drop files, but we can mock the event.
+ // Append a hidden upload element to the DOM.
+ $script = 'jQuery("").appendTo("body")';
+ $this->getSession()->executeScript($script);
+
+ // Find the element, and set its value to our new image.
+ $input = $this->assertSession()->elementExists('css', '#quickedit-image-test-input');
+ $filepath = $this->container->get('file_system')->realpath($file_uri);
+ $input->attachFile($filepath);
+
+ // Trigger the upload logic with a mock "drop" event.
+ $script = 'var e = jQuery.Event("drop");'
+ . 'e.originalEvent = {dataTransfer: {files: jQuery("#quickedit-image-test-input").get(0).files}};'
+ . 'e.preventDefault = e.stopPropagation = function () {};'
+ . 'jQuery(".quickedit-image-dropzone").trigger(e);';
+ $this->getSession()->executeScript($script);
+
+ // Wait for the dropzone element to be removed (i.e. loading is done).
+ $js_condition = <<assertJsCondition($js_condition, 20000);
+
+ }
+
+}
diff --git a/core/modules/image/tests/src/FunctionalJavascript/QuickEditImageTest.php b/core/modules/image/tests/src/FunctionalJavascript/QuickEditImageTest.php
index 12e43e75850b..b4444eff2b09 100644
--- a/core/modules/image/tests/src/FunctionalJavascript/QuickEditImageTest.php
+++ b/core/modules/image/tests/src/FunctionalJavascript/QuickEditImageTest.php
@@ -3,24 +3,24 @@
namespace Drupal\Tests\image\FunctionalJavascript;
use Drupal\file\Entity\File;
-use Drupal\FunctionalJavascriptTests\JavascriptTestBase;
use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
+use Drupal\Tests\quickedit\FunctionalJavascript\QuickEditJavascriptTestBase;
use Drupal\Tests\TestFileCreationTrait;
/**
- * Tests the JavaScript functionality of the "image" in-place editor.
- *
+ * @coversDefaultClass \Drupal\image\Plugin\InPlaceEditor\Image
* @group image
*/
-class QuickEditImageTest extends JavascriptTestBase {
+class QuickEditImageTest extends QuickEditJavascriptTestBase {
use ImageFieldCreationTrait;
use TestFileCreationTrait;
+ use QuickEditImageEditorTestTrait;
/**
* {@inheritdoc}
*/
- public static $modules = ['node', 'image', 'field_ui', 'contextual', 'quickedit', 'toolbar'];
+ public static $modules = ['node', 'image', 'field_ui'];
/**
* A user with permissions to edit Articles and use Quick Edit.
@@ -52,9 +52,12 @@ protected function setUp() {
}
/**
- * Tests if an image can be uploaded inline with Quick Edit.
+ * Test that quick editor works correctly with images.
+ *
+ * @covers ::isCompatible
+ * @covers ::getAttachments
*/
- public function testUpload() {
+ public function testImageInPlaceEditor() {
// Create a field with a basic filetype restriction.
$field_name = strtolower($this->randomMachineName());
$field_settings = [
@@ -114,52 +117,82 @@ public function testUpload() {
// Assert that the initial image is present.
$this->assertSession()->elementExists('css', $entity_selector . ' ' . $field_selector . ' ' . $original_image_selector);
- // Wait until Quick Edit loads.
- $condition = "jQuery('" . $entity_selector . " .quickedit').length > 0";
- $this->assertJsCondition($condition, 10000);
-
- // Initiate Quick Editing.
- $this->click('.contextual-toolbar-tab button');
- $this->click($entity_selector . ' [data-contextual-id] > button');
- $this->click($entity_selector . ' [data-contextual-id] .quickedit > a');
- $this->click($field_selector);
+ // Initial state.
+ $this->awaitQuickEditForEntity('node', 1);
+ $this->assertEntityInstanceStates([
+ 'node/1[0]' => 'closed',
+ ]);
+ $this->assertEntityInstanceFieldStates('node', 1, 0, [
+ 'node/1/title/en/full' => 'inactive',
+ 'node/1/uid/en/full' => 'inactive',
+ 'node/1/created/en/full' => 'inactive',
+ 'node/1/body/en/full' => 'inactive',
+ 'node/1/' . $field_name . '/en/full' => 'inactive',
+ ]);
- // Wait for the field info to load and set new alt text.
- $condition = "jQuery('.quickedit-image-field-info').length > 0";
- $this->assertJsCondition($condition, 10000);
- $input = $this->assertSession()->elementExists('css', '.quickedit-image-field-info input[name="alt"]');
- $input->setValue('New text');
+ // Start in-place editing of the article node.
+ $this->startQuickEditViaToolbar('node', 1, 0);
+ $this->assertEntityInstanceStates([
+ 'node/1[0]' => 'opened',
+ ]);
+ $this->assertQuickEditEntityToolbar((string) $node->label(), NULL);
+ $this->assertEntityInstanceFieldStates('node', 1, 0, [
+ 'node/1/title/en/full' => 'candidate',
+ 'node/1/uid/en/full' => 'candidate',
+ 'node/1/created/en/full' => 'candidate',
+ 'node/1/body/en/full' => 'candidate',
+ 'node/1/' . $field_name . '/en/full' => 'candidate',
+ ]);
- // Check that our Dropzone element exists.
+ // Click the image field.
+ $this->click($field_selector);
+ $this->awaitImageEditor();
$this->assertSession()->elementExists('css', $field_selector . ' .quickedit-image-dropzone');
+ $this->assertEntityInstanceFieldStates('node', 1, 0, [
+ 'node/1/title/en/full' => 'candidate',
+ 'node/1/uid/en/full' => 'candidate',
+ 'node/1/created/en/full' => 'candidate',
+ 'node/1/body/en/full' => 'candidate',
+ 'node/1/' . $field_name . '/en/full' => 'active',
+ ]);
- // Our headless browser can't drag+drop files, but we can mock the event.
- // Append a hidden upload element to the DOM.
- $script = 'jQuery("").appendTo("body")';
- $this->getSession()->executeScript($script);
-
- // Find the element, and set its value to our new image.
- $input = $this->assertSession()->elementExists('css', '#quickedit-image-test-input');
- $filepath = $this->container->get('file_system')->realpath($valid_images[1]->uri);
- $input->attachFile($filepath);
-
- // Trigger the upload logic with a mock "drop" event.
- $script = 'var e = jQuery.Event("drop");'
- . 'e.originalEvent = {dataTransfer: {files: jQuery("#quickedit-image-test-input").get(0).files}};'
- . 'e.preventDefault = e.stopPropagation = function () {};'
- . 'jQuery(".quickedit-image-dropzone").trigger(e);';
- $this->getSession()->executeScript($script);
+ // Type new 'alt' text.
+ $this->typeInImageEditorAltTextInput('New text');
+ $this->assertEntityInstanceFieldStates('node', 1, 0, [
+ 'node/1/title/en/full' => 'candidate',
+ 'node/1/uid/en/full' => 'candidate',
+ 'node/1/created/en/full' => 'candidate',
+ 'node/1/body/en/full' => 'candidate',
+ 'node/1/' . $field_name . '/en/full' => 'changed',
+ ]);
- // Wait for the dropzone element to be removed (i.e. loading is done).
- $condition = "jQuery('" . $field_selector . " .quickedit-image-dropzone').length == 0";
- $this->assertJsCondition($condition, 20000);
+ // Drag and drop an image.
+ $this->dropImageOnImageEditor($valid_images[1]->uri);
// To prevent 403s on save, we re-set our request (cookie) state.
$this->prepareRequest();
- // Save the change.
- $this->click('.quickedit-button.action-save');
- $this->assertSession()->assertWaitOnAjaxRequest();
+ // Click 'Save'.
+ $this->saveQuickEdit();
+ $this->assertEntityInstanceStates([
+ 'node/1[0]' => 'committing',
+ ]);
+ $this->assertEntityInstanceFieldStates('node', 1, 0, [
+ 'node/1/title/en/full' => 'candidate',
+ 'node/1/uid/en/full' => 'candidate',
+ 'node/1/created/en/full' => 'candidate',
+ 'node/1/body/en/full' => 'candidate',
+ 'node/1/' . $field_name . '/en/full' => 'saving',
+ ]);
+ $this->assertEntityInstanceFieldMarkup('node', 1, 0, [
+ 'node/1/' . $field_name . '/en/full' => '.quickedit-changed',
+ ]);
+
+ // Wait for the saving of the image field to complete.
+ $this->assertJsCondition("Drupal.quickedit.collections.entities.get('node/1[0]').get('state') === 'closed'");
+ $this->assertEntityInstanceStates([
+ 'node/1[0]' => 'closed',
+ ]);
// Re-visit the page to make sure the edit worked.
$this->drupalGet('node/' . $node->id());
diff --git a/core/modules/layout_builder/config/schema/layout_builder.schema.yml b/core/modules/layout_builder/config/schema/layout_builder.schema.yml
index 682caa78c45e..287215c8e9fa 100644
--- a/core/modules/layout_builder/config/schema/layout_builder.schema.yml
+++ b/core/modules/layout_builder/config/schema/layout_builder.schema.yml
@@ -44,3 +44,20 @@ layout_builder.component:
additional:
type: ignore
label: 'Additional data'
+
+inline_block_contents:
+ type: block_settings
+ label: 'Inline content block'
+ mapping:
+ view_mode:
+ type: string
+ lable: 'View mode'
+ block_revision_id:
+ type: integer
+ label: 'Block revision ID'
+ block_serialized:
+ type: string
+ label: 'Serialized block'
+
+block.settings.inline_block_content:*:
+ type: inline_block_contents
diff --git a/core/modules/layout_builder/layout_builder.module b/core/modules/layout_builder/layout_builder.module
index 2138a045f3f0..dd5af3bbdc3a 100644
--- a/core/modules/layout_builder/layout_builder.module
+++ b/core/modules/layout_builder/layout_builder.module
@@ -5,6 +5,7 @@
* Provides hook implementations for Layout Builder.
*/
+use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
@@ -12,6 +13,7 @@ use Drupal\field\FieldConfigInterface;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplayStorage;
use Drupal\layout_builder\Form\LayoutBuilderEntityViewDisplayForm;
+use Drupal\layout_builder\EntityOperations;
/**
* Implements hook_help().
@@ -81,3 +83,45 @@ function layout_builder_field_config_delete(FieldConfigInterface $field_config)
$sample_entity_generator->delete($field_config->getTargetEntityTypeId(), $field_config->getTargetBundle());
\Drupal::service('plugin.manager.block')->clearCachedDefinitions();
}
+
+/**
+ * Implements hook_entity_presave().
+ */
+function layout_builder_entity_presave(EntityInterface $entity) {
+ /** @var \Drupal\layout_builder\EntityOperations $entity_operations */
+ $entity_operations = \Drupal::service('class_resolver')->getInstanceFromDefinition(EntityOperations::class);
+ $entity_operations->handlePreSave($entity);
+}
+
+/**
+ * Implements hook_entity_delete().
+ */
+function layout_builder_entity_delete(EntityInterface $entity) {
+ /** @var \Drupal\layout_builder\EntityOperations $entity_operations */
+ $entity_operations = \Drupal::service('class_resolver')->getInstanceFromDefinition(EntityOperations::class);
+ $entity_operations->handleEntityDelete($entity);
+}
+
+/**
+ * Implements hook_cron().
+ */
+function layout_builder_cron() {
+ /** @var \Drupal\layout_builder\EntityOperations $entity_operations */
+ $entity_operations = \Drupal::service('class_resolver')->getInstanceFromDefinition(EntityOperations::class);
+ $entity_operations->removeUnused();
+}
+
+/**
+ * Implements hook_plugin_filter_TYPE_alter().
+ */
+function layout_builder_plugin_filter_block_alter(array &$definitions, array $extra, $consumer) {
+ // @todo Determine the 'inline_block_content' blocks should be allowe outside
+ // of layout_builder https://www.drupal.org/node/2979142.
+ if ($consumer !== 'layout_builder') {
+ foreach ($definitions as $id => $definition) {
+ if ($definition['id'] === 'inline_block_content') {
+ unset($definitions[$id]);
+ }
+ }
+ }
+}
diff --git a/core/modules/layout_builder/src/EntityOperations.php b/core/modules/layout_builder/src/EntityOperations.php
new file mode 100644
index 000000000000..70f222ba373c
--- /dev/null
+++ b/core/modules/layout_builder/src/EntityOperations.php
@@ -0,0 +1,446 @@
+entityTypeManager = $entityTypeManager;
+ if ($entityTypeManager->hasDefinition('block_content')) {
+ $this->storage = $entityTypeManager->getStorage('block_content');
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('entity_type.manager')
+ );
+ }
+
+ /**
+ * Remove all unused entities on save.
+ *
+ * Entities that were used in prevision revisions will be removed if not
+ * saving a new revision.
+ *
+ * @param \Drupal\Core\Entity\EntityInterface $entity
+ * The parent entity.
+ *
+ * @throws \Drupal\Core\Entity\EntityStorageException
+ */
+ protected function removeUnusedForEntityOnSave(EntityInterface $entity) {
+ // If the entity is new or '$entity->original' is not set then there will
+ // not be any unused inline blocks to remove.
+ if ($entity->isNew() || !isset($entity->original)) {
+ return;
+ }
+ $sections = $this->getEntitySections($entity);
+ // If this is a layout override and there are no sections then it is a new
+ // override.
+ if ($entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout') && empty($sections)) {
+ return;
+ }
+ // If this a new revision do not remove content_block entities.
+ if ($entity instanceof RevisionableInterface && $entity->isNewRevision()) {
+ return;
+ }
+ $original_sections = $this->getEntitySections($entity->original);
+ $current_revision_ids = $this->getInBlockRevisionIdsInSection($sections);
+ // If there are any revisions in the original that aren't current there may
+ // some blocks that need to be removed.
+ if ($original_revision_ids = array_diff($this->getInBlockRevisionIdsInSection($original_sections), $current_revision_ids)) {
+ if ($removed_ids = array_diff($this->getBlockIdsForRevisionIds($original_revision_ids), $this->getBlockIdsForRevisionIds($current_revision_ids))) {
+ $this->deleteBlocks($removed_ids);
+ }
+ }
+ }
+
+ /**
+ * Handles reacting to a deleting a parent entity.
+ *
+ * @param \Drupal\Core\Entity\EntityInterface $entity
+ * The parent entity.
+ *
+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+ * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
+ * @throws \Drupal\Core\Entity\EntityStorageException
+ */
+ public function handleEntityDelete(EntityInterface $entity) {
+ if ($this->isStorageAvailable() && $this->isLayoutCompatibleEntity($entity)) {
+ if (!$this->isUsingDataTables($entity->getEntityTypeId())) {
+ // If either entity type does not have a data table we need to remove
+ // 'parent_entity_id' so that we are able to find the entities to delete
+ // in ::getUnused(). We can not delete the actuall entites here because
+ // it may take too long in a single request. Also the 'block_content'
+ // entities may also have layout overrides that have their own inline
+ // block entities. Therefore deleting the block entities here could
+ // trigger a cascade delete.
+ $block_storage = $this->entityTypeManager->getStorage('block_content');
+ $query = $block_storage->getQuery();
+ $query->condition('parent_entity_id', $entity->id());
+ $query->condition('parent_entity_type', $entity->getEntityTypeId());
+ $block_ids = $query->execute();
+ foreach ($block_ids as $block_id) {
+ /** @var \Drupal\block_content\BlockContentInterface $block */
+ $block = $block_storage->load($block_id);
+ $block->set('parent_entity_id', NULL);
+ $block->save();
+ }
+ }
+ }
+ }
+
+ /**
+ * Gets the sections for an entity if any.
+ *
+ * @param \Drupal\Core\Entity\EntityInterface $entity
+ * The entity.
+ *
+ * @return \Drupal\layout_builder\Section[]|null
+ * The entity layout sections if available.
+ *
+ * @internal
+ */
+ protected function getEntitySections(EntityInterface $entity) {
+ if ($entity->getEntityTypeId() === 'entity_view_display' && $entity instanceof LayoutBuilderEntityViewDisplay) {
+ return $entity->getSections();
+ }
+ elseif ($entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout')) {
+ return $entity->get('layout_builder__layout')->getSections();
+ }
+ return NULL;
+ }
+
+ /**
+ * Handles saving a parent entity.
+ *
+ * @param \Drupal\Core\Entity\EntityInterface $entity
+ * The parent entity.
+ *
+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+ * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
+ * @throws \Drupal\Core\Entity\EntityStorageException
+ * @throws \Exception
+ */
+ public function handlePreSave(EntityInterface $entity) {
+ if (!$this->isStorageAvailable() || !$this->isLayoutCompatibleEntity($entity)) {
+ return;
+ }
+ $duplicate_blocks = FALSE;
+
+ if ($sections = $this->getEntitySections($entity)) {
+ if ($entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout')) {
+ if (!$entity->isNew() && isset($entity->original)) {
+ /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $original_sections_field */
+ $original_sections_field = $entity->original->get('layout_builder__layout');
+ if ($original_sections_field->isEmpty()) {
+ // @todo Is there a better way to tell if Layout Override is new?
+ // what if is overridden and all sections removed. Currently if you
+ // remove all sections from an override it reverts to the default.
+ // Is that a feature or a bug?
+ $duplicate_blocks = TRUE;
+ }
+ }
+ }
+ $new_revision = FALSE;
+ if ($entity instanceof RevisionableInterface) {
+ // If the parent entity will have a new revision create a new revision
+ // of the block.
+ // @todo Currently revisions are not actually created.
+ // @see https://www.drupal.org/node/2937199
+ // To bypass this always make a revision because the parent entity is
+ // instance of RevisionableInterface. After the issue is fixed only
+ // create a new revision if '$entity->isNewRevision()'.
+ $new_revision = TRUE;
+ }
+
+ foreach ($this->getInlineBlockComponents($sections) as $component) {
+ /** @var \Drupal\layout_builder\Plugin\Block\InlineBlockContentBlock $plugin */
+ $plugin = $component->getPlugin();
+ $plugin->saveBlockContent($entity, $new_revision, $duplicate_blocks);
+ $component->setConfiguration($plugin->getConfiguration());
+ }
+ }
+ $this->removeUnusedForEntityOnSave($entity);
+ }
+
+ /**
+ * Gets components that have Inline Block plugins.
+ *
+ * @param \Drupal\layout_builder\Section[] $sections
+ * The layout sections.
+ *
+ * @return \Drupal\layout_builder\SectionComponent[]
+ * The components that contain Inline Block plugins.
+ */
+ protected function getInlineBlockComponents(array $sections) {
+ $inline_components = [];
+ foreach ($sections as $section) {
+ $components = $section->getComponents();
+
+ foreach ($components as $component) {
+ $plugin = $component->getPlugin();
+ if ($plugin instanceof InlineBlockContentBlock) {
+ $inline_components[] = $component;
+ }
+ }
+ }
+ return $inline_components;
+ }
+
+ /**
+ * Determines if an entity can have a layout.
+ *
+ * @param \Drupal\Core\Entity\EntityInterface $entity
+ * The entity to check.
+ *
+ * @return bool
+ * TRUE if the entity can have a layout otherwise FALSE.
+ */
+ protected function isLayoutCompatibleEntity(EntityInterface $entity) {
+ return ($entity->getEntityTypeId() === 'entity_view_display' && $entity instanceof LayoutBuilderEntityViewDisplay) ||
+ ($entity instanceof FieldableEntityInterface && $entity->hasField('layout_builder__layout'));
+ }
+
+ /**
+ * Gets a block ID for a inline block content plugin.
+ *
+ * @param \Drupal\Component\Plugin\PluginInspectionInterface $plugin
+ * The inline block content plugin.
+ *
+ * @return int
+ * The block content ID or null none available.
+ */
+ protected function getPluginBlockId(PluginInspectionInterface $plugin) {
+ /** @var \Drupal\Component\Plugin\ConfigurablePluginInterface $plugin */
+ $configuration = $plugin->getConfiguration();
+ if (!empty($configuration['block_revision_id'])) {
+ $query = $this->storage->getQuery();
+ $query->condition('revision_id', $configuration['block_revision_id']);
+ return array_values($query->execute())[0];
+ }
+ return NULL;
+ }
+
+ /**
+ * Delete the content blocks and delete the usage records.
+ *
+ * @param int[] $block_content_ids
+ * The block content entity IDs.
+ *
+ * @throws \Drupal\Core\Entity\EntityStorageException
+ */
+ protected function deleteBlocks(array $block_content_ids) {
+ foreach ($block_content_ids as $block_content_id) {
+ if ($block = $this->storage->load($block_content_id)) {
+ $block->delete();
+ }
+ }
+ }
+
+ /**
+ * Removes unused block content entities.
+ *
+ * @param int $limit
+ * The maximum number of block content entities to remove.
+ *
+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+ * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
+ * @throws \Drupal\Core\Entity\EntityStorageException
+ */
+ public function removeUnused($limit = 100) {
+ if ($this->isStorageAvailable()) {
+ $this->deleteBlocks($this->getUnused($limit));
+ }
+ }
+
+ /**
+ * The block_content entity storage is available.
+ *
+ * If the 'block_content' module is not enable this the public methods on this
+ * class should not execute their operations.
+ *
+ * @return bool
+ * Whether the 'block_content' storage is available.
+ */
+ protected function isStorageAvailable() {
+ return !empty($this->storage);
+ }
+
+ /**
+ * Gets revision IDs for layout sections.
+ *
+ * @param \Drupal\layout_builder\Section[] $sections
+ * The layout sections.
+ *
+ * @return int[]
+ * The revision IDs.
+ */
+ protected function getInBlockRevisionIdsInSection(array $sections) {
+ $revision_ids = [];
+ foreach ($this->getInlineBlockComponents($sections) as $component) {
+ $configuration = $component->getPlugin()->getConfiguration();
+ if (!empty($configuration['block_revision_id'])) {
+ $revision_ids[] = $configuration['block_revision_id'];
+ }
+ }
+ return $revision_ids;
+ }
+
+ /**
+ * Gets blocks IDs for an array of revision IDs.
+ *
+ * @param int[] $revision_ids
+ * The revision IDs.
+ *
+ * @return int[]
+ * The block IDs.
+ */
+ protected function getBlockIdsForRevisionIds(array $revision_ids) {
+ if ($revision_ids) {
+ $query = $this->storage->getQuery();
+ $query->condition('revision_id', $revision_ids, 'IN');
+ $block_ids = $query->execute();
+ return $block_ids;
+ }
+ return [];
+
+ }
+
+ /**
+ * Get unused IDs of blocks.
+ *
+ * @param int $limit
+ * The limit of block IDs to return.
+ *
+ * @return int[]
+ * The block IDs.
+ *
+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+ * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
+ */
+ protected function getUnused($limit) {
+ $block_ids = [];
+ foreach ($this->entityTypeManager->getDefinitions() as $definition) {
+ if ($this->entityTypeSupportsLayouts($definition)) {
+ if ($this->isUsingDataTables($definition->id())) {
+ $new_block_ids = $this->getUnusedBlockIdsForEntityWithDataTable($limit - count($block_ids), $definition);
+ }
+ else {
+ // For parent entity types that don't use a datatable we remove
+ // 'parent_entity_id' on entity delete.
+ // @see ::handleEntityDelete()
+ $block_query = $this->entityTypeManager->getStorage('block_content')->getQuery();
+ $block_query->condition('parent_entity_type', $definition->id());
+ $block_query->notExists('parent_entity_id');
+ $block_query->range(0, $limit - count($block_ids));
+ $new_block_ids = $block_query->execute();
+ }
+ $block_ids = array_merge($block_ids, $new_block_ids);
+ if (count($block_ids) >= $limit) {
+ break;
+ }
+ }
+
+ }
+ return $block_ids;
+ }
+
+ /**
+ * Gets the unused block IDs for an entity type using a datatable.
+ *
+ * @param int $limit
+ * The limit of number of block IDs to retrieve.
+ * @param \Drupal\Core\Entity\EntityTypeInterface $parent_type_definition
+ * The parent entity type definition.
+ *
+ * @return int[]
+ * The block IDs.
+ */
+ protected function getUnusedBlockIdsForEntityWithDataTable($limit, EntityTypeInterface $parent_type_definition) {
+ $block_type_definition = $this->entityTypeManager->getDefinition('block_content');
+ $sub_query = Database::getConnection()
+ ->select($parent_type_definition->getDataTable(), 'parent');
+ $parent_id_key = $parent_type_definition->getKey('id');
+ $sub_query->fields('parent', [$parent_id_key]);
+ $sub_query->where("blocks.parent_entity_id = parent.$parent_id_key");
+
+ $query = Database::getConnection()->select($block_type_definition->getDataTable(), 'blocks');
+ $query->fields('blocks', [$block_type_definition->getKey('id')]);
+ $query->isNotNull('parent_entity_id');
+ $query->condition('parent_entity_type', $parent_type_definition->id());
+ $query->notExists($sub_query);
+ $query->range(0, $limit);
+ return $query->execute()->fetchCol();
+ }
+
+ /**
+ * Determines if parent entity and 'block_content' type are using datatables.
+ *
+ * @param string $parent_entity_type_id
+ * The parent entity type.
+ *
+ * @return bool
+ * TRUE if the parent entity and 'block_content' are using datatables.
+ */
+ protected function isUsingDataTables($parent_entity_type_id) {
+ return $this->entityTypeManager->getDefinition($parent_entity_type_id)->getDataTable() && $this->entityTypeManager->getDefinition('block_content')->getDataTable();
+ }
+
+ /**
+ * Checks if the entity type supports layouts.
+ *
+ * @param \Drupal\Core\Entity\EntityTypeInterface $definition
+ * The entity type definition.
+ *
+ * @return bool
+ * TRUE if the entity type supports layouts.
+ */
+ protected function entityTypeSupportsLayouts(EntityTypeInterface $definition) {
+ return $definition->id() === 'entity_view_display' || array_search(FieldableEntityInterface::class, class_implements($definition->getClass())) !== FALSE;
+ }
+
+}
diff --git a/core/modules/layout_builder/src/Form/RevertOverridesForm.php b/core/modules/layout_builder/src/Form/RevertOverridesForm.php
index b6d07d90890c..cf7d91844d17 100644
--- a/core/modules/layout_builder/src/Form/RevertOverridesForm.php
+++ b/core/modules/layout_builder/src/Form/RevertOverridesForm.php
@@ -103,6 +103,9 @@ public function buildForm(array $form, FormStateInterface $form_state, SectionSt
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
+ // @todo Remove this quick fix after https://www.drupal.org/node/2970801
+ $this->sectionStorage = \Drupal::service('plugin.manager.layout_builder.section_storage')->loadFromStorageId($this->sectionStorage->getStorageType(), $this->sectionStorage->getStorageId());
+
// Remove all sections.
while ($this->sectionStorage->count()) {
$this->sectionStorage->removeSection(0);
diff --git a/core/modules/layout_builder/src/Plugin/Block/InlineBlockContentBlock.php b/core/modules/layout_builder/src/Plugin/Block/InlineBlockContentBlock.php
new file mode 100644
index 000000000000..2496eae99470
--- /dev/null
+++ b/core/modules/layout_builder/src/Plugin/Block/InlineBlockContentBlock.php
@@ -0,0 +1,283 @@
+entityTypeManager = $entity_type_manager;
+ $this->entityDisplayRepository = $entity_display_repository;
+ if (!empty($this->configuration['block_revision_id']) || !empty($this->configuration['block_serialized'])) {
+ $this->isNew = FALSE;
+ }
+ }
+
+ /**
+ * {@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'),
+ $container->get('entity_display.repository')
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function defaultConfiguration() {
+ return [
+ 'view_mode' => 'full',
+ 'block_revision_id' => NULL,
+ 'block_serialized' => NULL,
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function blockForm($form, FormStateInterface $form_state) {
+ $block = $this->getEntity();
+
+ // Add the entity form display in a process callback so that #parents can
+ // be successfully propagated to field widgets.
+ $form['block_form'] = [
+ '#type' => 'container',
+ '#process' => [[static::class, 'processBlockForm']],
+ '#block' => $block,
+ ];
+
+ $options = $this->entityDisplayRepository->getViewModeOptionsByBundle('block_content', $block->bundle());
+
+ $form['view_mode'] = [
+ '#type' => 'select',
+ '#options' => $options,
+ '#title' => $this->t('View mode'),
+ '#description' => $this->t('The view mode in which to render the block.'),
+ '#default_value' => $this->configuration['view_mode'],
+ '#access' => count($options) > 1,
+ ];
+ return $form;
+ }
+
+ /**
+ * Process callback to insert a Custom Block form.
+ *
+ * @param array $element
+ * The containing element.
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
+ * The form state.
+ *
+ * @return array
+ * The containing element, with the Custom Block form inserted.
+ */
+ public static function processBlockForm(array $element, FormStateInterface $form_state) {
+ /** @var \Drupal\block_content\BlockContentInterface $block */
+ $block = $element['#block'];
+ EntityFormDisplay::collectRenderDisplay($block, 'edit')->buildForm($block, $element, $form_state);
+ $element['revision_log']['#access'] = FALSE;
+ $element['info']['#access'] = FALSE;
+ return $element;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function blockValidate($form, FormStateInterface $form_state) {
+ $block_form = $form['block_form'];
+ /** @var \Drupal\block_content\BlockContentInterface $block */
+ $block = $block_form['#block'];
+ $form_display = EntityFormDisplay::collectRenderDisplay($block, 'edit');
+ $complete_form_state = $form_state instanceof SubformStateInterface ? $form_state->getCompleteFormState() : $form_state;
+ $form_display->extractFormValues($block, $block_form, $complete_form_state);
+ $form_display->validateFormValues($block, $block_form, $complete_form_state);
+ // @todo Remove when https://www.drupal.org/project/drupal/issues/2948549 is closed.
+ $form_state->setTemporaryValue('block_form_parents', $block_form['#parents']);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function blockSubmit($form, FormStateInterface $form_state) {
+ $this->configuration['view_mode'] = $form_state->getValue('view_mode');
+
+ // @todo Remove when https://www.drupal.org/project/drupal/issues/2948549 is closed.
+ $block_form = NestedArray::getValue($form, $form_state->getTemporaryValue('block_form_parents'));
+ /** @var \Drupal\block_content\BlockContentInterface $block */
+ $block = $block_form['#block'];
+ $form_display = EntityFormDisplay::collectRenderDisplay($block, 'edit');
+ $complete_form_state = ($form_state instanceof SubformStateInterface) ? $form_state->getCompleteFormState() : $form_state;
+ $form_display->extractFormValues($block, $block_form, $complete_form_state);
+ $block->setInfo($this->configuration['label']);
+ $this->configuration['block_serialized'] = serialize($block);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function blockAccess(AccountInterface $account) {
+ if ($this->getEntity()) {
+ return $this->getEntity()->access('view', $account, TRUE);
+ }
+ return AccessResult::forbidden();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function build() {
+ $block = $this->getEntity();
+ return $this->entityTypeManager->getViewBuilder($block->getEntityTypeId())->view($block, $this->configuration['view_mode']);
+ }
+
+ /**
+ * Loads or creates the block content entity of the block.
+ *
+ * @return \Drupal\block_content\BlockContentInterface
+ * The block content entity.
+ *
+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+ * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
+ */
+ protected function getEntity() {
+ if (!isset($this->blockContent)) {
+ if (!empty($this->configuration['block_serialized'])) {
+ $this->blockContent = unserialize($this->configuration['block_serialized']);
+ }
+ elseif (!empty($this->configuration['block_revision_id'])) {
+ $entity = $this->entityTypeManager->getStorage('block_content')->loadRevision($this->configuration['block_revision_id']);
+ $this->blockContent = $entity;
+ }
+ else {
+ $this->blockContent = $this->entityTypeManager->getStorage('block_content')->create([
+ 'type' => $this->getDerivativeId(),
+ ]);
+ }
+ }
+ return $this->blockContent;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
+ $form = parent::buildConfigurationForm($form, $form_state);
+ if ($this->isNew) {
+ // If the Content Block is new then don't provide a default label.
+ // @todo Blocks may be serialized before the layout is saved so we
+ // can't check $this->getEntity()->isNew().
+ unset($form['label']['#default_value']);
+ }
+ $form['label']['#description'] = $this->t('The title of the block as shown to the user.');
+ return $form;
+ }
+
+ /**
+ * Saves the "block_content" entity for this plugin.
+ *
+ * @param \Drupal\Core\Entity\EntityInterface $parent_entity
+ * The parent entity.
+ * @param bool $new_revision
+ * Whether to create new revision.
+ * @param bool $duplicate_block
+ * Whether to duplicate the "block_content" entity.
+ *
+ * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
+ * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
+ * @throws \Drupal\Core\Entity\EntityStorageException
+ */
+ public function saveBlockContent(EntityInterface $parent_entity, $new_revision = FALSE, $duplicate_block = FALSE) {
+ /** @var \Drupal\block_content\BlockContentInterface $block */
+ $block = NULL;
+ if ($duplicate_block && !empty($this->configuration['block_revision_id']) && empty($this->configuration['block_serialized'])) {
+ $entity = $this->entityTypeManager->getStorage('block_content')->loadRevision($this->configuration['block_revision_id']);
+ $block = $entity->createDuplicate();
+ }
+ elseif (isset($this->configuration['block_serialized'])) {
+ $block = unserialize($this->configuration['block_serialized']);
+ if (!empty($this->configuration['block_revision_id']) && $duplicate_block) {
+ $block = $block->createDuplicate();
+ }
+ }
+ if ($block) {
+ $block->setParentEntity($parent_entity);
+ if ($new_revision) {
+ $block->setNewRevision();
+ }
+ $block->save();
+ $this->configuration['block_revision_id'] = $block->getRevisionId();
+ $this->configuration['block_serialized'] = NULL;
+ }
+ }
+
+}
diff --git a/core/modules/layout_builder/src/Plugin/Derivative/InlineBlockContentDeriver.php b/core/modules/layout_builder/src/Plugin/Derivative/InlineBlockContentDeriver.php
new file mode 100644
index 000000000000..07df40e8632f
--- /dev/null
+++ b/core/modules/layout_builder/src/Plugin/Derivative/InlineBlockContentDeriver.php
@@ -0,0 +1,57 @@
+entityTypeManager = $entity_type_manager;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container, $base_plugin_id) {
+ return new static(
+ $container->get('entity_type.manager')
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getDerivativeDefinitions($base_plugin_definition) {
+ $this->derivatives = [];
+ if ($this->entityTypeManager->hasDefinition('block_content_type')) {
+ $block_content_types = $this->entityTypeManager->getStorage('block_content_type')->loadMultiple();
+ foreach ($block_content_types as $id => $type) {
+ $this->derivatives[$id] = $base_plugin_definition;
+ $this->derivatives[$id]['admin_label'] = $type->label();
+ $this->derivatives[$id]['config_dependencies'][$type->getConfigDependencyKey()][] = $type->getConfigDependencyName();
+ }
+ }
+ return parent::getDerivativeDefinitions($base_plugin_definition);
+ }
+
+}
diff --git a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
index 40c0503cea78..4fbf86f56fa8 100644
--- a/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
+++ b/core/modules/layout_builder/tests/src/Functional/LayoutBuilderTest.php
@@ -101,6 +101,7 @@ public function testLayoutBuilderUi() {
// Save the defaults.
$assert_session->linkExists('Save Layout');
$this->clickLink('Save Layout');
+ $assert_session->pageTextContains('The layout has been saved.');
$assert_session->addressEquals("$field_ui_prefix/display/default");
// The node uses the defaults, no overrides available.
diff --git a/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockContentBlockTest.php b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockContentBlockTest.php
new file mode 100644
index 000000000000..6f9077d9c8ef
--- /dev/null
+++ b/core/modules/layout_builder/tests/src/FunctionalJavascript/InlineBlockContentBlockTest.php
@@ -0,0 +1,542 @@
+drupalPlaceBlock('local_tasks_block');
+
+ $this->createContentType(['type' => 'bundle_with_section_field', 'new_revision' => TRUE]);
+ $this->createNode([
+ 'type' => 'bundle_with_section_field',
+ 'title' => 'The node title',
+ 'body' => [
+ [
+ 'value' => 'The node body',
+ ],
+ ],
+ ]);
+ $this->createNode([
+ 'type' => 'bundle_with_section_field',
+ 'title' => 'The node2 title',
+ 'body' => [
+ [
+ 'value' => 'The node2 body',
+ ],
+ ],
+ ]);
+ $bundle = BlockContentType::create([
+ 'id' => 'basic',
+ 'label' => 'Basic block',
+ 'revision' => 1,
+ ]);
+ $bundle->save();
+ block_content_add_body_field($bundle->id());
+
+ $this->blockStorage = $this->container->get('entity_type.manager')->getStorage('block_content');
+ }
+
+ /**
+ * Tests adding and editing of inline blocks.
+ */
+ public function testInlineBlocks() {
+ $assert_session = $this->assertSession();
+ $page = $this->getSession()->getPage();
+
+ $this->drupalLogin($this->drupalCreateUser([
+ 'access contextual links',
+ 'configure any layout',
+ 'administer node display',
+ 'administer node fields',
+ ]));
+
+ $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
+ $this->drupalGet($field_ui_prefix . '/display/default');
+ $this->clickLink('Manage layout');
+ $assert_session->addressEquals("$field_ui_prefix/display-layout/default");
+ // Add a basic block with the body field set.
+ $this->addInlineBlockToLayout('Block title', 'The DEFAULT block body');
+ $this->assertSaveLayout();
+
+ $this->drupalGet('node/1');
+ $assert_session->pageTextContains('The DEFAULT block body');
+ $this->drupalGet('node/2');
+ $assert_session->pageTextContains('The DEFAULT block body');
+
+ // Enable overrides.
+ $this->drupalPostForm("$field_ui_prefix/display/default", ['layout[allow_custom]' => TRUE], 'Save');
+ $this->drupalGet('node/1/layout');
+
+ // Confirm the block can be edited.
+ $this->drupalGet('node/1/layout');
+ $this->configureInlineBlock('The DEFAULT block body', 'The NEW block body!');
+ $this->assertSaveLayout();
+ $this->drupalGet('node/1');
+ $assert_session->pageTextContains('The NEW block body');
+ $assert_session->pageTextNotContains('The DEFAULT block body');
+ $this->drupalGet('node/2');
+ // Node 2 should use default layout.
+ $assert_session->pageTextContains('The DEFAULT block body');
+ $assert_session->pageTextNotContains('The NEW block body');
+
+ // Add a basic block with the body field set.
+ $this->drupalGet('node/1/layout');
+ $this->addInlineBlockToLayout('2nd Block title', 'The 2nd block body');
+ $this->assertSaveLayout();
+ $this->drupalGet('node/1');
+ $assert_session->pageTextContains('The NEW block body!');
+ $assert_session->pageTextContains('The 2nd block body');
+ $this->drupalGet('node/2');
+ // Node 2 should use default layout.
+ $assert_session->pageTextContains('The DEFAULT block body');
+ $assert_session->pageTextNotContains('The NEW block body');
+ $assert_session->pageTextNotContains('The 2nd block body');
+
+ // Confirm the block can be edited.
+ $this->drupalGet('node/1/layout');
+ /* @var \Behat\Mink\Element\NodeElement $inline_block_2 */
+ $inline_block_2 = $page->findAll('css', static::INLINE_BLOCK_LOCATOR)[1];
+ $uuid = $inline_block_2->getAttribute('data-layout-block-uuid');
+ $block_css_locator = static::INLINE_BLOCK_LOCATOR . "[data-layout-block-uuid=\"$uuid\"]";
+ $this->configureInlineBlock('The 2nd block body', 'The 2nd NEW block body!', $block_css_locator);
+ $this->assertSaveLayout();
+ $this->drupalGet('node/1');
+ $assert_session->pageTextContains('The NEW block body!');
+ $assert_session->pageTextContains('The 2nd NEW block body!');
+ $this->drupalGet('node/2');
+ // Node 2 should use default layout.
+ $assert_session->pageTextContains('The DEFAULT block body');
+ $assert_session->pageTextNotContains('The NEW block body!');
+ $assert_session->pageTextNotContains('The 2nd NEW block body!');
+
+ // The default layout entity block should be changed.
+ $this->drupalGet("$field_ui_prefix/display-layout/default");
+ $assert_session->pageTextContains('The DEFAULT block body');
+ // Confirm default layout still only has 1 entity block.
+ $assert_session->elementsCount('css', static::INLINE_BLOCK_LOCATOR, 1);
+ }
+
+ /**
+ * Tests adding a new entity block and then not saving the layout.
+ *
+ * @dataProvider layoutNoSaveProvider
+ */
+ public function testNoLayoutSave($operation, $no_save_link_text, $confirm_button_text) {
+
+ $this->drupalLogin($this->drupalCreateUser([
+ 'access contextual links',
+ 'configure any layout',
+ 'administer node display',
+ ]));
+ $assert_session = $this->assertSession();
+ $page = $this->getSession()->getPage();
+ $this->assertEmpty($this->blockStorage->loadMultiple(), 'No entity blocks exist');
+ $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
+ // Enable overrides.
+ $this->drupalPostForm("$field_ui_prefix/display/default", ['layout[allow_custom]' => TRUE], 'Save');
+
+ $this->drupalGet('node/1/layout');
+ $this->addInlineBlockToLayout('Block title', 'The block body');
+ $this->clickLink($no_save_link_text);
+ if ($confirm_button_text) {
+ $page->pressButton($confirm_button_text);
+ }
+ $this->drupalGet('node/1');
+ $this->assertEmpty($this->blockStorage->loadMultiple(), 'No entity blocks were created when layout is canceled.');
+ $assert_session->pageTextNotContains('The block body');
+
+ $this->drupalGet('node/1/layout');
+
+ $this->addInlineBlockToLayout('Block title', 'The block body');
+ $this->assertSaveLayout();
+ $this->drupalGet('node/1');
+ $assert_session->pageTextContains('The block body');
+ $blocks = $this->blockStorage->loadMultiple();
+ $this->assertEquals(count($blocks), 1);
+ /* @var \Drupal\Core\Entity\ContentEntityBase $block */
+ $block = array_pop($blocks);
+ $revision_id = $block->getRevisionId();
+
+ // Confirm the block can be edited.
+ $this->drupalGet('node/1/layout');
+ $this->configureInlineBlock('The block body', 'The block updated body');
+ $assert_session->pageTextContains('The block updated body');
+
+ $this->clickLink($no_save_link_text);
+ if ($confirm_button_text) {
+ $page->pressButton($confirm_button_text);
+ }
+ $this->drupalGet('node/1');
+
+ $blocks = $this->blockStorage->loadMultiple();
+ // When reverting or canceling the update block should not be on the page.
+ $assert_session->pageTextNotContains('The block updated body');
+ if ($operation === 'cancel') {
+ // When canceling the original block body should appear.
+ $assert_session->pageTextContains('The block body');
+
+ $this->assertEquals(count($blocks), 1);
+ $block = array_pop($blocks);
+ $this->assertEquals($block->getRevisionId(), $revision_id);
+ $this->assertEquals($block->get('body')->getValue()[0]['value'], 'The block body');
+ }
+ else {
+ // The block should not be visible.
+ // Blocks are currently only deleted when the parent entity is deleted.
+ $assert_session->pageTextNotContains('The block body');
+ }
+ }
+
+ /**
+ * Provides test data for ::testNoLayoutSave().
+ */
+ public function layoutNoSaveProvider() {
+ return [
+ 'cancel' => [
+ 'cancel',
+ 'Cancel Layout',
+ NULL,
+ ],
+ 'revert' => [
+ 'revert',
+ 'Revert to defaults',
+ 'Revert',
+ ],
+ ];
+ }
+
+ /**
+ * Saves a layout and asserts the message is correct.
+ *
+ * @throws \Behat\Mink\Exception\ExpectationException
+ * @throws \Behat\Mink\Exception\ResponseTextException
+ */
+ protected function assertSaveLayout() {
+ $assert_session = $this->assertSession();
+ $assert_session->linkExists('Save Layout');
+ $this->clickLink('Save Layout');
+ $this->assertNotEmpty($assert_session->waitForElement('css', '.messages--status'));
+ if (stristr($this->getUrl(), 'admin/structure') === FALSE) {
+ $assert_session->pageTextContains('The layout override has been saved.');
+ }
+ else {
+ $assert_session->pageTextContains('The layout has been saved.');
+ }
+ }
+
+ /**
+ * Tests entity blocks revisioning.
+ */
+ public function testInlineBlocksRevisioning() {
+ $assert_session = $this->assertSession();
+ $page = $this->getSession()->getPage();
+
+ $this->drupalLogin($this->drupalCreateUser([
+ 'access contextual links',
+ 'configure any layout',
+ 'administer node display',
+ 'administer node fields',
+ 'administer nodes',
+ 'bypass node access',
+ ]));
+
+ // Enable override.
+ $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
+ $this->drupalPostForm("$field_ui_prefix/display/default", ['layout[allow_custom]' => TRUE], 'Save');
+ $this->drupalGet('node/1/layout');
+
+ // Add a entity block.
+ $this->addInlineBlockToLayout('Block title', 'The DEFAULT block body');
+ $this->assertSaveLayout();
+ $this->drupalGet('node/1');
+
+ $assert_session->pageTextContains('The DEFAULT block body');
+
+ /** @var \Drupal\node\NodeStorageInterface $node_storage */
+ $node_storage = $this->container->get('entity_type.manager')->getStorage('node');
+ $original_revision_id = $node_storage->getLatestRevisionId(1);
+
+ // Create a new revision.
+ $this->drupalGet('node/1/edit');
+ $page->findField('title[0][value]')->setValue('Node updated');
+ $page->pressButton('Save');
+
+ $this->drupalGet('node/1');
+ $assert_session->pageTextContains('The DEFAULT block body');
+
+ $assert_session->linkExists('Revisions');
+
+ // Update the block.
+ $this->drupalGet('node/1/layout');
+ $this->configureInlineBlock('The DEFAULT block body', 'The NEW block body');
+ $this->assertSaveLayout();
+ $this->drupalGet('node/1');
+ $assert_session->pageTextContains('The NEW block body');
+ $assert_session->pageTextNotContains('The DEFAULT block body');
+
+ $revision_url = "node/1/revisions/$original_revision_id";
+
+ // Ensure viewing the previous revision shows the previous block revision.
+ $this->drupalGet("$revision_url/view");
+ $assert_session->pageTextContains('The DEFAULT block body');
+ $assert_session->pageTextNotContains('The NEW block body');
+
+ // Revert to first revision.
+ $revision_url = "$revision_url/revert";
+ $this->drupalGet($revision_url);
+ $page->pressButton('Revert');
+
+ $this->drupalGet('node/1');
+ $assert_session->pageTextContains('The DEFAULT block body');
+ $assert_session->pageTextNotContains('The NEW block body');
+ }
+
+ /**
+ * Tests that entity blocks deleted correctly.
+ *
+ * @throws \Behat\Mink\Exception\ExpectationException
+ * @throws \Behat\Mink\Exception\ResponseTextException
+ */
+ public function testDeletion() {
+ /** @var \Drupal\Core\Cron $cron */
+ $cron = \Drupal::service('cron');
+ $this->drupalLogin($this->drupalCreateUser([
+ 'administer content types',
+ 'access contextual links',
+ 'configure any layout',
+ 'administer node display',
+ 'administer node fields',
+ 'administer nodes',
+ 'bypass node access',
+ ]));
+ $assert_session = $this->assertSession();
+ $page = $this->getSession()->getPage();
+
+ // Add a block to default layout.
+ $field_ui_prefix = 'admin/structure/types/manage/bundle_with_section_field';
+ $this->drupalGet($field_ui_prefix . '/display/default');
+ $this->clickLink('Manage layout');
+ $assert_session->addressEquals("$field_ui_prefix/display-layout/default");
+ $this->addInlineBlockToLayout('Block title', 'The DEFAULT block body');
+ $this->assertSaveLayout();
+
+ $this->assertCount(1, $this->blockStorage->loadMultiple());
+ $default_block_id = $this->getLatestBlockEntityId();
+
+ // Ensure the block shows up on node pages.
+ $this->drupalGet('node/1');
+ $assert_session->pageTextContains('The DEFAULT block body');
+ $this->drupalGet('node/2');
+ $assert_session->pageTextContains('The DEFAULT block body');
+
+ // Enable overrides.
+ $this->drupalPostForm("$field_ui_prefix/display/default", ['layout[allow_custom]' => TRUE], 'Save');
+
+ // Ensure we have 2 copies of the block in node overrides.
+ $this->drupalGet('node/1/layout');
+ $this->assertSaveLayout();
+ $node_1_block_id = $this->getLatestBlockEntityId();
+
+ $this->drupalGet('node/2/layout');
+ $this->assertSaveLayout();
+ $node_2_block_id = $this->getLatestBlockEntityId();
+ $this->assertCount(3, $this->blockStorage->loadMultiple());
+
+ $this->drupalGet($field_ui_prefix . '/display/default');
+ $this->clickLink('Manage layout');
+ $assert_session->addressEquals("$field_ui_prefix/display-layout/default");
+
+ $this->assertNotEmpty($this->blockStorage->load($default_block_id));
+ // Remove block from default.
+ $this->removeInlineBlockFromLayout();
+ $this->assertSaveLayout();
+ // Ensure the block in the default was deleted.
+ $this->blockStorage->resetCache([$default_block_id]);
+ $this->assertEmpty($this->blockStorage->load($default_block_id));
+ // Ensure other blocks still exist.
+ $this->assertCount(2, $this->blockStorage->loadMultiple());
+
+ $this->drupalGet('node/1/layout');
+ $assert_session->pageTextContains('The DEFAULT block body');
+
+ // Remove block from override.
+ // Currently revisions are not actually created so this check will not pass.
+ // @see https://www.drupal.org/node/2937199
+ // @todo Uncomment this portion when fixed.
+ /*
+ $this->removeInlineBlockFromLayout();
+ $this->assertSaveLayout();
+ $cron->run();
+ // Ensure entity block is not deleted because it is needed in revision.
+ $this->assertNotEmpty($this->blockStorage->load($node_1_block_id));
+ $this->assertCount(2, $this->blockStorage->loadMultiple());
+ */
+
+ // Ensure entity block is deleted when node is deleted.
+ $this->drupalGet('node/1/delete');
+ $page->pressButton('Delete');
+ $this->assertEmpty(Node::load(1));
+ $cron->run();
+ $this->assertEmpty($this->blockStorage->load($node_1_block_id));
+ $this->assertCount(1, $this->blockStorage->loadMultiple());
+
+ // Add another block to the default.
+ $this->drupalGet($field_ui_prefix . '/display/default');
+ $this->clickLink('Manage layout');
+ $assert_session->addressEquals("$field_ui_prefix/display-layout/default");
+ $this->addInlineBlockToLayout('Title 2', 'Body 2');
+ $this->assertSaveLayout();
+ $cron->run();
+ $default_block2_id = $this->getLatestBlockEntityId();
+ $this->assertCount(2, $this->blockStorage->loadMultiple());
+
+ // Delete the other node so bundle can be deleted.
+ $this->drupalGet('node/2/delete');
+ $page->pressButton('Delete');
+ $this->assertEmpty(Node::load(2));
+ $cron->run();
+ // Ensure entity block was deleted.
+ $this->assertEmpty($this->blockStorage->load($node_2_block_id));
+ $this->assertCount(1, $this->blockStorage->loadMultiple());
+
+ // Delete the bundle which has the default layout.
+ $this->drupalGet("$field_ui_prefix/delete");
+ $page->pressButton('Delete');
+ $cron->run();
+
+ // Ensure the entity block in default is deleted when bundle is deleted.
+ $this->assertEmpty($this->blockStorage->load($default_block2_id));
+ $this->assertCount(0, $this->blockStorage->loadMultiple());
+ }
+
+ /**
+ * Gets the latest block entity id.
+ */
+ protected function getLatestBlockEntityId() {
+ $block_ids = \Drupal::entityQuery('block_content')->sort('id', 'DESC')->range(0, 1)->execute();
+ $block_id = array_pop($block_ids);
+ $this->assertNotEmpty($this->blockStorage->load($block_id));
+ return $block_id;
+ }
+
+ /**
+ * Removes an entity block from the layout but does not save the layout.
+ */
+ protected function removeInlineBlockFromLayout() {
+ $assert_session = $this->assertSession();
+ $page = $this->getSession()->getPage();
+ $rendered_block = $page->find('css', static::INLINE_BLOCK_LOCATOR)->getText();
+ $this->assertNotEmpty($rendered_block);
+ $assert_session->pageTextContains($rendered_block);
+ $this->clickContextualLink(static::INLINE_BLOCK_LOCATOR, 'Remove block');
+ $assert_session->assertWaitOnAjaxRequest();
+ $page->find('css', '#drupal-off-canvas')->pressButton('Remove');
+ $assert_session->assertWaitOnAjaxRequest();
+ $assert_session->pageTextNotContains($rendered_block);
+ }
+
+ /**
+ * Adds an entity block to the layout.
+ *
+ * @param string $title
+ * The title field value.
+ * @param string $body
+ * The body field value.
+ *
+ * @throws \Behat\Mink\Exception\ElementNotFoundException
+ * @throws \Behat\Mink\Exception\ExpectationException
+ */
+ protected function addInlineBlockToLayout($title, $body) {
+ $assert_session = $this->assertSession();
+ $page = $this->getSession()->getPage();
+ $page->clickLink('Add Block');
+ $assert_session->assertWaitOnAjaxRequest();
+ $this->assertNotEmpty($assert_session->waitForElementVisible('css', '.block-categories details:contains(Create new block)'));
+ $this->clickLink('Basic block');
+ $assert_session->assertWaitOnAjaxRequest();
+ $assert_session->fieldValueEquals('Title', '');
+ $page->findField('Title')->setValue($title);
+ $textarea = $assert_session->elementExists('css', '[name="settings[block_form][body][0][value]"]');
+ $textarea->setValue($body);
+ $page->pressButton('Add Block');
+ // @todo Replace with 'assertNoElementAfterWait()' after
+ // https://www.drupal.org/project/drupal/issues/2892440.
+ $assert_session->assertWaitOnAjaxRequest();
+ $assert_session->elementNotExists('css', '#drupal-off-canvas');
+ $found_new_text = FALSE;
+ /** @var \Behat\Mink\Element\NodeElement $element */
+ foreach ($page->findAll('css', static::INLINE_BLOCK_LOCATOR) as $element) {
+ if (stristr($element->getText(), $body)) {
+ $found_new_text = TRUE;
+ break;
+ }
+ }
+ $this->assertNotEmpty($found_new_text, 'Found block text on page.');
+ }
+
+ /**
+ * Configures an inline block in the Layout Builder.
+ *
+ * @param string $old_body
+ * The old body field value.
+ * @param string $new_body
+ * The new body field value.
+ * @param string $block_css_locator
+ * The CSS locator to use to select the contextual link.
+ */
+ protected function configureInlineBlock($old_body, $new_body, $block_css_locator = NULL) {
+ $block_css_locator = $block_css_locator ?: static::INLINE_BLOCK_LOCATOR;
+ $assert_session = $this->assertSession();
+ $page = $this->getSession()->getPage();
+ $this->clickContextualLink($block_css_locator, 'Configure');
+ $textarea = $assert_session->waitForElementVisible('css', '[name="settings[block_form][body][0][value]"]');
+ $this->assertNotEmpty($textarea);
+ $this->assertSame($old_body, $textarea->getValue());
+ $textarea->setValue($new_body);
+ $page->pressButton('Update');
+ $assert_session->assertWaitOnAjaxRequest();
+ }
+
+}
diff --git a/core/modules/media/config/install/media.settings.yml b/core/modules/media/config/install/media.settings.yml
index 853e575c6717..5ad89e5e565e 100644
--- a/core/modules/media/config/install/media.settings.yml
+++ b/core/modules/media/config/install/media.settings.yml
@@ -1 +1,3 @@
icon_base_uri: 'public://media-icons/generic'
+iframe_domain: ''
+oembed_providers_url: 'https://oembed.com/providers.json'
diff --git a/core/modules/media/config/schema/media.schema.yml b/core/modules/media/config/schema/media.schema.yml
index b9156b23f86c..f78793b094a4 100644
--- a/core/modules/media/config/schema/media.schema.yml
+++ b/core/modules/media/config/schema/media.schema.yml
@@ -5,6 +5,12 @@ media.settings:
icon_base_uri:
type: string
label: 'Full URI to a folder where the media icons will be installed'
+ iframe_domain:
+ type: uri
+ label: 'Domain from which to serve oEmbed content in an iframe'
+ oembed_providers_url:
+ type: uri
+ label: 'The URL of the oEmbed providers database in JSON format'
media.type.*:
type: config_entity
@@ -40,6 +46,21 @@ field.formatter.settings.media_thumbnail:
type: field.formatter.settings.image
label: 'Media thumbnail field display format settings'
+field.formatter.settings.oembed:
+ type: mapping
+ label: 'oEmbed display format settings'
+ mapping:
+ max_width:
+ type: integer
+ label: 'Maximum width'
+ max_height:
+ type: integer
+ label: 'Maximum height'
+
+field.widget.settings.oembed_textfield:
+ type: field.widget.settings.string_textfield
+ label: 'oEmbed widget format settings'
+
media.source.*:
type: mapping
label: 'Media source settings'
@@ -60,6 +81,20 @@ media.source.video_file:
type: media.source.field_aware
label: '"Video" media source configuration'
+media.source.oembed:*:
+ type: media.source.field_aware
+ label: 'oEmbed media source configuration'
+ mapping:
+ thumbnails_directory:
+ type: uri
+ label: 'URI of thumbnail storage directory'
+ providers:
+ type: sequence
+ label: 'Allowed oEmbed providers'
+ sequence:
+ type: string
+ label: 'Provider name'
+
media.source.field_aware:
type: mapping
mapping:
diff --git a/core/modules/media/media.api.php b/core/modules/media/media.api.php
index 8de1c645754b..93244f58a8a1 100644
--- a/core/modules/media/media.api.php
+++ b/core/modules/media/media.api.php
@@ -20,6 +20,23 @@ function hook_media_source_info_alter(array &$sources) {
$sources['youtube']['label'] = t('Youtube rocks!');
}
+/**
+ * Alters an oEmbed resource URL before it is fetched.
+ *
+ * @param array $parsed_url
+ * A parsed URL, as returned by \Drupal\Component\Utility\UrlHelper::parse().
+ * @param \Drupal\media\OEmbed\Provider $provider
+ * The oEmbed provider for the resource.
+ *
+ * @see \Drupal\media\OEmbed\UrlResolverInterface::getResourceUrl()
+ */
+function hook_oembed_resource_url_alter(array &$parsed_url, \Drupal\media\OEmbed\Provider $provider) {
+ // Always serve YouTube videos from youtube-nocookie.com.
+ if ($provider->getName() === 'YouTube') {
+ $parsed_url['path'] = str_replace('://youtube.com/', '://youtube-nocookie.com/', $parsed_url['path']);
+ }
+}
+
/**
* @} End of "addtogroup hooks".
*/
diff --git a/core/modules/media/media.info.yml b/core/modules/media/media.info.yml
index d7a96484c81c..c5b85f502810 100644
--- a/core/modules/media/media.info.yml
+++ b/core/modules/media/media.info.yml
@@ -8,3 +8,4 @@ dependencies:
- drupal:file
- drupal:image
- drupal:user
+configure: media.settings
diff --git a/core/modules/media/media.install b/core/modules/media/media.install
index 90f9d99cd4da..d3386ea35a77 100644
--- a/core/modules/media/media.install
+++ b/core/modules/media/media.install
@@ -5,6 +5,9 @@
* Install, uninstall and update hooks for Media module.
*/
+use Drupal\Core\Url;
+use Drupal\media\MediaTypeInterface;
+use Drupal\media\Plugin\media\Source\OEmbedInterface;
use Drupal\user\RoleInterface;
use Drupal\user\Entity\Role;
@@ -75,6 +78,36 @@ function media_requirements($phase) {
}
}
}
+ elseif ($phase === 'runtime') {
+ // Check that oEmbed content is served in an iframe on a different domain,
+ // and complain if it isn't.
+ $domain = \Drupal::config('media.settings')->get('iframe_domain');
+
+ if (!\Drupal::service('media.oembed.iframe_url_helper')->isSecure($domain)) {
+ // Find all media types which use a source plugin that implements
+ // OEmbedInterface.
+ $media_types = \Drupal::entityTypeManager()
+ ->getStorage('media_type')
+ ->loadMultiple();
+
+ $oembed_types = array_filter($media_types, function (MediaTypeInterface $media_type) {
+ return $media_type->getSource() instanceof OEmbedInterface;
+ });
+
+ if ($oembed_types) {
+ // @todo Potentially allow site administrators to suppress this warning
+ // permanently. See https://www.drupal.org/project/drupal/issues/2962753
+ // for more information.
+ $requirements['media_insecure_iframe'] = [
+ 'title' => t('Media'),
+ 'description' => t('It is potentially insecure to display oEmbed content in a frame that is served from the same domain as your main Drupal site, as this may allow execution of third-party code. You can specify a different domain for serving oEmbed content here.', [
+ ':url' => Url::fromRoute('media.settings')->setAbsolute()->toString(),
+ ]),
+ 'severity' => REQUIREMENT_WARNING,
+ ];
+ }
+ }
+ }
return $requirements;
}
@@ -120,3 +153,13 @@ function media_update_8500() {
$role->save();
}
}
+
+/**
+ * Updates media.settings to support OEmbed.
+ */
+function media_update_8600() {
+ \Drupal::configFactory()->getEditable('media.settings')
+ ->set('iframe_domain', '')
+ ->set('oembed_providers_url', 'https://oembed.com/providers.json')
+ ->save(TRUE);
+}
diff --git a/core/modules/media/media.links.menu.yml b/core/modules/media/media.links.menu.yml
index e50c41217ba8..1bf5fff37d18 100644
--- a/core/modules/media/media.links.menu.yml
+++ b/core/modules/media/media.links.menu.yml
@@ -3,3 +3,9 @@ entity.media_type.collection:
parent: system.admin_structure
description: 'Manage media types.'
route_name: entity.media_type.collection
+
+media.settings:
+ title: 'Media settings'
+ parent: system.admin_config_media
+ description: 'Manage media settings.'
+ route_name: media.settings
diff --git a/core/modules/media/media.module b/core/modules/media/media.module
index 5079f0fa9a86..20d12b856cc5 100644
--- a/core/modules/media/media.module
+++ b/core/modules/media/media.module
@@ -5,6 +5,7 @@
* Provides media items.
*/
+use Drupal\Component\Plugin\DerivativeInspectionInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
@@ -15,6 +16,7 @@ use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Template\Attribute;
use Drupal\Core\Url;
use Drupal\field\FieldConfigInterface;
+use Drupal\media\Plugin\media\Source\OEmbedInterface;
/**
* Implements hook_help().
@@ -72,6 +74,11 @@ function media_theme() {
'render element' => 'element',
'base hook' => 'field_multiple_value_form',
],
+ 'media_oembed_iframe' => [
+ 'variables' => [
+ 'media' => NULL,
+ ],
+ ],
];
}
@@ -92,6 +99,7 @@ function media_entity_access(EntityInterface $entity, $operation, AccountInterfa
*/
function media_theme_suggestions_media(array $variables) {
$suggestions = [];
+ /** @var \Drupal\media\MediaInterface $media */
$media = $variables['elements']['#media'];
$sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
@@ -99,6 +107,31 @@ function media_theme_suggestions_media(array $variables) {
$suggestions[] = 'media__' . $media->bundle();
$suggestions[] = 'media__' . $media->bundle() . '__' . $sanitized_view_mode;
+ // Add suggestions based on the source plugin ID.
+ $source = $media->getSource();
+ if ($source instanceof DerivativeInspectionInterface) {
+ $source_id = $source->getBaseId();
+ $derivative_id = $source->getDerivativeId();
+ if ($derivative_id) {
+ $source_id .= '__derivative_' . $derivative_id;
+ }
+ }
+ else {
+ $source_id = $source->getPluginId();
+ }
+ $suggestions[] = "media__source_$source_id";
+
+ // If the source plugin uses oEmbed, add a suggestion based on the provider
+ // name, if available.
+ if ($source instanceof OEmbedInterface) {
+ $provider_id = $source->getMetadata($media, 'provider_name');
+ if ($provider_id) {
+ $provider_id = \Drupal::transliteration()->transliterate($provider_id);
+ $provider_id = preg_replace('/[^a-z0-9_]+/', '_', mb_strtolower($provider_id));
+ $suggestions[] = end($suggestions) . "__provider_$provider_id";
+ }
+ }
+
return $suggestions;
}
diff --git a/core/modules/media/media.routing.yml b/core/modules/media/media.routing.yml
index ea0858d46298..a9b634c89a24 100644
--- a/core/modules/media/media.routing.yml
+++ b/core/modules/media/media.routing.yml
@@ -24,3 +24,18 @@ entity.media.revision:
requirements:
_access_media_revision: 'view'
media: \d+
+
+media.oembed_iframe:
+ path: '/media/oembed'
+ defaults:
+ _controller: '\Drupal\media\Controller\OEmbedIframeController::render'
+ requirements:
+ _permission: 'view media'
+
+media.settings:
+ path: '/admin/config/media/media-settings'
+ defaults:
+ _form: '\Drupal\media\Form\MediaSettingsForm'
+ _title: 'Media settings'
+ requirements:
+ _permission: 'administer media'
diff --git a/core/modules/media/media.services.yml b/core/modules/media/media.services.yml
index f22f90a12464..8ff2305a35af 100644
--- a/core/modules/media/media.services.yml
+++ b/core/modules/media/media.services.yml
@@ -2,9 +2,20 @@ services:
plugin.manager.media.source:
class: Drupal\media\MediaSourceManager
parent: default_plugin_manager
-
access_check.media.revision:
class: Drupal\media\Access\MediaRevisionAccessCheck
arguments: ['@entity_type.manager']
tags:
- { name: access_check, applies_to: _access_media_revision }
+ media.oembed.url_resolver:
+ class: Drupal\media\OEmbed\UrlResolver
+ arguments: ['@media.oembed.provider_repository', '@media.oembed.resource_fetcher', '@http_client', '@module_handler', '@cache.default']
+ media.oembed.provider_repository:
+ class: Drupal\media\OEmbed\ProviderRepository
+ arguments: ['@http_client', '@config.factory', '@datetime.time', '@cache.default']
+ media.oembed.resource_fetcher:
+ class: Drupal\media\OEmbed\ResourceFetcher
+ arguments: ['@http_client', '@media.oembed.provider_repository', '@cache.default']
+ media.oembed.iframe_url_helper:
+ class: Drupal\media\IFrameUrlHelper
+ arguments: ['@router.request_context', '@private_key']
diff --git a/core/modules/media/src/Controller/OEmbedIframeController.php b/core/modules/media/src/Controller/OEmbedIframeController.php
new file mode 100644
index 000000000000..0e45d72f0434
--- /dev/null
+++ b/core/modules/media/src/Controller/OEmbedIframeController.php
@@ -0,0 +1,174 @@
+resourceFetcher = $resource_fetcher;
+ $this->urlResolver = $url_resolver;
+ $this->renderer = $renderer;
+ $this->logger = $logger;
+ $this->iFrameUrlHelper = $iframe_url_helper;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('media.oembed.resource_fetcher'),
+ $container->get('media.oembed.url_resolver'),
+ $container->get('renderer'),
+ $container->get('logger.factory')->get('media'),
+ $container->get('media.oembed.iframe_url_helper')
+ );
+ }
+
+ /**
+ * Renders an oEmbed resource.
+ *
+ * @param \Symfony\Component\HttpFoundation\Request $request
+ * The request object.
+ *
+ * @return \Symfony\Component\HttpFoundation\Response
+ * The response object.
+ *
+ * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
+ * Will be thrown if the 'hash' parameter does not match the expected hash
+ * of the 'url' parameter.
+ */
+ public function render(Request $request) {
+ $url = $request->query->get('url');
+ $max_width = $request->query->getInt('max_width', NULL);
+ $max_height = $request->query->getInt('max_height', NULL);
+
+ // Hash the URL and max dimensions, and ensure it is equal to the hash
+ // parameter passed in the query string.
+ $hash = $this->iFrameUrlHelper->getHash($url, $max_width, $max_height);
+ if (!Crypt::hashEquals($hash, $request->query->get('hash', ''))) {
+ throw new AccessDeniedHttpException('This resource is not available');
+ }
+
+ // Return a response instead of a render array so that the frame content
+ // will not have all the blocks and page elements normally rendered by
+ // Drupal.
+ $response = new CacheableResponse();
+ $response->addCacheableDependency(Url::createFromRequest($request));
+
+ try {
+ $resource_url = $this->urlResolver->getResourceUrl($url, $max_width, $max_height);
+ $resource = $this->resourceFetcher->fetchResource($resource_url);
+
+ // Render the content in a new render context so that the cacheability
+ // metadata of the rendered HTML will be captured correctly.
+ $content = $this->renderer->executeInRenderContext(new RenderContext(), function () use ($resource) {
+ $element = [
+ '#theme' => 'media_oembed_iframe',
+ // Even though the resource HTML is untrusted, IFrameMarkup::create()
+ // will create a trusted string. The only reason this is okay is
+ // because we are serving it in an iframe, which will mitigate the
+ // potential dangers of displaying third-party markup.
+ '#media' => IFrameMarkup::create($resource->getHtml()),
+ ];
+ return $this->renderer->render($element);
+ });
+
+ $response->setContent($content)->addCacheableDependency($resource);
+ }
+ catch (ResourceException $e) {
+ // Prevent the response from being cached.
+ $response->setMaxAge(0);
+
+ // The oEmbed system makes heavy use of exception wrapping, so log the
+ // entire exception chain to help with troubleshooting.
+ do {
+ // @todo Log additional information from ResourceException, to help with
+ // debugging, in https://www.drupal.org/project/drupal/issues/2972846.
+ $this->logger->error($e->getMessage());
+ $e = $e->getPrevious();
+ } while ($e);
+ }
+
+ return $response;
+ }
+
+}
diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php
index cca42de3878b..b7b274045f3c 100644
--- a/core/modules/media/src/Entity/Media.php
+++ b/core/modules/media/src/Entity/Media.php
@@ -181,25 +181,7 @@ public function getSource() {
* https://www.drupal.org/node/2878119
*/
protected function updateThumbnail($from_queue = FALSE) {
- $file_storage = \Drupal::service('entity_type.manager')->getStorage('file');
- $thumbnail_uri = $this->getThumbnailUri($from_queue);
- $existing = $file_storage->getQuery()
- ->condition('uri', $thumbnail_uri)
- ->execute();
-
- if ($existing) {
- $this->thumbnail->target_id = reset($existing);
- }
- else {
- /** @var \Drupal\file\FileInterface $file */
- $file = $file_storage->create(['uri' => $thumbnail_uri]);
- if ($owner = $this->getOwner()) {
- $file->setOwner($owner);
- }
- $file->setPermanent();
- $file->save();
- $this->thumbnail->target_id = $file->id();
- }
+ $this->thumbnail->target_id = $this->loadThumbnail($this->getThumbnailUri($from_queue))->id();
// Set the thumbnail alt.
$media_source = $this->getSource();
@@ -222,6 +204,52 @@ protected function updateThumbnail($from_queue = FALSE) {
return $this;
}
+ /**
+ * Loads the file entity for the thumbnail.
+ *
+ * If the file entity does not exist, it will be created.
+ *
+ * @param string $thumbnail_uri
+ * (optional) The URI of the thumbnail, used to load or create the file
+ * entity. If omitted, the default thumbnail URI will be used.
+ *
+ * @return \Drupal\file\FileInterface
+ * The thumbnail file entity.
+ */
+ protected function loadThumbnail($thumbnail_uri = NULL) {
+ $values = [
+ 'uri' => $thumbnail_uri ?: $this->getDefaultThumbnailUri(),
+ ];
+
+ $file_storage = $this->entityTypeManager()->getStorage('file');
+
+ $existing = $file_storage->loadByProperties($values);
+ if ($existing) {
+ $file = reset($existing);
+ }
+ else {
+ /** @var \Drupal\file\FileInterface $file */
+ $file = $file_storage->create($values);
+ if ($owner = $this->getOwner()) {
+ $file->setOwner($owner);
+ }
+ $file->setPermanent();
+ $file->save();
+ }
+ return $file;
+ }
+
+ /**
+ * Returns the URI of the default thumbnail.
+ *
+ * @return string
+ * The default thumbnail URI.
+ */
+ protected function getDefaultThumbnailUri() {
+ $default_thumbnail_filename = $this->getSource()->getPluginDefinition()['default_thumbnail_filename'];
+ return \Drupal::config('media.settings')->get('icon_base_uri') . '/' . $default_thumbnail_filename;
+ }
+
/**
* Updates the queued thumbnail for the media item.
*
@@ -257,17 +285,14 @@ public function updateQueuedThumbnail() {
protected function getThumbnailUri($from_queue) {
$thumbnails_queued = $this->bundle->entity->thumbnailDownloadsAreQueued();
if ($thumbnails_queued && $this->isNew()) {
- $default_thumbnail_filename = $this->getSource()->getPluginDefinition()['default_thumbnail_filename'];
- $thumbnail_uri = \Drupal::service('config.factory')->get('media.settings')->get('icon_base_uri') . '/' . $default_thumbnail_filename;
+ return $this->getDefaultThumbnailUri();
}
elseif ($thumbnails_queued && !$from_queue) {
- $thumbnail_uri = $this->get('thumbnail')->entity->getFileUri();
- }
- else {
- $thumbnail_uri = $this->getSource()->getMetadata($this, $this->getSource()->getPluginDefinition()['thumbnail_uri_metadata_attribute']);
+ return $this->get('thumbnail')->entity->getFileUri();
}
- return $thumbnail_uri;
+ $source = $this->getSource();
+ return $source->getMetadata($this, $source->getPluginDefinition()['thumbnail_uri_metadata_attribute']);
}
/**
@@ -305,30 +330,9 @@ protected function shouldUpdateThumbnail($is_new = FALSE) {
public function preSave(EntityStorageInterface $storage) {
parent::preSave($storage);
- $media_source = $this->getSource();
- foreach ($this->translations as $langcode => $data) {
- if ($this->hasTranslation($langcode)) {
- $translation = $this->getTranslation($langcode);
- // Try to set fields provided by the media source and mapped in
- // media type config.
- foreach ($translation->bundle->entity->getFieldMap() as $metadata_attribute_name => $entity_field_name) {
- // Only save value in entity field if empty. Do not overwrite existing
- // data.
- if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)->isEmpty() || $translation->hasSourceFieldChanged())) {
- $translation->set($entity_field_name, $media_source->getMetadata($translation, $metadata_attribute_name));
- }
- }
-
- // Try to set a default name for this media item if no name is provided.
- if ($translation->get('name')->isEmpty()) {
- $translation->setName($translation->getName());
- }
-
- // Set thumbnail.
- if ($translation->shouldUpdateThumbnail()) {
- $translation->updateThumbnail();
- }
- }
+ // If no thumbnail has been explicitly set, use the default thumbnail.
+ if ($this->get('thumbnail')->isEmpty()) {
+ $this->thumbnail->target_id = $this->loadThumbnail()->id();
}
}
@@ -369,6 +373,55 @@ public function preSaveRevision(EntityStorageInterface $storage, \stdClass $reco
}
}
+ /**
+ * {@inheritdoc}
+ */
+ public function save() {
+ // @todo If the source plugin talks to a remote API (e.g. oEmbed), this code
+ // might be performing a fair number of HTTP requests. This is dangerously
+ // brittle and should probably be handled by a queue, to avoid doing HTTP
+ // operations during entity save. As it is, doing this before calling
+ // parent::save() is a quick-fix to avoid doing HTTP requests in the middle
+ // of a database transaction (which begins once we call parent::save()). See
+ // https://www.drupal.org/project/drupal/issues/2976875 for more.
+
+ // In order for metadata to be mapped correctly, $this->original must be
+ // set. However, that is only set once parent::save() is called, so work
+ // around that by setting it here.
+ if (!isset($this->original) && $id = $this->id()) {
+ $this->original = $this->entityTypeManager()
+ ->getStorage('media')
+ ->loadUnchanged($id);
+ }
+
+ $media_source = $this->getSource();
+ foreach ($this->translations as $langcode => $data) {
+ if ($this->hasTranslation($langcode)) {
+ $translation = $this->getTranslation($langcode);
+ // Try to set fields provided by the media source and mapped in
+ // media type config.
+ foreach ($translation->bundle->entity->getFieldMap() as $metadata_attribute_name => $entity_field_name) {
+ // Only save value in entity field if empty. Do not overwrite existing
+ // data.
+ if ($translation->hasField($entity_field_name) && ($translation->get($entity_field_name)->isEmpty() || $translation->hasSourceFieldChanged())) {
+ $translation->set($entity_field_name, $media_source->getMetadata($translation, $metadata_attribute_name));
+ }
+ }
+
+ // Try to set a default name for this media item if no name is provided.
+ if ($translation->get('name')->isEmpty()) {
+ $translation->setName($translation->getName());
+ }
+
+ // Set thumbnail.
+ if ($translation->shouldUpdateThumbnail($this->isNew())) {
+ $translation->updateThumbnail();
+ }
+ }
+ }
+ return parent::save();
+ }
+
/**
* {@inheritdoc}
*/
diff --git a/core/modules/media/src/Form/MediaSettingsForm.php b/core/modules/media/src/Form/MediaSettingsForm.php
new file mode 100644
index 000000000000..d165fca56360
--- /dev/null
+++ b/core/modules/media/src/Form/MediaSettingsForm.php
@@ -0,0 +1,108 @@
+iFrameUrlHelper = $iframe_url_helper;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('config.factory'),
+ $container->get('media.oembed.iframe_url_helper')
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getFormId() {
+ return 'media_settings_form';
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function getEditableConfigNames() {
+ return ['media.settings'];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildForm(array $form, FormStateInterface $form_state) {
+ $domain = $this->config('media.settings')->get('iframe_domain');
+
+ if (!$this->iFrameUrlHelper->isSecure($domain)) {
+ $message = $this->t('It is potentially insecure to display oEmbed content in a frame that is served from the same domain as your main Drupal site, as this may allow execution of third-party code. Take a look here for more information.');
+ $this->messenger()->addWarning($message);
+ }
+
+ $description = '
' . $this->t('Displaying media assets from third-party services, such as YouTube or Twitter, can be risky. This is because many of these services return arbitrary HTML to represent those assets, and that HTML may contain executable JavaScript code. If handled improperly, this can increase the risk of your site being compromised.') . '
';
+ $description .= '
' . $this->t('In order to mitigate the risks, third-party assets are displayed in an iFrame, which effectively sandboxes any executable code running inside it. For even more security, the iFrame can be served from an alternate domain (that also points to your Drupal site), which you can configure on this page. This helps safeguard cookies and other sensitive information.') . '
';
+
+ $form['security'] = [
+ '#type' => 'details',
+ '#title' => $this->t('Security'),
+ '#description' => $description,
+ '#open' => TRUE,
+ ];
+ // @todo Figure out how and if we should validate that this domain actually
+ // points back to Drupal.
+ // See https://www.drupal.org/project/drupal/issues/2965979 for more info.
+ $form['security']['iframe_domain'] = [
+ '#type' => 'url',
+ '#title' => $this->t('iFrame domain'),
+ '#size' => 40,
+ '#maxlength' => 255,
+ '#default_value' => $domain,
+ '#description' => $this->t('Enter a different domain from which to serve oEmbed content, including the http:// or https:// prefix. This domain needs to point back to this site, or existing oEmbed content may not display correctly, or at all.'),
+ ];
+
+ return parent::buildForm($form, $form_state);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function submitForm(array &$form, FormStateInterface $form_state) {
+ $this->config('media.settings')
+ ->set('iframe_domain', $form_state->getValue('iframe_domain'))
+ ->save();
+
+ parent::submitForm($form, $form_state);
+ }
+
+}
diff --git a/core/modules/media/src/IFrameMarkup.php b/core/modules/media/src/IFrameMarkup.php
new file mode 100644
index 000000000000..b6efbbb12360
--- /dev/null
+++ b/core/modules/media/src/IFrameMarkup.php
@@ -0,0 +1,24 @@
+requestContext = $request_context;
+ $this->privateKey = $private_key;
+ }
+
+ /**
+ * Hashes an oEmbed resource URL.
+ *
+ * @param string $url
+ * The resource URL.
+ * @param int $max_width
+ * (optional) The maximum width of the resource.
+ * @param int $max_height
+ * (optional) The maximum height of the resource.
+ *
+ * @return string
+ * The hashed URL.
+ */
+ public function getHash($url, $max_width = NULL, $max_height = NULL) {
+ return Crypt::hmacBase64("$url:$max_width:$max_height", $this->privateKey->get() . Settings::getHashSalt());
+ }
+
+ /**
+ * Checks if an oEmbed URL can be securely displayed in an frame.
+ *
+ * @param string $url
+ * The URL to check.
+ *
+ * @return bool
+ * TRUE if the URL is considered secure, otherwise FALSE.
+ */
+ public function isSecure($url) {
+ if (!$url) {
+ return FALSE;
+ }
+ $url_host = parse_url($url, PHP_URL_HOST);
+ $system_host = parse_url($this->requestContext->getCompleteBaseUrl(), PHP_URL_HOST);
+
+ // The URL is secure if its domain is not the same as the domain of the base
+ // URL of the current request.
+ return $url_host && $system_host && $url_host !== $system_host;
+ }
+
+}
diff --git a/core/modules/media/src/MediaListBuilder.php b/core/modules/media/src/MediaListBuilder.php
index a0f1f02b21f5..237129bbb02d 100644
--- a/core/modules/media/src/MediaListBuilder.php
+++ b/core/modules/media/src/MediaListBuilder.php
@@ -115,11 +115,11 @@ public function buildRow(EntityInterface $entity) {
/** @var \Drupal\media\MediaInterface $entity */
if ($this->thumbnailStyleExists) {
$row['thumbnail'] = [];
- if ($thumbnail_url = $entity->getSource()->getMetadata($entity, 'thumbnail_uri')) {
+ if ($thumbnail_uri = $entity->getSource()->getMetadata($entity, 'thumbnail_uri')) {
$row['thumbnail']['data'] = [
'#theme' => 'image_style',
'#style_name' => 'thumbnail',
- '#uri' => $thumbnail_url,
+ '#uri' => $thumbnail_uri,
'#height' => 50,
];
}
diff --git a/core/modules/media/src/MediaSourceBase.php b/core/modules/media/src/MediaSourceBase.php
index 1edc8584508e..c01b9946ea0f 100644
--- a/core/modules/media/src/MediaSourceBase.php
+++ b/core/modules/media/src/MediaSourceBase.php
@@ -301,7 +301,9 @@ public function createSourceField(MediaTypeInterface $type) {
* returned. Otherwise, a new, unused one is generated.
*/
protected function getSourceFieldName() {
- $base_id = 'field_media_' . $this->getPluginId();
+ // Some media sources are using a deriver, so their plugin IDs may contain
+ // a separator (usually ':') which is not allowed in field names.
+ $base_id = 'field_media_' . str_replace(static::DERIVATIVE_SEPARATOR, '_', $this->getPluginId());
$tries = 0;
$storage = $this->entityTypeManager->getStorage('field_storage_config');
diff --git a/core/modules/media/src/OEmbed/Endpoint.php b/core/modules/media/src/OEmbed/Endpoint.php
new file mode 100644
index 000000000000..38d265d5bd78
--- /dev/null
+++ b/core/modules/media/src/OEmbed/Endpoint.php
@@ -0,0 +1,176 @@
+provider = $provider;
+ $this->schemes = array_map('mb_strtolower', $schemes);
+
+ $this->formats = $formats = array_map('mb_strtolower', $formats);
+ // Assert that only the supported formats are present.
+ assert(array_diff($formats, ['json', 'xml']) == []);
+
+ // Use the first provided format to build the endpoint URL. If no formats
+ // are provided, default to JSON.
+ $this->url = str_replace('{format}', reset($this->formats) ?: 'json', $url);
+
+ if (!UrlHelper::isValid($this->url, TRUE) || !UrlHelper::isExternal($this->url)) {
+ throw new \InvalidArgumentException('oEmbed endpoint must have a valid external URL');
+ }
+
+ $this->supportsDiscovery = (bool) $supports_discovery;
+ }
+
+ /**
+ * Returns the endpoint URL.
+ *
+ * The URL will be built with the first available format. If the endpoint
+ * does not provide any formats, JSON will be used.
+ *
+ * @return string
+ * The endpoint URL.
+ */
+ public function getUrl() {
+ return $this->url;
+ }
+
+ /**
+ * Returns the provider this endpoint belongs to.
+ *
+ * @return \Drupal\media\OEmbed\Provider
+ * The provider object.
+ */
+ public function getProvider() {
+ return $this->provider;
+ }
+
+ /**
+ * Returns list of URL schemes supported by the provider.
+ *
+ * @return string[]
+ * List of schemes.
+ */
+ public function getSchemes() {
+ return $this->schemes;
+ }
+
+ /**
+ * Returns list of supported formats.
+ *
+ * @return string[]
+ * List of formats.
+ */
+ public function getFormats() {
+ return $this->formats;
+ }
+
+ /**
+ * Returns whether the provider supports oEmbed discovery.
+ *
+ * @return bool
+ * Returns TRUE if the provides discovery, otherwise FALSE.
+ */
+ public function supportsDiscovery() {
+ return $this->supportsDiscovery;
+ }
+
+ /**
+ * Tries to match a URL against the endpoint schemes.
+ *
+ * @param string $url
+ * Media item URL.
+ *
+ * @return bool
+ * TRUE if the URL matches against the endpoint schemes, otherwise FALSE.
+ */
+ public function matchUrl($url) {
+ foreach ($this->getSchemes() as $scheme) {
+ // Convert scheme into a valid regular expression.
+ $regexp = str_replace(['.', '*'], ['\.', '.*'], $scheme);
+ if (preg_match("|^$regexp$|", $url)) {
+ return TRUE;
+ }
+ }
+ return FALSE;
+ }
+
+ /**
+ * Builds and returns the endpoint URL.
+ *
+ * @param string $url
+ * The canonical media URL.
+ *
+ * @return string
+ * URL of the oEmbed endpoint.
+ */
+ public function buildResourceUrl($url) {
+ $query = ['url' => $url];
+ return $this->getUrl() . '?' . UrlHelper::buildQuery($query);
+ }
+
+}
diff --git a/core/modules/media/src/OEmbed/Provider.php b/core/modules/media/src/OEmbed/Provider.php
new file mode 100644
index 000000000000..954dac1c9fd3
--- /dev/null
+++ b/core/modules/media/src/OEmbed/Provider.php
@@ -0,0 +1,100 @@
+name = $name;
+ $this->url = $url;
+
+ try {
+ foreach ($endpoints as $endpoint) {
+ $endpoint += ['formats' => [], 'schemes' => [], 'discovery' => FALSE];
+ $this->endpoints[] = new Endpoint($endpoint['url'], $this, $endpoint['schemes'], $endpoint['formats'], $endpoint['discovery']);
+ }
+ }
+ catch (\InvalidArgumentException $e) {
+ // Just skip all the invalid endpoints.
+ // @todo Log the exception message to help with debugging in
+ // https://www.drupal.org/project/drupal/issues/2972846.
+ }
+
+ if (empty($this->endpoints)) {
+ throw new ProviderException('Provider @name does not define any valid endpoints.', $this);
+ }
+ }
+
+ /**
+ * Returns the provider name.
+ *
+ * @return string
+ * Name of the provider.
+ */
+ public function getName() {
+ return $this->name;
+ }
+
+ /**
+ * Returns the provider URL.
+ *
+ * @return string
+ * URL of the provider.
+ */
+ public function getUrl() {
+ return $this->url;
+ }
+
+ /**
+ * Returns the provider endpoints.
+ *
+ * @return \Drupal\media\OEmbed\Endpoint[]
+ * List of endpoints this provider exposes.
+ */
+ public function getEndpoints() {
+ return $this->endpoints;
+ }
+
+}
diff --git a/core/modules/media/src/OEmbed/ProviderException.php b/core/modules/media/src/OEmbed/ProviderException.php
new file mode 100644
index 000000000000..259c939ed115
--- /dev/null
+++ b/core/modules/media/src/OEmbed/ProviderException.php
@@ -0,0 +1,40 @@
+' if not.
+ * @param \Drupal\media\OEmbed\Provider $provider
+ * (optional) The provider information.
+ * @param \Exception $previous
+ * (optional) The previous exception, if any.
+ */
+ public function __construct($message, Provider $provider = NULL, \Exception $previous = NULL) {
+ $this->provider = $provider;
+ $message = str_replace('@name', $provider ? $provider->getName() : '', $message);
+ parent::__construct($message, 0, $previous);
+ }
+
+}
diff --git a/core/modules/media/src/OEmbed/ProviderRepository.php b/core/modules/media/src/OEmbed/ProviderRepository.php
new file mode 100644
index 000000000000..dada7fb2553e
--- /dev/null
+++ b/core/modules/media/src/OEmbed/ProviderRepository.php
@@ -0,0 +1,122 @@
+httpClient = $http_client;
+ $this->providersUrl = $config_factory->get('media.settings')->get('oembed_providers_url');
+ $this->time = $time;
+ $this->cacheBackend = $cache_backend;
+ $this->maxAge = (int) $max_age;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getAll() {
+ $cache_id = 'media:oembed_providers';
+
+ $cached = $this->cacheGet($cache_id);
+ if ($cached) {
+ return $cached->data;
+ }
+
+ try {
+ $response = $this->httpClient->request('GET', $this->providersUrl);
+ }
+ catch (RequestException $e) {
+ throw new ProviderException("Could not retrieve the oEmbed provider database from $this->providersUrl", NULL, $e);
+ }
+
+ $providers = Json::decode((string) $response->getBody());
+
+ if (!is_array($providers) || empty($providers)) {
+ throw new ProviderException('Remote oEmbed providers database returned invalid or empty list.');
+ }
+
+ $keyed_providers = [];
+ foreach ($providers as $provider) {
+ try {
+ $name = (string) $provider['provider_name'];
+ $keyed_providers[$name] = new Provider($provider['provider_name'], $provider['provider_url'], $provider['endpoints']);
+ }
+ catch (ProviderException $e) {
+ // Just skip all the invalid providers.
+ // @todo Log the exception message to help with debugging.
+ }
+ }
+
+ $this->cacheSet($cache_id, $keyed_providers, $this->time->getCurrentTime() + $this->maxAge);
+ return $keyed_providers;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function get($provider_name) {
+ $providers = $this->getAll();
+
+ if (!isset($providers[$provider_name])) {
+ throw new \InvalidArgumentException("Unknown provider '$provider_name'");
+ }
+ return $providers[$provider_name];
+ }
+
+}
diff --git a/core/modules/media/src/OEmbed/ProviderRepositoryInterface.php b/core/modules/media/src/OEmbed/ProviderRepositoryInterface.php
new file mode 100644
index 000000000000..b6e63afacba8
--- /dev/null
+++ b/core/modules/media/src/OEmbed/ProviderRepositoryInterface.php
@@ -0,0 +1,40 @@
+provider = $provider;
+ $this->title = $title;
+ $this->authorName = $author_name;
+ $this->authorUrl = $author_url;
+
+ if (isset($cache_age) && is_numeric($cache_age)) {
+ // If the cache age is too big, it can overflow the 'expire' column of
+ // database cache backends, causing SQL exceptions. To prevent that,
+ // arbitrarily limit the cache age to 5 years. That should be enough.
+ $this->cacheMaxAge = Cache::mergeMaxAges((int) $cache_age, 157680000);
+ }
+
+ if ($thumbnail_url) {
+ $this->thumbnailUrl = $thumbnail_url;
+ $this->setThumbnailDimensions($thumbnail_width, $thumbnail_height);
+ }
+ }
+
+ /**
+ * Creates a link resource.
+ *
+ * @param string $url
+ * (optional) The URL of the resource.
+ * @param \Drupal\media\OEmbed\Provider $provider
+ * (optional) The resource provider.
+ * @param string $title
+ * (optional) A text title, describing the resource.
+ * @param string $author_name
+ * (optional) The name of the author/owner of the resource.
+ * @param string $author_url
+ * (optional) A URL for the author/owner of the resource.
+ * @param int $cache_age
+ * (optional) The suggested cache lifetime for this resource, in seconds.
+ * @param string $thumbnail_url
+ * (optional) A URL to a thumbnail image representing the resource. If this
+ * parameter is present, $thumbnail_width and $thumbnail_height must also be
+ * present.
+ * @param int $thumbnail_width
+ * (optional) The width of the thumbnail, in pixels. If this parameter is
+ * present, $thumbnail_url and $thumbnail_height must also be present.
+ * @param int $thumbnail_height
+ * (optional) The height of the thumbnail, in pixels. If this parameter is
+ * present, $thumbnail_url and $thumbnail_width must also be present.
+ *
+ * @return static
+ */
+ public static function link($url = NULL, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
+ $resource = new static($provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
+ $resource->type = self::TYPE_LINK;
+ $resource->url = $url;
+
+ return $resource;
+ }
+
+ /**
+ * Creates a photo resource.
+ *
+ * @param string $url
+ * The URL of the photo.
+ * @param int $width
+ * The width of the photo, in pixels.
+ * @param int $height
+ * The height of the photo, in pixels.
+ * @param \Drupal\media\OEmbed\Provider $provider
+ * (optional) The resource provider.
+ * @param string $title
+ * (optional) A text title, describing the resource.
+ * @param string $author_name
+ * (optional) The name of the author/owner of the resource.
+ * @param string $author_url
+ * (optional) A URL for the author/owner of the resource.
+ * @param int $cache_age
+ * (optional) The suggested cache lifetime for this resource, in seconds.
+ * @param string $thumbnail_url
+ * (optional) A URL to a thumbnail image representing the resource. If this
+ * parameter is present, $thumbnail_width and $thumbnail_height must also be
+ * present.
+ * @param int $thumbnail_width
+ * (optional) The width of the thumbnail, in pixels. If this parameter is
+ * present, $thumbnail_url and $thumbnail_height must also be present.
+ * @param int $thumbnail_height
+ * (optional) The height of the thumbnail, in pixels. If this parameter is
+ * present, $thumbnail_url and $thumbnail_width must also be present.
+ *
+ * @return static
+ */
+ public static function photo($url, $width, $height, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
+ if (empty($url)) {
+ throw new \InvalidArgumentException('Photo resources must provide a URL.');
+ }
+
+ $resource = static::link($url, $provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
+ $resource->type = self::TYPE_PHOTO;
+ $resource->setDimensions($width, $height);
+
+ return $resource;
+ }
+
+ /**
+ * Creates a rich resource.
+ *
+ * @param string $html
+ * The HTML representation of the resource.
+ * @param int $width
+ * The width of the resource, in pixels.
+ * @param int $height
+ * The height of the resource, in pixels.
+ * @param \Drupal\media\OEmbed\Provider $provider
+ * (optional) The resource provider.
+ * @param string $title
+ * (optional) A text title, describing the resource.
+ * @param string $author_name
+ * (optional) The name of the author/owner of the resource.
+ * @param string $author_url
+ * (optional) A URL for the author/owner of the resource.
+ * @param int $cache_age
+ * (optional) The suggested cache lifetime for this resource, in seconds.
+ * @param string $thumbnail_url
+ * (optional) A URL to a thumbnail image representing the resource. If this
+ * parameter is present, $thumbnail_width and $thumbnail_height must also be
+ * present.
+ * @param int $thumbnail_width
+ * (optional) The width of the thumbnail, in pixels. If this parameter is
+ * present, $thumbnail_url and $thumbnail_height must also be present.
+ * @param int $thumbnail_height
+ * (optional) The height of the thumbnail, in pixels. If this parameter is
+ * present, $thumbnail_url and $thumbnail_width must also be present.
+ *
+ * @return static
+ */
+ public static function rich($html, $width, $height, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
+ if (empty($html)) {
+ throw new \InvalidArgumentException('The resource must provide an HTML representation.');
+ }
+
+ $resource = new static($provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
+ $resource->type = self::TYPE_RICH;
+ $resource->html = $html;
+ $resource->setDimensions($width, $height);
+
+ return $resource;
+ }
+
+ /**
+ * Creates a video resource.
+ *
+ * @param string $html
+ * The HTML required to display the video.
+ * @param int $width
+ * The width of the video, in pixels.
+ * @param int $height
+ * The height of the video, in pixels.
+ * @param \Drupal\media\OEmbed\Provider $provider
+ * (optional) The resource provider.
+ * @param string $title
+ * (optional) A text title, describing the resource.
+ * @param string $author_name
+ * (optional) The name of the author/owner of the resource.
+ * @param string $author_url
+ * (optional) A URL for the author/owner of the resource.
+ * @param int $cache_age
+ * (optional) The suggested cache lifetime for this resource, in seconds.
+ * @param string $thumbnail_url
+ * (optional) A URL to a thumbnail image representing the resource. If this
+ * parameter is present, $thumbnail_width and $thumbnail_height must also be
+ * present.
+ * @param int $thumbnail_width
+ * (optional) The width of the thumbnail, in pixels. If this parameter is
+ * present, $thumbnail_url and $thumbnail_height must also be present.
+ * @param int $thumbnail_height
+ * (optional) The height of the thumbnail, in pixels. If this parameter is
+ * present, $thumbnail_url and $thumbnail_width must also be present.
+ *
+ * @return static
+ */
+ public static function video($html, $width, $height, Provider $provider = NULL, $title = NULL, $author_name = NULL, $author_url = NULL, $cache_age = NULL, $thumbnail_url = NULL, $thumbnail_width = NULL, $thumbnail_height = NULL) {
+ $resource = static::rich($html, $width, $height, $provider, $title, $author_name, $author_url, $cache_age, $thumbnail_url, $thumbnail_width, $thumbnail_height);
+ $resource->type = self::TYPE_VIDEO;
+
+ return $resource;
+ }
+
+ /**
+ * Returns the resource type.
+ *
+ * @return string
+ * The resource type. Will be one of the self::TYPE_* constants.
+ */
+ public function getType() {
+ return $this->type;
+ }
+
+ /**
+ * Returns the title of the resource.
+ *
+ * @return string|null
+ * The title of the resource, if known.
+ */
+ public function getTitle() {
+ return $this->title;
+ }
+
+ /**
+ * Returns the name of the resource author.
+ *
+ * @return string|null
+ * The name of the resource author, if known.
+ */
+ public function getAuthorName() {
+ return $this->authorName;
+ }
+
+ /**
+ * Returns the URL of the resource author.
+ *
+ * @return \Drupal\Core\Url|null
+ * The absolute URL of the resource author, or NULL if none is provided.
+ */
+ public function getAuthorUrl() {
+ return $this->authorUrl ? Url::fromUri($this->authorUrl)->setAbsolute() : NULL;
+ }
+
+ /**
+ * Returns the resource provider, if known.
+ *
+ * @return \Drupal\media\OEmbed\Provider|null
+ * The resource provider, or NULL if the provider is not known.
+ */
+ public function getProvider() {
+ return $this->provider;
+ }
+
+ /**
+ * Returns the URL of the resource's thumbnail image.
+ *
+ * @return \Drupal\Core\Url|null
+ * The absolute URL of the thumbnail image, or NULL if there isn't one.
+ */
+ public function getThumbnailUrl() {
+ return $this->thumbnailUrl ? Url::fromUri($this->thumbnailUrl)->setAbsolute() : NULL;
+ }
+
+ /**
+ * Returns the width of the resource's thumbnail image.
+ *
+ * @return int|null
+ * The thumbnail width in pixels, or NULL if there is no thumbnail.
+ */
+ public function getThumbnailWidth() {
+ return $this->thumbnailWidth;
+ }
+
+ /**
+ * Returns the height of the resource's thumbnail image.
+ *
+ * @return int|null
+ * The thumbnail height in pixels, or NULL if there is no thumbnail.
+ */
+ public function getThumbnailHeight() {
+ return $this->thumbnailHeight;
+ }
+
+ /**
+ * Returns the width of the resource.
+ *
+ * @return int|null
+ * The width of the resource in pixels, or NULL if the resource has no
+ * dimensions
+ */
+ public function getWidth() {
+ return $this->width;
+ }
+
+ /**
+ * Returns the height of the resource.
+ *
+ * @return int|null
+ * The height of the resource in pixels, or NULL if the resource has no
+ * dimensions.
+ */
+ public function getHeight() {
+ return $this->height;
+ }
+
+ /**
+ * Returns the URL of the resource. Only applies to 'photo' resources.
+ *
+ * @return \Drupal\Core\Url|null
+ * The resource URL, if it has one.
+ */
+ public function getUrl() {
+ if ($this->url) {
+ return Url::fromUri($this->url)->setAbsolute();
+ }
+ return NULL;
+ }
+
+ /**
+ * Returns the HTML representation of the resource.
+ *
+ * Only applies to 'rich' and 'video' resources.
+ *
+ * @return string|null
+ * The HTML representation of the resource, if it has one.
+ */
+ public function getHtml() {
+ return isset($this->html) ? (string) $this->html : NULL;
+ }
+
+ /**
+ * Sets the thumbnail dimensions.
+ *
+ * @param int $width
+ * The width of the resource.
+ * @param int $height
+ * The height of the resource.
+ *
+ * @throws \InvalidArgumentException
+ * If either $width or $height are not numbers greater than zero.
+ */
+ protected function setThumbnailDimensions($width, $height) {
+ $width = (int) $width;
+ $height = (int) $height;
+
+ if ($width > 0 && $height > 0) {
+ $this->thumbnailWidth = $width;
+ $this->thumbnailHeight = $height;
+ }
+ else {
+ throw new \InvalidArgumentException('The thumbnail dimensions must be numbers greater than zero.');
+ }
+ }
+
+ /**
+ * Sets the dimensions.
+ *
+ * @param int $width
+ * The width of the resource.
+ * @param int $height
+ * The height of the resource.
+ *
+ * @throws \InvalidArgumentException
+ * If either $width or $height are not numbers greater than zero.
+ */
+ protected function setDimensions($width, $height) {
+ $width = (int) $width;
+ $height = (int) $height;
+
+ if ($width > 0 && $height > 0) {
+ $this->width = $width;
+ $this->height = $height;
+ }
+ else {
+ throw new \InvalidArgumentException('The dimensions must be numbers greater than zero.');
+ }
+ }
+
+}
diff --git a/core/modules/media/src/OEmbed/ResourceException.php b/core/modules/media/src/OEmbed/ResourceException.php
new file mode 100644
index 000000000000..433867834e54
--- /dev/null
+++ b/core/modules/media/src/OEmbed/ResourceException.php
@@ -0,0 +1,67 @@
+url = $url;
+ $this->data = $data;
+ parent::__construct($message, 0, $previous);
+ }
+
+ /**
+ * Gets the URL of the resource which caused the exception.
+ *
+ * @return string
+ * The URL of the resource.
+ */
+ public function getUrl() {
+ return $this->url;
+ }
+
+ /**
+ * Gets the raw resource data, if available.
+ *
+ * @return array
+ * The resource data.
+ */
+ public function getData() {
+ return $this->data;
+ }
+
+}
diff --git a/core/modules/media/src/OEmbed/ResourceFetcher.php b/core/modules/media/src/OEmbed/ResourceFetcher.php
new file mode 100644
index 000000000000..0c210878feaa
--- /dev/null
+++ b/core/modules/media/src/OEmbed/ResourceFetcher.php
@@ -0,0 +1,197 @@
+httpClient = $http_client;
+ $this->providers = $providers;
+ $this->cacheBackend = $cache_backend;
+ $this->useCaches = isset($cache_backend);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function fetchResource($url) {
+ $cache_id = "media:oembed_resource:$url";
+
+ $cached = $this->cacheGet($cache_id);
+ if ($cached) {
+ return $this->createResource($cached->data, $url);
+ }
+
+ try {
+ $response = $this->httpClient->get($url);
+ }
+ catch (RequestException $e) {
+ throw new ResourceException('Could not retrieve the oEmbed resource.', $url, [], $e);
+ }
+
+ list($format) = $response->getHeader('Content-Type');
+ $content = (string) $response->getBody();
+
+ if (strstr($format, 'text/xml') || strstr($format, 'application/xml')) {
+ $encoder = new XmlEncoder();
+ $data = $encoder->decode($content, 'xml');
+ }
+ elseif (strstr($format, 'text/javascript') || strstr($format, 'application/json')) {
+ $data = Json::decode($content);
+ }
+ // If the response is neither XML nor JSON, we are in bat country.
+ else {
+ throw new ResourceException('The fetched resource did not have a valid Content-Type header.', $url);
+ }
+
+ $this->cacheSet($cache_id, $data);
+
+ return $this->createResource($data, $url);
+ }
+
+ /**
+ * Creates a Resource object from raw resource data.
+ *
+ * @param array $data
+ * The resource data returned by the provider.
+ * @param string $url
+ * The URL of the resource.
+ *
+ * @return \Drupal\media\OEmbed\Resource
+ * A value object representing the resource.
+ *
+ * @throws \Drupal\media\OEmbed\ResourceException
+ * If the resource cannot be created.
+ */
+ protected function createResource(array $data, $url) {
+ $data += [
+ 'title' => NULL,
+ 'author_name' => NULL,
+ 'author_url' => NULL,
+ 'provider_name' => NULL,
+ 'cache_age' => NULL,
+ 'thumbnail_url' => NULL,
+ 'thumbnail_width' => NULL,
+ 'thumbnail_height' => NULL,
+ 'width' => NULL,
+ 'height' => NULL,
+ 'url' => NULL,
+ 'html' => NULL,
+ 'version' => NULL,
+ ];
+
+ if ($data['version'] !== '1.0') {
+ throw new ResourceException("Resource version must be '1.0'", $url, $data);
+ }
+
+ // Prepare the arguments to pass to the factory method.
+ $provider = $data['provider_name'] ? $this->providers->get($data['provider_name']) : NULL;
+
+ // The Resource object will validate the data we create it with and throw an
+ // exception if anything looks wrong. For better debugging, catch those
+ // exceptions and wrap them in a more specific and useful exception.
+ try {
+ switch ($data['type']) {
+ case Resource::TYPE_LINK:
+ return Resource::link(
+ $data['url'],
+ $provider,
+ $data['title'],
+ $data['author_name'],
+ $data['author_url'],
+ $data['cache_age'],
+ $data['thumbnail_url'],
+ $data['thumbnail_width'],
+ $data['thumbnail_height']
+ );
+
+ case Resource::TYPE_PHOTO:
+ return Resource::photo(
+ $data['url'],
+ $data['width'],
+ $data['height'],
+ $provider,
+ $data['title'],
+ $data['author_name'],
+ $data['author_url'],
+ $data['cache_age'],
+ $data['thumbnail_url'],
+ $data['thumbnail_width'],
+ $data['thumbnail_height']
+ );
+
+ case Resource::TYPE_RICH:
+ return Resource::rich(
+ $data['html'],
+ $data['width'],
+ $data['height'],
+ $provider,
+ $data['title'],
+ $data['author_name'],
+ $data['author_url'],
+ $data['cache_age'],
+ $data['thumbnail_url'],
+ $data['thumbnail_width'],
+ $data['thumbnail_height']
+ );
+ case Resource::TYPE_VIDEO:
+ return Resource::video(
+ $data['html'],
+ $data['width'],
+ $data['height'],
+ $provider,
+ $data['title'],
+ $data['author_name'],
+ $data['author_url'],
+ $data['cache_age'],
+ $data['thumbnail_url'],
+ $data['thumbnail_width'],
+ $data['thumbnail_height']
+ );
+
+ default:
+ throw new ResourceException('Unknown resource type: ' . $data['type'], $url, $data);
+ }
+ }
+ catch (\InvalidArgumentException $e) {
+ throw new ResourceException($e->getMessage(), $url, $data, $e);
+ }
+ }
+
+}
diff --git a/core/modules/media/src/OEmbed/ResourceFetcherInterface.php b/core/modules/media/src/OEmbed/ResourceFetcherInterface.php
new file mode 100644
index 000000000000..b74fb6e2d9c4
--- /dev/null
+++ b/core/modules/media/src/OEmbed/ResourceFetcherInterface.php
@@ -0,0 +1,32 @@
+providers = $providers;
+ $this->resourceFetcher = $resource_fetcher;
+ $this->httpClient = $http_client;
+ $this->moduleHandler = $module_handler;
+ $this->cacheBackend = $cache_backend;
+ $this->useCaches = isset($cache_backend);
+ }
+
+ /**
+ * Runs oEmbed discovery and returns the endpoint URL if successful.
+ *
+ * @param string $url
+ * The resource's URL.
+ *
+ * @return string|bool
+ * URL of the oEmbed endpoint, or FALSE if the discovery was unsuccessful.
+ *
+ * @throws \Drupal\media\OEmbed\ResourceException
+ * If the resource cannot be retrieved.
+ */
+ protected function discoverResourceUrl($url) {
+ try {
+ $response = $this->httpClient->get($url);
+ }
+ catch (RequestException $e) {
+ throw new ResourceException('Could not fetch oEmbed resource.', $url, [], $e);
+ }
+
+ $document = Html::load((string) $response->getBody());
+ $xpath = new \DOMXpath($document);
+
+ return $this->findUrl($xpath, 'json') ?: $this->findUrl($xpath, 'xml');
+ }
+
+ /**
+ * Tries to find the oEmbed URL in a DOM.
+ *
+ * @param \DOMXPath $xpath
+ * Page HTML as DOMXPath.
+ * @param string $format
+ * Format of oEmbed resource. Possible values are 'json' and 'xml'.
+ *
+ * @return bool|string
+ * A URL to an oEmbed resource or FALSE if not found.
+ */
+ protected function findUrl(\DOMXPath $xpath, $format) {
+ $result = $xpath->query("//link[@type='application/$format+oembed']");
+ return $result->length ? $result->item(0)->getAttribute('href') : FALSE;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getProviderByUrl($url) {
+ // Check the URL against every scheme of every endpoint of every provider
+ // until we find a match.
+ foreach ($this->providers->getAll() as $provider_name => $provider_info) {
+ foreach ($provider_info->getEndpoints() as $endpoint) {
+ if ($endpoint->matchUrl($url)) {
+ return $provider_info;
+ }
+ }
+ }
+
+ $resource_url = $this->discoverResourceUrl($url);
+ if ($resource_url) {
+ return $this->resourceFetcher->fetchResource($resource_url)->getProvider();
+ }
+
+ throw new ResourceException('No matching provider found.', $url);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getResourceUrl($url, $max_width = NULL, $max_height = NULL) {
+ // Try to get the resource URL from the static cache.
+ if (isset($this->urlCache[$url])) {
+ return $this->urlCache[$url];
+ }
+
+ // Try to get the resource URL from the persistent cache.
+ $cache_id = "media:oembed_resource_url:$url:$max_width:$max_height";
+
+ $cached = $this->cacheGet($cache_id);
+ if ($cached) {
+ $this->urlCache[$url] = $cached->data;
+ return $this->urlCache[$url];
+ }
+
+ $provider = $this->getProviderByUrl($url);
+ $endpoints = $provider->getEndpoints();
+ $endpoint = reset($endpoints);
+ $resource_url = $endpoint->buildResourceUrl($url);
+
+ $parsed_url = UrlHelper::parse($resource_url);
+ if ($max_width) {
+ $parsed_url['query']['maxwidth'] = $max_width;
+ }
+ if ($max_height) {
+ $parsed_url['query']['maxheight'] = $max_height;
+ }
+ // Let other modules alter the resource URL, because some oEmbed providers
+ // provide extra parameters in the query string. For example, Instagram also
+ // supports the 'omitscript' parameter.
+ $this->moduleHandler->alter('oembed_resource_url', $parsed_url, $provider);
+ $resource_url = $parsed_url['path'] . '?' . UrlHelper::buildQuery($parsed_url['query']);
+
+ $this->urlCache[$url] = $resource_url;
+ $this->cacheSet($cache_id, $resource_url);
+
+ return $resource_url;
+ }
+
+}
diff --git a/core/modules/media/src/OEmbed/UrlResolverInterface.php b/core/modules/media/src/OEmbed/UrlResolverInterface.php
new file mode 100644
index 000000000000..b401f6201a52
--- /dev/null
+++ b/core/modules/media/src/OEmbed/UrlResolverInterface.php
@@ -0,0 +1,45 @@
+messenger = $messenger;
+ $this->resourceFetcher = $resource_fetcher;
+ $this->urlResolver = $url_resolver;
+ $this->logger = $logger_factory->get('media');
+ $this->config = $config_factory->get('media.settings');
+ $this->iFrameUrlHelper = $iframe_url_helper;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
+ return new static(
+ $plugin_id,
+ $plugin_definition,
+ $configuration['field_definition'],
+ $configuration['settings'],
+ $configuration['label'],
+ $configuration['view_mode'],
+ $configuration['third_party_settings'],
+ $container->get('messenger'),
+ $container->get('media.oembed.resource_fetcher'),
+ $container->get('media.oembed.url_resolver'),
+ $container->get('logger.factory'),
+ $container->get('config.factory'),
+ $container->get('media.oembed.iframe_url_helper')
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function defaultSettings() {
+ return [
+ 'max_width' => 0,
+ 'max_height' => 0,
+ ] + parent::defaultSettings();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function viewElements(FieldItemListInterface $items, $langcode) {
+ $element = [];
+ $max_width = $this->getSetting('max_width');
+ $max_height = $this->getSetting('max_height');
+
+ foreach ($items as $delta => $item) {
+ $main_property = $item->getFieldDefinition()->getFieldStorageDefinition()->getMainPropertyName();
+ $value = $item->{$main_property};
+
+ if (empty($value)) {
+ continue;
+ }
+
+ try {
+ $resource_url = $this->urlResolver->getResourceUrl($value, $max_width, $max_height);
+ $resource = $this->resourceFetcher->fetchResource($resource_url);
+ }
+ catch (ResourceException $exception) {
+ $this->logger->error("Could not retrieve the remote URL (@url).", ['@url' => $value]);
+ continue;
+ }
+
+ if ($resource->getType() === Resource::TYPE_LINK) {
+ $element[$delta] = [
+ '#title' => $resource->getTitle(),
+ '#type' => 'link',
+ '#url' => Url::fromUri($value),
+ ];
+ }
+ elseif ($resource->getType() === Resource::TYPE_PHOTO) {
+ $element[$delta] = [
+ '#theme' => 'image',
+ '#uri' => $resource->getUrl()->toString(),
+ '#width' => $max_width ?: $resource->getWidth(),
+ '#height' => $max_height ?: $resource->getHeight(),
+ ];
+ }
+ else {
+ $url = Url::fromRoute('media.oembed_iframe', [], [
+ 'query' => [
+ 'url' => $value,
+ 'max_width' => $max_width,
+ 'max_height' => $max_height,
+ 'hash' => $this->iFrameUrlHelper->getHash($value, $max_width, $max_height),
+ ],
+ ]);
+
+ $domain = $this->config->get('iframe_domain');
+ if ($domain) {
+ $url->setOption('base_url', $domain);
+ }
+
+ // Render videos and rich content in an iframe for security reasons.
+ // @see: https://oembed.com/#section3
+ $element[$delta] = [
+ '#type' => 'html_tag',
+ '#tag' => 'iframe',
+ '#attributes' => [
+ 'src' => $url->toString(),
+ 'frameborder' => 0,
+ 'scrolling' => FALSE,
+ 'allowtransparency' => TRUE,
+ 'width' => $max_width ?: $resource->getWidth(),
+ 'height' => $max_height ?: $resource->getHeight(),
+ ],
+ ];
+
+ CacheableMetadata::createFromObject($resource)
+ ->addCacheTags($this->config->getCacheTags())
+ ->applyTo($element[$delta]);
+ }
+ }
+ return $element;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsForm(array $form, FormStateInterface $form_state) {
+ return parent::settingsForm($form, $form_state) + [
+ 'max_width' => [
+ '#type' => 'number',
+ '#title' => $this->t('Maximum width'),
+ '#default_value' => $this->getSetting('max_width'),
+ '#size' => 5,
+ '#maxlength' => 5,
+ '#field_suffix' => $this->t('pixels'),
+ '#min' => 0,
+ ],
+ 'max_height' => [
+ '#type' => 'number',
+ '#title' => $this->t('Maximum height'),
+ '#default_value' => $this->getSetting('max_height'),
+ '#size' => 5,
+ '#maxlength' => 5,
+ '#field_suffix' => $this->t('pixels'),
+ '#min' => 0,
+ ],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function settingsSummary() {
+ $summary = parent::settingsSummary();
+ if ($this->getSetting('max_width') && $this->getSetting('max_height')) {
+ $summary[] = $this->t('Maximum size: %max_width x %max_height pixels', [
+ '%max_width' => $this->getSetting('max_width'),
+ '%max_height' => $this->getSetting('max_height'),
+ ]);
+ }
+ elseif ($this->getSetting('max_width')) {
+ $summary[] = $this->t('Maximum width: %max_width pixels', [
+ '%max_width' => $this->getSetting('max_width'),
+ ]);
+ }
+ elseif ($this->getSetting('max_height')) {
+ $summary[] = $this->t('Maximum height: %max_height pixels', [
+ '%max_height' => $this->getSetting('max_height'),
+ ]);
+ }
+ return $summary;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function isApplicable(FieldDefinitionInterface $field_definition) {
+ if ($field_definition->getTargetEntityTypeId() !== 'media') {
+ return FALSE;
+ }
+
+ if (parent::isApplicable($field_definition)) {
+ $media_type = $field_definition->getTargetBundle();
+
+ if ($media_type) {
+ $media_type = MediaType::load($media_type);
+ return $media_type && $media_type->getSource() instanceof OEmbedInterface;
+ }
+ }
+ return FALSE;
+ }
+
+}
diff --git a/core/modules/media/src/Plugin/Field/FieldWidget/OEmbedWidget.php b/core/modules/media/src/Plugin/Field/FieldWidget/OEmbedWidget.php
new file mode 100644
index 000000000000..1252265e45b8
--- /dev/null
+++ b/core/modules/media/src/Plugin/Field/FieldWidget/OEmbedWidget.php
@@ -0,0 +1,64 @@
+getEntity()->getSource();
+ $message = $this->t('You can link to media from the following services: @providers', ['@providers' => implode(', ', $source->getProviders())]);
+
+ if (!empty($element['#value']['#description'])) {
+ $element['value']['#description'] = [
+ '#theme' => 'item_list',
+ '#items' => [$element['value']['#description'], $message],
+ ];
+ }
+ else {
+ $element['value']['#description'] = $message;
+ }
+
+ return $element;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function isApplicable(FieldDefinitionInterface $field_definition) {
+ $target_bundle = $field_definition->getTargetBundle();
+
+ if (!parent::isApplicable($field_definition) || $field_definition->getTargetEntityTypeId() !== 'media' || !$target_bundle) {
+ return FALSE;
+ }
+ return MediaType::load($target_bundle)->getSource() instanceof OEmbedInterface;
+ }
+
+}
diff --git a/core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraint.php b/core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraint.php
new file mode 100644
index 000000000000..306353c02d23
--- /dev/null
+++ b/core/modules/media/src/Plugin/Validation/Constraint/OEmbedResourceConstraint.php
@@ -0,0 +1,50 @@
+urlResolver = $url_resolver;
+ $this->resourceFetcher = $resource_fetcher;
+ $this->logger = $logger_factory->get('media');
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('media.oembed.url_resolver'),
+ $container->get('media.oembed.resource_fetcher'),
+ $container->get('logger.factory')
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function validate($value, Constraint $constraint) {
+ /** @var \Drupal\media\MediaInterface $media */
+ $media = $value->getEntity();
+ /** @var \Drupal\media\Plugin\media\Source\OEmbedInterface $source */
+ $source = $media->getSource();
+
+ if (!($source instanceof OEmbedInterface)) {
+ throw new \LogicException('Media source must implement ' . OEmbedInterface::class);
+ }
+ $url = $source->getSourceFieldValue($media);
+
+ // Ensure that the URL matches a provider.
+ try {
+ $provider = $this->urlResolver->getProviderByUrl($url);
+ }
+ catch (ResourceException $e) {
+ $this->handleException($e, $constraint->unknownProviderMessage);
+ return;
+ }
+ catch (ProviderException $e) {
+ $this->handleException($e, $constraint->providerErrorMessage);
+ return;
+ }
+
+ // Ensure that the provider is allowed.
+ if (!in_array($provider->getName(), $source->getProviders(), TRUE)) {
+ $this->context->addViolation($constraint->disallowedProviderMessage, [
+ '@name' => $provider->getName(),
+ ]);
+ return;
+ }
+
+ // Verify that resource fetching works, because some URLs might match
+ // the schemes but don't support oEmbed.
+ try {
+ $endpoints = $provider->getEndpoints();
+ $resource_url = reset($endpoints)->buildResourceUrl($url);
+ $this->resourceFetcher->fetchResource($resource_url);
+ }
+ catch (ResourceException $e) {
+ $this->handleException($e, $constraint->invalidResourceMessage);
+ }
+ }
+
+ /**
+ * Handles exceptions that occur during validation.
+ *
+ * @param \Exception $e
+ * The caught exception.
+ * @param string $error_message
+ * (optional) The error message to set as a constraint violation.
+ */
+ protected function handleException(\Exception $e, $error_message = NULL) {
+ if ($error_message) {
+ $this->context->addViolation($error_message);
+ }
+
+ // The oEmbed system makes heavy use of exception wrapping, so log the
+ // entire exception chain to help with troubleshooting.
+ do {
+ // @todo If $e is a ProviderException or ResourceException, log additional
+ // debugging information contained in those exceptions in
+ // https://www.drupal.org/project/drupal/issues/2972846.
+ $this->logger->error($e->getMessage());
+ $e = $e->getPrevious();
+ } while ($e);
+ }
+
+}
diff --git a/core/modules/media/src/Plugin/media/Source/Image.php b/core/modules/media/src/Plugin/media/Source/Image.php
index 46f6782c742d..a83a5144d24b 100644
--- a/core/modules/media/src/Plugin/media/Source/Image.php
+++ b/core/modules/media/src/Plugin/media/Source/Image.php
@@ -6,7 +6,7 @@
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
-use Drupal\Core\File\FileSystem;
+use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Image\ImageFactory;
use Drupal\media\MediaInterface;
use Drupal\media\MediaTypeInterface;
@@ -51,7 +51,7 @@ class Image extends File {
/**
* The file system service.
*
- * @var \Drupal\Core\File\FileSystem
+ * @var \Drupal\Core\File\FileSystemInterface
*/
protected $fileSystem;
@@ -74,10 +74,10 @@ class Image extends File {
* The config factory service.
* @param \Drupal\Core\Image\ImageFactory $image_factory
* The image factory.
- * @param \Drupal\Core\File\FileSystem $file_system
+ * @param \Drupal\Core\File\FileSystemInterface $file_system
* The file system service.
*/
- public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, ImageFactory $image_factory, FileSystem $file_system) {
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, ConfigFactoryInterface $config_factory, ImageFactory $image_factory, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
$this->imageFactory = $image_factory;
diff --git a/core/modules/media/src/Plugin/media/Source/OEmbed.php b/core/modules/media/src/Plugin/media/Source/OEmbed.php
new file mode 100644
index 000000000000..a376366ce1ab
--- /dev/null
+++ b/core/modules/media/src/Plugin/media/Source/OEmbed.php
@@ -0,0 +1,466 @@
+ 'artwork',
+ * 'label' => t('Artwork'),
+ * 'description' => t('Use artwork from Flickr and DeviantArt.'),
+ * 'allowed_field_types' => ['string'],
+ * 'default_thumbnail_filename' => 'no-thumbnail.png',
+ * 'providers' => ['Deviantart.com', 'Flickr'],
+ * 'class' => 'Drupal\media\Plugin\media\Source\OEmbed',
+ * ];
+ * }
+ * @endcode
+ * The "Deviantart.com" and "Flickr" provider names are specified in
+ * https://oembed.com/providers.json. The
+ * \Drupal\media\Plugin\media\Source\OEmbed class already knows how to handle
+ * standard interactions with third-party oEmbed APIs, so there is no need to
+ * define a new class which extends it. With the code above, you will able to
+ * create media types which use the "Artwork" source plugin, and use those media
+ * types to link to assets on Deviantart and Flickr.
+ *
+ * @MediaSource(
+ * id = "oembed",
+ * label = @Translation("oEmbed source"),
+ * description = @Translation("Use oEmbed URL for reusable media."),
+ * allowed_field_types = {"string"},
+ * default_thumbnail_filename = "no-thumbnail.png",
+ * deriver = "Drupal\media\Plugin\media\Source\OEmbedDeriver",
+ * providers = {},
+ * )
+ */
+class OEmbed extends MediaSourceBase implements OEmbedInterface {
+
+ /**
+ * The logger channel for media.
+ *
+ * @var \Drupal\Core\Logger\LoggerChannelInterface
+ */
+ protected $logger;
+
+ /**
+ * The messenger service.
+ *
+ * @var \Drupal\Core\Messenger\MessengerInterface
+ */
+ protected $messenger;
+
+ /**
+ * The HTTP client.
+ *
+ * @var \GuzzleHttp\Client
+ */
+ protected $httpClient;
+
+ /**
+ * The oEmbed resource fetcher service.
+ *
+ * @var \Drupal\media\OEmbed\ResourceFetcherInterface
+ */
+ protected $resourceFetcher;
+
+ /**
+ * The OEmbed manager service.
+ *
+ * @var \Drupal\media\OEmbed\UrlResolverInterface
+ */
+ protected $urlResolver;
+
+ /**
+ * The iFrame URL helper service.
+ *
+ * @var \Drupal\media\IFrameUrlHelper
+ */
+ protected $iFrameUrlHelper;
+
+ /**
+ * Constructs a new OEmbed instance.
+ *
+ * @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\EntityTypeManagerInterface $entity_type_manager
+ * The entity type manager service.
+ * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
+ * The entity field manager service.
+ * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
+ * The config factory service.
+ * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
+ * The field type plugin manager service.
+ * @param \Drupal\Core\Logger\LoggerChannelInterface $logger
+ * The logger channel for media.
+ * @param \Drupal\Core\Messenger\MessengerInterface $messenger
+ * The messenger service.
+ * @param \GuzzleHttp\ClientInterface $http_client
+ * The HTTP client.
+ * @param \Drupal\media\OEmbed\ResourceFetcherInterface $resource_fetcher
+ * The oEmbed resource fetcher service.
+ * @param \Drupal\media\OEmbed\UrlResolverInterface $url_resolver
+ * The oEmbed URL resolver service.
+ * @param \Drupal\media\IFrameUrlHelper $iframe_url_helper
+ * The iFrame URL helper service.
+ */
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, ConfigFactoryInterface $config_factory, FieldTypePluginManagerInterface $field_type_manager, LoggerChannelInterface $logger, MessengerInterface $messenger, ClientInterface $http_client, ResourceFetcherInterface $resource_fetcher, UrlResolverInterface $url_resolver, IFrameUrlHelper $iframe_url_helper) {
+ parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $entity_field_manager, $field_type_manager, $config_factory);
+ $this->logger = $logger;
+ $this->messenger = $messenger;
+ $this->httpClient = $http_client;
+ $this->resourceFetcher = $resource_fetcher;
+ $this->urlResolver = $url_resolver;
+ $this->iFrameUrlHelper = $iframe_url_helper;
+ }
+
+ /**
+ * {@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'),
+ $container->get('entity_field.manager'),
+ $container->get('config.factory'),
+ $container->get('plugin.manager.field.field_type'),
+ $container->get('logger.factory')->get('media'),
+ $container->get('messenger'),
+ $container->get('http_client'),
+ $container->get('media.oembed.resource_fetcher'),
+ $container->get('media.oembed.url_resolver'),
+ $container->get('media.oembed.iframe_url_helper')
+ );
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getMetadataAttributes() {
+ return [
+ 'type' => $this->t('Resource type'),
+ 'title' => $this->t('Resource title'),
+ 'author_name' => $this->t('The name of the author/owner'),
+ 'author_url' => $this->t('The URL of the author/owner'),
+ 'provider_name' => $this->t("The name of the provider"),
+ 'provider_url' => $this->t('The URL of the provider'),
+ 'cache_age' => $this->t('Suggested cache lifetime'),
+ 'default_name' => $this->t('Default name of the media item'),
+ 'thumbnail_uri' => $this->t('Local URI of the thumbnail'),
+ 'thumbnail_width' => $this->t('Thumbnail width'),
+ 'thumbnail_height' => $this->t('Thumbnail height'),
+ 'url' => $this->t('The source URL of the resource'),
+ 'width' => $this->t('The width of the resource'),
+ 'height' => $this->t('The height of the resource'),
+ 'html' => $this->t('The HTML representation of the resource'),
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getMetadata(MediaInterface $media, $name) {
+ $media_url = $this->getSourceFieldValue($media);
+
+ try {
+ $resource_url = $this->urlResolver->getResourceUrl($media_url);
+ $resource = $this->resourceFetcher->fetchResource($resource_url);
+ }
+ catch (ResourceException $e) {
+ $this->messenger->addError($e->getMessage());
+ return NULL;
+ }
+
+ switch ($name) {
+ case 'default_name':
+ if ($title = $this->getMetadata($media, 'title')) {
+ return $title;
+ }
+ elseif ($url = $this->getMetadata($media, 'url')) {
+ return $url;
+ }
+ return parent::getMetadata($media, 'default_name');
+
+ case 'thumbnail_uri':
+ return $this->getLocalThumbnailUri($resource) ?: parent::getMetadata($media, 'thumbnail_uri');
+
+ case 'type':
+ return $resource->getType();
+
+ case 'title':
+ return $resource->getTitle();
+
+ case 'author_name':
+ return $resource->getAuthorName();
+
+ case 'author_url':
+ return $resource->getAuthorUrl();
+
+ case 'provider_name':
+ $provider = $resource->getProvider();
+ return $provider ? $provider->getName() : '';
+
+ case 'provider_url':
+ $provider = $resource->getProvider();
+ return $provider ? $provider->getUrl() : NULL;
+
+ case 'cache_age':
+ return $resource->getCacheMaxAge();
+
+ case 'thumbnail_width':
+ return $resource->getThumbnailWidth();
+
+ case 'thumbnail_height':
+ return $resource->getThumbnailHeight();
+
+ case 'url':
+ $url = $resource->getUrl();
+ return $url ? $url->toString() : NULL;
+
+ case 'width':
+ return $resource->getWidth();
+
+ case 'height':
+ return $resource->getHeight();
+
+ case 'html':
+ return $resource->getHtml();
+
+ default:
+ break;
+ }
+ return NULL;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
+ $form = parent::buildConfigurationForm($form, $form_state);
+
+ $domain = $this->configFactory->get('media.settings')->get('iframe_domain');
+ if (!$this->iFrameUrlHelper->isSecure($domain)) {
+ array_unshift($form, [
+ '#markup' => '
' . $this->t('It is potentially insecure to display oEmbed content in a frame that is served from the same domain as your main Drupal site, as this may allow execution of third-party code. You can specify a different domain for serving oEmbed content here (opens in a new window).', [
+ ':url' => Url::fromRoute('media.settings')->setAbsolute()->toString(),
+ ]) . '
',
+ ]);
+ }
+
+ $form['thumbnails_directory'] = [
+ '#type' => 'textfield',
+ '#title' => $this->t('Thumbnails location'),
+ '#default_value' => $this->configuration['thumbnails_directory'],
+ '#description' => $this->t('Thumbnails will be fetched from the provider for local usage. This is the URI of the directory where they will be placed.'),
+ '#required' => TRUE,
+ ];
+
+ $configuration = $this->getConfiguration();
+ $plugin_definition = $this->getPluginDefinition();
+
+ $form['providers'] = [
+ '#type' => 'checkboxes',
+ '#title' => $this->t('Allowed providers'),
+ '#default_value' => $configuration['providers'],
+ '#options' => array_combine($plugin_definition['providers'], $plugin_definition['providers']),
+ '#description' => $this->t('Optionally select the allowed oEmbed providers for this media type. If left blank, all providers will be allowed.'),
+ ];
+ return $form;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
+ parent::submitConfigurationForm($form, $form_state);
+ $configuration = $this->getConfiguration();
+ $configuration['providers'] = array_filter(array_values($configuration['providers']));
+ $this->setConfiguration($configuration);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
+ $thumbnails_directory = $form_state->getValue('thumbnails_directory');
+ if (!file_valid_uri($thumbnails_directory)) {
+ $form_state->setErrorByName('thumbnails_directory', $this->t('@path is not a valid path.', [
+ '@path' => $thumbnails_directory,
+ ]));
+ }
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function defaultConfiguration() {
+ return [
+ 'thumbnails_directory' => 'public://oembed_thumbnails',
+ 'providers' => [],
+ ] + parent::defaultConfiguration();
+ }
+
+ /**
+ * Returns the local URI for a resource thumbnail.
+ *
+ * If the thumbnail is not already locally stored, this method will attempt
+ * to download it.
+ *
+ * @param \Drupal\media\OEmbed\Resource $resource
+ * The oEmbed resource.
+ *
+ * @return string|null
+ * The local thumbnail URI, or NULL if it could not be downloaded, or if the
+ * resource has no thumbnail at all.
+ *
+ * @todo Determine whether or not oEmbed media thumbnails should be stored
+ * locally at all, and if so, whether that functionality should be
+ * toggle-able. See https://www.drupal.org/project/drupal/issues/2962751 for
+ * more information.
+ */
+ protected function getLocalThumbnailUri(Resource $resource) {
+ // If there is no remote thumbnail, there's nothing for us to fetch here.
+ $remote_thumbnail_url = $resource->getThumbnailUrl();
+ if (!$remote_thumbnail_url) {
+ return NULL;
+ }
+ $remote_thumbnail_url = $remote_thumbnail_url->toString();
+
+ // Compute the local thumbnail URI, regardless of whether or not it exists.
+ $configuration = $this->getConfiguration();
+ $directory = $configuration['thumbnails_directory'];
+ $local_thumbnail_uri = "$directory/" . Crypt::hashBase64($remote_thumbnail_url) . '.' . pathinfo($remote_thumbnail_url, PATHINFO_EXTENSION);
+
+ // If the local thumbnail already exists, return its URI.
+ if (file_exists($local_thumbnail_uri)) {
+ return $local_thumbnail_uri;
+ }
+
+ // The local thumbnail doesn't exist yet, so try to download it. First,
+ // ensure that the destination directory is writable, and if it's not,
+ // log an error and bail out.
+ if (!file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
+ $this->logger->warning('Could not prepare thumbnail destination directory @dir for oEmbed media.', [
+ '@dir' => $directory,
+ ]);
+ return NULL;
+ }
+
+ $error_message = 'Could not download remote thumbnail from {url}.';
+ $error_context = [
+ 'url' => $remote_thumbnail_url,
+ ];
+ try {
+ $response = $this->httpClient->get($remote_thumbnail_url);
+ if ($response->getStatusCode() === 200) {
+ $success = file_unmanaged_save_data((string) $response->getBody(), $local_thumbnail_uri, FILE_EXISTS_REPLACE);
+
+ if ($success) {
+ return $local_thumbnail_uri;
+ }
+ else {
+ $this->logger->warning($error_message, $error_context);
+ }
+ }
+ }
+ catch (RequestException $e) {
+ $this->logger->warning($e->getMessage());
+ }
+ return NULL;
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getSourceFieldConstraints() {
+ return [
+ 'oembed_resource' => [],
+ ];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function prepareViewDisplay(MediaTypeInterface $type, EntityViewDisplayInterface $display) {
+ $display->setComponent($this->getSourceFieldDefinition($type)->getName(), [
+ 'type' => 'oembed',
+ ]);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function prepareFormDisplay(MediaTypeInterface $type, EntityFormDisplayInterface $display) {
+ parent::prepareFormDisplay($type, $display);
+ $source_field = $this->getSourceFieldDefinition($type)->getName();
+
+ $display->setComponent($source_field, [
+ 'type' => 'oembed_textfield',
+ 'weight' => $display->getComponent($source_field)['weight'],
+ ]);
+ $display->removeComponent('name');
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getProviders() {
+ $configuration = $this->getConfiguration();
+ return $configuration['providers'] ?: $this->getPluginDefinition()['providers'];
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function createSourceField(MediaTypeInterface $type) {
+ $plugin_definition = $this->getPluginDefinition();
+
+ $label = (string) $this->t('@type URL', [
+ '@type' => $plugin_definition['label'],
+ ]);
+ return parent::createSourceField($type)->set('label', $label);
+ }
+
+}
diff --git a/core/modules/media/src/Plugin/media/Source/OEmbedDeriver.php b/core/modules/media/src/Plugin/media/Source/OEmbedDeriver.php
new file mode 100644
index 000000000000..8da3265f5b74
--- /dev/null
+++ b/core/modules/media/src/Plugin/media/Source/OEmbedDeriver.php
@@ -0,0 +1,32 @@
+derivatives = [
+ 'video' => [
+ 'id' => 'video',
+ 'label' => t('Remote video'),
+ 'description' => t('Use remote video URL for reusable media.'),
+ 'providers' => ['YouTube', 'Vimeo'],
+ 'default_thumbnail_filename' => 'video.png',
+ ] + $base_plugin_definition,
+ ];
+ return parent::getDerivativeDefinitions($base_plugin_definition);
+ }
+
+}
diff --git a/core/modules/media/src/Plugin/media/Source/OEmbedInterface.php b/core/modules/media/src/Plugin/media/Source/OEmbedInterface.php
new file mode 100644
index 000000000000..90d73e520662
--- /dev/null
+++ b/core/modules/media/src/Plugin/media/Source/OEmbedInterface.php
@@ -0,0 +1,23 @@
+
+
+
+ {{ media|raw }}
+
+
diff --git a/core/modules/media/tests/fixtures/oembed/photo_flickr.html b/core/modules/media/tests/fixtures/oembed/photo_flickr.html
new file mode 100644
index 000000000000..6ad06d81521d
--- /dev/null
+++ b/core/modules/media/tests/fixtures/oembed/photo_flickr.html
@@ -0,0 +1,8 @@
+
+
+
+
+
+
+
diff --git a/core/modules/media/tests/fixtures/oembed/photo_flickr.json b/core/modules/media/tests/fixtures/oembed/photo_flickr.json
new file mode 100644
index 000000000000..7cdc28e05616
--- /dev/null
+++ b/core/modules/media/tests/fixtures/oembed/photo_flickr.json
@@ -0,0 +1,12 @@
+{
+ "type": "photo",
+ "title": "Druplicon FTW!",
+ "width": "88",
+ "height": "100",
+ "url": "internal:\/core\/misc\/druplicon.png",
+ "thumbnail_url": "internal:\/core\/misc\/druplicon.png",
+ "thumbnail_width": 88,
+ "thumbnail_height": 100,
+ "provider_name": "Flickr",
+ "version": "1.0"
+}
diff --git a/core/modules/media/tests/fixtures/oembed/providers.json b/core/modules/media/tests/fixtures/oembed/providers.json
new file mode 100644
index 000000000000..e618ec40fa66
--- /dev/null
+++ b/core/modules/media/tests/fixtures/oembed/providers.json
@@ -0,0 +1,61 @@
+[
+ {
+ "provider_name": "Vimeo",
+ "provider_url": "https:\/\/vimeo.com\/",
+ "endpoints": [
+ {
+ "schemes": [
+ "https:\/\/vimeo.com\/*",
+ "https:\/\/vimeo.com\/album\/*\/video\/*",
+ "https:\/\/vimeo.com\/channels\/*\/*",
+ "https:\/\/vimeo.com\/groups\/*\/videos\/*",
+ "https:\/\/vimeo.com\/ondemand\/*\/*",
+ "https:\/\/player.vimeo.com\/video\/*"
+ ],
+ "url": "https:\/\/vimeo.com\/api\/oembed.{format}",
+ "discovery": true
+ }
+ ]
+ },
+ {
+ "provider_name": "Twitter",
+ "provider_url": "http:\/\/www.twitter.com\/",
+ "endpoints": [
+ {
+ "schemes": [
+ "https:\/\/twitter.com\/*\/status\/*",
+ "https:\/\/*.twitter.com\/*\/status\/*"
+ ],
+ "url": "https:\/\/publish.twitter.com\/oembed"
+
+ }
+ ]
+ },
+ {
+ "provider_name": "CollegeHumor",
+ "provider_url": "http:\/\/www.collegehumor.com\/",
+ "endpoints": [
+ {
+ "schemes": [
+ "http:\/\/www.collegehumor.com\/video\/*"
+ ],
+ "url": "http:\/\/www.collegehumor.com\/oembed.{format}",
+ "discovery": true
+ }
+ ]
+ },
+ {
+ "provider_name": "Flickr",
+ "provider_url": "http:\/\/www.flickr.com\/",
+ "endpoints": [
+ {
+ "schemes": [
+ "http:\/\/*.flickr.com\/photos\/*",
+ "http:\/\/flic.kr\/p\/*"
+ ],
+ "url": "http:\/\/www.flickr.com\/services\/oembed\/",
+ "discovery": true
+ }
+ ]
+ }
+]
diff --git a/core/modules/media/tests/fixtures/oembed/rich_twitter.json b/core/modules/media/tests/fixtures/oembed/rich_twitter.json
new file mode 100644
index 000000000000..f27b88199f1b
--- /dev/null
+++ b/core/modules/media/tests/fixtures/oembed/rich_twitter.json
@@ -0,0 +1,13 @@
+{
+ "url": "https:\/\/twitter.com\/drupaldevdays\/status\/935643039741202432",
+ "author_name": "Drupal Dev Days",
+ "author_url": "https:\/\/twitter.com\/drupaldevdays",
+ "html": "
",
+ "width": 480,
+ "height": 360,
+ "description": "Special thanks to Tendenci, formerly Schipul for sponsoring this video with training, equipment and time. The open source way. All creative however was self directed by the individuals - A. Hughes (www.schipul.com\/ahughes) featuring QCait (www.schipul.com\/qcait) - Hands On Drupal\n\nDrupal is a free software package that allows an individual or a community of users to easily publish, manage and organize a wide variety of content on a website.\n\nNeed a little Drupal help or just want to geek out with us? Visit our www.schipul.com\/drupal for more info - we'd love to connect!\n\nGo here for Drupal Common Terms and Suggested Modules : http:\/\/schipul.com\/en\/helpfiles\/v\/229",
+ "thumbnail_url": "internal:\/core\/misc\/druplicon.png",
+ "thumbnail_width": 295,
+ "thumbnail_height": 221
+}
diff --git a/core/modules/media/tests/modules/media_test_oembed/media_test_oembed.info.yml b/core/modules/media/tests/modules/media_test_oembed/media_test_oembed.info.yml
new file mode 100644
index 000000000000..2ad5d2fad331
--- /dev/null
+++ b/core/modules/media/tests/modules/media_test_oembed/media_test_oembed.info.yml
@@ -0,0 +1,8 @@
+name: Media oEmbed test
+description: 'Provides functionality to mimic an oEmbed provider.'
+type: module
+package: Testing
+version: VERSION
+core: 8.x
+dependencies:
+ - drupal:media
diff --git a/core/modules/media/tests/modules/media_test_oembed/media_test_oembed.module b/core/modules/media/tests/modules/media_test_oembed/media_test_oembed.module
new file mode 100644
index 000000000000..f3b283ca632f
--- /dev/null
+++ b/core/modules/media/tests/modules/media_test_oembed/media_test_oembed.module
@@ -0,0 +1,17 @@
+getName() === 'Vimeo') {
+ $parsed_url['query']['altered'] = 1;
+ }
+}
diff --git a/core/modules/media/tests/modules/media_test_oembed/media_test_oembed.routing.yml b/core/modules/media/tests/modules/media_test_oembed/media_test_oembed.routing.yml
new file mode 100644
index 000000000000..75ce685af710
--- /dev/null
+++ b/core/modules/media/tests/modules/media_test_oembed/media_test_oembed.routing.yml
@@ -0,0 +1,6 @@
+media_test_oembed.resource.get:
+ path: '/media_test_oembed/resource'
+ defaults:
+ _controller: '\Drupal\media_test_oembed\Controller\ResourceController::get'
+ requirements:
+ _access: 'TRUE'
diff --git a/core/modules/media/tests/modules/media_test_oembed/src/Controller/ResourceController.php b/core/modules/media/tests/modules/media_test_oembed/src/Controller/ResourceController.php
new file mode 100644
index 000000000000..ca401e72092f
--- /dev/null
+++ b/core/modules/media/tests/modules/media_test_oembed/src/Controller/ResourceController.php
@@ -0,0 +1,48 @@
+query->get('url');
+
+ $resources = \Drupal::state()->get(static::class, []);
+
+ $content = file_get_contents($resources[$asset_url]);
+ $response = new Response($content);
+ $response->headers->set('Content-Type', 'application/json');
+
+ return $response;
+ }
+
+ /**
+ * Maps an asset URL to a local fixture representing its oEmbed resource.
+ *
+ * @param string $asset_url
+ * The asset URL.
+ * @param string $resource_path
+ * The path of the oEmbed resource representing the asset.
+ */
+ public static function setResourceUrl($asset_url, $resource_path) {
+ $resources = \Drupal::state()->get(static::class, []);
+ $resources[$asset_url] = $resource_path;
+ \Drupal::state()->set(static::class, $resources);
+ }
+
+}
diff --git a/core/modules/media/tests/modules/media_test_oembed/src/MediaTestOembedServiceProvider.php b/core/modules/media/tests/modules/media_test_oembed/src/MediaTestOembedServiceProvider.php
new file mode 100644
index 000000000000..0ba3e0a7a690
--- /dev/null
+++ b/core/modules/media/tests/modules/media_test_oembed/src/MediaTestOembedServiceProvider.php
@@ -0,0 +1,26 @@
+getDefinition('media.oembed.provider_repository')
+ ->setClass(ProviderRepository::class);
+
+ $container->getDefinition('media.oembed.url_resolver')
+ ->setClass(UrlResolver::class);
+ }
+
+}
diff --git a/core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php b/core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php
new file mode 100644
index 000000000000..dc4cb8cbf6fb
--- /dev/null
+++ b/core/modules/media/tests/modules/media_test_oembed/src/ProviderRepository.php
@@ -0,0 +1,55 @@
+get(static::class) ?: parent::getAll();
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function get($provider_name) {
+ $providers = \Drupal::state()->get(static::class, []);
+
+ if (isset($providers[$provider_name])) {
+ return $providers[$provider_name];
+ }
+ return parent::get($provider_name);
+ }
+
+ /**
+ * Stores an oEmbed provider value object in state.
+ *
+ * @param \Drupal\media\OEmbed\Provider $provider
+ * The provider to store.
+ */
+ public function setProvider(Provider $provider) {
+ $providers = \Drupal::state()->get(static::class, []);
+ $name = $provider->getName();
+ $providers[$name] = $provider;
+ \Drupal::state()->set(static::class, $providers);
+ }
+
+}
diff --git a/core/modules/media/tests/modules/media_test_oembed/src/UrlResolver.php b/core/modules/media/tests/modules/media_test_oembed/src/UrlResolver.php
new file mode 100644
index 000000000000..acfbf8c32c3f
--- /dev/null
+++ b/core/modules/media/tests/modules/media_test_oembed/src/UrlResolver.php
@@ -0,0 +1,38 @@
+get(static::class, []);
+ $urls[$url] = $endpoint_url;
+ \Drupal::state()->set(static::class, $urls);
+ }
+
+ /**
+ * {@inheritdoc}
+ */
+ public function getResourceUrl($url, $max_width = NULL, $max_height = NULL) {
+ $urls = \Drupal::state()->get(static::class, []);
+
+ if (isset($urls[$url])) {
+ return $urls[$url];
+ }
+ return parent::getResourceUrl($url, $max_width, $max_height);
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php b/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php
new file mode 100644
index 000000000000..6f110f59624a
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php
@@ -0,0 +1,173 @@
+lockHttpClientToFixtures();
+ }
+
+ /**
+ * Data provider for testRender().
+ *
+ * @see ::testRender()
+ *
+ * @return array
+ */
+ public function providerRender() {
+ return [
+ 'Vimeo video' => [
+ 'https://vimeo.com/7073899',
+ 'video_vimeo.json',
+ [],
+ [
+ 'iframe' => [
+ 'src' => '/media/oembed?url=https%3A//vimeo.com/7073899',
+ 'width' => 480,
+ 'height' => 360,
+ ],
+ ],
+ ],
+ 'Vimeo video, resized' => [
+ 'https://vimeo.com/7073899',
+ 'video_vimeo.json?maxwidth=100&maxheight=100',
+ ['max_width' => 100, 'max_height' => 100],
+ [
+ 'iframe' => [
+ 'src' => '/media/oembed?url=https%3A//vimeo.com/7073899',
+ 'width' => 100,
+ 'height' => 100,
+ ],
+ ],
+ ],
+ 'tweet' => [
+ 'https://twitter.com/drupaldevdays/status/935643039741202432',
+ 'rich_twitter.json',
+ [],
+ [
+ 'iframe' => [
+ 'src' => '/media/oembed?url=https%3A//twitter.com/drupaldevdays/status/935643039741202432',
+ 'width' => 550,
+ 'height' => 360,
+ ],
+ ],
+ ],
+ 'Flickr photo' => [
+ 'https://www.flickr.com/photos/amazeelabs/26497866357',
+ 'photo_flickr.json',
+ [],
+ [
+ 'img' => [
+ 'src' => '/core/misc/druplicon.png',
+ 'width' => 88,
+ 'height' => 100,
+ ],
+ ],
+ ],
+ ];
+ }
+
+ /**
+ * Tests that oEmbed media types' display can be configured correctly.
+ */
+ public function testDisplayConfiguration() {
+ $account = $this->drupalCreateUser(['administer media display']);
+ $this->drupalLogin($account);
+
+ $media_type = $this->createMediaType([], 'oembed:video');
+ $this->drupalGet('/admin/structure/media/manage/' . $media_type->id() . '/display');
+ $assert = $this->assertSession();
+ $assert->statusCodeEquals(200);
+ // Test that the formatter doesn't try to check applicability for fields
+ // which do not have a specific target bundle.
+ // @see https://www.drupal.org/project/drupal/issues/2976795.
+ $assert->pageTextNotContains('Can only flip STRING and INTEGER values!');
+ }
+
+ /**
+ * Tests the oEmbed field formatter.
+ *
+ * @param string $url
+ * The canonical URL of the media asset to test.
+ * @param string $resource_url
+ * The oEmebd resource URL of the media asset to test.
+ * @param mixed $formatter_settings
+ * Settings for the oEmbed field formatter.
+ * @param array $selectors
+ * An array of arrays. Each key is a CSS selector targeting an element in
+ * the rendered output, and each value is an array of attributes, keyed by
+ * name, that the element is expected to have.
+ *
+ * @dataProvider providerRender
+ */
+ public function testRender($url, $resource_url, array $formatter_settings, array $selectors) {
+ $account = $this->drupalCreateUser(['view media']);
+ $this->drupalLogin($account);
+
+ $media_type = $this->createMediaType([], 'oembed:video');
+
+ $source = $media_type->getSource();
+ $source_field = $source->getSourceFieldDefinition($media_type);
+
+ EntityViewDisplay::create([
+ 'targetEntityType' => 'media',
+ 'bundle' => $media_type->id(),
+ 'mode' => 'full',
+ 'status' => TRUE,
+ ])->removeComponent('thumbnail')
+ ->setComponent($source_field->getName(), [
+ 'type' => 'oembed',
+ 'settings' => $formatter_settings,
+ ])
+ ->save();
+
+ $this->hijackProviderEndpoints();
+
+ ResourceController::setResourceUrl($url, $this->getFixturesDirectory() . '/' . $resource_url);
+ UrlResolver::setEndpointUrl($url, $resource_url);
+
+ $entity = Media::create([
+ 'bundle' => $media_type->id(),
+ $source_field->getName() => $url,
+ ]);
+ $entity->save();
+
+ $this->drupalGet($entity->toUrl());
+ $assert = $this->assertSession();
+ $assert->statusCodeEquals(200);
+ foreach ($selectors as $selector => $attributes) {
+ foreach ($attributes as $attribute => $value) {
+ $assert->elementAttributeContains('css', $selector, $attribute, $value);
+ }
+ }
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/MediaCacheTagsTest.php b/core/modules/media/tests/src/Functional/MediaCacheTagsTest.php
index e1b6cf7ef1b5..ece89f43eae2 100644
--- a/core/modules/media/tests/src/Functional/MediaCacheTagsTest.php
+++ b/core/modules/media/tests/src/Functional/MediaCacheTagsTest.php
@@ -4,7 +4,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\media\Entity\Media;
-use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase;
+use Drupal\Tests\system\Functional\Entity\EntityWithUriCacheTagsTestBase;
/**
* Tests the media items cache tags.
diff --git a/core/modules/media/tests/src/Functional/MediaSettingsTest.php b/core/modules/media/tests/src/Functional/MediaSettingsTest.php
new file mode 100644
index 000000000000..850d0e2b447d
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/MediaSettingsTest.php
@@ -0,0 +1,35 @@
+drupalLogin($this->createUser(['administer site configuration']));
+ }
+
+ /**
+ * Test that media warning appears if oEmbed media types exists.
+ */
+ public function testStatusPage() {
+ $assert_session = $this->assertSession();
+
+ $this->drupalGet('admin/reports/status');
+ $assert_session->pageTextNotContains('It is potentially insecure to display oEmbed content in a frame');
+
+ $this->createMediaType([], 'oembed:video');
+
+ $this->drupalGet('admin/reports/status');
+ $assert_session->pageTextContains('It is potentially insecure to display oEmbed content in a frame');
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php b/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php
index e0e529412249..274497ae0112 100644
--- a/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php
+++ b/core/modules/media/tests/src/Functional/MediaTemplateSuggestionsTest.php
@@ -40,7 +40,7 @@ public function testMediaThemeHookSuggestions() {
$variables['elements'] = $build;
$suggestions = \Drupal::moduleHandler()->invokeAll('theme_suggestions_media', [$variables]);
- $this->assertEquals($suggestions, ['media__full', 'media__' . $media_type->id(), 'media__' . $media_type->id() . '__full'], 'Found expected media suggestions.');
+ $this->assertEquals($suggestions, ['media__full', 'media__' . $media_type->id(), 'media__' . $media_type->id() . '__full', 'media__source_' . $media_type->getSource()->getPluginId()], 'Found expected media suggestions.');
}
}
diff --git a/core/modules/media/tests/src/Functional/ProviderRepositoryTest.php b/core/modules/media/tests/src/Functional/ProviderRepositoryTest.php
new file mode 100644
index 000000000000..43a71053ab22
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/ProviderRepositoryTest.php
@@ -0,0 +1,89 @@
+prophesize('\GuzzleHttp\Psr7\Response');
+ $response->getBody()->willReturn($content);
+
+ $client = $this->createMock('\GuzzleHttp\Client');
+ $client->method('request')->withAnyParameters()->willReturn($response->reveal());
+ $this->container->set('http_client', $client);
+
+ $this->setExpectedException(ProviderException::class, 'Remote oEmbed providers database returned invalid or empty list.');
+ $this->container->get('media.oembed.provider_repository')->getAll();
+ }
+
+ /**
+ * Data provider for testEmptyProviderList().
+ *
+ * @see ::testEmptyProviderList()
+ *
+ * @return array
+ */
+ public function providerEmptyProviderList() {
+ return [
+ 'empty array' => ['[]'],
+ 'empty string' => [''],
+ ];
+ }
+
+ /**
+ * Tests that provider discovery fails with a non-existent provider database.
+ *
+ * @param string $providers_url
+ * The URL of the provider database.
+ * @param string $exception_message
+ * The expected exception message.
+ *
+ * @dataProvider providerNonExistingProviderDatabase
+ */
+ public function testNonExistingProviderDatabase($providers_url, $exception_message) {
+ $this->config('media.settings')
+ ->set('oembed_providers_url', $providers_url)
+ ->save();
+
+ $this->setExpectedException(ProviderException::class, $exception_message);
+ $this->container->get('media.oembed.provider_repository')->getAll();
+ }
+
+ /**
+ * Data provider for testEmptyProviderList().
+ *
+ * @see ::testEmptyProviderList()
+ *
+ * @return array
+ */
+ public function providerNonExistingProviderDatabase() {
+ return [
+ [
+ 'http://oembed1.com/providers.json',
+ 'Could not retrieve the oEmbed provider database from http://oembed1.com/providers.json',
+ ],
+ [
+ 'http://oembed.com/providers1.json',
+ 'Could not retrieve the oEmbed provider database from http://oembed.com/providers1.json',
+ ],
+ ];
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/ResourceFetcherTest.php b/core/modules/media/tests/src/Functional/ResourceFetcherTest.php
new file mode 100644
index 000000000000..e10ef2e20939
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/ResourceFetcherTest.php
@@ -0,0 +1,72 @@
+useFixtureProviders();
+ $this->lockHttpClientToFixtures();
+ }
+
+ /**
+ * Data provider for testFetchResource().
+ *
+ * @return array
+ */
+ public function providerFetchResource() {
+ return [
+ 'JSON resource' => [
+ 'video_vimeo.json',
+ 'Vimeo',
+ 'Drupal Rap Video - Schipulcon09',
+ ],
+ 'XML resource' => [
+ 'video_collegehumor.xml',
+ 'CollegeHumor',
+ "Let's Not Get a Drink Sometime",
+ ],
+ ];
+ }
+
+ /**
+ * Tests resource fetching.
+ *
+ * @param string $resource_url
+ * The URL of the resource to fetch, relative to the base URL.
+ * @param string $provider_name
+ * The expected name of the resource provider.
+ * @param string $title
+ * The expected title of the resource.
+ *
+ * @covers ::fetchResource
+ *
+ * @dataProvider providerFetchResource
+ */
+ public function testFetchResource($resource_url, $provider_name, $title) {
+ /** @var \Drupal\media\OEmbed\Resource $resource */
+ $resource = $this->container->get('media.oembed.resource_fetcher')
+ ->fetchResource($resource_url);
+
+ $this->assertInstanceOf(Resource::class, $resource);
+ $this->assertSame($provider_name, $resource->getProvider()->getName());
+ $this->assertSame($title, $resource->getTitle());
+ }
+
+}
diff --git a/core/modules/media/tests/src/Functional/Update/MediaUpdateTest.php b/core/modules/media/tests/src/Functional/Update/MediaUpdateTest.php
index 2dbe28be3650..336033282d68 100644
--- a/core/modules/media/tests/src/Functional/Update/MediaUpdateTest.php
+++ b/core/modules/media/tests/src/Functional/Update/MediaUpdateTest.php
@@ -53,4 +53,25 @@ public function testBundlePermission() {
}
}
+ /**
+ * Tests that media.settings config is updated with oEmbed configuration.
+ *
+ * @see media_update_8600()
+ */
+ public function testOEmbedConfig() {
+ // The drupal-8.media-enabled.php fixture installs Media and all its config,
+ // which includes the oembed_providers_url and iframe_domain keys in
+ // media.settings. So, in order to prove that the update actually works,
+ // delete the values from config before running the update.
+ $this->config('media.settings')
+ ->clear('oembed_providers_url')
+ ->clear('iframe_domain')
+ ->save(TRUE);
+
+ $this->runUpdates();
+ $config = $this->config('media.settings');
+ $this->assertSame('https://oembed.com/providers.json', $config->get('oembed_providers_url'));
+ $this->assertSame('', $config->get('iframe_domain'));
+ }
+
}
diff --git a/core/modules/media/tests/src/Functional/UrlResolverTest.php b/core/modules/media/tests/src/Functional/UrlResolverTest.php
new file mode 100644
index 000000000000..1dfe5d6abee9
--- /dev/null
+++ b/core/modules/media/tests/src/Functional/UrlResolverTest.php
@@ -0,0 +1,133 @@
+lockHttpClientToFixtures();
+ $this->useFixtureProviders();
+ }
+
+ /**
+ * Data provider for testEndpointMatching().
+ *
+ * @see ::testEndpointMatching()
+ *
+ * @return array
+ */
+ public function providerEndpointMatching() {
+ return [
+ 'match by endpoint: Twitter' => [
+ 'https://twitter.com/Dries/status/999985431595880448',
+ 'https://publish.twitter.com/oembed?url=https%3A//twitter.com/Dries/status/999985431595880448',
+ ],
+ 'match by endpoint: Vimeo' => [
+ 'https://vimeo.com/14782834',
+ 'https://vimeo.com/api/oembed.json?url=https%3A//vimeo.com/14782834',
+ ],
+ 'match by endpoint: CollegeHumor' => [
+ 'http://www.collegehumor.com/video/40002870/lets-not-get-a-drink-sometime',
+ 'http://www.collegehumor.com/oembed.json?url=http%3A//www.collegehumor.com/video/40002870/lets-not-get-a-drink-sometime',
+ ],
+ ];
+ }
+
+ /**
+ * Tests resource URL resolution when the asset URL can be matched to a
+ * provider endpoint.
+ *
+ * @covers ::getProviderByUrl
+ * @covers ::getResourceUrl
+ *
+ * @param string $url
+ * The asset URL to resolve.
+ * @param string $resource_url
+ * The expected oEmbed resource URL of the asset.
+ *
+ * @dataProvider providerEndpointMatching
+ */
+ public function testEndpointMatching($url, $resource_url) {
+ $this->assertSame(
+ $resource_url,
+ $this->container->get('media.oembed.url_resolver')->getResourceUrl($url)
+ );
+ }
+
+ /**
+ * Tests that hook_oembed_resource_url_alter() is invoked.
+ *
+ * @depends testEndpointMatching
+ */
+ public function testResourceUrlAlterHook() {
+ $this->container->get('module_installer')->install(['media_test_oembed']);
+
+ $resource_url = $this->container->get('media.oembed.url_resolver')
+ ->getResourceUrl('https://vimeo.com/14782834');
+
+ $this->assertContains('altered=1', parse_url($resource_url, PHP_URL_QUERY));
+ }
+
+ /**
+ * Data provider for testUrlDiscovery().
+ *
+ * @see ::testUrlDiscovery()
+ *
+ * @return array
+ */
+ public function providerUrlDiscovery() {
+ return [
+ 'JSON resource' => [
+ 'video_vimeo.html',
+ 'https://vimeo.com/api/oembed.json?url=video_vimeo.html',
+ ],
+ 'XML resource' => [
+ 'video_collegehumor.html',
+ // The endpoint does not explicitly declare that it supports XML, so
+ // only JSON support is assumed, which is why the discovered URL
+ // contains '.json'. However, the fetched HTML file contains a
+ // relationship to an XML representation of the resource, with the
+ // application/xml+oembed MIME type.
+ 'http://www.collegehumor.com/oembed.json?url=video_collegehumor.html',
+ ],
+ ];
+ }
+
+ /**
+ * Tests URL resolution when the resource URL must be actively discovered by
+ * scanning the asset.
+ *
+ * @param string $url
+ * The asset URL to resolve.
+ * @param string $resource_url
+ * The expected oEmbed resource URL of the asset.
+ *
+ * @covers ::discoverResourceUrl
+ * @covers ::getProviderByUrl
+ * @covers ::getResourceUrl
+ *
+ * @dataProvider providerUrlDiscovery
+ */
+ public function testUrlDiscovery($url, $resource_url) {
+ $this->assertSame(
+ $this->container->get('media.oembed.url_resolver')->getResourceUrl($url),
+ $resource_url
+ );
+ }
+
+}
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
index 48b42916aa7c..0549d2d16cba 100644
--- a/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaDisplayTest.php
@@ -54,6 +54,7 @@ public function testMediaDisplay() {
// Enable the field on the display and verify it becomes visible on the UI.
$this->drupalGet("/admin/structure/media/manage/{$media_type->id()}/display");
+ $assert_session->buttonExists('Show row weights')->press();
$page->selectFieldOption('fields[name][region]', 'content');
$assert_session->waitForElementVisible('css', '#edit-fields-name-settings-edit');
$page->pressButton('Save');
diff --git a/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php
new file mode 100644
index 000000000000..dbffaf0c4e38
--- /dev/null
+++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php
@@ -0,0 +1,190 @@
+lockHttpClientToFixtures();
+ }
+
+ /**
+ * Tests the oembed media source.
+ */
+ public function testMediaOEmbedVideoSource() {
+ $media_type_id = 'test_media_oembed_type';
+ $provided_fields = [
+ 'type',
+ 'title',
+ 'default_name',
+ 'author_name',
+ 'author_url',
+ 'provider_name',
+ 'provider_url',
+ 'cache_age',
+ 'thumbnail_uri',
+ 'thumbnail_width',
+ 'thumbnail_height',
+ 'url',
+ 'width',
+ 'height',
+ 'html',
+ ];
+
+ $session = $this->getSession();
+ $page = $session->getPage();
+ $assert_session = $this->assertSession();
+
+ $this->doTestCreateMediaType($media_type_id, 'oembed:video', $provided_fields);
+
+ // Create custom fields for the media type to store metadata attributes.
+ $fields = [
+ 'field_string_width' => 'string',
+ 'field_string_height' => 'string',
+ 'field_string_author_name' => 'string',
+ ];
+ $this->createMediaTypeFields($fields, $media_type_id);
+
+ // Hide the name field widget to test default name generation.
+ $this->hideMediaTypeFieldWidget('name', $media_type_id);
+
+ $this->drupalGet("admin/structure/media/manage/$media_type_id");
+ // Only accept Vimeo videos.
+ $page->checkField("source_configuration[providers][Vimeo]");
+ $assert_session->selectExists('field_map[width]')->setValue('field_string_width');
+ $assert_session->selectExists('field_map[height]')->setValue('field_string_height');
+ $assert_session->selectExists('field_map[author_name]')->setValue('field_string_author_name');
+ $assert_session->buttonExists('Save')->press();
+
+ $this->hijackProviderEndpoints();
+ $video_url = 'https://vimeo.com/7073899';
+ ResourceController::setResourceUrl($video_url, $this->getFixturesDirectory() . '/video_vimeo.json');
+
+ // Create a media item.
+ $this->drupalGet("media/add/$media_type_id");
+ $assert_session->fieldExists('Remote video URL')->setValue($video_url);
+ $assert_session->buttonExists('Save')->press();
+
+ $assert_session->addressEquals('media/1');
+ /** @var \Drupal\media\MediaInterface $media */
+ $media = Media::load(1);
+
+ // The thumbnail should have been downloaded.
+ $thumbnail = $media->getSource()->getMetadata($media, 'thumbnail_uri');
+ $this->assertFileExists($thumbnail);
+
+ // Ensure the iframe exists and that its src attribute contains a coherent
+ // URL with the query parameters we expect.
+ $iframe_url = $assert_session->elementExists('css', 'iframe')->getAttribute('src');
+ $iframe_url = parse_url($iframe_url);
+ $this->assertStringEndsWith('/media/oembed', $iframe_url['path']);
+ $this->assertNotEmpty($iframe_url['query']);
+ $query = [];
+ parse_str($iframe_url['query'], $query);
+ $this->assertSame($video_url, $query['url']);
+ $this->assertNotEmpty($query['hash']);
+
+ // Make sure the thumbnail is displayed from uploaded image.
+ $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', '/oembed_thumbnails/' . basename($thumbnail));
+
+ // Load the media and check that all fields are properly populated.
+ $media = Media::load(1);
+ $this->assertSame('Drupal Rap Video - Schipulcon09', $media->getName());
+ $this->assertSame('480', $media->field_string_width->value);
+ $this->assertSame('360', $media->field_string_height->value);
+
+ // Try to create a media asset from a disallowed provider.
+ $this->drupalGet("media/add/$media_type_id");
+ $assert_session->fieldExists('Remote video URL')->setValue('http://www.collegehumor.com/video/40003213/grant-and-katie-are-starting-their-own-company');
+ $page->pressButton('Save');
+
+ $assert_session->pageTextContains('The CollegeHumor provider is not allowed.');
+
+ // Test anonymous access to media via iframe.
+ $this->drupalLogout();
+
+ // Without a hash should be denied.
+ $no_hash_query = array_diff_key($query, ['hash' => '']);
+ $this->drupalGet('media/oembed', ['query' => $no_hash_query]);
+ $assert_session->pageTextNotContains('By the power of Greyskull, Vimeo works!');
+ $assert_session->pageTextContains('Access denied');
+
+ // A correct query should be allowed because the anonymous role has the
+ // 'view media' permission.
+ $this->drupalGet('media/oembed', ['query' => $query]);
+ $assert_session->pageTextContains('By the power of Greyskull, Vimeo works!');
+
+ // Remove the 'view media' permission to test that this restricts access.
+ $role = Role::load(AccountInterface::ANONYMOUS_ROLE);
+ $role->revokePermission('view media');
+ $role->save();
+ $this->drupalGet('media/oembed', ['query' => $query]);
+ $assert_session->pageTextNotContains('By the power of Greyskull, Vimeo works!');
+ $assert_session->pageTextContains('Access denied');
+ }
+
+ /**
+ * Test that a security warning appears if iFrame domain is not set.
+ */
+ public function testOEmbedSecurityWarning() {
+ $media_type_id = 'test_media_oembed_type';
+ $source_id = 'oembed:video';
+
+ $session = $this->getSession();
+ $page = $session->getPage();
+ $assert_session = $this->assertSession();
+
+ $this->drupalGet('admin/structure/media/add');
+ $page->fillField('label', $media_type_id);
+ $this->getSession()
+ ->wait(5000, "jQuery('.machine-name-value').text() === '{$media_type_id}'");
+
+ // Make sure the source is available.
+ $assert_session->fieldExists('Media source');
+ $assert_session->optionExists('Media source', $source_id);
+ $page->selectFieldOption('Media source', $source_id);
+ $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
+ $this->assertNotEmpty($result);
+
+ $assert_session->pageTextContains('It is potentially insecure to display oEmbed content in a frame');
+
+ $this->config('media.settings')->set('iframe_domain', 'http://example.com')->save();
+
+ $this->drupalGet('admin/structure/media/add');
+ $page->fillField('label', $media_type_id);
+ $this->getSession()
+ ->wait(5000, "jQuery('.machine-name-value').text() === '{$media_type_id}'");
+
+ // Make sure the source is available.
+ $assert_session->fieldExists('Media source');
+ $assert_session->optionExists('Media source', $source_id);
+ $page->selectFieldOption('Media source', $source_id);
+ $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
+ $this->assertNotEmpty($result);
+
+ $assert_session->pageTextNotContains('It is potentially insecure to display oEmbed content in a frame');
+ }
+
+}
diff --git a/core/modules/media/tests/src/Kernel/OEmbedIframeControllerTest.php b/core/modules/media/tests/src/Kernel/OEmbedIframeControllerTest.php
new file mode 100644
index 000000000000..84fe4397daae
--- /dev/null
+++ b/core/modules/media/tests/src/Kernel/OEmbedIframeControllerTest.php
@@ -0,0 +1,56 @@
+ [
+ '',
+ ],
+ 'invalid hash' => [
+ $this->randomString(),
+ ],
+ ];
+ }
+
+ /**
+ * Tests validation of the 'hash' query string parameter.
+ *
+ * @param string $hash
+ * The 'hash' query string parameter.
+ *
+ * @dataProvider providerBadHashParameter
+ *
+ * @covers ::render
+ */
+ public function testBadHashParameter($hash) {
+ /** @var callable $controller */
+ $controller = $this->container
+ ->get('controller_resolver')
+ ->getControllerFromDefinition('\Drupal\media\Controller\OEmbedIframeController::render');
+
+ $this->assertInternalType('callable', $controller);
+
+ $this->setExpectedException('\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException', 'This resource is not available');
+ $request = new Request([
+ 'url' => 'https://example.com/path/to/resource',
+ 'hash' => $hash,
+ ]);
+ $controller($request);
+ }
+
+}
diff --git a/core/modules/media/tests/src/Traits/OEmbedTestTrait.php b/core/modules/media/tests/src/Traits/OEmbedTestTrait.php
new file mode 100644
index 000000000000..a3699ff69072
--- /dev/null
+++ b/core/modules/media/tests/src/Traits/OEmbedTestTrait.php
@@ -0,0 +1,89 @@
+baseUrl . '/' . $this->getFixturesDirectory();
+ }
+
+ /**
+ * Forces Media to use the provider database in the fixtures directory.
+ */
+ protected function useFixtureProviders() {
+ $this->config('media.settings')
+ ->set('oembed_providers_url', $this->getFixturesUrl() . '/providers.json')
+ ->save();
+ }
+
+ /**
+ * Configures the http_client service so that all requests are carried out
+ * relative to the URL of the fixtures directory. For example, after calling
+ * this method, a request for foobar.html will actually request
+ * http://test-site/path/to/fuxtures/foobar.html.
+ */
+ protected function lockHttpClientToFixtures() {
+ $this->writeSettings([
+ 'settings' => [
+ 'http_client_config' => [
+ 'base_uri' => (object) [
+ 'value' => $this->getFixturesUrl() . '/',
+ 'required' => TRUE,
+ ],
+ ],
+ ],
+ ]);
+ }
+
+ /**
+ * Ensures that all oEmbed provider endpoints defined in the fixture
+ * providers.json will use the media_test_oembed.resource.get route as their
+ * URL.
+ *
+ * This requires the media_test_oembed module in order to work.
+ */
+ protected function hijackProviderEndpoints() {
+ $providers = $this->getFixturesDirectory() . '/providers.json';
+ $providers = file_get_contents($providers);
+ $providers = Json::decode($providers);
+
+ $endpoint_url = Url::fromRoute('media_test_oembed.resource.get')
+ ->setAbsolute()
+ ->toString();
+
+ /** @var \Drupal\media_test_oembed\ProviderRepository $provider_repository */
+ $provider_repository = $this->container->get('media.oembed.provider_repository');
+
+ foreach ($providers as &$provider) {
+ foreach ($provider['endpoints'] as &$endpoint) {
+ $endpoint['url'] = $endpoint_url;
+ }
+ $provider_repository->setProvider(
+ new Provider($provider['provider_name'], $provider['provider_url'], $provider['endpoints'])
+ );
+ }
+ }
+
+}
diff --git a/core/modules/media/tests/src/Unit/IFrameUrlHelperTest.php b/core/modules/media/tests/src/Unit/IFrameUrlHelperTest.php
new file mode 100644
index 000000000000..99a1362b486f
--- /dev/null
+++ b/core/modules/media/tests/src/Unit/IFrameUrlHelperTest.php
@@ -0,0 +1,89 @@
+ [
+ '/path/to/media.php',
+ 'http://www.example.com/',
+ FALSE,
+ ],
+ 'no base URL domain' => [
+ 'http://www.example.com/media.php',
+ '/invalid/base/url',
+ FALSE,
+ ],
+ 'same domain' => [
+ 'http://www.example.com/media.php',
+ 'http://www.example.com/',
+ FALSE,
+ ],
+ 'different domain' => [
+ 'http://www.example.com/media.php',
+ 'http://www.example-assets.com/',
+ TRUE,
+ ],
+ 'same subdomain' => [
+ 'http://foo.example.com/media.php',
+ 'http://foo.example.com/',
+ FALSE,
+ ],
+ 'different subdomain' => [
+ 'http://assets.example.com/media.php',
+ 'http://foo.example.com/',
+ TRUE,
+ ],
+ 'subdomain and top-level domain' => [
+ 'http://assets.example.com/media.php',
+ 'http://example.com/',
+ TRUE,
+ ],
+ ];
+ }
+
+ /**
+ * Tests that isSecure() behaves properly.
+ *
+ * @param string $url
+ * The URL to test for security.
+ * @param string $base_url
+ * The base URL to compare $url against.
+ * @param bool $secure
+ * The expected result of isSecure().
+ *
+ * @covers ::isSecure
+ *
+ * @dataProvider providerIsSecure
+ */
+ public function testIsSecure($url, $base_url, $secure) {
+ $request_context = $this->prophesize(RequestContext::class);
+ $request_context->getCompleteBaseUrl()->willReturn($base_url);
+ $url_helper = new IFrameUrlHelper(
+ $request_context->reveal(),
+ $this->prophesize(PrivateKey::class)->reveal()
+ );
+
+ $this->assertSame($secure, $url_helper->isSecure($url));
+ }
+
+}
diff --git a/core/modules/media_library/config/install/core.entity_view_mode.media.media_library.yml b/core/modules/media_library/config/install/core.entity_view_mode.media.media_library.yml
new file mode 100644
index 000000000000..3406f026b491
--- /dev/null
+++ b/core/modules/media_library/config/install/core.entity_view_mode.media.media_library.yml
@@ -0,0 +1,12 @@
+langcode: en
+status: true
+dependencies:
+ enforced:
+ module:
+ - media_library
+ module:
+ - media
+id: media.media_library
+label: 'Media library'
+targetEntityType: media
+cache: true
diff --git a/core/modules/media_library/config/install/views.view.media_library.yml b/core/modules/media_library/config/install/views.view.media_library.yml
new file mode 100644
index 000000000000..8c9e784e6008
--- /dev/null
+++ b/core/modules/media_library/config/install/views.view.media_library.yml
@@ -0,0 +1,447 @@
+langcode: en
+status: true
+dependencies:
+ config:
+ - core.entity_view_mode.media.media_library
+ enforced:
+ module:
+ - media_library
+ module:
+ - media
+ - user
+id: media_library
+label: 'Media library'
+module: views
+description: ''
+tag: ''
+base_table: media_field_data
+base_field: mid
+core: 8.x
+display:
+ default:
+ display_plugin: default
+ id: default
+ display_title: Master
+ position: 0
+ display_options:
+ access:
+ type: perm
+ options:
+ perm: 'access media overview'
+ cache:
+ type: tag
+ options: { }
+ query:
+ type: views_query
+ options:
+ disable_sql_rewrite: false
+ distinct: false
+ replica: false
+ query_comment: ''
+ query_tags: { }
+ exposed_form:
+ type: basic
+ options:
+ submit_button: 'Apply Filters'
+ reset_button: false
+ reset_button_label: Reset
+ exposed_sorts_label: 'Sort by'
+ expose_sort_order: false
+ sort_asc_label: Asc
+ sort_desc_label: Desc
+ pager:
+ type: mini
+ options:
+ items_per_page: 25
+ offset: 0
+ id: 0
+ total_pages: null
+ expose:
+ items_per_page: false
+ items_per_page_label: 'Items per page'
+ items_per_page_options: '5, 10, 25, 50'
+ items_per_page_options_all: false
+ items_per_page_options_all_label: '- All -'
+ offset: false
+ offset_label: Offset
+ tags:
+ previous: ‹‹
+ next: ››
+ style:
+ type: default
+ options:
+ grouping: { }
+ row_class: 'media-library-item js-click-to-select'
+ default_row_class: true
+ row:
+ type: fields
+ options:
+ default_field_elements: true
+ inline: { }
+ separator: ''
+ hide_empty: false
+ fields:
+ media_bulk_form:
+ id: media_bulk_form
+ table: media
+ field: media_bulk_form
+ relationship: none
+ group_type: group
+ admin_label: ''
+ label: ''
+ exclude: false
+ alter:
+ alter_text: false
+ text: ''
+ make_link: false
+ path: ''
+ absolute: false
+ external: false
+ replace_spaces: false
+ path_case: none
+ trim_whitespace: false
+ alt: ''
+ rel: ''
+ link_class: ''
+ prefix: ''
+ suffix: ''
+ target: ''
+ nl2br: false
+ max_length: 0
+ word_boundary: true
+ ellipsis: true
+ more_link: false
+ more_link_text: ''
+ more_link_path: ''
+ strip_tags: false
+ trim: false
+ preserve_tags: ''
+ html: false
+ element_type: ''
+ element_class: js-click-to-select__checkbox
+ element_label_type: ''
+ element_label_class: ''
+ element_label_colon: false
+ element_wrapper_type: ''
+ element_wrapper_class: ''
+ element_default_classes: true
+ empty: ''
+ hide_empty: false
+ empty_zero: false
+ hide_alter_empty: true
+ action_title: Action
+ include_exclude: exclude
+ selected_actions: { }
+ entity_type: media
+ plugin_id: bulk_form
+ rendered_entity:
+ id: rendered_entity
+ table: media
+ field: rendered_entity
+ relationship: none
+ group_type: group
+ admin_label: ''
+ label: ''
+ exclude: false
+ alter:
+ alter_text: false
+ text: ''
+ make_link: false
+ path: ''
+ absolute: false
+ external: false
+ replace_spaces: false
+ path_case: none
+ trim_whitespace: false
+ alt: ''
+ rel: ''
+ link_class: ''
+ prefix: ''
+ suffix: ''
+ target: ''
+ nl2br: false
+ max_length: 0
+ word_boundary: true
+ ellipsis: true
+ more_link: false
+ more_link_text: ''
+ more_link_path: ''
+ strip_tags: false
+ trim: false
+ preserve_tags: ''
+ html: false
+ element_type: ''
+ element_class: media-library-item__content
+ element_label_type: ''
+ element_label_class: ''
+ element_label_colon: false
+ element_wrapper_type: ''
+ element_wrapper_class: ''
+ element_default_classes: true
+ empty: ''
+ hide_empty: false
+ empty_zero: false
+ hide_alter_empty: true
+ view_mode: media_library
+ entity_type: media
+ plugin_id: rendered_entity
+ filters:
+ status:
+ id: status
+ table: media_field_data
+ field: status
+ relationship: none
+ group_type: group
+ admin_label: ''
+ operator: '='
+ value: '1'
+ group: 1
+ exposed: true
+ expose:
+ operator_id: ''
+ label: 'Publishing status'
+ description: null
+ use_operator: false
+ operator: status_op
+ identifier: status
+ required: true
+ remember: false
+ multiple: false
+ remember_roles:
+ authenticated: authenticated
+ is_grouped: true
+ group_info:
+ label: Published
+ description: ''
+ identifier: status
+ optional: true
+ widget: select
+ multiple: false
+ remember: false
+ default_group: All
+ default_group_multiple: { }
+ group_items:
+ 1:
+ title: Published
+ operator: '='
+ value: '1'
+ 2:
+ title: Unpublished
+ operator: '='
+ value: '0'
+ plugin_id: boolean
+ entity_type: media
+ entity_field: status
+ name:
+ id: name
+ table: media_field_data
+ field: name
+ relationship: none
+ group_type: group
+ admin_label: ''
+ operator: contains
+ value: ''
+ group: 1
+ exposed: true
+ expose:
+ operator_id: name_op
+ label: Name
+ description: ''
+ use_operator: false
+ operator: name_op
+ identifier: name
+ required: false
+ remember: false
+ multiple: false
+ remember_roles:
+ authenticated: authenticated
+ anonymous: '0'
+ administrator: '0'
+ is_grouped: false
+ group_info:
+ label: ''
+ description: ''
+ identifier: ''
+ optional: true
+ widget: select
+ multiple: false
+ remember: false
+ default_group: All
+ default_group_multiple: { }
+ group_items: { }
+ entity_type: media
+ entity_field: name
+ plugin_id: string
+ bundle:
+ id: bundle
+ table: media_field_data
+ field: bundle
+ relationship: none
+ group_type: group
+ admin_label: ''
+ operator: in
+ value: { }
+ group: 1
+ exposed: true
+ expose:
+ operator_id: bundle_op
+ label: 'Media type'
+ description: ''
+ use_operator: false
+ operator: bundle_op
+ identifier: type
+ required: false
+ remember: false
+ multiple: false
+ remember_roles:
+ authenticated: authenticated
+ anonymous: '0'
+ administrator: '0'
+ reduce: false
+ is_grouped: false
+ group_info:
+ label: 'Media type'
+ description: null
+ identifier: bundle
+ optional: true
+ widget: select
+ multiple: false
+ remember: false
+ default_group: All
+ default_group_multiple: { }
+ group_items:
+ 1: { }
+ 2: { }
+ 3: { }
+ entity_type: media
+ entity_field: bundle
+ plugin_id: bundle
+ sorts:
+ created:
+ id: created
+ table: media_field_data
+ field: created
+ relationship: none
+ group_type: group
+ admin_label: ''
+ order: DESC
+ exposed: true
+ expose:
+ label: 'Newest first'
+ granularity: second
+ entity_type: media
+ entity_field: created
+ plugin_id: date
+ name:
+ id: name
+ table: media_field_data
+ field: name
+ relationship: none
+ group_type: group
+ admin_label: ''
+ order: ASC
+ exposed: true
+ expose:
+ label: 'Name (A-Z)'
+ entity_type: media
+ entity_field: name
+ plugin_id: standard
+ name_1:
+ id: name_1
+ table: media_field_data
+ field: name
+ relationship: none
+ group_type: group
+ admin_label: ''
+ order: DESC
+ exposed: true
+ expose:
+ label: 'Name (Z-A)'
+ entity_type: media
+ entity_field: name
+ plugin_id: standard
+ title: Media
+ header: { }
+ footer: { }
+ empty: { }
+ relationships: { }
+ arguments:
+ bundle:
+ id: bundle
+ table: media_field_data
+ field: bundle
+ relationship: none
+ group_type: group
+ admin_label: ''
+ default_action: ignore
+ exception:
+ value: all
+ title_enable: false
+ title: All
+ title_enable: false
+ title: ''
+ default_argument_type: fixed
+ default_argument_options:
+ argument: ''
+ default_argument_skip_url: false
+ summary_options:
+ base_path: ''
+ count: true
+ items_per_page: 25
+ override: false
+ summary:
+ sort_order: asc
+ number_of_records: 0
+ format: default_summary
+ specify_validation: false
+ validate:
+ type: none
+ fail: 'not found'
+ validate_options: { }
+ glossary: false
+ limit: 0
+ case: none
+ path_case: none
+ transform_dash: false
+ break_phrase: false
+ entity_type: media
+ entity_field: bundle
+ plugin_id: string
+ display_extenders: { }
+ use_ajax: true
+ css_class: media-library-view
+ cache_metadata:
+ max-age: 0
+ contexts:
+ - 'languages:language_interface'
+ - url
+ - url.query_args
+ - 'url.query_args:sort_by'
+ - user.permissions
+ tags: { }
+ page:
+ display_plugin: page
+ id: page
+ display_title: Page
+ position: 1
+ display_options:
+ display_extenders: { }
+ path: admin/content/media
+ menu:
+ type: tab
+ title: Media
+ description: 'Allows users to browse and administer media items'
+ expanded: false
+ parent: system.admin_content
+ weight: 5
+ context: '0'
+ menu_name: admin
+ cache_metadata:
+ max-age: 0
+ contexts:
+ - 'languages:language_interface'
+ - url
+ - url.query_args
+ - 'url.query_args:sort_by'
+ - user.permissions
+ tags: { }
diff --git a/core/modules/media_library/config/optional/core.entity_view_display.media.audio.media_library.yml b/core/modules/media_library/config/optional/core.entity_view_display.media.audio.media_library.yml
new file mode 100644
index 000000000000..e74e3ebde6b2
--- /dev/null
+++ b/core/modules/media_library/config/optional/core.entity_view_display.media.audio.media_library.yml
@@ -0,0 +1,29 @@
+langcode: en
+status: true
+dependencies:
+ config:
+ - core.entity_view_mode.media.media_library
+ - field.field.media.audio.field_media_audio_file
+ - image.style.thumbnail
+ - media.type.audio
+ module:
+ - image
+id: media.audio.media_library
+targetEntityType: media
+bundle: audio
+mode: media_library
+content:
+ thumbnail:
+ type: image
+ weight: 0
+ region: content
+ label: hidden
+ settings:
+ image_style: thumbnail
+ image_link: ''
+ third_party_settings: { }
+hidden:
+ created: true
+ field_media_audio_file: true
+ name: true
+ uid: true
diff --git a/core/modules/media_library/config/optional/core.entity_view_display.media.file.media_library.yml b/core/modules/media_library/config/optional/core.entity_view_display.media.file.media_library.yml
new file mode 100644
index 000000000000..e09e611b9550
--- /dev/null
+++ b/core/modules/media_library/config/optional/core.entity_view_display.media.file.media_library.yml
@@ -0,0 +1,29 @@
+langcode: en
+status: true
+dependencies:
+ config:
+ - core.entity_view_mode.media.media_library
+ - field.field.media.file.field_media_file
+ - image.style.thumbnail
+ - media.type.file
+ module:
+ - image
+id: media.file.media_library
+targetEntityType: media
+bundle: file
+mode: media_library
+content:
+ thumbnail:
+ type: image
+ weight: 0
+ region: content
+ label: hidden
+ settings:
+ image_style: thumbnail
+ image_link: ''
+ third_party_settings: { }
+hidden:
+ created: true
+ field_media_file: true
+ name: true
+ uid: true
diff --git a/core/modules/media_library/config/optional/core.entity_view_display.media.image.media_library.yml b/core/modules/media_library/config/optional/core.entity_view_display.media.image.media_library.yml
new file mode 100644
index 000000000000..a916760ad99b
--- /dev/null
+++ b/core/modules/media_library/config/optional/core.entity_view_display.media.image.media_library.yml
@@ -0,0 +1,29 @@
+langcode: en
+status: true
+dependencies:
+ config:
+ - core.entity_view_mode.media.media_library
+ - field.field.media.image.field_media_image
+ - image.style.medium
+ - media.type.image
+ module:
+ - image
+id: media.image.media_library
+targetEntityType: media
+bundle: image
+mode: media_library
+content:
+ thumbnail:
+ type: image
+ weight: 0
+ region: content
+ label: hidden
+ settings:
+ image_style: medium
+ image_link: ''
+ third_party_settings: { }
+hidden:
+ created: true
+ field_media_image: true
+ name: true
+ uid: true
diff --git a/core/modules/media_library/config/optional/core.entity_view_display.media.video.media_library.yml b/core/modules/media_library/config/optional/core.entity_view_display.media.video.media_library.yml
new file mode 100644
index 000000000000..33d4e8855553
--- /dev/null
+++ b/core/modules/media_library/config/optional/core.entity_view_display.media.video.media_library.yml
@@ -0,0 +1,29 @@
+langcode: en
+status: true
+dependencies:
+ config:
+ - core.entity_view_mode.media.media_library
+ - field.field.media.video.field_media_video_file
+ - image.style.thumbnail
+ - media.type.video
+ module:
+ - image
+id: media.video.media_library
+targetEntityType: media
+bundle: video
+mode: media_library
+content:
+ thumbnail:
+ type: image
+ weight: 0
+ region: content
+ label: hidden
+ settings:
+ image_style: thumbnail
+ image_link: ''
+ third_party_settings: { }
+hidden:
+ created: true
+ field_media_video_file: true
+ name: true
+ uid: true
diff --git a/core/modules/media_library/css/media_library.module.css b/core/modules/media_library/css/media_library.module.css
new file mode 100644
index 000000000000..11ad56dd922e
--- /dev/null
+++ b/core/modules/media_library/css/media_library.module.css
@@ -0,0 +1,60 @@
+/**
+* @file media_library.module.css
+*/
+
+.media-library-page-form {
+ display: flex;
+ flex-wrap: wrap;
+}
+
+.media-library-page-form > .form-actions {
+ flex-basis: 100%;
+}
+
+.media-library-page-form__header > div,
+.media-library-view .form--inline {
+ display: flex;
+ flex-wrap: wrap;
+}
+
+.media-library-page-form__header {
+ flex-basis: 100%;
+}
+
+.media-library-item {
+ position: relative;
+}
+
+.media-library-item .js-click-to-select__trigger {
+ overflow: hidden;
+ cursor: pointer;
+}
+
+.media-library-view .form-actions {
+ align-self: flex-end;
+}
+
+.media-library-item .js-click-to-select__checkbox {
+ position: absolute;
+ display: block;
+ z-index: 1;
+ top: 5px;
+ right: 0;
+}
+
+.media-library-item__status {
+ position: absolute;
+ top: 10px;
+ left: 2px;
+ pointer-events: none;
+}
+
+.media-library-select-all {
+ flex-basis: 100%;
+}
+
+@media screen and (max-width: 600px) {
+ .media-library-view .form-actions {
+ flex-basis: 100%;
+ }
+}
diff --git a/core/modules/media_library/css/media_library.theme.css b/core/modules/media_library/css/media_library.theme.css
new file mode 100644
index 000000000000..0427143538e3
--- /dev/null
+++ b/core/modules/media_library/css/media_library.theme.css
@@ -0,0 +1,151 @@
+/**
+ * @file media_library.theme.css
+ *
+ * @todo Move into the Seven theme when this module is marked as stable.
+ * @see https://www.drupal.org/project/drupal/issues/2980769
+ */
+
+.media-library-page-form__header .form-item {
+ margin-right: 8px;
+}
+
+#drupal-modal .view-header {
+ margin: 16px 0;
+}
+
+.media-library-item {
+ justify-content: center;
+ vertical-align: top;
+ padding: 2px;
+ border: 1px solid #ebebeb;
+ margin: 16px 16px 2px 2px;
+ width: 180px;
+ background: #fff;
+ transition: border-color 0.2s, color 0.2s, background 0.2s;
+}
+
+.media-library-view .form-actions {
+ margin: 0.75em 0;
+}
+
+.media-library-item .field--name-thumbnail {
+ background-color: #ebebeb;
+ margin: 2px;
+ overflow: hidden;
+ text-align: center;
+}
+
+.media-library-item .field--name-thumbnail img {
+ height: 180px;
+ object-fit: contain;
+ object-position: center center;
+}
+
+.media-library-item.is-hover,
+.media-library-item.checked,
+.media-library-item.is-focus {
+ border-color: #40b6ff;
+ border-width: 3px;
+ border-radius: 3px;
+ margin: 14px 14px 0 0;
+}
+
+.media-library-item.checked {
+ border-color: #0076c0;
+}
+
+.media-library-item .js-click-to-select__checkbox input {
+ width: 30px;
+ height: 30px;
+}
+
+.media-library-item .js-click-to-select__checkbox .form-item {
+ margin: 0;
+}
+
+.media-library-item__preview {
+ padding-bottom: 44px;
+}
+
+.media-library-item__status {
+ color: #e4e4e4;
+ font-style: italic;
+ background: #666;
+ padding: 5px 10px;
+ font-size: 12px;
+}
+
+.media-library-item .views-field-operations {
+ height: 30px;
+}
+
+.media-library-item .views-field-operations .dropbutton-wrapper {
+ display: inline-block;
+ position: absolute;
+ right: 5px;
+ bottom: 5px;
+}
+
+.media-library-item__attributes {
+ position: absolute;
+ bottom: 0;
+ display: block;
+ padding: 10px;
+ max-width: calc(100% - 20px);
+ max-height: calc(100% - 60px);
+ overflow: hidden;
+ background: white;
+}
+
+.media-library-item__name {
+ font-size: 14px;
+}
+
+.media-library-item__name a {
+ display: block;
+ text-decoration: underline;
+ margin: 2px;
+ white-space: nowrap;
+ overflow: hidden;
+ text-overflow: ellipsis;
+}
+
+.media-library-item__attributes:hover .media-library-item__name a,
+.media-library-item__name a:focus,
+.media-library-item.is-focus .media-library-item__name a,
+.media-library-item.checked .media-library-item__name a {
+ white-space: normal;
+}
+
+.media-library-item__name a:focus {
+ border: 2px solid;
+ margin: 0;
+}
+
+.media-library-item__type {
+ font-size: 12px;
+ color: #696969;
+}
+
+.media-library-select-all {
+ margin: 10px 0 10px 0;
+}
+
+.media-library-select-all input {
+ margin-right: 10px;
+}
+
+@media screen and (max-width: 600px) {
+ .media-library-item {
+ width: 150px;
+ }
+ .media-library-item .field--name-thumbnail img {
+ height: 150px;
+ width: 150px;
+ }
+ .media-library-item .views-field-operations .dropbutton-wrapper {
+ position: relative;
+ right: 0;
+ border: 0;
+ }
+}
diff --git a/core/modules/media_library/js/media_library.click_to_select.es6.js b/core/modules/media_library/js/media_library.click_to_select.es6.js
new file mode 100644
index 000000000000..321ffe0c73a9
--- /dev/null
+++ b/core/modules/media_library/js/media_library.click_to_select.es6.js
@@ -0,0 +1,31 @@
+/**
+ * @file media_library.click_to_select.es6.js
+ */
+
+(($, Drupal) => {
+ /**
+ * Allows users to select an element which checks a hidden checkbox.
+ */
+ Drupal.behaviors.ClickToSelect = {
+ attach(context) {
+ $('.js-click-to-select__trigger', context)
+ .once('media-library-click-to-select')
+ .on('click', (event) => {
+ // Links inside the trigger should not be click-able.
+ event.preventDefault();
+ // Click the hidden checkbox when the trigger is clicked.
+ const $input = $(event.currentTarget)
+ .closest('.js-click-to-select')
+ .find('.js-click-to-select__checkbox input');
+ $input.prop('checked', !$input.prop('checked')).trigger('change');
+ });
+ $('.js-click-to-select__checkbox input', context)
+ .once('media-library-click-to-select')
+ .on('change', ({ currentTarget }) => {
+ $(currentTarget)
+ .closest('.js-click-to-select')
+ .toggleClass('checked', $(currentTarget).prop('checked'));
+ });
+ },
+ };
+})(jQuery, Drupal);
diff --git a/core/modules/media_library/js/media_library.click_to_select.js b/core/modules/media_library/js/media_library.click_to_select.js
new file mode 100644
index 000000000000..4bc041c43e56
--- /dev/null
+++ b/core/modules/media_library/js/media_library.click_to_select.js
@@ -0,0 +1,24 @@
+/**
+* DO NOT EDIT THIS FILE.
+* See the following change record for more information,
+* https://www.drupal.org/node/2815083
+* @preserve
+**/
+
+(function ($, Drupal) {
+ Drupal.behaviors.ClickToSelect = {
+ attach: function attach(context) {
+ $('.js-click-to-select__trigger', context).once('media-library-click-to-select').on('click', function (event) {
+ event.preventDefault();
+
+ var $input = $(event.currentTarget).closest('.js-click-to-select').find('.js-click-to-select__checkbox input');
+ $input.prop('checked', !$input.prop('checked')).trigger('change');
+ });
+ $('.js-click-to-select__checkbox input', context).once('media-library-click-to-select').on('change', function (_ref) {
+ var currentTarget = _ref.currentTarget;
+
+ $(currentTarget).closest('.js-click-to-select').toggleClass('checked', $(currentTarget).prop('checked'));
+ });
+ }
+ };
+})(jQuery, Drupal);
\ No newline at end of file
diff --git a/core/modules/media_library/js/media_library.view.es6.js b/core/modules/media_library/js/media_library.view.es6.js
new file mode 100644
index 000000000000..26e9a5b2635e
--- /dev/null
+++ b/core/modules/media_library/js/media_library.view.es6.js
@@ -0,0 +1,58 @@
+/**
+ * @file media_library.view.es6.js
+ */
+(($, Drupal) => {
+ /**
+ * Adds hover effect to media items.
+ */
+ Drupal.behaviors.MediaLibraryHover = {
+ attach(context) {
+ $('.media-library-item .js-click-to-select__trigger,.media-library-item .js-click-to-select__checkbox', context).once('media-library-item-hover')
+ .on('mouseover mouseout', ({ currentTarget, type }) => {
+ $(currentTarget).closest('.media-library-item').toggleClass('is-hover', type === 'mouseover');
+ });
+ },
+ };
+
+ /**
+ * Adds focus effect to media items.
+ */
+ Drupal.behaviors.MediaLibraryFocus = {
+ attach(context) {
+ $('.media-library-item .js-click-to-select__checkbox input', context).once('media-library-item-focus')
+ .on('focus blur', ({ currentTarget, type }) => {
+ $(currentTarget).closest('.media-library-item').toggleClass('is-focus', type === 'focus');
+ });
+ },
+ };
+
+ /**
+ * Adds checkbox to select all items in the library.
+ */
+ Drupal.behaviors.MediaLibrarySelectAll = {
+ attach(context) {
+ const $view = $('.media-library-view', context).once('media-library-select-all');
+ if ($view.length && $view.find('.media-library-item').length) {
+ const $checkbox = $('')
+ .on('click', ({ currentTarget }) => {
+ // Toggle all checkboxes.
+ const $checkboxes = $(currentTarget)
+ .closest('.media-library-view')
+ .find('.media-library-item input[type="checkbox"]');
+ $checkboxes
+ .prop('checked', $(currentTarget).prop('checked'))
+ .trigger('change');
+ // Announce the selection.
+ const announcement = $(currentTarget).prop('checked') ?
+ Drupal.t('Zero items selected') :
+ Drupal.t('All @count items selected', { '@count': $checkboxes.length });
+ Drupal.announce(announcement);
+ });
+ const $label = $('')
+ .text(Drupal.t('Select all media'));
+ $label.prepend($checkbox);
+ $view.find('.media-library-item').first().before($label);
+ }
+ },
+ };
+})(jQuery, Drupal);
diff --git a/core/modules/media_library/js/media_library.view.js b/core/modules/media_library/js/media_library.view.js
new file mode 100644
index 000000000000..1cde60c3acfb
--- /dev/null
+++ b/core/modules/media_library/js/media_library.view.js
@@ -0,0 +1,50 @@
+/**
+* DO NOT EDIT THIS FILE.
+* See the following change record for more information,
+* https://www.drupal.org/node/2815083
+* @preserve
+**/
+
+(function ($, Drupal) {
+ Drupal.behaviors.MediaLibraryHover = {
+ attach: function attach(context) {
+ $('.media-library-item .js-click-to-select__trigger,.media-library-item .js-click-to-select__checkbox', context).once('media-library-item-hover').on('mouseover mouseout', function (_ref) {
+ var currentTarget = _ref.currentTarget,
+ type = _ref.type;
+
+ $(currentTarget).closest('.media-library-item').toggleClass('is-hover', type === 'mouseover');
+ });
+ }
+ };
+
+ Drupal.behaviors.MediaLibraryFocus = {
+ attach: function attach(context) {
+ $('.media-library-item .js-click-to-select__checkbox input', context).once('media-library-item-focus').on('focus blur', function (_ref2) {
+ var currentTarget = _ref2.currentTarget,
+ type = _ref2.type;
+
+ $(currentTarget).closest('.media-library-item').toggleClass('is-focus', type === 'focus');
+ });
+ }
+ };
+
+ Drupal.behaviors.MediaLibrarySelectAll = {
+ attach: function attach(context) {
+ var $view = $('.media-library-view', context).once('media-library-select-all');
+ if ($view.length && $view.find('.media-library-item').length) {
+ var $checkbox = $('').on('click', function (_ref3) {
+ var currentTarget = _ref3.currentTarget;
+
+ var $checkboxes = $(currentTarget).closest('.media-library-view').find('.media-library-item input[type="checkbox"]');
+ $checkboxes.prop('checked', $(currentTarget).prop('checked')).trigger('change');
+
+ var announcement = $(currentTarget).prop('checked') ? Drupal.t('Zero items selected') : Drupal.t('All @count items selected', { '@count': $checkboxes.length });
+ Drupal.announce(announcement);
+ });
+ var $label = $('').text(Drupal.t('Select all media'));
+ $label.prepend($checkbox);
+ $view.find('.media-library-item').first().before($label);
+ }
+ }
+ };
+})(jQuery, Drupal);
\ No newline at end of file
diff --git a/core/modules/media_library/media_library.info.yml b/core/modules/media_library/media_library.info.yml
new file mode 100644
index 000000000000..9f7aa912e405
--- /dev/null
+++ b/core/modules/media_library/media_library.info.yml
@@ -0,0 +1,10 @@
+name: 'Media library'
+type: module
+description: 'Provides a library for re-using Media Items.'
+package: Core (Experimental)
+version: VERSION
+core: 8.x
+dependencies:
+ - drupal:media
+ - drupal:views
+ - drupal:user
diff --git a/core/modules/media_library/media_library.install b/core/modules/media_library/media_library.install
new file mode 100644
index 000000000000..79a97868b7b6
--- /dev/null
+++ b/core/modules/media_library/media_library.install
@@ -0,0 +1,49 @@
+getDisplay('media_page_list');
+ if (!empty($display)) {
+ $display['display_options']['path'] = 'admin/content/media-table';
+ unset($display['display_options']['menu']);
+ $view->trustData()->save();
+ }
+ }
+}
+
+/**
+ * Implements hook_uninstall().
+ */
+function media_library_uninstall() {
+ // Restore the path to the original media view.
+ /** @var \Drupal\views\Entity\View $view */
+ if ($view = View::load('media')) {
+ $display = &$view->getDisplay('media_page_list');
+ if (!empty($display)) {
+ $display['display_options']['path'] = 'admin/content/media';
+ $display['display_options']['menu'] = [
+ 'type' => 'tab',
+ 'title' => 'Media',
+ 'description' => '',
+ 'expanded' => FALSE,
+ 'parent' => '',
+ 'weight' => 0,
+ 'context' => '0',
+ 'menu_name' => 'main',
+ ];
+ $view->trustData()->save();
+ }
+ }
+}
diff --git a/core/modules/media_library/media_library.libraries.yml b/core/modules/media_library/media_library.libraries.yml
new file mode 100644
index 000000000000..e32dbe074b49
--- /dev/null
+++ b/core/modules/media_library/media_library.libraries.yml
@@ -0,0 +1,25 @@
+style:
+ version: VERSION
+ css:
+ component:
+ css/media_library.module.css: {}
+ theme:
+ css/media_library.theme.css: {}
+
+click_to_select:
+ version: VERSION
+ js:
+ js/media_library.click_to_select.js: {}
+ dependencies:
+ - core/drupal
+ - core/jquery.once
+
+view:
+ version: VERSION
+ js:
+ js/media_library.view.js: {}
+ dependencies:
+ - media_library/style
+ - media_library/click_to_select
+ - core/drupal.announce
+ - core/jquery.once
diff --git a/core/modules/media_library/media_library.links.action.yml b/core/modules/media_library/media_library.links.action.yml
new file mode 100644
index 000000000000..6a37769cf17f
--- /dev/null
+++ b/core/modules/media_library/media_library.links.action.yml
@@ -0,0 +1,5 @@
+media_library.add:
+ route_name: entity.media.add_page
+ title: 'Add media'
+ appears_on:
+ - view.media_library.page
diff --git a/core/modules/media_library/media_library.links.task.yml b/core/modules/media_library/media_library.links.task.yml
new file mode 100644
index 000000000000..757ecd853249
--- /dev/null
+++ b/core/modules/media_library/media_library.links.task.yml
@@ -0,0 +1,10 @@
+media_library.grid:
+ title: 'Grid'
+ parent_id: entity.media.collection
+ route_name: entity.media.collection
+ weight: 10
+media_library.table:
+ title: 'Table'
+ parent_id: entity.media.collection
+ route_name: view.media.media_page_list
+ weight: 20
diff --git a/core/modules/media_library/media_library.module b/core/modules/media_library/media_library.module
new file mode 100644
index 000000000000..63b3cadb96a8
--- /dev/null
+++ b/core/modules/media_library/media_library.module
@@ -0,0 +1,109 @@
+' . t('About') . '';
+ $output .= '
' . t('The Media library module overrides the /admin/content/media view to provide a rich visual interface for performing administrative operations on media. For more information, see the online documentation for the Media library module.', [':media' => 'https://www.drupal.org/docs/8/core/modules/media']) . '
';
+ return $output;
+ }
+}
+
+/**
+ * Implements hook_theme().
+ */
+function media_library_theme() {
+ return [
+ 'media__media_library' => [
+ 'base hook' => 'media',
+ ],
+ ];
+}
+
+/**
+ * Implements hook_views_post_render().
+ */
+function media_library_views_post_render(ViewExecutable $view, &$output, CachePluginBase $cache) {
+ if ($view->id() === 'media_library') {
+ $output['#attached']['library'][] = 'media_library/view';
+ }
+}
+
+/**
+ * Implements hook_preprocess_media().
+ */
+function media_library_preprocess_media(&$variables) {
+ if ($variables['view_mode'] === 'media_library') {
+ /** @var \Drupal\media\MediaInterface $media */
+ $media = $variables['media'];
+ $variables['#cache']['contexts'][] = 'user.permissions';
+ $rel = $media->access('edit') ? 'edit-form' : 'canonical';
+ $variables['url'] = $media->toUrl($rel, [
+ 'language' => $media->language(),
+ ]);
+ $variables['preview_attributes'] = new Attribute();
+ $variables['preview_attributes']->addClass('media-library-item__preview', 'js-click-to-select__trigger');
+ $variables['metadata_attributes'] = new Attribute();
+ $variables['metadata_attributes']->addClass('media-library-item__attributes');
+ $variables['status'] = $media->isPublished();
+ }
+}
+
+/**
+ * Alter the bulk form to add a more accessible label.
+ *
+ * @param array $form
+ * An associative array containing the structure of the form.
+ * @param \Drupal\Core\Form\FormStateInterface $form_state
+ * The current state of the form.
+ *
+ * @todo Remove in https://www.drupal.org/project/drupal/issues/2969660
+ */
+function media_library_form_views_form_media_library_page_alter(array &$form, FormStateInterface $form_state) {
+ if (isset($form['media_bulk_form']) && isset($form['output'])) {
+ $form['#attributes']['class'][] = 'media-library-page-form';
+ $form['header']['#attributes']['class'][] = 'media-library-page-form__header';
+ /** @var \Drupal\views\ViewExecutable $view */
+ $view = $form['output'][0]['#view'];
+ foreach (Element::getVisibleChildren($form['media_bulk_form']) as $key) {
+ if (isset($view->result[$key])) {
+ $media = $view->field['media_bulk_form']->getEntity($view->result[$key]);
+ $form['media_bulk_form'][$key]['#title'] = t('Select @label', [
+ '@label' => $media->label(),
+ ]);
+ }
+ }
+ }
+}
+
+/**
+ * Implements hook_local_tasks_alter().
+ *
+ * Removes tasks for the Media library if the view display no longer exists.
+ */
+function media_library_local_tasks_alter(&$local_tasks) {
+ /** @var \Symfony\Component\Routing\RouteCollection $route_collection */
+ $route_collection = \Drupal::service('router')->getRouteCollection();
+ foreach (['media_library.grid', 'media_library.table'] as $key) {
+ if (isset($local_tasks[$key]) && !$route_collection->get($local_tasks[$key]['route_name'])) {
+ unset($local_tasks[$key]);
+ }
+ }
+}
diff --git a/core/modules/media_library/templates/media--media-library.html.twig b/core/modules/media_library/templates/media--media-library.html.twig
new file mode 100644
index 000000000000..a55024a22d2d
--- /dev/null
+++ b/core/modules/media_library/templates/media--media-library.html.twig
@@ -0,0 +1,50 @@
+{#
+/**
+ * @file
+ * Default theme implementation to present a media entity in the media library.
+ *
+ * Available variables:
+ * - media: The entity with limited access to object properties and methods.
+ * Only method names starting with "get", "has", or "is" and a few common
+ * methods such as "id", "label", and "bundle" are available. For example:
+ * - entity.getEntityTypeId() will return the entity type ID.
+ * - entity.hasField('field_example') returns TRUE if the entity includes
+ * field_example. (This does not indicate the presence of a value in this
+ * field.)
+ * Calling other methods, such as entity.delete(), will result in an exception.
+ * See \Drupal\Core\Entity\EntityInterface for a full list of methods.
+ * - name: Name of the media.
+ * - content: Media content.
+ * - title_prefix: Additional output populated by modules, intended to be
+ * displayed in front of the main title tag that appears in the template.
+ * - title_suffix: Additional output populated by modules, intended to be
+ * displayed after the main title tag that appears in the template.
+ * - view_mode: View mode; for example, "teaser" or "full".
+ * - attributes: HTML attributes for the containing element.
+ * - title_attributes: Same as attributes, except applied to the main title
+ * tag that appears in the template.
+ * - url: Direct URL of the media.
+ * - preview_attributes: HTML attributes for the preview wrapper.
+ * - metadata_attributes: HTML attributes for the expandable metadata area.
+ * - status: Whether or not the Media is published.
+ *
+ * @see template_preprocess_media()
+ *
+ * @ingroup themeable
+ */
+#}
+
+ {% if content %}
+