From d6caa271211ac9eb5c8663d1a1dd7ceb0246f31a Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Wed, 9 May 2018 23:58:06 +0100 Subject: [PATCH] Issue #2930407 by tstoeckler, lauriii, alexpott: Allow specifying summary attributes for details elements --- core/includes/form.inc | 6 +++--- core/lib/Drupal/Core/Render/Element/Details.php | 3 +++ .../migrate_drupal_ui/src/Form/ReviewForm.php | 16 ++++------------ .../src/Form/FormTestGroupDetailsForm.php | 7 +++++++ .../tests/src/Functional/Form/ElementTest.php | 8 ++++++++ 5 files changed, 25 insertions(+), 15 deletions(-) diff --git a/core/includes/form.inc b/core/includes/form.inc index 052c9ce87a9..d248b7b5a9a 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -235,13 +235,13 @@ function template_preprocess_fieldset(&$variables) { * @param array $variables * An associative array containing: * - element: An associative array containing the properties of the element. - * Properties used: #attributes, #children, #open, - * #description, #id, #title, #value, #optional. + * Properties used: #attributes, #children, #description, #required, + * #summary_attributes, #title, #value. */ function template_preprocess_details(&$variables) { $element = $variables['element']; $variables['attributes'] = $element['#attributes']; - $variables['summary_attributes'] = new Attribute(); + $variables['summary_attributes'] = new Attribute($element['#summary_attributes']); if (!empty($element['#title'])) { $variables['summary_attributes']['role'] = 'button'; if (!empty($element['#attributes']['id'])) { diff --git a/core/lib/Drupal/Core/Render/Element/Details.php b/core/lib/Drupal/Core/Render/Element/Details.php index 92bbb32bd1e..e45131c8eaf 100644 --- a/core/lib/Drupal/Core/Render/Element/Details.php +++ b/core/lib/Drupal/Core/Render/Element/Details.php @@ -15,6 +15,8 @@ use Drupal\Core\Render\Element; * - #title: The title of the details container. Defaults to "Details". * - #open: Indicates whether the container should be open by default. * Defaults to FALSE. + * - #summary_attributes: An array of attributes to apply to the + * element. * * Usage example: * @code @@ -43,6 +45,7 @@ class Details extends RenderElement { $class = get_class($this); return [ '#open' => FALSE, + '#summary_attributes' => [], '#value' => NULL, '#process' => [ [$class, 'processGroup'], diff --git a/core/modules/migrate_drupal_ui/src/Form/ReviewForm.php b/core/modules/migrate_drupal_ui/src/Form/ReviewForm.php index 5fc6446945f..403ef351f3b 100644 --- a/core/modules/migrate_drupal_ui/src/Form/ReviewForm.php +++ b/core/modules/migrate_drupal_ui/src/Form/ReviewForm.php @@ -260,12 +260,8 @@ class ReviewForm extends MigrateUpgradeFormBase { $missing_module_list = [ '#type' => 'details', '#open' => TRUE, - '#title' => [ - '#type' => 'html_tag', - '#tag' => 'span', - '#value' => $this->t('Modules that will not be upgraded'), - '#attributes' => ['id' => ['error']], - ], + '#title' => $this->t('Modules that will not be upgraded'), + '#summary_attributes' => ['id' => ['error']], '#description' => $this->t('There are no modules installed on your new site to replace these modules. If you proceed with the upgrade now, configuration and/or content needed by these modules will not be available on your new site. For more information, see Review the pre-upgrade analysis in the Upgrading to Drupal 8 handbook.', [':review' => 'https://www.drupal.org/docs/8/upgrade/upgrade-using-web-browser#pre-upgrade-analysis', ':migrate' => 'https://www.drupal.org/docs/8/upgrade']), '#weight' => 2, ]; @@ -301,12 +297,8 @@ class ReviewForm extends MigrateUpgradeFormBase { // Available migrations. $available_module_list = [ '#type' => 'details', - '#title' => [ - '#type' => 'html_tag', - '#tag' => 'span', - '#value' => $this->t('Modules that will be upgraded'), - '#attributes' => ['id' => ['checked']], - ], + '#title' => $this->t('Modules that will be upgraded'), + '#summary_attributes' => ['id' => ['checked']], '#weight' => 3, ]; diff --git a/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupDetailsForm.php b/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupDetailsForm.php index 12a05e12d2b..32722fdb066 100644 --- a/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupDetailsForm.php +++ b/core/modules/system/tests/modules/form_test/src/Form/FormTestGroupDetailsForm.php @@ -39,6 +39,13 @@ class FormTestGroupDetailsForm extends FormBase { '#type' => 'textfield', '#title' => 'Nest in details element', ]; + $form['summary_attributes'] = [ + '#type' => 'details', + '#title' => 'Details element with summary attributes', + '#summary_attributes' => [ + 'data-summary-attribute' => 'test', + ], + ]; return $form; } diff --git a/core/modules/system/tests/src/Functional/Form/ElementTest.php b/core/modules/system/tests/src/Functional/Form/ElementTest.php index 57da8daf278..6eefa484bdd 100644 --- a/core/modules/system/tests/src/Functional/Form/ElementTest.php +++ b/core/modules/system/tests/src/Functional/Form/ElementTest.php @@ -223,4 +223,12 @@ class ElementTest extends BrowserTestBase { $this->assertText('I am an error on the details element.'); } + /** + * Tests summary attributes of details. + */ + public function testDetailsSummaryAttributes() { + $this->drupalGet('form-test/group-details'); + $this->assertTrue($this->cssSelect('summary[data-summary-attribute="test"]')); + } + }