[PP-1] Base fields miss out on Views data from hook_field_views_data()

Created on 15 May 2015, almost 10 years ago
Updated 16 August 2023, over 1 year ago

Problem/Motivation

#1838242: Provide Views integration for datetime field β†’ uses hook_field_views_data() to add proper views integration. However, due to limitations of the Views API, this mechanism does not work for base table fields.
Datetime is the *only* module that adds field types, apparently (the rest have moved into \Drupal\Core), so until πŸ“Œ Allow @FieldType to customize views data Needs work is in this might be overly complicated.

Proposed resolution

TBD

Remaining tasks

Find a solution

User interface changes

N/A

API changes

N/A

πŸ“Œ Task
Status

Postponed

Version

11.0 πŸ”₯

Component
DatetimeΒ  β†’

Last updated 6 days ago

Created by

πŸ‡ΊπŸ‡ΈUnited States tim.plunkett Philadelphia

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA
  • πŸ‡ͺπŸ‡ΈSpain psf_ Huelva

    Hi,

    Notes that may light somebody.

    I found why the default type is string to datetime basefields. In web/core/modules/views/views.views.inc we have a switch that set it how string:

    // Expose data for each field property individually.
      foreach ($field_columns as $column => $attributes) {
        $allow_sort = TRUE;
    
        // Identify likely filters and arguments for each column based on field type.
        switch ($attributes['type']) {
          case 'int':
          case 'mediumint':
          case 'tinyint':
          case 'bigint':
          case 'serial':
          case 'numeric':
          case 'float':
            $filter = 'numeric';
            $argument = 'numeric';
            $sort = 'standard';
            if ($field_storage->getType() == 'boolean') {
              $filter = 'boolean';
            }
            break;
    
          case 'blob':
            // It does not make sense to sort by blob.
            $allow_sort = FALSE;
          default:
            $filter = 'string';
            $argument = 'string';
            $sort = 'standard';
            break;
        }
    ...
    

    The datetime field type must not have a valid filter handler defined.

    In datetime_type_field_views_data_helper(), in datetime core module, we have a comment that say:

    // @todo This code only covers configurable fields, handle base table fields
    // in https://www.drupal.org/node/2489476 β†’ .

  • πŸ‡ͺπŸ‡ΈSpain psf_ Huelva

    I think that the problem is the base fields don't have Field Storage.

    If I execute:

    $fieldStorage = \Drupal::entityTypeManager()
          ->getStorage('field_storage_config');
        $fields = $fieldStorage->loadMultiple();
    

    $fields don't have any base fields.

  • First commit to issue fork.
  • Merge request !11140Resolve #2489476 "Pp 1 base fields" β†’ (Open) created by maskedjellybean
  • Pipeline finished with Failed
    about 1 month ago
    Total: 112s
    #417329
  • πŸ‡ΊπŸ‡ΈUnited States maskedjellybean Portland, OR

    I realize the status of this is postponed, but the fix for issue as I understand it seemed doable so I opened a MR.

    This is what I think the goal of this issue is (correct me if I'm wrong):

    If you have a base table (in Drupal speak, apparently this means a table unrelated to any entity) that has a datetime field/column, it should be filterable in views the same way that a timestamp integer field would be. Additionally the datetime field should be displayed in the view the same way that a timestamp field can be displayed.

    If that's correct, then the MR does the trick for me.

    For example, given this:

    /**
     * Implements hook_schema().
     */
    function my_module_schema() {
      $schema['my_custom_table'] = [
        'description' => 'A custom table.',
        'fields' => [
          'date' => [
            'type' => 'varchar',
            'mysql_type' => 'datetime',
            'not null' => TRUE,
          ],
        ],
      ];
    
      return $schema;
    }
    
    /**
     * Implements hook_views_data().
     */
    function my_module_views_data() {
      // Make my_custom_table table
      // queryable by views.
      $data['my_custom_table'] = [
        'table' => [
          'group' => t('A group'),
          'provider' => t('my_module'),
          'base' => [
            'field' => 'date',
            'title' => 'An example table',
            'help' => 'This is an example table description.'
          ],
        ],
        'date' => [
          'title' => t('A datetime field/column'),
          'help' => t('This is a datetime field/column.'),
          'field' => [
            // ID of field handler plugin to use.
            'id' => 'datetime',
          ],
          'sort' => [
            // ID of sort handler plugin to use.
            'id' => 'datetime',
          ],
          'filter' => [
            // ID of filter handler plugin to use.
            'id' => 'datetime',
          ],
          'argument' => [
            // ID of argument handler plugin to use.
            'id' => 'datetime',
          ],
        ],
      ];
    }
    

    I can make a view of "An example table", and add a "A datetime field/column" field and filter.

  • πŸ‡¨πŸ‡­Switzerland berdir Switzerland

    > If you have a base table (in Drupal speak, apparently this means a table unrelated to any entity)

    That is not what base table means. Base table and base field specifically refers to entity base fields to goal is to have them declared automatically viewing any manual views data hooks.

  • πŸ‡ΊπŸ‡ΈUnited States maskedjellybean Portland, OR

    Ah perhaps I'm in the wrong place then. I'll open a different issue. The problem I'm trying to solve is:
    1. Views does not support filtering on a datetime database field.
    2. Views does not support displaying a datetime database field.

Production build 0.71.5 2024