I am creating a custom views filter for a custom views field. The following SQL will accomplish the filter I seek:
select nid, moderation_state, content_entity_id, MAX(content_entity_revision_id)
from node inner join content_moderation_state_field_revision on nid = content_entity_id
where content_entity_id = 59 group by content_entity_id
In my views filter plugin php file, I have the following code to add the above sql to the filter query:
/**
* Helper function that builds the query.
*/
public function query() {
if (!empty($this->value)) {
$configuration = [
'table' => 'content_moderation_state_field_revision',
'field' => 'content_entity_id',
'left_table' => 'node_field_data',
'left_field' => 'nid',
'operator' => '=',
];
$join = Views::pluginManager('join')->createInstance('standard', $configuration);
$this->query->addRelationship('content_moderation_state_field_revision', $join, 'node_field_data');
$this->query->addExpression('MAX(content_moderation_state_field_revision.content_entity_revision_id)');
$this->query->groupBy("content_moderation_state_field_revision.content_entity_id");
$this->query->addWhere('AND', 'content_moderation_state_field_revision', $this->value, 'IN');
}
}
This error is generated when a query is executed:
Error: Call to undefined method Drupal\views\Plugin\views\query\Sql::addExpression()
While addExpression() is part of core database api it is not supported in the views query plugin sql api,
So, I need a workaround to add the MAX() expression to the query. Any suggestions on how to do this?