Query
2 sites
No security coverage
This module provides a service for creating generic condition objects that can be used to build system-specific queries. It allows developers to define conditions independently of how they will be executed, making it easier to integrate with various query builders.
A module which provides a service for creating generic Condition objects.
These objects may be passed to any number of functions capable of building a system-specific query based on the passed Query object. It is up to the function consuming the Condition object(s) to build the actual query.
This module provides query service, which can be used with formfactorykits' UserAutoCompleteKit & NodeAutoCompleteKit objects.
Example
/** @var \Drupal\query\Services\QueryInterface $q */
$q = \Drupal::service('query');
$result = example_get_events([
$q->condition('type')
->isEqualTo('event'),
$q->condition('published')
->is(),
$q->condition('month')
->isBetween(2, 10)
->isNotIn([3,5,7]),
]);
Consumer Function Example
/**
* @param Condition[] $conditions
* @return array
*/
function example_get_events(array $conditions = []) {
$query = '';
foreach ($conditions as $condition) {
$groupConjunction = $condition->getGroupConjunction();
$key = $condition->getKey();
foreach ($condition->getRequirementGroups() as $group) {
$conjunction = $group->getConjunction();
foreach ($group->getRequirements() as $requirement) {
$operator = $requirement->getOperator();
switch ($operator) {
case Operator::TYPE_EQUALS:
$value = $requirement->getValue();
// TODO: Add `$key == $value` condition to $query.
break;
case Operator::TYPE_NOT_EQUALS:
$value = $requirement->getValue();
// TODO: Add `$key != $value` condition to $query.
break;
case Operator::TYPE_IN:
$values = $requirement->getValues();
// TODO: Add `$key IN($values)` condition to $query.
break;
default:
throw new \DomainException(vsprintf('Unsupported operator: %s', [
$operator,
]));
}
}
// TODO: Append $conjunction to $query if necessary.
}
// TODO: Append $groupConjunction to $query if necessary.
}
// TODO: Execute $query.
}