Unverified Commit c6e7444f authored by Alex Pott's avatar Alex Pott
Browse files

feat: #3182064 Allow min & max exposed numeric/date filters to be configured

By: pameeela
By: sophiavs
By: alexpott
By: smustgrave
By: ranjith_kumar_k_u
By: id.conky
By: scott_euser
By: dcam
By: xjm
parent 6e1ee72f
Loading
Loading
Loading
Loading
+2 −0
Changes for core/modules/datetime/tests/modules/datetime_test/test_views/views.view.test_exposed_filter_datetime.yml: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -61,6 +61,8 @@ display:
            placeholder: ''
            min_placeholder: ''
            max_placeholder: ''
            min_label: ''
            max_label: ''
          is_grouped: false
          group_info:
            label: ''
+6 −0
Changes for core/modules/views/config/schema/views.filter.schema.yml: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -96,6 +96,12 @@ views.filter.numeric:
        placeholder:
          type: label
          label: 'Placeholder'
        min_label:
          type: label
          label: 'Minimum value label'
        max_label:
          type: label
          label: 'Maximum value label'

views.filter_value.numeric:
  type: mapping
+12 −0
Changes for core/modules/views/src/Plugin/views/filter/Date.php: 12 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -22,9 +22,21 @@ protected function defineOptions() {
    // Value is already set up properly, we're just adding our new field to it.
    $options['value']['contains']['type']['default'] = 'date';

    $options['expose']['contains']['min_label'] = ['default' => $this->t('Start date')];
    $options['expose']['contains']['max_label'] = ['default' => $this->t('End date')];

    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultExposeOptions(): void {
    parent::defaultExposeOptions();
    $this->options['expose']['min_label'] = $this->t('Start date');
    $this->options['expose']['max_label'] = $this->t('End date');
  }

  /**
   * Add a type selector to the value form.
   */
+21 −4
Changes for core/modules/views/src/Plugin/views/filter/NumericFilter.php: 21 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -37,6 +37,8 @@ protected function defineOptions() {
    $options['expose']['contains']['placeholder'] = ['default' => ''];
    $options['expose']['contains']['min_placeholder'] = ['default' => ''];
    $options['expose']['contains']['max_placeholder'] = ['default' => ''];
    $options['expose']['contains']['min_label'] = ['default' => $this->t('Minimum')];
    $options['expose']['contains']['max_label'] = ['default' => $this->t('Maximum')];

    return $options;
  }
@@ -49,6 +51,8 @@ public function defaultExposeOptions() {
    $this->options['expose']['min_placeholder'] = NULL;
    $this->options['expose']['max_placeholder'] = NULL;
    $this->options['expose']['placeholder'] = NULL;
    $this->options['expose']['min_label'] = $this->t('Minimum');
    $this->options['expose']['max_label'] = $this->t('Maximum');
  }

  /**
@@ -71,6 +75,18 @@ public function buildExposeForm(&$form, FormStateInterface $form_state) {
      '#size' => 40,
      '#description' => $this->t('Hint text that appears inside the Max field when empty.'),
    ];
    $form['expose']['min_label'] = [
      '#type' => 'textfield',
      '#default_value' => $this->options['expose']['min_label'],
      '#title' => $this->t('Minimum value label'),
      '#size' => 40,
    ];
    $form['expose']['max_label'] = [
      '#type' => 'textfield',
      '#default_value' => $this->options['expose']['max_label'],
      '#title' => $this->t('Maximum value label'),
      '#size' => 40,
    ];
    // Setup #states for all operators with two value.
    $states = [[':input[name="options[expose][use_operator]"]' => ['checked' => TRUE]]];
    foreach ($this->operatorValues(2) as $operator) {
@@ -78,8 +94,9 @@ public function buildExposeForm(&$form, FormStateInterface $form_state) {
        ':input[name="options[operator]"]' => ['value' => $operator],
      ];
    }
    $form['expose']['min_placeholder']['#states']['visible'] = $states;
    $form['expose']['max_placeholder']['#states']['visible'] = $states;
    foreach (['min_placeholder', 'max_placeholder', 'min_label', 'max_label'] as $key) {
      $form['expose'][$key]['#states']['visible'] = $states;
    }

    $form['expose']['placeholder'] = [
      '#type' => 'textfield',
@@ -297,7 +314,7 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
    if ($two_value_operators_available) {
      $form['value']['min'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Min'),
        '#title' => $this->options['expose']['min_label'],
        '#size' => 30,
        '#default_value' => $this->value['min'],
      ];
@@ -306,7 +323,7 @@ protected function valueForm(&$form, FormStateInterface $form_state) {
      }
      $form['value']['max'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Max'),
        '#title' => $this->options['expose']['max_label'],
        '#size' => 30,
        '#default_value' => $this->value['max'],
      ];
+43 −0
Changes for core/modules/views/src/ViewsConfigUpdater.php: 43 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -133,4 +133,47 @@ protected function processDisplayHandlers(ViewEntityInterface $view, $return_on_
    return $changed;
  }

  /**
   * Checks if a filter's expose section is missing min_label or max_label.
   *
   * @param \Drupal\views\ViewEntityInterface $view
   *   The view entity.
   *
   * @return bool
   *   TRUE if any filter needs the update.
   */
  public function needsMinMaxLabelUpdate(ViewEntityInterface $view): bool {
    return $this->processDisplayHandlers($view, FALSE, function (&$handler, $handler_type) {
      return $this->processMinMaxLabelUpdate($handler, $handler_type);
    });
  }

  /**
   * Adds default min_label and max_label to exposed numeric-type filters.
   *
   * Only filters that already have min_placeholder (indicating they derive from
   * NumericFilter) but lack min_label are updated. The legacy hardcoded labels
   * "Min" and "Max" are used so existing sites are visually unchanged.
   *
   * @param array $handler
   *   A display handler.
   * @param string $handler_type
   *   The handler type.
   *
   * @return bool
   *   Whether the handler was updated.
   */
  public function processMinMaxLabelUpdate(array &$handler, string $handler_type): bool {
    if (
      $handler_type === 'filter' &&
      array_key_exists('min_placeholder', $handler['expose'] ?? []) &&
      !array_key_exists('min_label', $handler['expose'] ?? [])
    ) {
      $handler['expose']['min_label'] = 'Min';
      $handler['expose']['max_label'] = 'Max';
      return TRUE;
    }
    return FALSE;
  }

}
Loading