diff --git a/core/modules/views/config/schema/views.data_types.schema.yml b/core/modules/views/config/schema/views.data_types.schema.yml index 0683380..bf115c9 100644 --- a/core/modules/views/config/schema/views.data_types.schema.yml +++ b/core/modules/views/config/schema/views.data_types.schema.yml @@ -657,6 +657,9 @@ views_style: rendered_strip: type: boolean label: 'Remove tags from rendered output' + title_element_type: + type: string + label: 'Title element' row_class: type: string label: 'Row class' diff --git a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php index cde5ecb..d9a2c28 100644 --- a/core/modules/views/src/Plugin/views/field/FieldPluginBase.php +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php @@ -11,6 +11,7 @@ use Drupal\Core\Url as CoreUrl; use Drupal\views\Plugin\views\HandlerBase; use Drupal\views\Plugin\views\display\DisplayPluginBase; +use Drupal\views\Plugin\views\style\StylePluginBase; use Drupal\views\Render\ViewsRenderPipelineMarkup; use Drupal\views\ResultRow; use Drupal\views\ViewExecutable; @@ -557,7 +558,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ); $form['element_type'] = array( '#title' => $this->t('HTML element'), - '#options' => $this->getElements(), + '#options' => StylePluginBase::getHtmlElementTypes(), '#type' => 'select', '#default_value' => $this->options['element_type'], '#description' => $this->t('Choose the HTML element to wrap around this field, e.g. H1, H2, etc.'), @@ -602,7 +603,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ); $form['element_label_type'] = array( '#title' => $this->t('Label HTML element'), - '#options' => $this->getElements(FALSE), + '#options' => StylePluginBase::getHtmlElementTypes(), '#type' => 'select', '#default_value' => $this->options['element_label_type'], '#description' => $this->t('Choose the HTML element to wrap around this label, e.g. H1, H2, etc.'), @@ -646,7 +647,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ); $form['element_wrapper_type'] = array( '#title' => $this->t('Wrapper HTML element'), - '#options' => $this->getElements(FALSE), + '#options' => StylePluginBase::getHtmlElementTypes(), '#type' => 'select', '#default_value' => $this->options['element_wrapper_type'], '#description' => $this->t('Choose the HTML element to wrap around this field and label, e.g. H1, H2, etc. This may not be used if the field and label are not rendered together, such as with a table.'), @@ -774,7 +775,7 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ':input[name="options[alter][make_link]"]' => array('checked' => TRUE), ), ), - '#options' => array( + '#options' => array( 'none' => $this->t('No transform'), 'upper' => $this->t('Upper case'), 'lower' => $this->t('Lower case'), diff --git a/core/modules/views/src/Plugin/views/style/StylePluginBase.php b/core/modules/views/src/Plugin/views/style/StylePluginBase.php index 3bee96a..18b976a 100644 --- a/core/modules/views/src/Plugin/views/style/StylePluginBase.php +++ b/core/modules/views/src/Plugin/views/style/StylePluginBase.php @@ -258,6 +258,7 @@ public function evenEmpty() { protected function defineOptions() { $options = parent::defineOptions(); $options['grouping'] = array('default' => array()); + $options['title_element_type'] = array('default' => 'h3'); if ($this->usesRowClass()) { $options['row_class'] = array('default' => ''); $options['default_row_class'] = array('default' => TRUE); @@ -329,6 +330,20 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ); } } + + $form['title_element_type'] = array( + '#title' => t('Title HTML Element'), + '#type' => 'select', + '#default_value' => $this->options['title_element_type'], + '#options' => static::getHtmlElementTypes(), + '#description' => t('Choose the HTML element to wrap around the grouping title of a view.'), + // Hide the element unless at least one grouping field is configured. + '#states' => array( + 'invisible' => array( + ':input[name="style_options[grouping][0][field]"]' => array('value' => ''), + ), + ), + ); } if ($this->usesRowClass()) { @@ -409,6 +424,27 @@ public function wizardSubmit(&$form, FormStateInterface $form_state, WizardInter } /** + * Provide a list of elements valid for field HTML. + * + * This function can be overridden by fields that want more or fewer + * elements available, though this seems like it would be an incredibly + * rare occurence. + */ + public static function getHtmlElementTypes() { + static $elements = NULL; + if (!isset($elements)) { + // @todo Add possible html5 elements. + $elements = array( + '' => t('- Use default -'), + '0' => t('- None -'), + ); + $elements += \Drupal::config('views.settings')->get('field_rewrite_elements'); + } + + return $elements; + } + + /** * Called by the view builder to see if this style handler wants to * interfere with the sorts. If so it should build; if it returns * any non-TRUE value, normal sorting will NOT be added to the query. @@ -505,6 +541,7 @@ public function renderGroupingSets($sets, $level = 0) { '#view' => $this->view, '#grouping' => $this->options['grouping'][$level], '#rows' => $set['rows'], + '#title_element_type' => $this->options['title_element_type'], ); } // Render as a record set. @@ -521,6 +558,7 @@ public function renderGroupingSets($sets, $level = 0) { $single_output['#grouping_level'] = $level; $single_output['#title'] = $set['group']; + $single_output['#title_element_type'] = $this->options['title_element_type']; $output[] = $single_output; } unset($this->view->row_index); diff --git a/core/modules/views/src/Plugin/views/style/Table.php b/core/modules/views/src/Plugin/views/style/Table.php index c49f6e9..e8241fe 100644 --- a/core/modules/views/src/Plugin/views/style/Table.php +++ b/core/modules/views/src/Plugin/views/style/Table.php @@ -76,6 +76,9 @@ protected function defineOptions() { $options['description'] = array('default' => ''); $options['empty_table'] = array('default' => FALSE); + // Override to set the title to caption by default. + $options['title_element_type']['default'] = 'caption'; + return $options; } diff --git a/core/modules/views/src/Tests/Handler/FieldWebTest.php b/core/modules/views/src/Tests/Handler/FieldWebTest.php index e852069..35971e8 100644 --- a/core/modules/views/src/Tests/Handler/FieldWebTest.php +++ b/core/modules/views/src/Tests/Handler/FieldWebTest.php @@ -1,6 +1,7 @@ getElements(); + $element_types = StylePluginBase::getHTMLElementTypes(); $expected_elements = array( '', 0, diff --git a/core/modules/views/src/Tests/Plugin/StyleTest.php b/core/modules/views/src/Tests/Plugin/StyleTest.php index 6773c83..712d730 100644 --- a/core/modules/views/src/Tests/Plugin/StyleTest.php +++ b/core/modules/views/src/Tests/Plugin/StyleTest.php @@ -284,6 +284,33 @@ function testCustomRowClasses() { } /** + * Tests the custom element type for group titles. + */ + public function testCustomElementTitle{ + $style_plugins = array('default', 'grid', 'table', 'html_list'); + $view = views_get_view('test_view'); + + foreach ($style_plugins as $plugin) { + $view->initDisplay(); + + $style = $view->display_handler->getOption('style'); + $style['type'] = $plugin; + $style['options']['title_element_type'] = 'h6'; + $style['options']['grouping'] = 'name'; + $view->display_handler->overrideOption('style', $style); + + $view->initStyle(); + + $output = $view->preview(); + $this->drupalSetContent(drupal_render($output)); + $elements = $this->xpath('//h6'); + $this->assertEqual(count($elements), 5, format_string('Custom element tag found for @plugin plugin', array('@plugin' => $plugin))); + + $view->destroy(); + } + } + + /** * Stores a view output in the elements. */ protected function storeViewPreview($output) { diff --git a/core/modules/views/templates/views-view-grid.html.twig b/core/modules/views/templates/views-view-grid.html.twig index f2b1552..470d674 100644 --- a/core/modules/views/templates/views-view-grid.html.twig +++ b/core/modules/views/templates/views-view-grid.html.twig @@ -6,6 +6,7 @@ * Available variables: * - attributes: HTML attributes for the wrapping element. * - title: The title of this group of rows. + * - title_element_type: The HTML tag element for the title. * - view: The view object. * - rows: The rendered view results. * - options: The view plugin style options. @@ -51,7 +52,7 @@ %} {% endif %} {% if title %} -

{{ title }}

+ <{{ title_element_type }}>{{ title }} {% endif %} {% if options.alignment == 'horizontal' %} diff --git a/core/modules/views/templates/views-view-list.html.twig b/core/modules/views/templates/views-view-list.html.twig index 8de787c..88dcc7c 100644 --- a/core/modules/views/templates/views-view-list.html.twig +++ b/core/modules/views/templates/views-view-list.html.twig @@ -9,6 +9,7 @@ * - attributes: The row's HTML attributes. * - content: The row's contents. * - title: The title of this group of rows. May be empty. + * - title_element_type: The HTML tag element for the title. * - list: @todo. * - type: Starting tag will be either a ul or ol. * - attributes: HTML attributes for the list element. @@ -22,7 +23,7 @@ {% endif %} {% if title %} -

{{ title }}

+ <{{ title_element_type }}>{{ title }} {% endif %} <{{ list.type }}{{ list.attributes }}> diff --git a/core/modules/views/templates/views-view-table.html.twig b/core/modules/views/templates/views-view-table.html.twig index 0c0b445..192c83d 100644 --- a/core/modules/views/templates/views-view-table.html.twig +++ b/core/modules/views/templates/views-view-table.html.twig @@ -7,6 +7,7 @@ * - attributes: Remaining HTML attributes for the element. * - class: HTML classes that can be used to style contextually through CSS. * - title : The title of this group of rows. + * - title_element_type: The HTML tag element for the title. * - header: The table header columns. * - attributes: Remaining HTML attributes for the element. * - content: HTML classes to apply to each header cell, indexed by @@ -45,7 +46,7 @@ {% if caption %} {{ caption }} {% else %} - {{ title }} + <{{ title_element_type }}>{{ title }} {% endif %} {% if (summary is not empty) or (description is not empty) %}
diff --git a/core/modules/views/templates/views-view-unformatted.html.twig b/core/modules/views/templates/views-view-unformatted.html.twig index 7ee1f81..37bb49e 100644 --- a/core/modules/views/templates/views-view-unformatted.html.twig +++ b/core/modules/views/templates/views-view-unformatted.html.twig @@ -5,6 +5,7 @@ * * Available variables: * - title: The title of this group of rows. May be empty. + * - title_element_type: The HTML tag element for the title. * - rows: A list of the view's row items. * - attributes: The row's HTML attributes. * - content: The row's content. @@ -18,7 +19,7 @@ */ #} {% if title %} -

{{ title }}

+ <{{ title_element_type }}>{{ title }} {% endif %} {% for row in rows %} {% diff --git a/core/modules/views/views.module b/core/modules/views/views.module index e1a4d8c..659bcf4 100644 --- a/core/modules/views/views.module +++ b/core/modules/views/views.module @@ -113,7 +113,7 @@ function views_theme($existing, $type, $theme, $path) { 'attachment_before' => array(), 'attachment_after' => array(), ), - 'style' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL), + 'style' => array('view' => NULL, 'options' => NULL, 'rows' => NULL, 'title' => NULL, 'title_element_type' => NULL), 'row' => array('view' => NULL, 'options' => NULL, 'row' => NULL, 'field_alias' => NULL), 'exposed_form' => array('view' => NULL, 'options' => NULL), 'pager' => array( diff --git a/core/modules/views/views.theme.inc b/core/modules/views/views.theme.inc index 94c519e..39807bf 100644 --- a/core/modules/views/views.theme.inc +++ b/core/modules/views/views.theme.inc @@ -648,7 +648,7 @@ function template_preprocess_views_view_table(&$variables) { $variables['summary'] = $handler->options['summary']; $variables['description'] = $handler->options['description']; - $variables['caption_needed'] |= !empty($variables['summary']) || !empty($variables['description']); + $variables['caption_needed'] |= !empty($variables['summary']) || !empty($variables['description']) || !empty($variables['title']); $variables['responsive'] = FALSE; // If the table has headers and it should react responsively to columns hidden