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 82d14f9..d65ce93 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/PluginBase.php @@ -73,8 +73,8 @@ public function __construct(array $configuration, $plugin_id, array $plugin_defi * The options configured for this plugin. */ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) { - $this->setOptionDefaults($this->options, $this->defineOptions()); $this->view = $view; + $this->setOptionDefaults($this->options, $this->defineOptions()); $this->displayHandler = $display; $this->unpackOptions($this->options, $options); diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php b/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php index 5f20f4a..3121f62 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php @@ -410,7 +410,15 @@ function use_string_group_by() { 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 field labels enabled by + // default. + if (!isset($this->view->style_plugin)) { + $this->view->initStyle(); + } + if ($this->view->style_plugin->defaultFieldLabels()) { + $options['label']['default'] = $this->definition['title']; + } $options['exclude'] = array('default' => FALSE, 'bool' => TRUE); $options['alter'] = array( 'contains' => array( 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 5f993d4..cd9932e 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 @@ -83,6 +83,13 @@ protected $rendered_fields; /** + * Should field labels be enabled by default. + * + * @var bool + */ + protected $defaultFieldLabels = FALSE; + + /** * Overrides \Drupal\views\Plugin\views\PluginBase::init(). * * The style options might come externally as the style can be sourced from at @@ -168,6 +175,15 @@ function uses_tokens() { } /** + * 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. */ function get_row_class($row_index) { diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/style/Table.php b/core/modules/views/lib/Drupal/views/Plugin/views/style/Table.php index 8ab0337..8af440e 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/style/Table.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/style/Table.php @@ -48,6 +48,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/lib/Drupal/views/Tests/Handler/FieldWebTest.php b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php index 43acdab..3246a6d 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Handler/FieldWebTest.php @@ -330,6 +330,8 @@ public function testFieldClasses() { $id_field = $view->field['id']; $id_field->options['element_default_classes'] = FALSE; + // Setup some kind of label by default. + $id_field->options['label'] = $this->randomName(); $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/lib/Drupal/views/Tests/Plugin/StyleTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleTest.php index f567068..a6bb309 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/StyleTest.php @@ -123,12 +123,14 @@ function _testGrouping($stripped = FALSE) { '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', diff --git a/core/modules/views/lib/Drupal/views/Tests/UI/FieldTest.php b/core/modules/views/lib/Drupal/views/Tests/UI/FieldTest.php new file mode 100644 index 0000000..0d0bf90 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/UI/FieldTest.php @@ -0,0 +1,44 @@ + 'Field UI', + 'description' => 'Tests the UI of field handlers.', + 'group' => 'Views UI', + ); + } + + /** + * 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['human_name'] = $this->randomName(16); + $view['name'] = strtolower($this->randomName(16)); + $view['show[wizard_key]'] = 'node'; + $view['page[create]'] = TRUE; + $view['page[style][style_plugin]'] = 'default'; + $view['page[path]'] = $view['name']; + $this->drupalPost('admin/structure/views/add', $view, t('Save & exit')); + + $view = views_get_view($view['name']); + $view->initHandlers(); + $this->assertEqual($view->field['title']->options['label'], '', 'The field label for normal styles are empty.'); + } +} diff --git a/core/modules/views/lib/Drupal/views/Tests/UI/StyleTableTest.php b/core/modules/views/lib/Drupal/views/Tests/UI/StyleTableTest.php new file mode 100644 index 0000000..ca526e7 --- /dev/null +++ b/core/modules/views/lib/Drupal/views/Tests/UI/StyleTableTest.php @@ -0,0 +1,44 @@ + 'Style: Table UI', + 'description' => 'Tests the UI of views when using the table style.', + 'group' => 'Views UI', + ); + } + + /** + * Tests created a table style view. + */ + public function testWizard() { + // Create a new view and check that the first field has a label. + $view = array(); + $view['human_name'] = $this->randomName(16); + $view['name'] = strtolower($this->randomName(16)); + $view['show[wizard_key]'] = 'node'; + $view['page[create]'] = TRUE; + $view['page[style][style_plugin]'] = 'table'; + $view['page[path]'] = $view['name']; + $this->drupalPost('admin/structure/views/add', $view, t('Save & exit')); + + $view = views_get_view($view['name']); + $view->initHandlers(); + $this->assertEqual($view->field['title']->options['label'], 'Title', 'The field label for table styles is not empty.'); + } + +} diff --git a/core/modules/views/tests/views_test_config/test_views/views.view.test_click_sort.yml b/core/modules/views/tests/views_test_config/test_views/views.view.test_click_sort.yml index f387260..7aad205 100644 --- a/core/modules/views/tests/views_test_config/test_views/views.view.test_click_sort.yml +++ b/core/modules/views/tests/views_test_config/test_views/views.view.test_click_sort.yml @@ -10,16 +10,19 @@ display: id: id table: views_test_data field: id + label: ID plugin_id: numeric name: id: name table: views_test_data field: name + label: Name plugin_id: string created: id: created table: views_test_data field: created + label: Created plugin_id: date display_options: access: