Commit abd15444 authored by Dries's avatar Dries

Issue #1907902 by dawehner, damiankloip: Move views_get_handler() into the handler plugin manager.

parent 2c616129
......@@ -7,30 +7,118 @@
namespace Drupal\views\Plugin;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Core\Plugin\Factory\ContainerFactory;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\views\Plugin\Discovery\ViewsHandlerDiscovery;
use Drupal\views\ViewsData;
/**
* Plugin type manager for all views handlers.
*/
class ViewsHandlerManager extends PluginManagerBase {
/**
* The views data cache.
*
* @var \Drupal\views\ViewsData
*/
protected $viewsData;
/**
* The handler type.
*
* @var string
*
* @see \Drupal\views\ViewExecutable::viewsHandlerTypes().
*/
protected $handlerType;
/**
* Constructs a ViewsHandlerManager object.
*
* @param string $type
* @param string $handler_type
* The plugin type, for example filter.
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations,
* @param \Drupal\views\ViewsData $views_data
* The views data cache.
*/
public function __construct($type, \Traversable $namespaces) {
$this->discovery = new ViewsHandlerDiscovery($type, $namespaces);
$this->discovery = new CacheDecorator($this->discovery, "views:$type", 'views_info');
public function __construct($handler_type, \Traversable $namespaces, ViewsData $views_data) {
$this->discovery = new ViewsHandlerDiscovery($handler_type, $namespaces);
$this->discovery = new CacheDecorator($this->discovery, "views:$handler_type", 'views_info');
$this->factory = new ContainerFactory($this);
$this->viewsData = $views_data;
$this->handlerType = $handler_type;
}
/**
* Fetches a handler from the data cache.
*
* @param array $item
* An associative array representing the handler to be retrieved:
* - table: The name of the table containing the handler.
* - field: The name of the field the handler represents.
* - optional: (optional) Whether or not this handler is optional. If a
* handler is missing and not optional, a debug message will be displayed.
* Defaults to FALSE.
* @param string|null $override
* (optional) Override the actual handler object with this plugin ID. Used for
* aggregation when the handler is redirected to the aggregation handler.
*
* @return \Drupal\views\Plugin\views\HandlerBase
* An instance of a handler object. May be a broken handler instance.
*/
public function getHandler($item, $override = NULL) {
$table = $item['table'];
$field = $item['field'];
$optional = isset($item['optional']) ? $item['optional'] : FALSE;
// Get the plugin manager for this type.
$data = $this->viewsData->get($table);
if (isset($data[$field][$this->handlerType])) {
$definition = $data[$field][$this->handlerType];
foreach (array('group', 'title', 'title short', 'help', 'real field', 'real table') as $key) {
if (!isset($definition[$key])) {
// First check the field level.
if (!empty($data[$field][$key])) {
$definition[$key] = $data[$field][$key];
}
// Then if that doesn't work, check the table level.
elseif (!empty($data['table'][$key])) {
$definition[$key] = $data['table'][$key];
}
}
}
// @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.
}
}
}
if (!$optional) {
// debug(t("Missing handler: @table @field @type", array('@table' => $table, '@field' => $field, '@type' => $this->handlerType)));
}
// Finally, use the 'broken' handler.
return $this->createInstance('broken');
}
}
......@@ -875,7 +875,7 @@ public function submitTemporaryForm($form, &$form_state) {
// Create a new handler and unpack the options from the form onto it. We
// can use that for storage.
$handler = views_get_handler($item, $handler_type, $override);
$handler = Views::handlerManager($handler_type)->getHandler($item, $override);
$handler->init($executable, $executable->display_handler, $item);
// Add the incoming options to existing options because items using
......
......@@ -898,7 +898,7 @@ public function getHandlers($type) {
$handler_type = $type;
}
if ($handler = views_get_handler($info, $handler_type, $override)) {
if ($handler = Views::handlerManager($handler_type)->getHandler($info, $override)) {
// Special override for area types so they know where they come from.
if ($handler instanceof AreaPluginBase) {
$handler->areaType = $type;
......
......@@ -9,6 +9,7 @@
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
use Drupal\views\Views;
/**
* Exposed form plugin that provides an exposed form with required input.
......@@ -83,7 +84,7 @@ public function preRender($values) {
'content' => $this->options['text_input_required'],
'format' => $this->options['text_input_required_format'],
);
$handler = views_get_handler($options, 'area');
$handler = Views::handlerManager('area')->getHandler($options);
$handler->init($this->view, $options);
$this->displayHandler->handlers['empty'] = array(
'area' => $handler,
......
......@@ -71,7 +71,7 @@ public function buildOptionsForm(&$form, &$form_state) {
$relationship_options = array();
foreach ($relationships as $relationship) {
$relationship_handler = views_get_handler($relationship, 'relationship');
$relationship_handler = Views::handlerManager('relationship')->getHandler($relationship);
// If this relationship is valid for this type, add it to the list.
$data = Views::viewsData()->get($relationship['table']);
......
......@@ -10,6 +10,7 @@
use Drupal\Component\Annotation\PluginID;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\ViewExecutable;
use Drupal\views\Views;
/**
* Handler for GROUP BY on simple numeric fields.
......@@ -25,7 +26,7 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o
parent::init($view, $display, $options);
// Initialize the original handler.
$this->handler = views_get_handler($options, 'sort');
$this->handler = Views::handlerManager('sort')->getHandler($options);
$this->handler->init($view, $display, $options);
}
......
......@@ -529,17 +529,17 @@ public function testClickSortable() {
'table' => 'views_test_data',
'field' => 'name',
);
$plugin = views_get_handler($item, 'field');
$plugin = $this->container->get('plugin.manager.views.field')->getHandler($item);
$this->assertTrue($plugin->clickSortable(), 'TRUE as a default value is correct.');
// Test that clickSortable is TRUE by when set TRUE in the data.
$item['field'] = 'id';
$plugin = views_get_handler($item, 'field');
$plugin = $this->container->get('plugin.manager.views.field')->getHandler($item);
$this->assertTrue($plugin->clickSortable(), 'TRUE as a views data value is correct.');
// Test that clickSortable is FALSE by when set FALSE in the data.
$item['field'] = 'job';
$plugin = views_get_handler($item, 'field');
$plugin = $this->container->get('plugin.manager.views.field')->getHandler($item);
$this->assertFalse($plugin->clickSortable(), 'FALSE as a views data value is correct.');
}
......
......@@ -80,7 +80,7 @@ public function testHandlers() {
if (isset($field_info[$type]['id'])) {
$options = array();
if ($type == 'filter') {
$handler = views_get_handler($item, $type);
$handler = $this->container->get("plugin.manager.views.$type")->getHandler($item);
if ($handler instanceof InOperator) {
$options['value'] = array(1);
}
......
......@@ -10,6 +10,7 @@
use Drupal\views\ViewExecutable;
use Drupal\views\Tests\ViewTestBase;
use Drupal\views\Plugin\views\HandlerBase;
use Drupal\views\Views;
/**
* Tests abstract handlers of views.
......@@ -105,7 +106,7 @@ function testBreakPhraseString() {
'table' => 'node',
'field' => 'title',
);
$handler = views_get_handler($item, 'argument');
$handler = $this->container->get('plugin.manager.views.argument')->getHandler($item);
$this->assertEqual($handler, HandlerBase::breakPhraseString('', $handler), 'The breakPhraseString() method works correctly.');
// test ors
......@@ -164,7 +165,7 @@ function testBreakPhrase() {
'table' => 'node',
'field' => 'title',
);
$handler = views_get_handler($item, 'argument');
$handler = $this->container->get('plugin.manager.views.argument')->getHandler($item);
$this->assertEqual($handler, HandlerBase::breakPhrase('', $handler), 'The breakPhrase() method works correctly.');
// Generate three random numbers which can be used below;
......
......@@ -51,7 +51,7 @@ public function testViewsGetHandler() {
'table' => $this->randomName(),
'field' => $this->randomName(),
);
$handler = views_get_handler($item, $type);
$handler = Views::handlerManager($type)->getHandler($item);
$this->assertEqual('Drupal\views\Plugin\views\\' . $type . '\Broken', get_class($handler), t('Make sure that a broken handler of type: @type are created', array('@type' => $type)));
}
......@@ -66,7 +66,7 @@ public function testViewsGetHandler() {
);
foreach ($data as $id => $field_data) {
if (!in_array($id, array('title', 'help'))) {
$handler = views_get_handler($item, $id);
$handler = Views::handlerManager($id)->getHandler($item);
$this->assertInstanceHandler($handler, $table, $field, $id);
}
}
......@@ -78,7 +78,7 @@ public function testViewsGetHandler() {
'table' => 'views_test_data',
'field' => 'job',
);
$handler = views_get_handler($item, 'filter', 'standard');
$handler = Views::handlerManager('filter')->getHandler($item, 'standard');
$this->assertTrue($handler instanceof Standard);
// @todo Reinstate these tests when the debug() in views_get_handler() is
......@@ -91,7 +91,7 @@ public function testViewsGetHandler() {
'table' => 'views_test_data',
'field' => 'field_invalid',
);
views_get_handler($item, 'field');
Views::handlerManager('field')->getHandler($item);
$this->assertTrue(strpos($this->lastErrorMessage, format_string("Missing handler: @table @field @type", array('@table' => 'views_test_data', '@field' => 'field_invalid', '@type' => 'field'))) !== FALSE, 'An invalid field name throws a debug message.');
unset($this->lastErrorMessage);
......@@ -99,7 +99,7 @@ public function testViewsGetHandler() {
'table' => 'table_invalid',
'field' => 'id',
);
views_get_handler($item, 'filter');
Views::handlerManager('filter')->getHandler($item);
$this->assertEqual(strpos($this->lastErrorMessage, format_string("Missing handler: @table @field @type", array('@table' => 'table_invalid', '@field' => 'id', '@type' => 'filter'))) !== FALSE, 'An invalid table name throws a debug message.');
unset($this->lastErrorMessage);
......@@ -108,7 +108,7 @@ public function testViewsGetHandler() {
'field' => 'id',
'optional' => FALSE,
);
views_get_handler($item, 'filter');
Views::handlerManager('filter')->getHandler($item);
$this->assertEqual(strpos($this->lastErrorMessage, format_string("Missing handler: @table @field @type", array('@table' => 'table_invalid', '@field' => 'id', '@type' => 'filter'))) !== FALSE, 'An invalid table name throws a debug message.');
unset($this->lastErrorMessage);
......@@ -117,8 +117,7 @@ public function testViewsGetHandler() {
'field' => 'id',
'optional' => TRUE,
);
views_get_handler($item, 'filter');
Views::handlerManager('filter')->getHandler($item);
$this->assertFalse($this->lastErrorMessage, "An optional handler does not throw a debug message.");
unset($this->lastErrorMessage);
......
......@@ -56,4 +56,13 @@ public static function pluginManager($type) {
return Drupal::service('plugin.manager.views.' . $type);
}
/**
* Returns the plugin manager for a certain views handler type.
*
* @return \Drupal\views\Plugin\ViewsHandlerManager
*/
public static function handlerManager($type) {
return Drupal::service('plugin.manager.views.' . $type);
}
}
......@@ -831,73 +831,6 @@ function views_library_info() {
return $libraries;
}
/**
* Fetch a handler from the data cache.
*
* @param array $item
* An associative array representing the handler to be retrieved:
* - table: The name of the table containing the handler.
* - field: The name of the field the handler represents.
* - optional: (optional) Whether or not this handler is optional. If a
* handler is missing and not optional, a debug message will be displayed.
* Defaults to FALSE.
* @param string $type
* The type of handler. i.e, sort, field, argument, filter, relationship
* @param string|null $override
* (optional) Override the actual handler object with this plugin ID. Used for
* aggregation when the handler is redirected to the aggregation handler.
*
* @return views_handler
* An instance of a handler object. May be views_handler_broken.
*/
function views_get_handler($item, $type, $override = NULL) {
$table = $item['table'];
$field = $item['field'];
$optional = isset($item['optional']) ? $item['optional'] : FALSE;
// Get the plugin manager for this type.
$manager = Views::pluginManager($type);
$data = Views::viewsData()->get($table);
if (isset($data[$field][$type])) {
$definition = $data[$field][$type];
foreach (array('group', 'title', 'title short', 'help', 'real field', 'real table') as $key) {
if (!isset($definition[$key])) {
// First check the field level
if (!empty($data[$field][$key])) {
$definition[$key] = $data[$field][$key];
}
// Then if that doesn't work, check the table level
elseif (!empty($data['table'][$key])) {
$definition[$key] = $data['table'][$key];
}
}
}
// @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 $manager->createInstance($plugin_id, $definition);
}
catch (PluginException $e) {
// If that fails, use the original handler.
try {
return $manager->createInstance($definition['id'], $definition);
}
catch (PluginException $e) {
// Deliberately empty, this case is handled generically below.
}
}
}
if (!$optional) {
//debug(t("Missing handler: @table @field @type", array('@table' => $table, '@field' => $field, '@type' => $type)));
}
// Finally, use the 'broken' handler.
return $manager->createInstance('broken');
}
/**
* Fetch a list of all base tables available
*
......
......@@ -4,10 +4,10 @@ services:
arguments: [access, '@container.namespaces']
plugin.manager.views.area:
class: Drupal\views\Plugin\ViewsHandlerManager
arguments: [area, '@container.namespaces']
arguments: [area, '@container.namespaces', '@views.views_data']
plugin.manager.views.argument:
class: Drupal\views\Plugin\ViewsHandlerManager
arguments: [argument, '@container.namespaces']
arguments: [argument, '@container.namespaces', '@views.views_data']
plugin.manager.views.argument_default:
class: Drupal\views\Plugin\ViewsPluginManager
arguments: [argument_default, '@container.namespaces']
......@@ -28,13 +28,13 @@ services:
arguments: [exposed_form, '@container.namespaces']
plugin.manager.views.field:
class: Drupal\views\Plugin\ViewsHandlerManager
arguments: [field, '@container.namespaces']
arguments: [field, '@container.namespaces', '@views.views_data']
plugin.manager.views.filter:
class: Drupal\views\Plugin\ViewsHandlerManager
arguments: [filter, '@container.namespaces']
arguments: [filter, '@container.namespaces', '@views.views_data']
plugin.manager.views.join:
class: Drupal\views\Plugin\ViewsHandlerManager
arguments: [join, '@container.namespaces']
arguments: [join, '@container.namespaces', '@views.views_data']
plugin.manager.views.pager:
class: Drupal\views\Plugin\ViewsPluginManager
arguments: [pager, '@container.namespaces']
......@@ -43,13 +43,13 @@ services:
arguments: [query, '@container.namespaces']
plugin.manager.views.relationship:
class: Drupal\views\Plugin\ViewsHandlerManager
arguments: [relationship, '@container.namespaces']
arguments: [relationship, '@container.namespaces', '@views.views_data']
plugin.manager.views.row:
class: Drupal\views\Plugin\ViewsPluginManager
arguments: [row, '@container.namespaces']
plugin.manager.views.sort:
class: Drupal\views\Plugin\ViewsHandlerManager
arguments: [sort, '@container.namespaces']
arguments: [sort, '@container.namespaces', '@views.views_data']
plugin.manager.views.style:
class: Drupal\views\Plugin\ViewsPluginManager
arguments: [style, '@container.namespaces']
......
......@@ -96,7 +96,7 @@ public function buildForm(array $form, array &$form_state) {
if ($type == 'relationship' && $id == $relationship['id']) {
break;
}
$relationship_handler = views_get_handler($relationship, 'relationship');
$relationship_handler = Views::handlerManager('relationship')->getHandler($relationship);
// ignore invalid/broken relationships.
if (empty($relationship_handler)) {
continue;
......@@ -225,7 +225,7 @@ public function submitForm(array &$form, array &$form_state) {
// Create a new handler and unpack the options from the form onto it. We
// can use that for storage.
$handler = views_get_handler($item, $handler_type, $override);
$handler = Views::handlerManager($handler_type)->getHandler($item, $override);
$handler->init($executable, $executable->display_handler, $item);
// Add the incoming options to existing options because items using
......
......@@ -7,6 +7,7 @@
namespace Drupal\views_ui\Form\Ajax;
use Drupal\views\Views;
use Drupal\views\ViewStorageInterface;
use Drupal\views\ViewExecutable;
......@@ -99,7 +100,7 @@ public function submitForm(array &$form, array &$form_state) {
$type = $form_state['type'];
$id = $form_state['id'];
$handler = views_get_handler($item, $type);
$handler = Views::handlerManager($type)->getHandler($item);
$executable = $form_state['view']->get('executable');
$handler->init($executable, $executable->display_handler, $item);
......
......@@ -469,7 +469,7 @@ public function submitItemAdd($form, &$form_state) {
'table' => $table,
'field' => $field,
);
$handler = views_get_handler($item, $key);
$handler = Views::handlerManager($key)->getHandler($item);
if ($this->executable->displayHandlers->get('default')->useGroupBy() && $handler->usesGroupBy()) {
$this->addFormToStack('config-item-group', $form_state['display_id'], $type, $id);
}
......
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