n How to Add a condition to the query or a condition group. in Drupal 8 | CodimTh

Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

To Add a condition to the query or a condition group. in Drupal 8 you can use condition() method like this:

For example, to find all entities containing both the Turkish 'merhaba' and the Polish 'siema' within a 'greetings' text field:

$entity_ids = \Drupal::entityQuery($entity_type)
  ->condition('greetings', 'merhaba', '=', 'tr')
  ->condition('greetings.value', 'siema', '=', 'pl')
  ->execute();

Parameters:

$field: Name of the field being queried. It must contain a field name, optionally followed by a column name. The column can be the reference property, usually "entity", for reference fields and that can be followed similarly by a field name and so on. Additionally, the target entity type can be specified by appending the ":target_entity_type_id" to "entity".

Examples:

  • nid
  • tags.value
  • tags
  • tags.entity.name
  • tags.entity:taxonomy_term.name
  • uid.entity.name
  • uid.entity:user.name

"tags" "is the same as "tags.value" as value is the default column. If two or more conditions have the same field names they apply to the same delta within that field. In order to limit the condition to a specific item a numeric delta should be added between the field name and the column name.

$value: The value for $field. In most cases, this is a scalar and it's treated as case-insensitive. For more complex operators, it is an array. The meaning of each element in the array is dependent on $operator.

$operator: Possible values:

  • '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS', 'ENDS_WITH': These operators expect $value to be a literal of the same type as the column.
  • 'IN', 'NOT IN': These operators expect $value to be an array of literals of the same type as the column.
  • 'BETWEEN': This operator expects $value to be an array of two literals of the same type as the column.

$langcode: Language code (optional). If omitted, any translation satisfies the condition. However, if two or more conditions omit the langcode within one condition group then they are presumed to apply to the same translation. If within one condition group one condition has a langcode and another does not they are not presumed to apply to the same translation.

 

Exists / notExists for NOT NULL / NULL checking:

$query->isNull($field);
$query->isNotNull($field);
$query->exists($field);
$query->notExists($field);


->exists('field_name'); which is a shortcut for ->condition('field_name', NULL, 'NOT NULL'); 

To look for fields that are NULL, you can use ->notExists('field_name')

Example how to use notExists():

$query= Drupal::service('entity.query')->get('myentity');
$group = $query->orConditionGroup()
->notExists('my_field')
->condition('my_field', '53', '<>');
$ids = $query->condition($group)->execute();

Example how to use conditions:

$query = \Drupal::entityQuery('node')
->condition('status', 1)
->condition('changed', REQUEST_TIME, '<');

$group = $query->orConditionGroup()
->condition('title', 'cat', 'CONTAINS')
->condition('field_tags.entity.name', 'cats');

$nids = $query->condition($group)->execute();

 

Condition Groups:

Condition groups can be used to create more complex where clauses (including ORs) in the condition of the query. There are two condition group types:

  • orConditionGroup - returns an object of conditions joined with ORs
  • andConditionGroup - returns an object of conditions joined with ANDs

Conditions groups return an object, which can then be added as a condition to the query.

Example:

$orGroup1 = $query->orConditionGroup()
  ->condition('a', 1)
  ->condition('b', 1);
  
$andGroup1 = $query->andConditionGroup()
  ->condition('c', 1)
  ->condition('d', 1);
  
$orGroup2 = $query->orConditionGroup()
  ->condition($andGroup1)
  ->condition('e', 1);
  
$query->condition($orGroup1);
$query->condition($orGroup2);

Riadh Rahmi

Senior Web Developer PHP/Drupal & Laravel

I am a senior web developer, I have experience in planning and developing large scale dynamic web solutions especially in Drupal & Laravel.

Web Posts

Search

Page Facebook