Conditions
This cheat sheet provides an overview of commonly used methods, classes, and interfaces for content entities.
/sites/default/files/drupal-content-entity-8.0.pdf
Content entities inherit much of their behavior from entities. See Working with the Entity API for these shared features.
Managing Field Values
Create custom fields using the baseFieldDefinitions method for your content entity.
- Load a custom entity instance into a local variable $Custom_Entity
- Define a custom field named "custom_field"
- Store some data
Code Example
$custom_field_value = $Custom_Entity->custom_field->value; // Perform some kind of data manipulation $Custom_Entity->custom_field->value = $custom_field_value; $Custom_Entity->save();
Example content entities:
- node
- comment
- user
Configuration entities use the Entity API to store configuration in the database.
Differences from Content Entities
- Integrates with the CMI API for export capabilities
- No fields
- Uses schema file (Content Entities use hook_schema())
Tutorials
- Drupal example: Creating a configuration entity type in Drupal 8
- Change record: https://www.drupal.org/node/1818734
In Drupal 8, bundles are a type of container for information that holds field definitions or settings. They are sometimes referred to as "subtypes." Bundles are optional and sit below entity types in the hierarchy of information containers.
- Entity variants (content and configuration; many entities are paired, e.g., blocks)
- Entity Types
- Bundles or subtypes (optional)
Examples of these container types:
- Entity variants:
1) Content
Content entity types:
1. Node:
- Node bundles, also known as content types:
* Article
* Basic Page
2. Taxonomy
- Taxonomy bundles, also known as vocabularies:
* <Vocabulary A>
* <Vocabulary B>
* <Vocabulary Etc.>
3. Blocks
- Custom Block Bundles, also known as Block Types:
* <Block Type L>
* <Block Type M>
* <Block Type Etc.>
4. User (no child bundles)
5. <Custom content entity type X>
6. <Custom content entity type Y>
7. <Custom content entity type Etc.>
2) Configuration
Configuration entity types:
* Custom Block types (no child bundles)
* View (no child bundles)
* Menu (no child bundles)
* Role (no child bundles)
* <Custom config entity type I>
* <Custom config entity type II>
* <Custom config entity type Etc.>
Using WHERE Clauses in Dynamic Queries
To add a WHERE clause to a dynamic query, use the condition()
method:
$query->condition('bundle', 'article', '=');
This instructs the query to filter results to the 'article' bundle. The field name should match the alias used in addField()
or addExpression()
.
Condition Parameters
- $field - Field to compare (required)
- $value - Value to compare (optional, defaults to NULL)
- $operator - Comparison operator (optional, defaults to '=')
Supported Operators
Standard comparison operators '=', '<>', '<', '<=', '>', '>=' are supported by all database engines Drupal supports.
Using IN, NOT IN
$users = [2, 5, 17, 22]; $query->condition('uid', $users, 'IN');
Using BETWEEN, NOT BETWEEN
$query->condition('count', [5, 10], 'BETWEEN');
Using IS NULL, IS NOT NULL, EXISTS, NOT EXISTS
Use these instead of condition()
with NULL:
$query->isNull($field); $query->isNotNull($field); $query->exists($field); $query->notExists($field);
Other Operators
Other operators like BINARY or database-specific ones may or may not work across all systems. Be cautious when building portable modules.
Using Multiple Conditions
$query->condition('bundle', 'article', '='); $query->condition('status', 1, '=');
Multiple conditions are combined with AND by default.
Condition Groups
Use condition groups for complex WHERE logic. Available groups:
orConditionGroup()
andConditionGroup()
Example:
$orGroup = $query->orConditionGroup() ->condition('promoted', 1) ->condition('uid', [2, 4, 7, 22], 'IN'); $query->condition($orGroup);
Which results in:
WHERE (promoted = 1 OR uid IN (2,4,7,22))
Nested condition groups allow even more complex logic:
$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);
Drupal’s online documentation is © 2000-2020 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.