3 namespace Drupal\views\Plugin;
5 use Drupal\Component\Plugin\FallbackPluginManagerInterface;
6 use Drupal\Core\Cache\CacheBackendInterface;
7 use Drupal\Core\Extension\ModuleHandlerInterface;
8 use Drupal\Core\Plugin\DefaultPluginManager;
9 use Drupal\views\ViewsData;
10 use Symfony\Component\DependencyInjection\Container;
11 use Drupal\views\Plugin\views\HandlerBase;
14 * Plugin type manager for all views handlers.
16 class ViewsHandlerManager extends DefaultPluginManager implements FallbackPluginManagerInterface {
19 * The views data cache.
21 * @var \Drupal\views\ViewsData
30 * @see \Drupal\views\ViewExecutable::getHandlerTypes().
32 protected $handlerType;
35 * Constructs a ViewsHandlerManager object.
37 * @param string $handler_type
38 * The plugin type, for example filter.
39 * @param \Traversable $namespaces
40 * An object that implements \Traversable which contains the root paths
41 * keyed by the corresponding namespace to look for plugin implementations,
42 * @param \Drupal\views\ViewsData $views_data
43 * The views data cache.
44 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
45 * Cache backend instance to use.
46 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
47 * The module handler to invoke the alter hook with.
49 public function __construct($handler_type, \Traversable $namespaces, ViewsData $views_data, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
50 $plugin_definition_annotation_name = 'Drupal\views\Annotation\Views' . Container::camelize($handler_type);
51 $plugin_interface = 'Drupal\views\Plugin\views\ViewsHandlerInterface';
52 if ($handler_type == 'join') {
53 $plugin_interface = 'Drupal\views\Plugin\views\join\JoinPluginInterface';
55 parent::__construct("Plugin/views/$handler_type", $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name);
57 $this->setCacheBackend($cache_backend, "views:$handler_type");
58 $this->alterInfo('views_plugins_' . $handler_type);
60 $this->viewsData = $views_data;
61 $this->handlerType = $handler_type;
63 'plugin_type' => $handler_type,
68 * Fetches a handler from the data cache.
71 * An associative array representing the handler to be retrieved:
72 * - table: The name of the table containing the handler.
73 * - field: The name of the field the handler represents.
74 * @param string|null $override
75 * (optional) Override the actual handler object with this plugin ID. Used for
76 * aggregation when the handler is redirected to the aggregation handler.
78 * @return \Drupal\views\Plugin\views\ViewsHandlerInterface
79 * An instance of a handler object. May be a broken handler instance.
81 public function getHandler($item, $override = NULL) {
82 $table = $item['table'];
83 $field = $item['field'];
84 // Get the plugin manager for this type.
85 $data = $this->viewsData->get($table);
87 if (isset($data[$field][$this->handlerType])) {
88 $definition = $data[$field][$this->handlerType];
89 foreach (['group', 'title', 'title short', 'label', 'help', 'real field', 'real table', 'entity type', 'entity field'] as $key) {
90 if (!isset($definition[$key])) {
91 // First check the field level.
92 if (!empty($data[$field][$key])) {
93 $definition[$key] = $data[$field][$key];
95 // Then if that doesn't work, check the table level.
96 elseif (!empty($data['table'][$key])) {
97 $definition_key = $key === 'entity type' ? 'entity_type' : $key;
98 $definition[$definition_key] = $data['table'][$key];
103 // @todo This is crazy. Find a way to remove the override functionality.
104 $plugin_id = $override ?: $definition['id'];
105 // Try to use the overridden handler.
106 $handler = $this->createInstance($plugin_id, $definition);
107 if ($override && method_exists($handler, 'broken') && $handler->broken()) {
108 $handler = $this->createInstance($definition['id'], $definition);
113 // Finally, use the 'broken' handler.
114 return $this->createInstance('broken', ['original_configuration' => $item]);
120 public function createInstance($plugin_id, array $configuration = []) {
121 $instance = parent::createInstance($plugin_id, $configuration);
122 if ($instance instanceof HandlerBase) {
123 $instance->setModuleHandler($this->moduleHandler);
124 $instance->setViewsData($this->viewsData);
132 public function getFallbackPluginId($plugin_id, array $configuration = []) {