PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/views/src/Plugin/ViewsHandlerManager.php

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