annotate core/modules/views/src/Plugin/ViewsHandlerManager.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\views\Plugin;
Chris@0 4
Chris@0 5 use Drupal\Component\Plugin\FallbackPluginManagerInterface;
Chris@0 6 use Drupal\Core\Cache\CacheBackendInterface;
Chris@0 7 use Drupal\Core\Extension\ModuleHandlerInterface;
Chris@0 8 use Drupal\Core\Plugin\DefaultPluginManager;
Chris@0 9 use Drupal\views\ViewsData;
Chris@0 10 use Symfony\Component\DependencyInjection\Container;
Chris@0 11 use Drupal\views\Plugin\views\HandlerBase;
Chris@0 12
Chris@0 13 /**
Chris@0 14 * Plugin type manager for all views handlers.
Chris@0 15 */
Chris@0 16 class ViewsHandlerManager extends DefaultPluginManager implements FallbackPluginManagerInterface {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * The views data cache.
Chris@0 20 *
Chris@0 21 * @var \Drupal\views\ViewsData
Chris@0 22 */
Chris@0 23 protected $viewsData;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The handler type.
Chris@0 27 *
Chris@0 28 * @var string
Chris@0 29 *
Chris@0 30 * @see \Drupal\views\ViewExecutable::getHandlerTypes().
Chris@0 31 */
Chris@0 32 protected $handlerType;
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Constructs a ViewsHandlerManager object.
Chris@0 36 *
Chris@0 37 * @param string $handler_type
Chris@0 38 * The plugin type, for example filter.
Chris@0 39 * @param \Traversable $namespaces
Chris@0 40 * An object that implements \Traversable which contains the root paths
Chris@0 41 * keyed by the corresponding namespace to look for plugin implementations,
Chris@0 42 * @param \Drupal\views\ViewsData $views_data
Chris@0 43 * The views data cache.
Chris@0 44 * @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
Chris@0 45 * Cache backend instance to use.
Chris@0 46 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
Chris@0 47 * The module handler to invoke the alter hook with.
Chris@0 48 */
Chris@0 49 public function __construct($handler_type, \Traversable $namespaces, ViewsData $views_data, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler) {
Chris@0 50 $plugin_definition_annotation_name = 'Drupal\views\Annotation\Views' . Container::camelize($handler_type);
Chris@0 51 $plugin_interface = 'Drupal\views\Plugin\views\ViewsHandlerInterface';
Chris@0 52 if ($handler_type == 'join') {
Chris@0 53 $plugin_interface = 'Drupal\views\Plugin\views\join\JoinPluginInterface';
Chris@0 54 }
Chris@0 55 parent::__construct("Plugin/views/$handler_type", $namespaces, $module_handler, $plugin_interface, $plugin_definition_annotation_name);
Chris@0 56
Chris@0 57 $this->setCacheBackend($cache_backend, "views:$handler_type");
Chris@0 58 $this->alterInfo('views_plugins_' . $handler_type);
Chris@0 59
Chris@0 60 $this->viewsData = $views_data;
Chris@0 61 $this->handlerType = $handler_type;
Chris@0 62 $this->defaults = [
Chris@0 63 'plugin_type' => $handler_type,
Chris@0 64 ];
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * Fetches a handler from the data cache.
Chris@0 69 *
Chris@0 70 * @param array $item
Chris@0 71 * An associative array representing the handler to be retrieved:
Chris@0 72 * - table: The name of the table containing the handler.
Chris@0 73 * - field: The name of the field the handler represents.
Chris@0 74 * @param string|null $override
Chris@0 75 * (optional) Override the actual handler object with this plugin ID. Used for
Chris@0 76 * aggregation when the handler is redirected to the aggregation handler.
Chris@0 77 *
Chris@0 78 * @return \Drupal\views\Plugin\views\ViewsHandlerInterface
Chris@0 79 * An instance of a handler object. May be a broken handler instance.
Chris@0 80 */
Chris@0 81 public function getHandler($item, $override = NULL) {
Chris@0 82 $table = $item['table'];
Chris@0 83 $field = $item['field'];
Chris@0 84 // Get the plugin manager for this type.
Chris@0 85 $data = $this->viewsData->get($table);
Chris@0 86
Chris@0 87 if (isset($data[$field][$this->handlerType])) {
Chris@0 88 $definition = $data[$field][$this->handlerType];
Chris@0 89 foreach (['group', 'title', 'title short', 'label', 'help', 'real field', 'real table', 'entity type', 'entity field'] as $key) {
Chris@0 90 if (!isset($definition[$key])) {
Chris@0 91 // First check the field level.
Chris@0 92 if (!empty($data[$field][$key])) {
Chris@0 93 $definition[$key] = $data[$field][$key];
Chris@0 94 }
Chris@0 95 // Then if that doesn't work, check the table level.
Chris@0 96 elseif (!empty($data['table'][$key])) {
Chris@0 97 $definition_key = $key === 'entity type' ? 'entity_type' : $key;
Chris@0 98 $definition[$definition_key] = $data['table'][$key];
Chris@0 99 }
Chris@0 100 }
Chris@0 101 }
Chris@0 102
Chris@0 103 // @todo This is crazy. Find a way to remove the override functionality.
Chris@0 104 $plugin_id = $override ?: $definition['id'];
Chris@0 105 // Try to use the overridden handler.
Chris@0 106 $handler = $this->createInstance($plugin_id, $definition);
Chris@0 107 if ($override && method_exists($handler, 'broken') && $handler->broken()) {
Chris@0 108 $handler = $this->createInstance($definition['id'], $definition);
Chris@0 109 }
Chris@0 110 return $handler;
Chris@0 111 }
Chris@0 112
Chris@0 113 // Finally, use the 'broken' handler.
Chris@0 114 return $this->createInstance('broken', ['original_configuration' => $item]);
Chris@0 115 }
Chris@0 116
Chris@0 117 /**
Chris@0 118 * {@inheritdoc}
Chris@0 119 */
Chris@0 120 public function createInstance($plugin_id, array $configuration = []) {
Chris@0 121 $instance = parent::createInstance($plugin_id, $configuration);
Chris@0 122 if ($instance instanceof HandlerBase) {
Chris@0 123 $instance->setModuleHandler($this->moduleHandler);
Chris@0 124 $instance->setViewsData($this->viewsData);
Chris@0 125 }
Chris@0 126 return $instance;
Chris@0 127 }
Chris@0 128
Chris@0 129 /**
Chris@0 130 * {@inheritdoc}
Chris@0 131 */
Chris@0 132 public function getFallbackPluginId($plugin_id, array $configuration = []) {
Chris@0 133 return 'broken';
Chris@0 134 }
Chris@0 135
Chris@0 136 }