GroupBy()
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.>
Group By and Having
To group by a specified field, use the groupBy()
method:
$query->groupBy('uid');
The above tells the query to group results by the uid
field. This should be the alias created by addField()
or addExpression()
.
To get the row count grouped by a field (e.g., uid
):
$query->addExpression('count(uid)', 'uid_node_count');
To group by multiple fields, simply call groupBy()
multiple times in the desired order.
Having
You can filter aggregate values using having()
:
$query->having('COUNT(uid) >= :matches', [':matches' => $limit]);
This finds cases where uid
count is greater than or equal to $limit
. User values should be passed via the second parameter for security.
Group By + Having Example
Count nodes by UID:
$query = $connection->select('node', 'n') ->fields('n', ['uid']); $query->addExpression('count(uid)', 'uid_node_count'); $query->groupBy("n.uid"); $query->execute();
Now add a having()
clause to only include UIDs with at least 2 nodes:
$query = $connection->select('node', 'n') ->fields('n',['uid']); $query->addExpression('count(uid)', 'uid_node_count'); $query->groupBy("n.uid"); $query->having('COUNT(uid) >= :matches', [':matches' => 2]); $results = $query->execute();
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.