I have a content type with 2 fields: field1
and field2
I would like to create a view filter which display node based on a calculation on those two fields.
The filter should display only nodes if field1 - field2
is lower then a given amount.
Here is my try: At the end of the code, I don't know how to create the query
class Myfilter extends FilterPluginBase implements ContainerFactoryPluginInterface {
protected ViewsHandlerManager $joinHandler;
private int $amount;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $route_match, ViewsHandlerManager $join_handler) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->joinHandler = $join_handler;
$this->amount = 0;
if (($node = $route_match->getParameter('node')) && ($node->bundle()=='my_bundle')) {
$this->amount = $node->get('field_inv_amount')->value;
}
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration, $plugin_id, $plugin_definition,
$container->get('current_route_match'),
$container->get('plugin.manager.views.join')
);
}
public function query() {
//parent::query();
$fields = [
'field1',
'field2',
];
foreach ($fields as $field) {
$table_field_name = 'node__' . $field;
$configuration = [
'table' => $table_field_name,
'field' => 'nid',
'left_table' => 'node_field_data',
'left_field' => 'nid',
'operator' => '=',
];
$join = $this->joinHandler->createInstance('standard', $configuration);
$this->query->addRelationship($table_field_name, $join, 'node_field_data');
}
// I am stuck here... field1-field2 should be < $this->amount
$this->query->addWhere( ???? );
}
}
Any hint will be appreciated