Commit 53e5a482 authored by webchick's avatar webchick

Issue #1783196 by dawehner, damiankloip, tim.plunkett: Get the views plugin...

Issue #1783196 by dawehner, damiankloip, tim.plunkett: Get the views plugin manager from the DIC when possible.
parent 396a6475
......@@ -22,6 +22,7 @@ function comment_views_data() {
'access query tag' => 'comment_access',
);
$data['comment']['table']['entity type'] = 'comment';
$data['comment']['table']['wizard_id'] = 'comment';
// Fields
......
......@@ -27,6 +27,7 @@ function file_views_data() {
),
);
$data['file_managed']['table']['entity type'] = 'file';
$data['file_managed']['table']['wizard_id'] = 'file_managed';
// fid
$data['file_managed']['fid'] = array(
......
......@@ -30,6 +30,7 @@ function node_views_data() {
),
);
$data['node']['table']['entity type'] = 'node';
$data['node']['table']['wizard_id'] = 'node';
// node table -- fields
......@@ -422,6 +423,8 @@ function node_views_data() {
// have a group defined will go into this field by default.
$data['node_revision']['table']['entity type'] = 'node';
$data['node_revision']['table']['group'] = t('Content revision');
$data['node_revision']['table']['wizard_id'] = 'node_revision';
// Advertise this table as a possible base table
$data['node_revision']['table']['base'] = array(
......
......@@ -105,6 +105,8 @@ function taxonomy_views_data() {
'access query tag' => 'term_access',
);
$data['taxonomy_term_data']['table']['entity type'] = 'taxonomy_term';
$data['taxonomy_term_data']['table']['wizard_id'] = 'taxonomy_term';
// The term data table
$data['taxonomy_term_data']['table']['join'] = array(
......
......@@ -24,6 +24,7 @@ function user_views_data() {
'access query tag' => 'user_access',
);
$data['users']['table']['entity type'] = 'user';
$data['users']['table']['wizard_id'] = 'user';
// uid
$data['users']['uid'] = array(
......
......@@ -224,7 +224,7 @@ public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
return FALSE;
}
$plugin = views_get_plugin_definition('display', $plugin_id);
$plugin = drupal_container()->get('plugin.manager.views.display')->getDefinition($plugin_id);
if (empty($plugin)) {
$plugin['title'] = t('Broken');
}
......
<?php
/**
* @file
* Definition of Drupal\views\Plugin\Type\DefaultWizardDeriver.
*/
namespace Drupal\views\Plugin\Type;
use Drupal\Component\Plugin\Derivative\DerivativeInterface;
/**
* A derivative class which provides automatic wizards for all base tables.
*/
class DefaultWizardDeriver implements DerivativeInterface {
/**
* Stores all base table plugin information.
*
* @var array
*/
protected $derivatives = array();
/**
* Implements Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
*/
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
return $this->derivatives[$derivative_id];
}
$this->getDerivativeDefinitions($base_plugin_definition);
return $this->derivatives[$derivative_id];
}
/**
* Implements Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
$base_tables = array_keys(views_fetch_base_tables());
$this->derivatives = array();
foreach ($base_tables as $table) {
$views_info = views_fetch_data($table);
if (empty($views_info['table']['wizard_id'])) {
$this->derivatives[$table] = array(
'id' => 'standard',
'base_table' => $table,
'title' => $views_info['table']['base']['title'],
'class' => 'Drupal\views\Plugin\views\wizard\Standard'
);
}
}
return $this->derivatives;
}
}
......@@ -8,8 +8,9 @@
namespace Drupal\views\Plugin\Type;
use Drupal\Component\Plugin\PluginManagerBase;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator;
use Drupal\Component\Plugin\Discovery\ProcessDecorator;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Plugin\Discovery\AlterDecorator;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
......@@ -23,6 +24,7 @@ public function __construct() {
$this->discovery = new AnnotatedClassDiscovery('views', 'wizard');
$this->discovery = new AlterDecorator($this->discovery, 'views_plugins_wizard');
$this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition'));
$this->discovery = new DerivativeDiscoveryDecorator($this->discovery);
$this->discovery = new CacheDecorator($this->discovery, 'views:wizard', 'views_info');
$this->factory = new DefaultFactory($this);
$this->defaults = array(
......
......@@ -301,7 +301,7 @@ public function buildOptionsForm(&$form, &$form_state) {
),
);
$plugins = views_get_plugin_definitions('argument_validator');
$plugins = drupal_container()->get('plugin.manager.views.argument_validator')->getDefinitions();
foreach ($plugins as $id => $info) {
if (!empty($info['no_ui'])) {
continue;
......@@ -496,7 +496,7 @@ function default_actions($which = NULL) {
* default action is set to provide default argument.
*/
function default_argument_form(&$form, &$form_state) {
$plugins = views_get_plugin_definitions('argument_default');
$plugins = drupal_container()->get('plugin.manager.views.argument_default')->getDefinitions();
$options = array();
$form['default_argument_skip_url'] = array(
......@@ -560,7 +560,7 @@ function default_argument_form(&$form, &$form_state) {
* default action is set to display one.
*/
function default_summary_form(&$form, &$form_state) {
$style_plugins = views_get_plugin_definitions('style');
$style_plugins = drupal_container()->get('plugin.manager.views.style')->getDefinitions();
$summary_plugins = array();
$format_options = array();
foreach ($style_plugins as $key => $plugin) {
......@@ -1053,7 +1053,7 @@ function get_plugin($type = 'argument_default', $name = NULL) {
$options = $this->options[$options_name];
}
$plugin = views_get_plugin($type, $name);
$plugin = drupal_container()->get("plugin.manager.views.$type")->createInstance($name);
if ($plugin) {
// Style plugins expects different parameters as argument related plugins.
if ($type == 'style') {
......
......@@ -105,8 +105,9 @@ public function init(ViewExecutable $view, &$display, $options = NULL) {
$this->extender = array();
$extenders = views_get_enabled_display_extenders();
if (!empty($extenders)) {
$manager = drupal_container()->get('plugin.manager.views.display_extender');
foreach ($extenders as $extender) {
$plugin = views_get_plugin('display_extender', $extender);
$plugin = $manager->createInstance($extender);
if ($plugin) {
$plugin->init($this->view, $this);
$this->extender[$extender] = $plugin;
......
......@@ -61,7 +61,7 @@ public function executeHookMenu($callbacks) {
$access_plugin = $this->getPlugin('access');
if (!isset($access_plugin)) {
$access_plugin = views_get_plugin('access', 'none');
$access_plugin = drupal_container()->get("plugin.manager.views.access")->createInstance('none');
}
// Get access callback might return an array of the callback + the dynamic arguments.
......
<?php
/**
* @file
* Definition of Drupal\views\Plugin\views\wizard\Standard.
*/
namespace Drupal\views\Plugin\views\wizard;
use Drupal\Core\Annotation\Plugin;
/**
* @Plugin(
* id = "standard",
* derivative = "Drupal\views\Plugin\Type\DefaultWizardDeriver"
* )
*/
class Standard extends WizardPluginBase {
}
......@@ -507,9 +507,7 @@ public static function getSelected($form_state, $parents, $default_value, $eleme
protected function build_form_style(array &$form, array &$form_state, $type) {
$style_form =& $form['displays'][$type]['options']['style'];
$style = $style_form['style_plugin']['#default_value'];
// @fixme
$style_plugin = views_get_plugin('style', $style);
$style_plugin = drupal_container()->get("plugin.manager.views.style")->createInstance($style);
if (isset($style_plugin) && $style_plugin->usesRowPlugin()) {
$options = $this->row_style_options();
$style_form['row_plugin'] = array(
......@@ -690,7 +688,7 @@ protected function build_display_options($form, $form_state) {
protected function alter_display_options(&$display_options, $form, $form_state) {
foreach ($display_options as $display_type => $options) {
// Allow style plugins to hook in and provide some settings.
$style_plugin = views_get_plugin('style', $options['style']['type']);
$style_plugin = drupal_container()->get("plugin.manager.views.style")->createInstance($options['style']['type']);
$style_plugin->wizard_submit($form, $form_state, $this, $display_options, $display_type);
}
}
......@@ -840,7 +838,7 @@ protected function default_display_filters_user(array $form, array &$form_state)
$table_data = views_fetch_data($table);
// If the 'in' operator is being used, map the values to an array.
$handler = $table_data[$bundle_key]['filter']['id'];
$handler_definition = views_get_plugin_definition('filter', $handler);
$handler_definition = drupal_container()->get('plugin.manager.views.filter')->getDefinition($handler);
if ($handler == 'in_operator' || is_subclass_of($handler_definition['class'], 'Drupal\\views\\Plugin\\views\\filter\\InOperator')) {
$value = drupal_map_assoc(array($form_state['values']['show']['type']));
}
......
......@@ -52,7 +52,7 @@ protected function viewsData() {
*/
public function testFilterQuery() {
// Check that we can find the test filter plugin.
$plugin = views_get_plugin('filter', 'test_filter');
$plugin = drupal_container()->get("plugin.manager.views.filter")->createInstance('test_filter');
$this->assertTrue($plugin instanceof FilterPlugin, 'Test filter plugin found.');
$view = views_get_view('test_filter');
......
......@@ -88,7 +88,7 @@ public function testPluginInstances() {
foreach ($this->definitions as $type => $plugins) {
// Get a plugin manager for this type.
$manager = $container->get("plugin.manager.views.$type");
foreach ($plugins as $definition) {
foreach ($plugins as $id => $definition) {
// Get a reflection class for this plugin.
// We only want to test true plugins, i.e. They extend PluginBase.
$reflection = new \ReflectionClass($definition['class']);
......@@ -96,8 +96,8 @@ public function testPluginInstances() {
// Create a plugin instance and check what it is. This is not just
// good to check they can be created but for throwing any notices for
// method signatures etc... too.
$instance = $manager->createInstance($definition['id']);
$this->assertTrue($instance instanceof $definition['class'], format_string('Instance of @type:@id created', array('@type' => $type, '@id' => $definition['id'])));
$instance = $manager->createInstance($id);
$this->assertTrue($instance instanceof $definition['class'], format_string('Instance of @type:@id created', array('@type' => $type, '@id' => $id)));
}
}
}
......
......@@ -577,7 +577,7 @@ public function initDisplay() {
// Instantiate all displays
foreach ($this->storage->get('display') as $id => $display) {
$this->displayHandlers[$id] = views_get_plugin('display', $display['display_plugin']);
$this->displayHandlers[$id] = drupal_container()->get("plugin.manager.views.display")->createInstance($display['display_plugin']);
if (!empty($this->displayHandlers[$id])) {
// Initialize the new display handler with data.
// @todo Refactor display to not need the handler data by reference.
......@@ -688,7 +688,7 @@ public function initStyle() {
$this->style_options = $style['options'];
}
$this->style_plugin = views_get_plugin('style', $this->plugin_name);
$this->style_plugin = drupal_container()->get("plugin.manager.views.style")->createInstance($this->plugin_name);
if (empty($this->style_plugin)) {
return FALSE;
......@@ -2207,11 +2207,12 @@ public function setItemOption($display_id, $type, $id, $option, $value) {
public function &newDisplay($id) {
// Create a handler.
$display = $this->storage->get('display');
$this->displayHandlers[$id] = views_get_plugin('display', $display[$id]['display_plugin']);
$manager = drupal_container()->get("plugin.manager.views.display");
$this->displayHandlers[$id] = $manager->createInstance($display[$id]['display_plugin']);
if (empty($this->displayHandlers[$id])) {
// provide a 'default' handler as an emergency. This won't work well but
// it will keep things from crashing.
$this->displayHandlers[$id] = views_get_plugin('display', 'default');
$this->displayHandlers[$id] = $manager->createInstance('default');
}
if (!empty($this->displayHandlers[$id])) {
......
......@@ -840,7 +840,8 @@ function views_add_contextual_links(&$render_element, $location, ViewExecutable
// Also do not do anything if the display plugin has not defined any
// contextual links that are intended to be displayed in the requested
// location.
$plugin = views_get_plugin_definition('display', $view->displayHandlers[$display_id]->display['display_plugin']);
$plugin_id = $view->displayHandlers[$display_id]->getPluginId();
$plugin = drupal_container()->get('plugin.manager.views.display')->getDefinition($plugin_id);
// If contextual_links_locations are not set, provide a sane default. (To
// avoid displaying any contextual links at all, a display plugin can still
// set 'contextual_links_locations' to, e.g., {""}.)
......@@ -1230,6 +1231,59 @@ function views_fetch_data($table = NULL, $reset = FALSE) {
return _views_fetch_data($table, $reset);
}
/**
* Fetch a list of all base tables available
*
* @return array
* A keyed array of in the form of 'base_table' => 'Description'.
*/
function views_fetch_base_tables() {
static $base_tables = array();
if (empty($base_tables)) {
$tables = array();
$data = views_fetch_data();
foreach ($data as $table => $info) {
if (!empty($info['table']['base'])) {
$tables[$table] = array(
'title' => $info['table']['base']['title'],
'description' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
);
}
}
uasort($tables, '_views_weight_sort');
$base_tables = $tables;
}
return $base_tables;
}
/**
* Sorts a structured array by the 'weight' and then by 'title' element.
*
* Callback for uasort() in views_fetch_base_tables().
*
* @param array $a
* First item for comparison. The compared items should be associative arrays
* that include a 'weight' and title element.
* @param array $b
* Second item for comparison.
*
* @return int
* -1 if the first item comes first, 1 if second and 0 if they have the same
* order.
*/
function _views_weight_sort($a, $b) {
if ($a['weight'] != $b['weight']) {
return $a['weight'] < $b['weight'] ? -1 : 1;
}
if ($a['title'] != $b['title']) {
return $a['title'] < $b['title'] ? -1 : 1;
}
return 0;
}
/**
* Fetch a list of all base tables available
*
......@@ -1245,7 +1299,7 @@ function views_fetch_data($table = NULL, $reset = FALSE) {
* A keyed array of in the form of 'base_table' => 'Description'.
*/
function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
$definitions = views_get_plugin_definitions($type);
$definitions = drupal_container()->get("plugin.manager.views.$type")->getDefinitions();
$plugins = array();
foreach ($definitions as $id => $plugin) {
......@@ -1291,39 +1345,17 @@ function views_get_plugin($type, $plugin_id) {
/**
* Gets all the views plugin definitions.
*
* @param string|false $type
* Either the plugin type, or FALSE to load all definitions.
*
* @return array
* An array of plugin definitions for a single type, or an associative array
* of plugin definitions keyed by plugin type.
* An array of plugin definitions for all types.
*/
function views_get_plugin_definitions($type = FALSE) {
$plugins = array();
$plugin_types = $type ? array($type) : ViewExecutable::getPluginTypes();
function views_get_plugin_definitions() {
$container = drupal_container();
foreach ($plugin_types as $plugin_type) {
$plugins = array();
foreach (ViewExecutable::getPluginTypes() as $plugin_type) {
$plugins[$plugin_type] = $container->get("plugin.manager.views.$plugin_type")->getDefinitions();
}
if ($type) {
return $plugins[$type];
}
return $plugins;
}
/**
* Gets the plugin definition from a plugin type with a specific ID.
*
* @param string $type
* The plugin type, e.g., 'access' or 'display'.
* @param string $plugin_id
* The name of the plugin, e.g., 'standard'.
*
* @return array
* A plugin definition.
*/
function views_get_plugin_definition($type, $plugin_id) {
return drupal_container()->get("plugin.manager.views.$type")->getDefinition($plugin_id);
return $plugins;
}
/**
......@@ -1389,7 +1421,7 @@ function views_get_applicable_views($type) {
// Loop on array keys because something seems to muck with $view->display
// a bit in PHP4.
foreach (array_keys($display) as $id) {
$plugin = views_get_plugin_definition('display', $display[$id]['display_plugin']);
$plugin = drupal_container()->get('plugin.manager.views.display')->getDefinition($display[$id]['display_plugin']);
if (!empty($plugin[$type])) {
// This view uses_hook_menu. Clone it so that different handlers
// don't trip over each other, and add it to the list.
......
......@@ -2113,45 +2113,6 @@ function views_ui_autocomplete_tag($string = '') {
return new JsonResponse($matches);
}
function _views_weight_sort($a, $b) {
if ($a['weight'] != $b['weight']) {
return $a['weight'] < $b['weight'] ? -1 : 1;
}
if ($a['title'] != $b['title']) {
return $a['title'] < $b['title'] ? -1 : 1;
}
return 0;
}
/**
* Fetch a list of all base tables available
*
* @return
* A keyed array of in the form of 'base_table' => 'Description'.
*/
function views_fetch_base_tables() {
static $base_tables = array();
if (empty($base_tables)) {
$weights = array();
$tables = array();
$data = views_fetch_data();
foreach ($data as $table => $info) {
if (!empty($info['table']['base'])) {
$tables[$table] = array(
'title' => $info['table']['base']['title'],
'description' => !empty($info['table']['base']['help']) ? $info['table']['base']['help'] : '',
'weight' => !empty($info['table']['base']['weight']) ? $info['table']['base']['weight'] : 0,
);
}
}
uasort($tables, '_views_weight_sort');
$base_tables = $tables;
}
return $base_tables;
}
function _views_sort_types($a, $b) {
$a_group = drupal_strtolower($a['group']);
$b_group = drupal_strtolower($b['group']);
......
......@@ -87,7 +87,7 @@ public function form(array $form, array &$form_state, EntityInterface $view) {
// Create the "Show" dropdown, which allows the base table of the view to be
// selected.
$wizard_plugins = views_ui_get_wizards();
$wizard_plugins = drupal_container()->get("plugin.manager.views.wizard")->getDefinitions();
$options = array();
foreach ($wizard_plugins as $key => $wizard) {
$options[$key] = $wizard['title'];
......@@ -106,7 +106,7 @@ public function form(array $form, array &$form_state, EntityInterface $view) {
// Build the rest of the form based on the currently selected wizard plugin.
$wizard_key = $show_form['wizard_key']['#default_value'];
$wizard_instance = views_get_plugin('wizard', $wizard_key);
$wizard_instance = drupal_container()->get("plugin.manager.views.wizard")->createInstance($wizard_key);
$form = $wizard_instance->build_form($form, $form_state);
return $form;
......@@ -142,10 +142,12 @@ protected function actions(array $form, array &$form_state) {
* Overrides Drupal\Core\Entity\EntityFormController::validate().
*/
public function validate(array $form, array &$form_state) {
$wizard = views_ui_get_wizard($form_state['values']['show']['wizard_key']);
$form_state['wizard'] = $wizard;
$form_state['wizard_instance'] = views_get_plugin('wizard', $wizard['id']);
$wizard_type = $form_state['values']['show']['wizard_key'];
$wizard_instance = drupal_container()->get('plugin.manager.views.wizard')->createInstance($wizard_type);
$form_state['wizard'] = $wizard_instance->getDefinition();
$form_state['wizard_instance'] = $wizard_instance;
$errors = $form_state['wizard_instance']->validateView($form, $form_state);
foreach ($errors as $name => $message) {
form_set_error($name, $message);
}
......
......@@ -470,79 +470,6 @@ function views_ui_view_preview_section_rows_links(ViewUI $view) {
return $links;
}
/**
* Fetch metadata on a specific views ui wizard plugin.
*
* @param $wizard_type
* Name of a wizard, or name of a base table.
*
* @return
* An array with information about the requested wizard type.
*/
function views_ui_get_wizard($wizard_type) {
$wizard = views_get_plugin_definition('wizard', $wizard_type);
// @todo - handle this via an alter hook instead.
if (!$wizard) {
// Must be a base table using the default wizard plugin.
$base_tables = views_fetch_base_tables();
if (!empty($base_tables[$wizard_type])) {
$wizard = views_ui_views_wizard_defaults();
$wizard['base_table'] = $wizard_type;
$wizard['title'] = $base_tables[$wizard_type]['title'];
}
}
return $wizard;
}
/**
* Fetch metadata for all content_type plugins.
*
* @return
* An array of arrays with information about all available views wizards.
*/
function views_ui_get_wizards() {
$wizard_plugins = views_get_plugin_definitions('wizard');
$wizard_tables = array();
foreach ($wizard_plugins as $name => $info) {
$wizard_tables[$info['base_table']] = TRUE;
}
$base_tables = views_fetch_base_tables();
$default_wizard = views_ui_views_wizard_defaults();
// Find base tables with no wizard.
// @todo - handle this via an alter hook for plugins?
foreach ($base_tables as $table => $info) {
if (!isset($wizard_tables[$table])) {
$wizard = $default_wizard;
$wizard['title'] = $info['title'];
$wizard['base_table'] = $table;
$wizard_plugins[$table] = $wizard;
}
}
return $wizard_plugins;
}
/**
* Helper function to define the default values for a Views wizard plugin.
*
* @return
* An array of defaults for a views wizard.
*/
function views_ui_views_wizard_defaults() {
return array(
// The children may, for example, be a different variant for each node type.
'get children' => NULL,
'get child' => NULL,
// title and base table must be populated. They are empty here just
// so they are documented.
'title' => '',
'base_table' => NULL,
);
}
function views_ui_get_form_wizard_instance($wizard) {
return views_get_plugin('wizard', $wizard['name']);
}
/**
* Implements hook_views_plugins_display_alter().
*/
......
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