From 9696e518fb008fb5a3345d7c66bb2aa84a1c533a Mon Sep 17 00:00:00 2001 From: Alex Pott <alex.a.pott@googlemail.com> Date: Wed, 16 Apr 2014 12:44:16 +0100 Subject: [PATCH] Issue #2174021 by Xano, dawehner, damiankloip: Make \Drupal\views\Plugin\views\HandlerBase unit-testable. --- .../views/Plugin/ViewsHandlerManager.php | 12 +++ .../Drupal/views/Plugin/views/HandlerBase.php | 82 ++++++++++++++++--- 2 files changed, 84 insertions(+), 10 deletions(-) diff --git a/core/modules/views/lib/Drupal/views/Plugin/ViewsHandlerManager.php b/core/modules/views/lib/Drupal/views/Plugin/ViewsHandlerManager.php index 79f757b3cddb..dae3026e6cc9 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/ViewsHandlerManager.php +++ b/core/modules/views/lib/Drupal/views/Plugin/ViewsHandlerManager.php @@ -130,4 +130,16 @@ public function getHandler($item, $override = NULL) { return $this->createInstance('broken', array('optional' => $optional, 'original_configuration' => $item)); } + /** + * {@inheritdoc} + */ + public function createInstance($plugin_id, array $configuration = array()) { + $instance = parent::createInstance($plugin_id, $configuration); + if ($instance instanceof HandlerBase) { + $instance->setModuleHandler($this->moduleHandler); + $instance->setViewsData($this->viewsData); + } + return $instance; + } + } diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php index 6196fc326468..e0bf952fe644 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php @@ -11,6 +11,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\Component\Utility\UrlHelper; use Drupal\Component\Utility\Xss; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Render\Element; use Drupal\Core\Session\AccountInterface; use Drupal\views\Plugin\views\display\DisplayPluginBase; @@ -18,6 +19,7 @@ use Drupal\views\ViewExecutable; use Drupal\Core\Database\Database; use Drupal\views\Views; +use Drupal\views\ViewsData; abstract class HandlerBase extends PluginBase { @@ -85,6 +87,20 @@ abstract class HandlerBase extends PluginBase { */ protected $optional = FALSE; + /** + * The module handler. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * The views data service. + * + * @var \Drupal\views\ViewsData + */ + protected $viewsData; + /** * Constructs a Handler object. */ @@ -174,7 +190,7 @@ public function adminLabel($short = FALSE) { return $title; } $title = ($short && isset($this->definition['title short'])) ? $this->definition['title short'] : $this->definition['title']; - return t('!group: !title', array('!group' => $this->definition['group'], '!title' => $title)); + return $this->t('!group: !title', array('!group' => $this->definition['group'], '!title' => $title)); } /** @@ -260,9 +276,9 @@ protected function caseTransform($string, $option) { default: return $string; case 'upper': - return drupal_strtoupper($string); + return Unicode::strtoupper($string); case 'lower': - return drupal_strtolower($string); + return Unicode::strtolower($string); case 'ucfirst': return Unicode::ucfirst($string); case 'ucwords': @@ -294,8 +310,8 @@ public function buildOptionsForm(&$form, &$form_state) { ); $form['admin_label']['admin_label'] = array( '#type' => 'textfield', - '#title' => t('Administrative title'), - '#description' => t('This title will be displayed on the views edit page instead of the default one. This might be useful if you have the same item twice.'), + '#title' => $this->t('Administrative title'), + '#description' => $this->t('This title will be displayed on the views edit page instead of the default one. This might be useful if you have the same item twice.'), '#default_value' => $this->options['admin_label'], '#parents' => array('options', 'admin_label'), ); @@ -309,7 +325,30 @@ public function buildOptionsForm(&$form, &$form_state) { ); // Allow to alter the default values brought into the form. // @todo Do we really want to keep this hook. - \Drupal::moduleHandler()->alter('views_handler_options', $this->options, $this->view); + $this->getModuleHandler()->alter('views_handler_options', $this->options, $this->view); + } + + /** + * Gets the module handler. + * + * @return \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected function getModuleHandler() { + if (!$this->moduleHandler) { + $this->moduleHandler = \Drupal::moduleHandler(); + } + + return $this->moduleHandler; + } + + /** + * Sets the module handler. + * + * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler + * The module handler. + */ + public function setModuleHandler(ModuleHandlerInterface $module_handler) { + $this->moduleHandler = $module_handler; } /** @@ -336,9 +375,9 @@ public function buildGroupByForm(&$form, &$form_state) { $form['group_type'] = array( '#type' => 'select', - '#title' => t('Aggregation type'), + '#title' => $this->t('Aggregation type'), '#default_value' => $this->options['group_type'], - '#description' => t('Select the aggregation function to use on this field.'), + '#description' => $this->t('Select the aggregation function to use on this field.'), '#options' => $group_types, ); } @@ -649,6 +688,29 @@ public function getDateField() { return $this->query->getDateField("$this->tableAlias.$this->realField"); } + /** + * Gets views data service. + * + * @return \Drupal\views\ViewsData + */ + protected function getViewsData() { + if (!$this->viewsData) { + $this->viewsData = Views::viewsData(); + } + + return $this->viewsData; + } + + /** + * Sets the views data service. + * + * @param \Drupal\views\ViewsData $views_data + * The views data. + */ + public function setViewsData(ViewsData $views_data) { + $this->viewsData = $views_data; + } + /** * Fetches a handler to join one table to a primary table from the data cache. * @@ -705,10 +767,10 @@ public function getEntityType() { // If the user has configured a relationship on the handler take that into // account. if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') { - $views_data = Views::viewsData()->get($this->view->relationship->table); + $views_data = $this->getViewsData()->get($this->view->relationship->table); } else { - $views_data = Views::viewsData()->get($this->view->storage->get('base_table')); + $views_data = $this->getViewsData()->get($this->view->storage->get('base_table')); } if (isset($views_data['table']['entity type'])) { -- GitLab