Verified Commit 9fdb2038 authored by Lee Rowlands's avatar Lee Rowlands
Browse files

Issue #2625136 by heddn, dww, kevin.dutra, sukanya.ramakrishnan, finne,...

Issue #2625136 by heddn, dww, kevin.dutra, sukanya.ramakrishnan, finne, jofitz, Saviktor, rensingh99, matsbla, plach, benjifisher, larowlan, jhedstrom, Lendude, GoZ, catch, jweakley, worldlinemine, Charles Belov, dawehner, alexpott, lauriii: Fix label visibility and add wrapper container for exposed numeric/date filters with multiple form elements
parent fb46d274
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
@@ -914,6 +914,17 @@ public function buildExposedForm(&$form, FormStateInterface $form_state) {
      $this->exposedTranslate($form[$operator], 'operator');

      unset($form['operator']);

      // When the operator and value forms are both in play, enclose them within
      // a wrapper.
      if (!empty($this->options['expose']['identifier'])) {
        $wrapper = $this->options['expose']['identifier'] . '_wrapper';
        $this->buildValueWrapper($form, $wrapper);
        $form[$operator]['#title_display'] = 'invisible';

        $form[$wrapper][$operator] = $form[$operator];
        unset($form[$operator]);
      }
    }

    // Build the form and set the value based on the identifier.
@@ -931,6 +942,43 @@ public function buildExposedForm(&$form, FormStateInterface $form_state) {
      if ($value != 'value') {
        unset($form['value']);
      }

      // When the operator and value forms are both in play, enclose them within
      // a wrapper, for usability. Also wrap if the value form is comprised of
      // multiple elements.
      if ((!empty($this->options['expose']['use_operator']) && !empty($this->options['expose']['operator_id'])) || count(Element::children($form[$value]))) {
        $wrapper = $value . '_wrapper';
        $this->buildValueWrapper($form, $wrapper);
        $form[$wrapper][$value] = $form[$value];
        unset($form[$value]);
      }
    }
  }

  /**
   * Builds wrapper for value and operator forms.
   *
   * @param array $form
   *   The form.
   * @param string $wrapper_identifer
   *   The key to use for the wrapper element.
   */
  protected function buildValueWrapper(&$form, $wrapper_identifer) {
    // If both the field and the operator are exposed, this will end up being
    // called twice. We don't want to wipe out what's already there, so if it
    // exists already, do nothing.
    if (!isset($form[$wrapper_identifer])) {
      $form[$wrapper_identifer] = [
        '#type' => 'fieldset',
      ];

      $exposed_info = $this->exposedInfo();
      if (!empty($exposed_info['label'])) {
        $form[$wrapper_identifer]['#title'] = $exposed_info['label'];
      }
      if (!empty($exposed_info['description'])) {
        $form[$wrapper_identifer]['#description'] = $exposed_info['description'];
      }
    }
  }

+2 −3
Original line number Diff line number Diff line
@@ -277,17 +277,16 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
    if ($two_value_operators_available) {
      $form['value']['min'] = [
        '#type' => 'textfield',
        '#title' => !$exposed ? $this->t('Min') : $this->exposedInfo()['label'],
        '#title' => $this->t('Min'),
        '#size' => 30,
        '#default_value' => $this->value['min'],
        '#description' => !$exposed ? '' : $this->exposedInfo()['description'],
      ];
      if (!empty($this->options['expose']['min_placeholder'])) {
        $form['value']['min']['#attributes']['placeholder'] = $this->options['expose']['min_placeholder'];
      }
      $form['value']['max'] = [
        '#type' => 'textfield',
        '#title' => !$exposed ? $this->t('And max') : $this->t('And'),
        '#title' => $this->t('Max'),
        '#size' => 30,
        '#default_value' => $this->value['max'],
      ];
+27 −4
Original line number Diff line number Diff line
@@ -113,11 +113,34 @@ public function testFilterNumericUI() {
    $this->assertConfigSchemaByName('views.view.test_view');

    $this->drupalPostForm(NULL, [], t('Update preview'));
    // Check the max field label.
    $this->assertRaw('<label for="edit-age-max">And</label>', 'Max field label found');
    $this->assertRaw('<label for="edit-age-min">Age between</label>', 'Min field label found');
    // Check the field (wrapper) label.
    $this->assertSession()->elementTextContains('css', 'fieldset#edit-age-wrapper legend', 'Age between');
    // Check the min/max labels.
    $min_element_label = $this->xpath('//fieldset[contains(@id, "edit-age-wrapper")]//label[contains(@for, "edit-age-min") and contains(text(), "Min")]');
    $this->assertCount(1, $min_element_label);
    $max_element_label = $this->xpath('//fieldset[contains(@id, "edit-age-wrapper")]//label[contains(@for, "edit-age-max") and contains(text(), "Max")]');
    $this->assertCount(1, $max_element_label);
    // Check that the description is shown in the right place.
    $this->assertEqual(trim($this->cssSelect('.form-item-age-min .description')[0]->getText()), 'Description of the exposed filter');
    $this->assertEquals(trim($this->cssSelect('#edit-age-wrapper--description')[0]->getText()), 'Description of the exposed filter');

    // Change to an operation that only requires one form element ('>').
    $this->drupalGet('admin/structure/views/nojs/handler/test_view/default/filter/age');
    $edit = [];
    $edit['options[expose][label]'] = 'Age greater than';
    $edit['options[expose][description]'] = 'Description of the exposed filter';
    $edit['options[operator]'] = '>';
    $edit['options[value][value]'] = 1000;
    $this->drupalPostForm(NULL, $edit, t('Apply'));
    $this->drupalPostForm('admin/structure/views/view/test_view', [], t('Save'));
    $this->assertConfigSchemaByName('views.view.test_view');

    $this->drupalPostForm(NULL, [], t('Update preview'));

    // Make sure the label is visible and that there's no fieldset wrapper.
    $label = $this->xpath('//label[contains(@for, "edit-age") and contains(text(), "Age greater than")]');
    $this->assertCount(1, $label);
    $fieldset = $this->xpath('//fieldset[contains(@id, "edit-age-wrapper")]');
    $this->assertEmpty($fieldset);
  }

}