Commit a3c71654 authored by tim.plunkett's avatar tim.plunkett

Issue #1765984 by tim.plunkett: Unify and simplify the creation of a plugin instance.

parent 1d7dd18c
......@@ -347,8 +347,7 @@ function views_ui_add_form($form, &$form_state) {
$wizard_key = $show_form['wizard_key']['#default_value'];
views_include_handlers();
$info = views_get_plugin_definition('wizard', $wizard_key);
$wizard_instance = views_get_plugin_instance($wizard_key, $info);
$wizard_instance = views_get_plugin('wizard', $wizard_key);
$form = $wizard_instance->build_form($form, $form_state);
......@@ -655,10 +654,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']);
$definition = views_get_plugin_definition('wizard', $wizard['id']);
$form_state['wizard'] = $wizard;
$form_state['wizard_instance'] = views_get_plugin_instance($wizard['id'], $definition);
$errors = $form_state['wizard_instance']->validate($form, $form_state);
$form_state['wizard_instance'] = views_get_plugin('wizard', $wizard['id']);
$errors = $form_state['wizard_instance']->validateView($form, $form_state);
foreach ($errors as $name => $message) {
form_set_error($name, $message);
}
......@@ -1519,7 +1517,6 @@ function views_ui_get_display_tab_details($view, $display) {
'#attributes' => array('id' => 'edit-display-settings-details'),
);
$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.
......
......@@ -6,45 +6,6 @@
*/
use Drupal\Core\Database\Database;
use Drupal\Component\Plugin\Exception\PluginException;
/**
* Prepare a handler's data by checking defaults and such.
*/
function _views_prepare_handler($definition, $data, $field, $type) {
foreach (array('group', 'title', 'title short', 'help', 'real field') 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];
}
}
}
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
......@@ -59,7 +20,7 @@ function views_get_table_join($table, $base_table) {
else {
$id = 'standard';
}
$handler = views_get_plugin_instance('join', $id);
$handler = views_get_plugin('join', $id);
// Fill in some easy defaults
$handler->definition = $h;
......
......@@ -11,6 +11,7 @@
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery;
use Drupal\Core\Plugin\Discovery\CacheDecorator;
use Drupal\Component\Plugin\Exception\PluginException;
class ViewsPluginManager extends PluginManagerBase {
......@@ -74,52 +75,24 @@ 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.
* Creates a plugin from an ID.
*
* @param string $plugin_id
* The plugin ID.
* @param array|false $definition
* Either the definition array, or FALSE if it needs to be loaded.
*
* @return Drupal\views\Plugin\views\PluginBase
* The plugin instance.
*/
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);
public function createPluginFromId($plugin_id, $definition = FALSE) {
if (!$definition) {
$definition = $this->getDefinition($plugin_id);
}
catch (PluginException $e) {
$instance = $this->createInstance($definition['id']);
if (!empty($definition)) {
$instance = $this->createInstance($plugin_id, array('type' => $this->type, 'definition' => $definition));
return $instance;
}
$instance->is_handler = TRUE;
$instance->plugin_type = $this->$type;
$instance->setDefinition($definition);
// let the handler have something like a constructor.
$instance->construct();
return $instance;
}
}
......@@ -69,6 +69,14 @@ abstract class HandlerBase extends PluginBase {
*/
public $relationship = NULL;
/**
* Constructs a Handler object.
*/
public function __construct(array $configuration, $plugin_id) {
parent::__construct($configuration, $plugin_id);
$this->is_handler = TRUE;
}
/**
* Init the handler with necessary data.
*
......
......@@ -59,6 +59,12 @@ abstract class PluginBase extends ComponentPluginBase {
public function __construct(array $configuration, $plugin_id) {
$this->configuration = $configuration;
$this->plugin_id = $plugin_id;
$this->plugin_type = $configuration['type'];
$this->definition = $configuration['definition'];
if (isset($this->definition['field'])) {
$this->real_field = $this->definition['field'];
}
$this->construct();
}
/**
......@@ -90,14 +96,7 @@ protected function defineOptions() { return array(); }
* Views handlers use a special construct function so that we can more
* easily construct them with variable arguments.
*/
public function construct() { $this->setDefaultOptions(); }
/**
* Set default options.
* For backward compatibility, it sends the options array; this is a
* feature that will likely disappear at some point.
*/
protected function setDefaultOptions() {
public function construct() {
$this->setOptionDefaults($this->options, $this->defineOptions());
}
......@@ -138,7 +137,7 @@ public function unpackOptions(&$storage, $options, $definition = NULL, $all = TR
$localization_keys = $this->localization_keys;
}
// but plugins don't because there isn't a common init() these days.
else if (!empty($this->is_plugin)) {
else if (empty($this->is_handler)) {
if ($this->plugin_type != 'display') {
$localization_keys = array($this->view->current_display);
$localization_keys[] = $this->plugin_type;
......@@ -197,19 +196,6 @@ public function unpackOptions(&$storage, $options, $definition = NULL, $all = TR
}
}
/**
* Let the handler know what its full definition is.
*/
public function setDefinition($definition) {
$this->definition = $definition;
if (isset($definition['id'])) {
$this->plugin_id = $definition['id'];
}
if (isset($definition['field'])) {
$this->real_field = $definition['field'];
}
}
public function destroy() {
if (isset($this->view)) {
unset($this->view);
......
......@@ -24,6 +24,7 @@ public function adminLabel($short = FALSE) {
return t('Broken/missing handler');
}
public function defineOptions() { return array(); }
public function ensureMyTable() { /* No table to ensure! */ }
public function query($group_by = FALSE) { /* No query to run */ }
function render($empty = FALSE) { return ''; }
......
......@@ -24,6 +24,7 @@ public function adminLabel($short = FALSE) {
return t('Broken/missing handler');
}
public function defineOptions() { return array(); }
public function ensureMyTable() { /* No table to ensure! */ }
public function query($group_by = FALSE) { /* No query to run */ }
public function buildOptionsForm(&$form, &$form_state) {
......
......@@ -817,7 +817,7 @@ public function getPlugin($type = 'style', $name = NULL) {
$plugin = views_get_plugin($type, $name);
}
else {
$plugin = views_get_plugin_instance('query', $name);
$plugin = views_get_plugin('query', $name);
}
if (!$plugin) {
......
......@@ -24,6 +24,7 @@ public function adminLabel($short = FALSE) {
return t('Broken/missing handler');
}
public function defineOptions() { return array(); }
public function ensureMyTable() { /* No table to ensure! */ }
public function query($group_by = FALSE) { /* No query to run */ }
public function buildOptionsForm(&$form, &$form_state) {
......
......@@ -24,6 +24,7 @@ public function adminLabel($short = FALSE) {
return t('Broken/missing handler');
}
public function defineOptions() { return array(); }
public function ensureMyTable() { /* No table to ensure! */ }
public function query($group_by = FALSE) { /* No query to run */ }
public function buildOptionsForm(&$form, &$form_state) {
......
......@@ -1663,7 +1663,7 @@ function get_aggregation_info() {
'method' => 'aggregation_method_simple',
'handler' => array(
'argument' => 'groupby_numeric',
'field' => 'groupby_numeric',
'field' => 'numeric',
'filter' => 'groupby_numeric',
'sort' => 'groupby_numeric',
),
......@@ -1673,7 +1673,7 @@ function get_aggregation_info() {
'method' => 'aggregation_method_distinct',
'handler' => array(
'argument' => 'groupby_numeric',
'field' => 'groupby_numeric',
'field' => 'numeric',
'filter' => 'groupby_numeric',
'sort' => 'groupby_numeric',
),
......@@ -1683,7 +1683,7 @@ function get_aggregation_info() {
'method' => 'aggregation_method_simple',
'handler' => array(
'argument' => 'groupby_numeric',
'field' => 'groupby_numeric',
'field' => 'numeric',
'filter' => 'groupby_numeric',
'sort' => 'groupby_numeric',
),
......@@ -1693,7 +1693,7 @@ function get_aggregation_info() {
'method' => 'aggregation_method_simple',
'handler' => array(
'argument' => 'groupby_numeric',
'field' => 'groupby_numeric',
'field' => 'numeric',
'filter' => 'groupby_numeric',
'sort' => 'groupby_numeric',
),
......@@ -1703,7 +1703,7 @@ function get_aggregation_info() {
'method' => 'aggregation_method_simple',
'handler' => array(
'argument' => 'groupby_numeric',
'field' => 'groupby_numeric',
'field' => 'numeric',
'filter' => 'groupby_numeric',
'sort' => 'groupby_numeric',
),
......@@ -1713,7 +1713,7 @@ function get_aggregation_info() {
'method' => 'aggregation_method_simple',
'handler' => array(
'argument' => 'groupby_numeric',
'field' => 'groupby_numeric',
'field' => 'numeric',
'filter' => 'groupby_numeric',
'sort' => 'groupby_numeric',
),
......@@ -1723,7 +1723,7 @@ function get_aggregation_info() {
'method' => 'aggregation_method_simple',
'handler' => array(
'argument' => 'groupby_numeric',
'field' => 'groupby_numeric',
'field' => 'numeric',
'filter' => 'groupby_numeric',
'sort' => 'groupby_numeric',
),
......
......@@ -24,6 +24,7 @@ public function adminLabel($short = FALSE) {
return t('Broken/missing handler');
}
public function defineOptions() { return array(); }
public function ensureMyTable() { /* No table to ensure! */ }
public function query() { /* No query to run */ }
public function buildOptionsForm(&$form, &$form_state) {
......
......@@ -377,7 +377,7 @@ public function query() {
else {
$id = 'subquery';
}
$join = views_get_plugin_instance('join', $id);
$join = views_get_plugin('join', $id);
$join->definition = $def;
$join->construct();
......
......@@ -138,7 +138,7 @@ public function query() {
else {
$id = 'standard';
}
$join = views_get_plugin_instance('join', $id);
$join = views_get_plugin('join', $id);
$join->definition = $def;
$join->options = $this->options;
......
......@@ -24,6 +24,7 @@ public function adminLabel($short = FALSE) {
return t('Broken/missing handler');
}
public function defineOptions() { return array(); }
public function ensureMyTable() { /* No table to ensure! */ }
public function query($group_by = FALSE) { /* No query to run */ }
public function buildOptionsForm(&$form, &$form_state) {
......
......@@ -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_plugin_instance('join');
$join = views_get_plugin('join', 'standard');
$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']);
......
......@@ -12,14 +12,6 @@
*/
interface WizardInterface {
/**
* Constructs a wizard plugin object.
*
* @param array $definition
* The information stored in the annotation definition.
*/
function __construct(array $definition);
/**
* Form callback to build other elements in the "show" form.
*
......@@ -48,7 +40,7 @@ function build_form(array $form, array &$form_state);
* An empty array if the view is valid; an array of error strings if it is
* not.
*/
public function validate(array $form, array &$form_state);
public function validateView(array $form, array &$form_state);
/**
* Creates a view from values that have already been validated.
......
......@@ -8,6 +8,7 @@
namespace Drupal\views\Plugin\views\wizard;
use Drupal\views\View;
use Drupal\views\Plugin\views\PluginBase;
use Drupal\views\Plugin\views\wizard\WizardInterface;
/**
......@@ -16,7 +17,7 @@
* This is a very generic Views Wizard class that can be constructed for any
* base table.
*/
abstract class WizardPluginBase implements WizardInterface {
abstract class WizardPluginBase extends PluginBase implements WizardInterface {
/**
* The base table connected with the wizard.
......@@ -49,13 +50,6 @@ abstract class WizardPluginBase implements WizardInterface {
*/
protected $validated_views = array();
/**
* The wizard plugin definition, like the base_table.
*
* @var array
*/
protected $plugin = array();
/**
* The table column used for sorting by create date of this wizard.
*
......@@ -118,12 +112,8 @@ abstract class WizardPluginBase implements WizardInterface {
* @param array $definition
* The information stored in the annotation definition.
*/
function __construct(array $definition) {
$this->base_table = $definition['base_table'];
// @todo: Move plugin to definition to keep it consistent with the rest of
// views.
$this->plugin = $definition;
function construct() {
$this->base_table = $this->definition['base_table'];
$entities = entity_get_info();
foreach ($entities as $entity_type => $entity_info) {
......@@ -999,7 +989,7 @@ protected function set_validated_view(array $form, array &$form_state, View $vie
*
* Instantiates the view from the form submission and validates its values.
*/
public function validate(array $form, array &$form_state) {
public function validateView(array $form, array &$form_state) {
$view = $this->instantiate_view($form, $form_state);
$errors = $view->validate();
if (!is_array($errors) || empty($errors)) {
......
......@@ -22,7 +22,7 @@ class UpgradeTestCase extends ViewTestBase {
*
* @var array
*/
public static $modules = array('views_ui', 'php');
public static $modules = array('views_ui', 'block', 'php');
public static function getInfo() {
return array(
......
......@@ -1046,7 +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';
$this->query = views_get_plugin_instance('query', $plugin);
$this->query = views_get_plugin('query', $plugin);
if (empty($this->query)) {
return FALSE;
......
......@@ -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_plugin_instance('join');
$join = views_get_plugin('join', 'standard');
$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_plugin_instance('join');
$join = views_get_plugin('join', 'standard');
$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_plugin_instance('join', $id);
$first_join = views_get_plugin('join', $id);
$first_join->definition = $first;
$first_join->construct();
......@@ -85,7 +85,7 @@ public function query() {
else {
$id = 'standard';
}
$second_join = views_get_plugin_instance('join', $id);
$second_join = views_get_plugin('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_plugin_instance('join');
$join = views_get_plugin('join', 'standard');
$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_plugin_instance('join');
$join = views_get_plugin('join', 'standard');
$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_plugin_instance('join');
$join = views_get_plugin('join', 'standard');
$join->definition = $def;
$join->construct();
......
......@@ -101,7 +101,7 @@ public function query() {
else {
$id = 'standard';
}
$join = views_get_plugin_instance('join', $id);
$join = views_get_plugin('join', $id);
$join->definition = $def;
$join->construct();
......
......@@ -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_get_plugin_defintion('display', $view->display[$display_id]->display_plugin);
$plugin = views_get_plugin_definition('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_get_plugin_definitions($table);
$data = views_fetch_data($table, FALSE);
$handler = NULL;
views_include('handlers');
......@@ -1329,7 +1329,30 @@ function views_get_handler($table, $field, $key, $override = NULL) {
$data[$field][$key]['override handler'] = $override;
}
$handler = _views_prepare_handler($data[$field][$key], $data, $field, $key);
$definition = $data[$field][$key];
$type = $key;
foreach (array('group', 'title', 'title short', 'help', 'real field') 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.
$manager = new ViewsPluginManager($type);
$plugin_id = !empty($definition['override handler']) ? $definition['override handler'] : $definition['id'];
try {
return $manager->createPluginFromId($plugin_id, $definition);
}
catch (PluginException $e) {
return $manager->createPluginFromId($definition['id'], $definition);
}
}
if ($handler) {
......@@ -1344,7 +1367,12 @@ function views_get_handler($table, $field, $key, $override = NULL) {
'table' => $table,
'field' => $field,
);
return _views_create_handler($key, $broken);
// In case the manager was already initialized above, try to reuse it.
if (!isset($manager)) {
$manager = new ViewsPluginManager($key);
}
return $manager->createPluginFromId('broken');
}
/**
......@@ -1402,24 +1430,30 @@ function views_fetch_plugin_names($type, $key = NULL, $base = array()) {
}
/**
* Get a handler for a plugin
* Get an instance of a plugin.