Commit 1d7dd18c authored by aspilicious's avatar aspilicious Committed by tim.plunkett

Issue #1765984 by aspilicious: Add standardized helper functions for creating a plugin instance.

parent b558c592
......@@ -9,7 +9,6 @@
use Drupal\views\TempStore\UserTempStore;
use Drupal\views\View;
use Drupal\views\Analyzer;
use Drupal\views\Plugin\Type\ViewsPluginManager;
use Drupal\views\Plugin\views\wizard\WizardException;
/**
......@@ -348,9 +347,9 @@ function views_ui_add_form($form, &$form_state) {
$wizard_key = $show_form['wizard_key']['#default_value'];
views_include_handlers();
$manager = new ViewsPluginManager('wizard');
$info = $manager->getDefinition($wizard_key);
$wizard_instance = $manager->createInstance($wizard_key, $info);
$info = views_get_plugin_definition('wizard', $wizard_key);
$wizard_instance = views_get_plugin_instance($wizard_key, $info);
$form = $wizard_instance->build_form($form, $form_state);
$form['save'] = array(
......@@ -656,10 +655,9 @@ function views_ui_nojs_submit($form, &$form_state) {
*/
function views_ui_wizard_form_validate($form, &$form_state) {
$wizard = views_ui_get_wizard($form_state['values']['show']['wizard_key']);
$manager = new ViewsPluginManager('wizard');
$definition = $manager->getDefinition($wizard['id']);
$definition = views_get_plugin_definition('wizard', $wizard['id']);
$form_state['wizard'] = $wizard;
$form_state['wizard_instance'] = $manager->createInstance($wizard['id'], $definition);
$form_state['wizard_instance'] = views_get_plugin_instance($wizard['id'], $definition);
$errors = $form_state['wizard_instance']->validate($form, $form_state);
foreach ($errors as $name => $message) {
form_set_error($name, $message);
......@@ -1521,7 +1519,7 @@ function views_ui_get_display_tab_details($view, $display) {
'#attributes' => array('id' => 'edit-display-settings-details'),
);
$plugin = views_fetch_plugin_data('display', $view->display[$display->id]->display_plugin);
$plugin = views_get_plugin_definition('display', $view->display[$display->id]->display_plugin);
// The following is for display purposes only. We need to determine if there is more than one button and wrap
// the buttons in a .ctools-dropbutton class if more than one is present. Otherwise, we'll just wrap the
// actions in the .ctools-button class.
......
......@@ -5,8 +5,6 @@
* Load Views' data so that it knows what is available to build queries from.
*/
use Drupal\views\Plugin\Type\ViewsPluginManager;
/**
* Fetch Views' data from the cache
*
......@@ -85,30 +83,6 @@ function _views_data_process_entity_types(&$data) {
}
}
/**
* Fetch the plugin data from cache.
*/
function _views_fetch_plugin_data($type = NULL, $id = NULL, $reset = FALSE) {
if (!$type && !$id) {
$plugins = array();
$plugin_types = array('access', 'area', 'argument', 'argument_default', 'argument_validator', 'cache', 'display_extender', 'display', 'exposed_form', 'field', 'filter', 'join', 'localization', 'pager', 'query', 'relationship', 'row', 'sort', 'style', 'wizard');
foreach ($plugin_types as $plugin_type) {
$manager = new ViewsPluginManager($plugin_type);
$plugins[$plugin_type] = $manager->getDefinitions();
}
return $plugins;
}
$manager = new ViewsPluginManager($type);
if (!$id) {
return $manager->getDefinitions();
}
else {
return $manager->getDefinition($id);
}
}
/**
* Set a cached item in the views cache.
*
......
......@@ -6,51 +6,8 @@
*/
use Drupal\Core\Database\Database;
use Drupal\views\Plugin\Type\ViewsPluginManager;
use Drupal\Component\Plugin\Exception\PluginException;
/**
* Instantiate and construct a new plugin.
*/
function _views_create_plugin($type, $definition) {
$manager = new ViewsPluginManager($type);
$instance = $manager->createInstance($definition['id']);
$instance->is_plugin = TRUE;
$instance->plugin_type = $type;
$instance->setDefinition($definition);
// Let the handler have something like a constructor.
$instance->construct();
return $instance;
}
/**
* Instantiate and construct a new handler
*/
function _views_create_handler($type, $definition) {
$manager = new ViewsPluginManager($type);
// @todo This is crazy. Find a way to remove the override functionality.
$id = !empty($definition['override handler']) ? $definition['override handler'] : $definition['id'];
try {
$instance = $manager->createInstance($id);
}
catch (PluginException $e) {
$instance = $manager->createInstance($definition['id']);
}
$instance->is_handler = TRUE;
$instance->plugin_type = $type;
$instance->setDefinition($definition);
// let the handler have something like a constructor.
$instance->construct();
return $instance;
}
/**
* Prepare a handler's data by checking defaults and such.
*/
......@@ -71,6 +28,24 @@ function _views_prepare_handler($definition, $data, $field, $type) {
return _views_create_handler($type, $definition);
}
/**
* Get a handler for a plugin
*
* @param string $type
* The plugin type like access or display.
* @param string $id
* The name of the plugin like standard.
*
* @return views_plugin
*
* The created plugin object.
*/
function _views_create_handler($type, $definition) {
$manager = new ViewsPluginManager($type);
$plugin = $manager->createHandlerFromDefinition($definition);
return $plugin;
}
/**
* Fetch a handler to join one table to a primary table from the data cache
*/
......@@ -84,7 +59,7 @@ function views_get_table_join($table, $base_table) {
else {
$id = 'standard';
}
$handler = views_get_join($id);
$handler = views_get_plugin_instance('join', $id);
// Fill in some easy defaults
$handler->definition = $h;
......
......@@ -73,4 +73,53 @@ public function processDefinition(&$definition, $plugin_id) {
);
}
/**
* Creates a plugin from an id.
*/
public function createPluginFromId($id) {
$definition = $this->getDefinition($id);
if (!empty($definition)) {
$plugin = $this->createPluginFromDefinition($defintion);
return $plugin;
}
}
/**
* Creates a plugin from a definition.
*/
public function createPluginFromDefinition($definition) {
$instance = $this->createInstance($definition['id']);
$instance->is_plugin = TRUE;
$instance->plugin_type = $this->$type;
$instance->setDefinition($definition);
// Let the handler have something like a constructor.
$instance->construct();
return $instance;
}
/**
* Creates a handler from a definition.
*/
public function createHandlerFromDefinition($definition) {
// @todo This is crazy. Find a way to remove the override functionality.
$id = !empty($definition['override handler']) ? $definition['override handler'] : $definition['id'];
try {
$instance = $this->createInstance($id);
}
catch (PluginException $e) {
$instance = $this->createInstance($definition['id']);
}
$instance->is_handler = TRUE;
$instance->plugin_type = $this->$type;
$instance->setDefinition($definition);
// let the handler have something like a constructor.
$instance->construct();
return $instance;
}
}
......@@ -184,7 +184,7 @@ function list_form(&$form, &$form_state) {
);
$displays = array();
foreach (views_fetch_plugin_data('display') as $id => $info) {
foreach (views_get_plugin_definitions('display') as $id => $info) {
if (!empty($info['admin'])) {
$displays[$id] = $info['admin'];
}
......
......@@ -303,7 +303,7 @@ public function buildOptionsForm(&$form, &$form_state) {
);
$validate_types = array('none' => t('- Basic validation -'));
$plugins = views_fetch_plugin_data('argument_validator');
$plugins = views_get_plugin_definitions('argument_validator');
foreach ($plugins as $id => $info) {
if (!empty($info['no_ui'])) {
continue;
......@@ -498,7 +498,7 @@ function default_actions($which = NULL) {
* default action is set to provide default argument.
*/
function default_argument_form(&$form, &$form_state) {
$plugins = views_fetch_plugin_data('argument_default');
$plugins = views_get_plugin_definitions('argument_default');
$options = array();
$form['default_argument_skip_url'] = array(
......@@ -562,7 +562,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_fetch_plugin_data('style');
$style_plugins = views_get_plugin_definitions('style');
$summary_plugins = array();
$format_options = array();
foreach ($style_plugins as $key => $plugin) {
......
......@@ -9,7 +9,6 @@
use Drupal\views\View;
use Drupal\views\Plugin\views\PluginBase;
use Drupal\views\Plugin\Type\ViewsPluginManager;
/**
* @defgroup views_display_plugins Views display plugins
......@@ -818,8 +817,7 @@ public function getPlugin($type = 'style', $name = NULL) {
$plugin = views_get_plugin($type, $name);
}
else {
$plugin_type = new ViewsPluginManager('query');
$plugin = $plugin_type->createInstance($name);
$plugin = views_get_plugin_instance('query', $name);
}
if (!$plugin) {
......@@ -1116,9 +1114,8 @@ public function optionsSummary(&$categories, &$options) {
'desc' => t('Change the title that this display will use.'),
);
$manager = new ViewsPluginManager('style');
$name = $this->getOption('style_plugin');
$style_plugin = $manager->getDefinition($name);
$style_plugin = views_get_plugin_definition('style', $name);
$style_plugin_instance = $this->getPlugin('style', $name);
$style_summary = empty($style_plugin['title']) ? t('Missing style plugin') : $style_plugin_instance->summaryTitle();
$style_title = empty($style_plugin['title']) ? t('Missing style plugin') : $style_plugin_instance->pluginTitle();
......@@ -1139,9 +1136,8 @@ public function optionsSummary(&$categories, &$options) {
}
if ($style_plugin_instance->usesRowPlugin()) {
$manager = new ViewsPluginManager('row');
$name = $this->getOption('row_plugin');
$row_plugin = $manager->getDefinition($name);
$row_plugin = views_get_plugin_definition('row', $name);
$row_plugin_instance = $this->getPlugin('row', $name);
$row_summary = empty($row_plugin['title']) ? t('Missing style plugin') : $row_plugin_instance->summaryTitle();
$row_title = empty($row_plugin['title']) ? t('Missing style plugin') : $row_plugin_instance->pluginTitle();
......@@ -1639,7 +1635,6 @@ public function buildOptionsForm(&$form, &$form_state) {
}
break;
case 'style_plugin':
$manager = new ViewsPluginManager('style');
$form['#title'] .= t('How should this view be styled');
$form['style_plugin'] = array(
'#type' => 'radios',
......
......@@ -377,7 +377,7 @@ public function query() {
else {
$id = 'subquery';
}
$join = views_get_join($id);
$join = views_get_plugin_instance('join', $id);
$join->definition = $def;
$join->construct();
......
......@@ -138,7 +138,7 @@ public function query() {
else {
$id = 'standard';
}
$join = views_get_join($id);
$join = views_get_plugin_instance('join', $id);
$join->definition = $def;
$join->options = $this->options;
......
......@@ -46,7 +46,7 @@ public function query() {
$max_depth = isset($this->definition['max depth']) ? $this->definition['max depth'] : MENU_MAX_DEPTH;
for ($i = 1; $i <= $max_depth; ++$i) {
if ($this->options['sort_within_level']) {
$join = views_get_join();
$join = views_get_plugin_instance('join');
$join->construct('menu_links', $this->table_alias, $this->field . $i, 'mlid');
$menu_links = $this->query->add_table('menu_links', NULL, $join);
$this->query->add_orderby($menu_links, 'weight', $this->options['order']);
......
......@@ -9,7 +9,6 @@
use Drupal\views\View;
use Drupal\views\Plugin\views\wizard\WizardInterface;
use Drupal\views\Plugin\Type\ViewsPluginManager;
/**
* Provides the interface and base class for Views Wizard plugins.
......@@ -749,8 +748,7 @@ protected function default_display_filters_user($form, $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'];
$plugin_manager = new ViewsPluginManager('filter');
$handler_definition = $plugin_manager->getDefinition($handler);
$handler_definition = views_get_plugin_definition('filter', $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']));
}
......
......@@ -32,8 +32,8 @@ protected function setUp() {
$this->normal_role = $this->drupalCreateRole(array());
$this->normal_user = $this->drupalCreateUser(array('views_test test permission'));
$this->normal_user->roles[$this->normal_role] = $this->normal_role;
// Reset the plugin data.
views_fetch_plugin_data(NULL, NULL, TRUE);
// @todo when all the plugin information is cached make a reset function and
// call it here.
}
/**
......
......@@ -32,7 +32,8 @@ protected function setUp() {
config('views.settings')->set('localization_plugin', 'test_localization')->save();
// Reset the plugin data.
views_fetch_plugin_data(NULL, NULL, TRUE);
// @todo when all the plugin information is cached make a reset function and
// call it here.
$this->strings = array(
'Master1',
'Apply1',
......
......@@ -8,7 +8,6 @@
namespace Drupal\views;
use Symfony\Component\HttpFoundation\Response;
use Drupal\views\Plugin\Type\ViewsPluginManager;
/**
* @defgroup views_objects Objects that represent a View or part of a view
......@@ -1047,8 +1046,7 @@ public function initQuery() {
// Create and initialize the query object.
$plugin = !empty($views_data['table']['base']['query class']) ? $views_data['table']['base']['query class'] : 'views_query';
$plugin_type = new ViewsPluginManager('query');
$this->query = $plugin_type->createInstance($plugin);
$this->query = views_get_plugin_instance('query', $plugin);
if (empty($this->query)) {
return FALSE;
......
......@@ -63,7 +63,7 @@ public function addDisplay($plugin_id = 'page', $title = NULL, $id = NULL) {
return FALSE;
}
$plugin = views_fetch_plugin_data('display', $plugin_id);
$plugin = views_get_plugin_definition('display', $plugin_id);
if (empty($plugin)) {
$plugin['title'] = t('Broken');
}
......
......@@ -27,7 +27,7 @@ public function query() {
// have to join in a specially related user table.
$this->ensureMyTable();
// join 'users' to this table via vid
$join = views_get_join();
$join = views_get_plugin_instance('join');
$join->construct('users', $this->table_alias, 'last_comment_uid', 'uid');
$join->extra = array(array('field' => 'uid', 'operator' => '!=', 'value' => '0'));
......
......@@ -25,7 +25,7 @@ class NcsLastCommentName extends SortPluginBase {
public function query() {
$this->ensureMyTable();
$join = views_get_join();
$join = views_get_plugin_instance('join');
$join->construct('users', $this->table_alias, 'last_comment_uid', 'uid');
// @todo this might be safer if we had an ensure_relationship rather than guessing
......
......@@ -58,7 +58,7 @@ public function query() {
else {
$id = 'standard';
}
$first_join = views_get_join($id);
$first_join = views_get_plugin_instance('join', $id);
$first_join->definition = $first;
$first_join->construct();
......@@ -85,7 +85,7 @@ public function query() {
else {
$id = 'standard';
}
$second_join = views_get_join($id);
$second_join = views_get_plugin_instance('join', $id);
$second_join->definition = $second;
$second_join->construct();
$second_join->adjusted = TRUE;
......
......@@ -62,7 +62,7 @@ public function query($group_by = FALSE) {
$search_condition = db_and();
// Create a new join to relate the 'search_total' table to our current 'search_index' table.
$join = views_get_join();
$join = views_get_plugin_instance('join');
$join->construct('search_total', $search_index, 'word', 'word');
$search_total = $this->query->add_relationship('search_total', $join, $search_index);
......
......@@ -142,7 +142,7 @@ public function query() {
$search_condition = db_and();
// Create a new join to relate the 'serach_total' table to our current 'search_index' table.
$join = views_get_join();
$join = views_get_plugin_instance('join');
$join->construct('search_total', $search_index, 'word', 'word');
$search_total = $this->query->add_relationship('search_total', $join, $search_index);
......
......@@ -92,7 +92,7 @@ public function query() {
$def['table formula'] = $query;
}
$join = views_get_join('standard');
$join = views_get_plugin_instance('join');
$join->definition = $def;
$join->construct();
......
......@@ -101,7 +101,7 @@ public function query() {
else {
$id = 'standard';
}
$join = views_get_join($id);
$join = views_get_plugin_instance('join', $id);
$join->definition = $def;
$join->construct();
......
......@@ -186,7 +186,7 @@ function views_theme($existing, $type, $theme, $path) {
'variables' => array('view' => NULL, 'grouping' => NULL, 'grouping_level' => NULL, 'rows' => NULL, 'title' => NULL),
);
$plugins = views_fetch_plugin_data();
$plugins = views_get_plugin_definitions();
// Register theme functions for all style plugins
foreach ($plugins as $type => $info) {
......@@ -346,7 +346,7 @@ function _views_find_module_templates($cache, $path) {
* keyed by machine name.
*/
function views_plugin_list() {
$plugin_data = views_fetch_plugin_data();
$plugin_data = views_get_plugin_definitions();
$plugins = array();
foreach (views_get_enabled_views() as $view) {
foreach ($view->display as $display_id => $display) {
......@@ -910,7 +910,7 @@ function views_add_contextual_links(&$render_element, $location, $view, $display
// 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_fetch_plugin_data('display', $view->display[$display_id]->display_plugin);
$plugin = views_get_plugin_defintion('display', $view->display[$display_id]->display_plugin);
// 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., {""}.)
......@@ -1277,7 +1277,7 @@ function views_include_handlers($reset = FALSE) {
function views_get_handler($table, $field, $key, $override = NULL) {
static $recursion_protection = array();
$data = views_fetch_data($table, FALSE);
$data = views_get_plugin_definitions($table);
$handler = NULL;
views_include('handlers');
......@@ -1358,14 +1358,6 @@ function views_fetch_data($table = NULL, $move = TRUE, $reset = FALSE) {
// -----------------------------------------------------------------------
// Views plugin functions
/**
* Fetch the plugin data from cache.
*/
function views_fetch_plugin_data($type = NULL, $id = NULL, $reset = FALSE) {
views_include('cache');
return _views_fetch_plugin_data($type, $id, $reset);
}
/**
* Fetch a list of all base tables available
*
......@@ -1381,8 +1373,7 @@ function views_fetch_plugin_data($type = NULL, $id = NULL, $reset = FALSE) {
* A keyed array of in the form of 'base_table' => 'Description'.
*/
function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
$manager = new ViewsPluginManager($type);
$definitions = $manager->getDefinitions();
$definitions = views_get_plugin_definitions($type);
$plugins = array();
foreach ($definitions as $id => $plugin) {
......@@ -1422,14 +1413,33 @@ function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
*
* The created plugin object.
*/
function views_get_plugin($type, $id, $reset = FALSE) {
views_include('handlers');
function views_get_plugin($type, $id) {
$manager = new ViewsPluginManager($type);
$definition = $manager->getDefinition($id);
if (!empty($definition)) {
return _views_create_plugin($type, $definition);
$manager->createPluginFromId($id);
}
/**
* Gets all the views plugin definitions
*/
function views_get_plugin_definitions($type = FALSE) {
$plugins = array();
$plugin_types = $type ? array($type) : array('access', 'area', 'argument', 'argument_default', 'argument_validator', 'cache', 'display_extender', 'display', 'exposed_form', 'field', 'filter', 'join', 'localization', 'pager', 'query', 'relationship', 'row', 'sort', 'style', 'wizard');
foreach ($plugin_types as $plugin_type) {
$manager = new ViewsPluginManager($plugin_type);
$plugins[$plugin_type] = $manager->getDefinitions();
}
if ($type) {
return $plugins[$type];
}
return $plugins;
}
/**
* Gets the plugin definition from a plugin type with a specific id.
*/
function views_get_plugin_definition($type, $id) {
$manager = new ViewsPluginManager($type);
return $manager->getDefintion($id);
}
/**
......@@ -1440,9 +1450,9 @@ function views_get_plugin($type, $id, $reset = FALSE) {
*
* @return Drupal\views\Plugin\views\join\JoinPluginBase
*/
function views_get_join($plugin_id = 'standard') {
$manager = new ViewsPluginManager('join');
return $manager->createInstance($plugin_id);
function views_get_plugin_instance($type, $plugin_id = 'standard', array $configuration = array()) {
$manager = new ViewsPluginManager($type);
return $manager->createInstance($plugin_id, $configuration);
}
/**
......@@ -1547,7 +1557,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($view->display) as $id) {
$plugin = views_fetch_plugin_data('display', $view->display[$id]->display_plugin);
$plugin = views_get_plugin_defintion('display', $view->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.
......