logo

Extra Block Types (EBT) - New Layout Builder experienceâť—

Extra Block Types (EBT) - styled, customizable block types: Slideshows, Tabs, Cards, Accordions and many others. Built-in settings for background, DOM Box, javascript plugins. Experience the future of layout building today.

Demo EBT modules Download EBT modules

âť—Extra Paragraph Types (EPT) - New Paragraphs experience

Extra Paragraph Types (EPT) - analogical paragraph based set of modules.

Demo EPT modules Download EPT modules

Scroll
12/04/2025, by Ivan

Add a Field

To add a field to a Select query, use the addField() method:

$title_field = $query->addField('n', 'title', 'my_title');

The code above instructs the query to select the “title” field of the table with alias “n” and assign it the alias “my_title”. If an alias is not specified, it will be generated automatically. In the vast majority of cases, the generated alias will simply be the field name. In this example, it will be “title”. If that alias already exists, the alias will be the table name and field name. In this example, it would be “n_title”. If that alias also exists, a counter will be added to the alias until an unused alias is found, for example “n_title_2”.

Note that if you are creating and populating the query manually and do not specify an alias, and the default alias is not available, there is almost certainly a bug in your code. However, if you are writing a hook_query_alter() implementation, you cannot know for sure which aliases are already in use, so you should always use the generated alias.

Add Multiple Fields

To select multiple fields, call addField() several times in the desired order. Note that in most cases the order of fields should not matter, and if it does, there is likely a flaw in the module’s business logic.

Alternatively, you can use the fields() method to add multiple fields at once.

$query->fields('n', ['nid', 'title', 'created', 'uid']);

The method above is equivalent to calling addField() four times, once for each field. However, fields() does not support specifying an alias for a field. It also returns the query object itself, so the method can be chained, rather than returning any generated aliases. If you need to know the generated alias, use addField() or getFields() to access the raw internal field structure.

Calling fields() without a field list will result in a “SELECT *” query.

$query->fields('n');

This will cause “n.*” to be included in the query’s field list. Note that aliases will not be created. If the table using SELECT * contains a field that is also specified directly from another table, a field name collision may occur in the result set. In this case, the result set will contain only one of the fields with the shared name. For this reason, using SELECT * is not recommended.

Return Only One Field Using fetchField

Use the fetchField method to return only one field with the query, for example, as follows (a slightly silly example):

$query = $connection->select('node', 'n');
$query->condition('n.nid', 123);
$query->addField('n', 'title');
$result = $query->execute();
return $result->fetchField();