From fc9584d55a2b85bca7f50af633a157f030def98e Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Fri, 28 Jun 2013 07:14:39 +0100 Subject: [PATCH] Issue #2027031 by damiankloip: Move views_theme_functions() to ViewExecutable method. --- .../Drupal/views/Plugin/views/PluginBase.php | 2 +- .../views/display/DisplayPluginBase.php | 2 +- .../Drupal/views/Plugin/views/pager/Full.php | 2 +- .../Plugin/views/style/StylePluginBase.php | 2 +- .../Drupal/views/Tests/ViewExecutableTest.php | 2 +- .../views/lib/Drupal/views/ViewExecutable.php | 34 +++++++ .../views/Tests/ViewExecutableUnitTest.php | 89 +++++++++++++++++++ core/modules/views/views.module | 10 +-- core/modules/views/views.theme.inc | 33 +------ 9 files changed, 130 insertions(+), 46 deletions(-) create mode 100644 core/modules/views/tests/Drupal/views/Tests/ViewExecutableUnitTest.php diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php index bc8769ed615..5693a38b4f0 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php @@ -207,7 +207,7 @@ abstract class PluginBase extends ContainerFactoryPluginBase { * Provide a full list of possible theme templates used by this style. */ public function themeFunctions() { - return views_theme_functions($this->definition['theme'], $this->view, $this->view->display_handler->display); + return $this->view->buildThemeFunctions($this->definition['theme']); } /** diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php index 64b337e9507..b0cbb7ba24c 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php @@ -2452,7 +2452,7 @@ abstract class DisplayPluginBase extends PluginBase { if (!empty($this->view->exposed_raw_input)) { $url_options['query'] = $this->view->exposed_raw_input; } - $theme = views_theme_functions('views_more', $this->view, $this->view->display_handler->display); + $theme = $this->view->buildThemeFunctions('views_more'); $path = check_url(url($path, $url_options)); return theme($theme, array('more_url' => $path, 'link_text' => check_plain($this->useMoreText()), 'view' => $this->view)); diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/pager/Full.php b/core/modules/views/lib/Drupal/views/Plugin/views/pager/Full.php index 08a5978eed1..52adb761821 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/pager/Full.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/pager/Full.php @@ -81,7 +81,7 @@ class Full extends SqlBase { * Overrides \Drupal\views\Plugin\views\pager\PagerPluginBase::render(). */ function render($input) { - $pager_theme = views_theme_functions('pager', $this->view, $this->view->display_handler->display); + $pager_theme = $this->view->buildThemeFunctions('pager'); // The 0, 1, 3, 4 index are correct. See theme_pager documentation. $tags = array( 0 => $this->options['tags']['first'], diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/style/StylePluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/style/StylePluginBase.php index a221f8fc754..e271d8e8627 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/style/StylePluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/style/StylePluginBase.php @@ -457,7 +457,7 @@ abstract class StylePluginBase extends PluginBase { */ public function renderGroupingSets($sets, $level = 0) { $output = array(); - $theme_functions = views_theme_functions($this->groupingTheme, $this->view, $this->view->display_handler->display); + $theme_functions = $this->view->buildThemeFunctions($this->groupingTheme); foreach ($sets as $set) { $row = reset($set['rows']); // Render as a grouping set. diff --git a/core/modules/views/lib/Drupal/views/Tests/ViewExecutableTest.php b/core/modules/views/lib/Drupal/views/Tests/ViewExecutableTest.php index 37d4d8191bb..ee363700cd0 100644 --- a/core/modules/views/lib/Drupal/views/Tests/ViewExecutableTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/ViewExecutableTest.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\views\Tests\ViewExecutableTest. + * Contains \Drupal\views\Tests\ViewExecutableUnitTest. */ namespace Drupal\views\Tests; diff --git a/core/modules/views/lib/Drupal/views/ViewExecutable.php b/core/modules/views/lib/Drupal/views/ViewExecutable.php index 25c387e46f1..84af7bb5a76 100644 --- a/core/modules/views/lib/Drupal/views/ViewExecutable.php +++ b/core/modules/views/lib/Drupal/views/ViewExecutable.php @@ -10,6 +10,7 @@ namespace Drupal\views; use Drupal; use Symfony\Component\HttpFoundation\Response; use Drupal\views\ViewStorageInterface; +use Drupal\Component\Utility\Tags; /** * @defgroup views_objects Objects that represent a View or part of a view @@ -2111,4 +2112,37 @@ class ViewExecutable { } } + /** + * Provide a full array of possible theme functions to try for a given hook. + * + * @param string $hook + * The hook to use. This is the base theme/template name. + * + * @return array + * An array of theme hook suggestions. + */ + public function buildThemeFunctions($hook) { + $themes = array(); + $display = isset($this->display_handler) ? $this->display_handler->display : NULL; + $id = $this->storage->id(); + + if ($display) { + $themes[] = $hook . '__' . $id . '__' . $display['id']; + $themes[] = $hook . '__' . $display['id']; + // Add theme suggestions for each single tag. + foreach (Tags::explode($this->storage->get('tag')) as $tag) { + $themes[] = $hook . '__' . preg_replace('/[^a-z0-9]/', '_', strtolower($tag)); + } + + if ($display['id'] != $display['display_plugin']) { + $themes[] = $hook . '__' . $id . '__' . $display['display_plugin']; + $themes[] = $hook . '__' . $display['display_plugin']; + } + } + $themes[] = $hook . '__' . $id; + $themes[] = $hook; + + return $themes; + } + } diff --git a/core/modules/views/tests/Drupal/views/Tests/ViewExecutableUnitTest.php b/core/modules/views/tests/Drupal/views/Tests/ViewExecutableUnitTest.php new file mode 100644 index 00000000000..5855bc43159 --- /dev/null +++ b/core/modules/views/tests/Drupal/views/Tests/ViewExecutableUnitTest.php @@ -0,0 +1,89 @@ + 'View executable test', + 'description' => 'Tests methods on the \Drupal\views\ViewExecutable class', + 'group' => 'Views', + ); + } + + /** + * Tests the buildThemeFunctions() method. + */ + public function testBuildThemeFunctions() { + $config = array( + 'id' => 'test_view', + 'tag' => 'OnE, TWO, and three', + 'display' => array( + 'default' => array( + 'id' => 'default', + 'display_plugin' => 'default', + 'display_title' => 'Default', + ), + ), + ); + + $storage = new View($config, 'view'); + $view = new ViewExecutable($storage); + + $expected = array( + 'test_hook__test_view', + 'test_hook' + ); + $this->assertEquals($expected, $view->buildThemeFunctions('test_hook')); + + // Add a mock display. + $display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase') + ->disableOriginalConstructor() + ->getMock(); + $display->display = $config['display']['default']; + $view->display_handler = $display; + + $expected = array( + 'test_hook__test_view__default', + 'test_hook__default', + 'test_hook__one', + 'test_hook__two', + 'test_hook__and_three', + 'test_hook__test_view', + 'test_hook' + ); + $this->assertEquals($expected, $view->buildThemeFunctions('test_hook')); + + //Change the name of the display plugin and make sure that is in the array. + $view->display_handler->display['display_plugin'] = 'default2'; + + $expected = array( + 'test_hook__test_view__default', + 'test_hook__default', + 'test_hook__one', + 'test_hook__two', + 'test_hook__and_three', + 'test_hook__test_view__default2', + 'test_hook__default2', + 'test_hook__test_view', + 'test_hook' + ); + $this->assertEquals($expected, $view->buildThemeFunctions('test_hook')); + } + +} diff --git a/core/modules/views/views.module b/core/modules/views/views.module index 5daa3313a44..d48526fa1a1 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -1360,7 +1360,7 @@ function views_exposed_form($form, &$form_state) { ); $form['#action'] = url($view->display_handler->getUrl()); - $form['#theme'] = views_theme_functions('views_exposed_form', $view, $display); + $form['#theme'] = $view->buildThemeFunctions('views_exposed_form'); $form['#id'] = drupal_clean_css_identifier('views_exposed_form-' . check_plain($view->storage->id()) . '-' . check_plain($display['id'])); // $form['#attributes']['class'] = array('views-exposed-form'); @@ -1450,14 +1450,6 @@ function views_exposed_form_cache($views_name, $display_name, $form_output = NUL return empty($views_exposed[$views_name][$display_name]) ? FALSE : $views_exposed[$views_name][$display_name]; } -/** - * Build a list of theme function names for use most everywhere. - */ -function views_theme_functions($hook, ViewExecutable $view, $display = NULL) { - Drupal::moduleHandler()->loadInclude('views', 'inc', 'views.theme'); - return _views_theme_functions($hook, $view, $display); -} - /** * Implements hook_query_TAG_alter(). * diff --git a/core/modules/views/views.theme.inc b/core/modules/views/views.theme.inc index 627e8ed7f74..237a91ec9a7 100644 --- a/core/modules/views/views.theme.inc +++ b/core/modules/views/views.theme.inc @@ -10,37 +10,6 @@ use Drupal\Core\Language\Language; use Drupal\Core\Template\Attribute; use Drupal\views\ViewExecutable; -/** - * Provide a full array of possible themes to try for a given hook. - * - * @param string $hook - * The hook to use. This is the base theme/template name. - * @param \Drupal\views\ViewExecutable $view - * The view being rendered. - * @param string|null $display - * The display being rendered, if applicable. - */ -function _views_theme_functions($hook, ViewExecutable $view, $display = NULL) { - $themes = array(); - - if ($display) { - $themes[] = $hook . '__' . $view->storage->id() . '__' . $display['id']; - $themes[] = $hook . '__' . $display['id']; - // Add theme suggestions for each single tag. - foreach (drupal_explode_tags($view->storage->get('tag')) as $tag) { - $themes[] = $hook . '__' . preg_replace('/[^a-z0-9]/', '_', strtolower($tag)); - } - - if ($display['id'] != $display['display_plugin']) { - $themes[] = $hook . '__' . $view->storage->id() . '__' . $display['display_plugin']; - $themes[] = $hook . '__' . $display['display_plugin']; - } - } - $themes[] = $hook . '__' . $view->storage->id(); - $themes[] = $hook; - return $themes; -} - /** * Prepares variables for view templates. * @@ -1252,6 +1221,6 @@ function theme_views_mini_pager($vars) { * Important! When adding a new template to your theme, be sure to flush the * theme registry cache! * - * @see _views_theme_functions() + * @see \Drupal\views\ViewExecutable::buildThemeFunctions() * @} */