From d60d5be0b968e43b902e6f745f2666b6d8a9470c Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Tue, 9 Sep 2014 10:47:17 +0100 Subject: [PATCH] Issue #1831674 by lokapujya, longwave, dawehner, Mirakolous, neoligero, Kevin Morse | Bojhan: "Create a label" should be off by default, with an opt-in for style plugins. --- .../views.view.test_user_changed.yml | 1 + .../Plugin/views/field/FieldPluginBase.php | 12 ++++-- .../Plugin/views/style/StylePluginBase.php | 16 ++++++++ .../views/src/Plugin/views/style/Table.php | 7 ++++ .../views/src/Tests/Handler/FieldWebTest.php | 2 + .../views/src/Tests/Plugin/StyleTest.php | 3 ++ .../test_views/views.view.test_click_sort.yml | 3 ++ .../views_ui/src/Tests/FieldUITest.php | 30 ++++++++++++-- .../views_ui/src/Tests/GroupByTest.php | 2 +- .../views_ui/src/Tests/StyleTableTest.php | 40 +++++++++++++++++++ 10 files changed, 109 insertions(+), 7 deletions(-) create mode 100644 core/modules/views_ui/src/Tests/StyleTableTest.php diff --git a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_changed.yml b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_changed.yml index f4f820fd2a0..b1d90805f5d 100644 --- a/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_changed.yml +++ b/core/modules/user/tests/modules/user_test_views/test_views/views.view.test_user_changed.yml @@ -27,6 +27,7 @@ display: id: changed table: users_field_data field: changed + label: Updated date provider: user date_format: html_date filters: { } diff --git a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php index d4e04f943ed..5fffc0bd679 100644 --- a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php @@ -428,7 +428,13 @@ abstract class FieldPluginBase extends HandlerBase { protected function defineOptions() { $options = parent::defineOptions(); - $options['label'] = array('default' => $this->definition['title'], 'translatable' => TRUE); + $options['label'] = array('default' => '', 'translatable' => TRUE); + // Some styles (for example table) should have labels enabled by default. + $style = $this->view->getStyle(); + if (isset($style) && $style->defaultFieldLabels()) { + $options['label']['default'] = $this->definition['title']; + } + $options['exclude'] = array('default' => FALSE, 'bool' => TRUE); $options['alter'] = array( 'contains' => array( @@ -864,10 +870,10 @@ abstract class FieldPluginBase extends HandlerBase { // Setup the tokens for fields. $previous = $this->getPreviousFieldLabels(); foreach ($previous as $id => $label) { - $options[t('Fields')]["[$id]"] = $label; + $options[t('Fields')]["[$id]"] = substr(strrchr($label, ":"), 2 ); } // Add the field to the list of options. - $options[t('Fields')]["[{$this->options['id']}]"] = $this->label(); + $options[t('Fields')]["[{$this->options['id']}]"] = substr(strrchr($this->adminLabel(), ":"), 2 ); $count = 0; // This lets us prepare the key as we want it printed. foreach ($this->view->display_handler->getHandlers('argument') as $arg => $handler) { diff --git a/core/modules/views/src/Plugin/views/style/StylePluginBase.php b/core/modules/views/src/Plugin/views/style/StylePluginBase.php index 0cafc1d66ec..b1f8e4e36f6 100644 --- a/core/modules/views/src/Plugin/views/style/StylePluginBase.php +++ b/core/modules/views/src/Plugin/views/style/StylePluginBase.php @@ -104,6 +104,13 @@ abstract class StylePluginBase extends PluginBase { */ protected $groupingTheme = 'views_view_grouping'; + /** + * Should field labels be enabled by default. + * + * @var bool + */ + protected $defaultFieldLabels = FALSE; + /** * Overrides \Drupal\views\Plugin\views\PluginBase::init(). * @@ -189,6 +196,15 @@ abstract class StylePluginBase extends PluginBase { } } + /** + * Return TRUE if this style enables field labels by default. + * + * @return bool + */ + public function defaultFieldLabels() { + return $this->defaultFieldLabels; + } + /** * Return the token replaced row class for the specified row. */ diff --git a/core/modules/views/src/Plugin/views/style/Table.php b/core/modules/views/src/Plugin/views/style/Table.php index 69c8b180081..92012fed394 100644 --- a/core/modules/views/src/Plugin/views/style/Table.php +++ b/core/modules/views/src/Plugin/views/style/Table.php @@ -49,6 +49,13 @@ class Table extends StylePluginBase { */ protected $usesRowClass = TRUE; + /** + * Should field labels be enabled by default. + * + * @var bool + */ + protected $defaultFieldLabels = TRUE; + /** * Contains the current active sort column. * @var string diff --git a/core/modules/views/src/Tests/Handler/FieldWebTest.php b/core/modules/views/src/Tests/Handler/FieldWebTest.php index 71769f2960f..92800fb9f6c 100644 --- a/core/modules/views/src/Tests/Handler/FieldWebTest.php +++ b/core/modules/views/src/Tests/Handler/FieldWebTest.php @@ -326,6 +326,8 @@ class FieldWebTest extends HandlerTestBase { $id_field = $view->field['id']; $id_field->options['element_default_classes'] = FALSE; + // Setup some kind of label by default. + $id_field->options['label'] = $this->randomMachineName(); $output = $view->preview(); $output = drupal_render($output); $this->assertFalse($this->xpathContent($output, '//div[contains(@class, :class)]', array(':class' => 'field-content'))); diff --git a/core/modules/views/src/Tests/Plugin/StyleTest.php b/core/modules/views/src/Tests/Plugin/StyleTest.php index 074abb77620..72d7403e2b7 100644 --- a/core/modules/views/src/Tests/Plugin/StyleTest.php +++ b/core/modules/views/src/Tests/Plugin/StyleTest.php @@ -121,18 +121,21 @@ class StyleTest extends ViewTestBase { 'table' => 'views_test_data', 'field' => 'name', 'relationship' => 'none', + 'label' => 'Name', ), 'job' => array( 'id' => 'job', 'table' => 'views_test_data', 'field' => 'job', 'relationship' => 'none', + 'label' => 'Job', ), 'age' => array( 'id' => 'age', 'table' => 'views_test_data', 'field' => 'age', 'relationship' => 'none', + 'label' => 'Age', ), )); diff --git a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_click_sort.yml b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_click_sort.yml index 32d25cfdfc6..2bcbbda0c95 100644 --- a/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_click_sort.yml +++ b/core/modules/views/tests/modules/views_test_config/test_views/views.view.test_click_sort.yml @@ -10,18 +10,21 @@ display: id: id table: views_test_data field: id + label: ID plugin_id: numeric provider: views_test_data name: id: name table: views_test_data field: name + label: Name plugin_id: string provider: views_test_data created: id: created table: views_test_data field: created + label: created plugin_id: date provider: views_test_data display_options: diff --git a/core/modules/views_ui/src/Tests/FieldUITest.php b/core/modules/views_ui/src/Tests/FieldUITest.php index 9c3e43b95b1..fac6e466cf7 100644 --- a/core/modules/views_ui/src/Tests/FieldUITest.php +++ b/core/modules/views_ui/src/Tests/FieldUITest.php @@ -7,6 +7,8 @@ namespace Drupal\views_ui\Tests; +use Drupal\views\Views; + /** * Tests the UI of field handlers. * @@ -28,14 +30,14 @@ class FieldUITest extends UITestBase { public function testFieldUI() { // Ensure the field is not marked as hidden on the first run. $this->drupalGet('admin/structure/views/view/test_view/edit'); - $this->assertText('Views test: Name (Name)'); - $this->assertNoText('Views test: Name (Name) [' . t('hidden') . ']'); + $this->assertText('Views test: Name'); + $this->assertNoText('Views test: Name [' . t('hidden') . ']'); // Hides the field and check whether the hidden label is appended. $edit_handler_url = 'admin/structure/views/nojs/handler/test_view/default/field/name'; $this->drupalPostForm($edit_handler_url, array('options[exclude]' => TRUE), t('Apply')); - $this->assertText('Views test: Name (Name) [' . t('hidden') . ']'); + $this->assertText('Views test: Name [' . t('hidden') . ']'); // Ensure that the expected tokens appear in the UI. $edit_handler_url = 'admin/structure/views/nojs/handler/test_view/default/field/age'; @@ -57,4 +59,26 @@ class FieldUITest extends UITestBase { $this->assertEqual((string) $result[2], '[name] == Name'); } + /** + * Tests the field labels. + */ + public function testFieldLabel() { + // Create a view with unformatted style and make sure the fields have no + // labels by default. + $view = array(); + $view['label'] = $this->randomMachineName(16); + $view['id'] = strtolower($this->randomMachineName(16)); + $view['description'] = $this->randomMachineName(16); + $view['show[wizard_key]'] = 'node'; + $view['page[create]'] = TRUE; + $view['page[style][style_plugin]'] = 'default'; + $view['page[title]'] = $this->randomMachineName(16); + $view['page[path]'] = $view['id']; + $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit')); + + $view = Views::getView($view['id']); + $view->initHandlers(); + $this->assertEqual($view->field['title']->options['label'], '', 'The field label for normal styles are empty.'); + } + } diff --git a/core/modules/views_ui/src/Tests/GroupByTest.php b/core/modules/views_ui/src/Tests/GroupByTest.php index 04db69cdc5e..a359be6a2ec 100644 --- a/core/modules/views_ui/src/Tests/GroupByTest.php +++ b/core/modules/views_ui/src/Tests/GroupByTest.php @@ -42,7 +42,7 @@ class GroupByTest extends UITestBase { // Change the groupby type in the UI. $this->drupalPostForm($edit_groubpy_url, array('options[group_type]' => 'count'), t('Apply')); - $this->assertLink('COUNT(Views test: ID) (ID)', 0, 'The count setting is displayed in the UI'); + $this->assertLink('COUNT(Views test: ID)', 0, 'The count setting is displayed in the UI'); $this->drupalPostForm(NULL, array(), t('Save')); diff --git a/core/modules/views_ui/src/Tests/StyleTableTest.php b/core/modules/views_ui/src/Tests/StyleTableTest.php new file mode 100644 index 00000000000..0cd3982911d --- /dev/null +++ b/core/modules/views_ui/src/Tests/StyleTableTest.php @@ -0,0 +1,40 @@ +randomMachineName(16); + $view['id'] = strtolower($this->randomMachineName(16)); + $view['show[wizard_key]'] = 'node'; + $view['page[create]'] = TRUE; + $view['page[style][style_plugin]'] = 'table'; + $view['page[title]'] = $this->randomMachineName(16); + $view['page[path]'] = $view['id']; + $this->drupalPostForm('admin/structure/views/add', $view, t('Save and edit')); + + $view = Views::getView($view['id']); + $view->initHandlers(); + $this->assertEqual($view->field['title']->options['label'], 'Title', 'The field label for table styles is not empty.'); + } + +}