Skip to content

Instantly share code, notes, and snippets.

@arfaram
Created March 17, 2022 18:47
Show Gist options
  • Save arfaram/28c65c4bfc46cb5f6d64d09bef138333 to your computer and use it in GitHub Desktop.
Save arfaram/28c65c4bfc46cb5f6d64d09bef138333 to your computer and use it in GitHub Desktop.
Add IsFieldEmpty Visitor support for eZPlatform /Ibexa 2.5.

This implementation adds IsFieldEmpty Visitor support for eZPlatform /Ibexa 2.5.

Unfortunately, this is only implemented in 3.x . The doc says that is available for 2.5. But the Vistor it is not implemented.

Either you have to index the field your self and add custom implementation or you can just use the code in this gist.

You can request content with empty field (not set) or not. Example get content that contains image relation field.

Usage:

 $criterions[] = new Criterion\IsFieldEmpty('image_relation', false); // non-empty field
 $criterions[] = new Criterion\IsFieldEmpty('image_relation'); // empty (default true)
<?php
declare(strict_types=1);
namespace App\Query\Visitors;
use eZ\Publish\API\Repository\Values\Content\Query\Criterion;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\Search\Common\FieldNameResolver;
use eZ\Publish\Core\Search\Common\FieldValueMapper;
use EzSystems\EzPlatformSolrSearchEngine\Query\Common\CriterionVisitor\Field;
use EzSystems\EzPlatformSolrSearchEngine\Query\CriterionVisitor;
/**
* Visits the IsFieldEmpty criterion.
*/
final class FieldEmpty extends Field
{
public function __construct(
FieldNameResolver $fieldNameResolver,
FieldValueMapper $fieldValueMapper
) {
parent::__construct($fieldNameResolver, $fieldValueMapper);
}
/**
* Check if visitor is applicable to current criterion.
*/
public function canVisit(Criterion $criterion): bool
{
return $criterion instanceof Criterion\IsFieldEmpty;
}
/**
* Map field value to a proper Solr representation.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If no searchable fields are found for the given criterion target.
*
* @param \EzSystems\EzPlatformSolrSearchEngine\Query\CriterionVisitor $subVisitor
*/
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null): string
{
$searchFields = $this->getSearchFields($criterion);
if (empty($searchFields)) {
throw new InvalidArgumentException('$criterion->target', "No searchable fields found for the given criterion target '{$criterion->target}'.");
}
$criterion->value = (array)$criterion->value;
$queries = [];
foreach ($searchFields as $name => $fieldType) {
foreach ($criterion->value as $value) {
//$value true or false
if($value){
$queries[] = '* -'.$name . ':[* TO *]'; //Field not exist , was not indexed because is empty
}else{
$queries[] = $name . ':[* TO *]'; //field exist and is not empty
}
}
}
return '(' . implode(' OR ', $queries) . ')';
}
}
services:
_defaults:
# automatically injects dependencies in your services
autowire: true
# automatically registers your services as commands, event subscribers, etc.
autoconfigure: true
# this means you cannot fetch services directly from the container via $container->get()
# if you need to do this, you can override this setting on individual services
public: false
App\Query\Visitors\FieldEmpty:
arguments:
- "@ezpublish.search.common.field_name_resolver"
- "@ezpublish.search.common.field_value_mapper.aggregate"
tags:
- {name: ezpublish.search.solr.query.content.criterion_visitor}
- {name: ezpublish.search.solr.query.location.criterion_visitor}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment