Add formula processing in JoinPluginBase::buildExtra

Created on 9 February 2024, about 1 year ago

Problem/Motivation

JoinPluginBase::extra mentions the possibility to add a formula in the "field" key of the extra section in a join definition.
I couldn't find any hint in the code for this option, I assume that this is a left over comment from 7.x.

I habe a case where adding a formula would improve the filter performance as I can add some checks directly into the join condition. I could solve it with a having expression but this would mean to join more entries than necessary.

As the option seems to have existed at some point, I added it back in with specific keys "formula" and "left_formula".

Steps to reproduce

Current docs say they allow treating the field key as a formula:

$definition = [
  'type' => 'INNER',
  'table' => 'node__field_XX',
  'field' => 'entity_id',
  'left_table' => 'node_field_data',
  'left_field' => 'nid',
  'operator' => '=',
  'extra' => [
    0 => [
      'field' => '(field_XXX_end_value IS NULL OR field_XXX_end_value > CURDATE() - INTERVAL 1 DAY)',
      'value' => '',
      'operator' => '',
    ]
  ],
];
$join = Views::pluginManager('join')->createInstance('standard', $definition);
$query->queueTable('node__field_XXX', NULL, $join);

This will result in a SQL error as "JoinPluginBase::buildExtra" will treat the expression as a field name.

Proposed resolution

I extended "JoinPluginBase::buildExtra" to accept new keys "formula" and "left_formula" in the extra section.
The example from above would now look like this:

$definition = [
  'type' => 'INNER',
  'table' => 'node__field_XX',
  'field' => 'entity_id',
  'left_table' => 'node_field_data',
  'left_field' => 'nid',
  'operator' => '=',
  'extra' => [
    0 => [
      'formula' => '(field_XXX_end_value IS NULL OR field_XXX_end_value > CURDATE() - INTERVAL 1 DAY)',
    ]
  ],
];
$join = Views::pluginManager('join')->createInstance('standard', $definition);
$query->queueTable('node__field_XXX', NULL, $join);

Merge request link

Remaining tasks

- Review and tests

User interface changes

- None

API changes

- Adds new keys to the extra section of JoinPluginBase.

Data model changes

- None

Release notes snippet

Feature request
Status

Active

Version

10.2

Component
Views 

Last updated 1 day ago

Created by

🇨🇭Switzerland pvbergen

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

Comments & Activities

Production build 0.71.5 2024