Provide views handler (filters) for the fields

Created on 23 July 2024, 3 months ago
Updated 13 August 2024, 2 months ago

Problem/Motivation

If not too hard, it would be nice to have views handlers for the fields, especially the two boolean fields.

Currently, the basic handler only allows filtering by string, so you have a text input where you'd have to enter 1 or 0 for example, but a radio or checkbox would be expected like for other boolean fields.

As a bonus the date field etc. could also get its type, but only if it's simply to add these views definitions.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Active

Version

1.0

Component

Code (commerce_product_availability)

Created by

🇩🇪Germany Anybody Porta Westfalica

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @Anybody
  • Issue was unassigned.
  • 🇩🇪Germany Anybody Porta Westfalica

    Filters seem to be generally available through the field schema already!

    For example after adding the availability field on a commerce product variation, you can add a filter for "orderable".

    But the type is string, so maybe we need to alter the information to be boolean for views: https://git.drupalcode.org/project/drupal/-/blob/11.x/core/modules/views...

    On the other hand, I wonder why views isn't able to detect that based on the Schema, so this may need further investigations before, so we don't hack something that just needs to be resolved in core.

  • 🇩🇪Germany Anybody Porta Westfalica

  • 🇩🇪Germany Anybody Porta Westfalica

    Here's what ChatGPT says about using a checkbox for the "Orderable" option (unclear if this is correct!):

    To display a custom field type as a boolean checkbox in a Drupal 10 Views filter, you can create a custom Views filter plugin. Here’s a step-by-step guide:

    1. **Create the Custom Filter Plugin**:
    - Define a new filter plugin by extending the `FilterPluginBase` class.
    - Implement the `valueForm` method to define the checkbox options.
    - Implement the `query` method to handle the filtering logic.

    2. **Example Code**:
    - Create a file named `BooleanFilter.php` in your custom module's `src/Plugin/views/filter` directory.

    ```php
    namespace Drupal\your_module\Plugin\views\filter;

    use Drupal\Core\Form\FormStateInterface;
    use Drupal\views\Plugin\views\filter\FilterPluginBase;

    /**
    * Defines a filter for boolean fields.
    *
    * @ViewsFilter("boolean_filter")
    */
    class BooleanFilter extends FilterPluginBase {

    /**
    * {@inheritdoc}
    */
    protected function valueForm(&$form, FormStateInterface $form_state) {
    $form['value'] = [
    '#type' => 'checkbox',
    '#title' => $this->t('Boolean Filter'),
    '#default_value' => $this->value,
    ];
    }

    /**
    * {@inheritdoc}
    */
    public function query() {
    $this->ensureMyTable();
    $value = $this->value ? 1 : 0;
    $this->query->addWhere($this->options['group'], "{$this->tableAlias}.{$this->realField}", $value);
    }
    }
    ```

    3. **Register the Plugin**:
    - Implement `hook_views_data()` in your module to register the custom filter.

    ```php
    /**
    * Implements hook_views_data().
    */
    function your_module_views_data() {
    $data = [];
    $data['your_table']['boolean_filter'] = [
    'title' => t('Boolean Filter'),
    'filter' => [
    'title' => t('Boolean Filter'),
    'help' => t('Filter by boolean value.'),
    'id' => 'boolean_filter',
    ],
    ];
    return $data;
    }
    ```

    4. **Use the Filter in Views**:
    - Add the custom filter to your view and expose it to users.

    ```php
    $view = \Drupal\views\Views::getView('your_view');
    $view->display_handler->setOption('filters', [
    'boolean_filter' => [
    'id' => 'boolean_filter',
    'table' => 'your_table',
    'field' => 'your_field',
    'exposed' => TRUE,
    ],
    ]);
    ```

    This setup will allow you to use a checkbox for your custom field type in the Views filter, providing a more user-friendly interface¹².

    If you have any specific questions or run into issues, feel free to ask!

    Quelle: Unterhaltung mit Copilot, 10.10.2024
    (1) [SOLVED] Creating filter for 2 boolean fields | Drupal.org. https://www.drupal.org/forum/support/module-development-and-code-questio... .
    (2) Define a Custom Views Filter Plugin for Drupal 8, 9, and 10 - Drupalize.Me. https://drupalize.me/tutorial/define-custom-views-filter-plugin.
    (3) Drupal 10 Views: How To Setup Faceted Search - Xandermar LLC. https://www.xandermar.com/articles/drupal-10-views-how-setup-faceted-search.
    (4) class views_handler_filter_boolean_operator | Drupal API. https://api.drupal.org/api/views/handlers%21views_handler_filter_boolean....
    (5) Conditional Form Fields | Form API | Drupal Wiki guide on Drupal.org. https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields .

    For the select filter (availability status):

    To display a custom field type as a boolean checkbox in a Drupal 10 Views filter, you can create a custom Views filter plugin. Here’s a step-by-step guide:

    1. **Create the Custom Filter Plugin**:
    - Define a new filter plugin by extending the `FilterPluginBase` class.
    - Implement the `valueForm` method to define the checkbox options.
    - Implement the `query` method to handle the filtering logic.

    2. **Example Code**:
    - Create a file named `BooleanFilter.php` in your custom module's `src/Plugin/views/filter` directory.

    ```php
    namespace Drupal\your_module\Plugin\views\filter;

    use Drupal\Core\Form\FormStateInterface;
    use Drupal\views\Plugin\views\filter\FilterPluginBase;

    /**
    * Defines a filter for boolean fields.
    *
    * @ViewsFilter("boolean_filter")
    */
    class BooleanFilter extends FilterPluginBase {

    /**
    * {@inheritdoc}
    */
    protected function valueForm(&$form, FormStateInterface $form_state) {
    $form['value'] = [
    '#type' => 'checkbox',
    '#title' => $this->t('Boolean Filter'),
    '#default_value' => $this->value,
    ];
    }

    /**
    * {@inheritdoc}
    */
    public function query() {
    $this->ensureMyTable();
    $value = $this->value ? 1 : 0;
    $this->query->addWhere($this->options['group'], "{$this->tableAlias}.{$this->realField}", $value);
    }
    }
    ```

    3. **Register the Plugin**:
    - Implement `hook_views_data()` in your module to register the custom filter.

    ```php
    /**
    * Implements hook_views_data().
    */
    function your_module_views_data() {
    $data = [];
    $data['your_table']['boolean_filter'] = [
    'title' => t('Boolean Filter'),
    'filter' => [
    'title' => t('Boolean Filter'),
    'help' => t('Filter by boolean value.'),
    'id' => 'boolean_filter',
    ],
    ];
    return $data;
    }
    ```

    4. **Use the Filter in Views**:
    - Add the custom filter to your view and expose it to users.

    ```php
    $view = \Drupal\views\Views::getView('your_view');
    $view->display_handler->setOption('filters', [
    'boolean_filter' => [
    'id' => 'boolean_filter',
    'table' => 'your_table',
    'field' => 'your_field',
    'exposed' => TRUE,
    ],
    ]);
    ```

    This setup will allow you to use a checkbox for your custom field type in the Views filter, providing a more user-friendly interface¹².

    If you have any specific questions or run into issues, feel free to ask!

    Quelle: Unterhaltung mit Copilot, 10.10.2024
    (1) [SOLVED] Creating filter for 2 boolean fields | Drupal.org. https://www.drupal.org/forum/support/module-development-and-code-questio... .
    (2) Define a Custom Views Filter Plugin for Drupal 8, 9, and 10 - Drupalize.Me. https://drupalize.me/tutorial/define-custom-views-filter-plugin.
    (3) Drupal 10 Views: How To Setup Faceted Search - Xandermar LLC. https://www.xandermar.com/articles/drupal-10-views-how-setup-faceted-search.
    (4) class views_handler_filter_boolean_operator | Drupal API. https://api.drupal.org/api/views/handlers%21views_handler_filter_boolean....
    (5) Conditional Form Fields | Form API | Drupal Wiki guide on Drupal.org. https://www.drupal.org/docs/drupal-apis/form-api/conditional-form-fields .

  • 🇩🇪Germany Anybody Porta Westfalica

    Setting priority to normal as this would be helpful to find product variations with certain availability status.

  • 🇩🇪Germany Grevil

    Put it on my list.

Production build 0.71.5 2024