Nested values, tokens and Search API¶
These examples use sub-field machine names without a prefix, as a compound
built in code has them. Field UI adds field_ to each name, so a compound
built through the tutorial has field_title where
these examples have title.
Use sub-field machine names as typed-data paths. With a loaded Conference node
and a populated first field_sessions item:
$session = $node->get('field_sessions')->get(0);
$title = $session->get('title')->get('value')->getValue();
$session->get('title')->get('value')->setValue('Opening keynote');
Validate and save the host afterwards as in define and use a compound.
| Path from one session | Value |
|---|---|
title.value |
String |
link.uri, link.title |
Link URL and label |
description.processed |
Filtered text |
track.target_id, track.entity |
Term ID and referenced entity |
topics.1.value |
Second capped topic |
speaker.person.value |
Nested speaker name |
Capped values belong to their outer item. Check for empty items and references
before dereferencing. Database names such as title__value are not typed-data
paths.
Tokens¶
With Token enabled, the token browser lists sub-field values:
[node:field_sessions:0:title:value]
[node:field_sessions:0:topics:1:value]
[node:field_sessions:0:track:entity:name]
It also lists the date formats and image styles of installed field types.
Host tokens inside sub-field values¶
A sub-field sees the same tokens it would see as a field directly on its host.
Link text of [node:title] link, or a viewfield argument of [node:nid],
resolves against the node that carries the compound, at any nesting depth.
A formatter's token data names the compound item, not the host. Compound Field
answers each other token type from the nearest entity in the item's host chain
that has that type, and never looks past the outermost host. From a nested
session speaker, [node:nid] is the node's ID and, with Token,
[cfi_session:title:value] is the enclosing session's title. Data passed in by
the caller always wins, and the host translation being rendered is used.
When an entity in the chain answers, the outermost host's cache tags are added, even if a nested compound item answered: saving any item saves its outermost host. A sub-field's token replacement carries the same cacheability as on its host, because core adds the token data's own entities, and a compound item reports its outermost host's cache tags.
This works without Token for the types core publishes, such as node, user
and term. With Token, every entity type it maps is available.
A sub-field value that names its own field's token, such as link text of
[node:field_sessions], renders that field again and recurses. The same
happens with an ordinary field on the host.
Search API¶
On the host content index's Fields screen, expand the compound and select named sub-fields; expand a reference's entity for target properties. Avoid flattened columns, whose names can change with the schema.
Indexing repeated compounds collects values across items. Ordinary search fields lose the association between a topic and its session; design filters accordingly.