Commit 099e0fa7 authored by alexpott's avatar alexpott

Issue #1822048 by dawehner, tim.plunkett, damiankloip, EclipseGc, martin107,...

Issue #1822048 by dawehner, tim.plunkett, damiankloip, EclipseGc, martin107, neclimdul: Introduce a generic fallback plugin mechanism.
parent bb3ee207
<?php
/**
* @file
* Contains FallbackPluginManagerInterface.php.
*/
namespace Drupal\Component\Plugin;
/**
* An interface implemented by plugin managers with fallback plugin behaviors.
*/
interface FallbackPluginManagerInterface {
/**
* Returns a fallback id for a missing plugin.
*
* @param string $plugin_id
* The ID of the missing requested plugin.
* @param array $configuration
* An array of configuration relevant to the plugin instance.
*
* @return string
* The id of an existing plugin to use for ... why isn't this in the factory?
*/
public function getFallbackPluginId($plugin_id, array $configuration = array());
}
......@@ -8,6 +8,7 @@
namespace Drupal\Component\Plugin;
use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
/**
* Base class for plugin managers.
......@@ -55,7 +56,20 @@ public function getDefinitions() {
* {@inheritdoc}
*/
public function createInstance($plugin_id, array $configuration = array()) {
return $this->factory->createInstance($plugin_id, $configuration);
// If this PluginManager has fallback capabilities catch
// PluginNotFoundExceptions.
if ($this instanceof FallbackPluginManagerInterface) {
try {
return $this->factory->createInstance($plugin_id, $configuration);
}
catch (PluginNotFoundException $e) {
$fallback_id = $this->getFallbackPluginId($plugin_id, $configuration);
return $this->factory->createInstance($fallback_id, $configuration);
}
}
else {
return $this->factory->createInstance($plugin_id, $configuration);
}
}
/**
......
......@@ -7,6 +7,7 @@
namespace Drupal\filter;
use Drupal\Component\Plugin\FallbackPluginManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
......@@ -20,7 +21,7 @@
* @see \Drupal\filter\Plugin\FilterBase
* @see plugin_api
*/
class FilterPluginManager extends DefaultPluginManager {
class FilterPluginManager extends DefaultPluginManager implements FallbackPluginManagerInterface {
/**
* Constructs a FilterPluginManager object.
......@@ -42,14 +43,8 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
/**
* {@inheritdoc}
*/
public function getDefinition($plugin_id, $exception_on_invalid = TRUE) {
$definitions = $this->getDefinitions();
// Avoid using a ternary that would create a copy of the array.
if (isset($definitions[$plugin_id])) {
return $definitions[$plugin_id];
}
// If the requested filter is missing, use the null filter.
return $definitions['filter_null'];
public function getFallbackPluginId($plugin_id, array $configuration = array()) {
return 'filter_null';
}
}
......@@ -7,17 +7,18 @@
namespace Drupal\views\Plugin;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\FallbackPluginManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\views\ViewsData;
use Symfony\Component\DependencyInjection\Container;
use Drupal\views\Plugin\views\HandlerBase;
/**
* Plugin type manager for all views handlers.
*/
class ViewsHandlerManager extends DefaultPluginManager {
class ViewsHandlerManager extends DefaultPluginManager implements FallbackPluginManagerInterface {
/**
* The views data cache.
......@@ -105,18 +106,11 @@ public function getHandler($item, $override = NULL) {
// @todo This is crazy. Find a way to remove the override functionality.
$plugin_id = $override ? : $definition['id'];
// Try to use the overridden handler.
try {
return $this->createInstance($plugin_id, $definition);
}
catch (PluginException $e) {
// If that fails, use the original handler.
try {
return $this->createInstance($definition['id'], $definition);
}
catch (PluginException $e) {
// Deliberately empty, this case is handled generically below.
}
$handler = $this->createInstance($plugin_id, $definition);
if ($override && method_exists($handler, 'broken') && $handler->broken()) {
$handler = $this->createInstance($definition['id'], $definition);
}
return $handler;
}
// Finally, use the 'broken' handler.
......@@ -135,4 +129,10 @@ public function createInstance($plugin_id, array $configuration = array()) {
return $instance;
}
/**
* {@inheritdoc}
*/
public function getFallbackPluginId($plugin_id, array $configuration = array()) {
return 'broken';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment