comparison core/modules/views/src/Plugin/ViewsHandlerManager.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\views\Plugin;
4
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;
12
13 /**
14 * Plugin type manager for all views handlers.
15 */
16 class ViewsHandlerManager extends DefaultPluginManager implements FallbackPluginManagerInterface {
17
18 /**
19 * The views data cache.
20 *
21 * @var \Drupal\views\ViewsData
22 */
23 protected $viewsData;
24
25 /**
26 * The handler type.
27 *
28 * @var string
29 *
30 * @see \Drupal\views\ViewExecutable::getHandlerTypes().
31 */
32 protected $handlerType;
33
34 /**
35 * Constructs a ViewsHandlerManager object.
36 *
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.
48 */
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';
54 }
55 parent::__construct("Plugin/views/$handler_type", $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name);
56
57 $this->setCacheBackend($cache_backend, "views:$handler_type");
58 $this->alterInfo('views_plugins_' . $handler_type);
59
60 $this->viewsData = $views_data;
61 $this->handlerType = $handler_type;
62 $this->defaults = [
63 'plugin_type' => $handler_type,
64 ];
65 }
66
67 /**
68 * Fetches a handler from the data cache.
69 *
70 * @param array $item
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.
77 *
78 * @return \Drupal\views\Plugin\views\ViewsHandlerInterface
79 * An instance of a handler object. May be a broken handler instance.
80 */
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);
86
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];
94 }
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];
99 }
100 }
101 }
102
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);
109 }
110 return $handler;
111 }
112
113 // Finally, use the 'broken' handler.
114 return $this->createInstance('broken', ['original_configuration' => $item]);
115 }
116
117 /**
118 * {@inheritdoc}
119 */
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);
125 }
126 return $instance;
127 }
128
129 /**
130 * {@inheritdoc}
131 */
132 public function getFallbackPluginId($plugin_id, array $configuration = []) {
133 return 'broken';
134 }
135
136 }