The implementation of step definitions for paragraphs authoring is based on the sharing of the state of the most recently created content item.
There are 2 important pieces:
- Current host entity stack - tracks all entities through step definitions.
- Host entity lookup by current bundle and a field name - allows to traverse through host entity stack to find the first fitting candidate for currently added paragraphs item.
Step definition can be used in 2 syntaxes:
And I add <bundle> paragraph to <host_field_name>
And I add <bundle> paragraph to <host_field_name> with fields: <TableNode>
where
<bundle>
- paragraph_item bundle of the currently added paragraphs_item.<host_field_name>
- field name of the host entity that should accept the currently added paragraphs_item.<TableNode>
- table of field names and their values for the currently added paragraphs_item.
The definition will do the following:
- Validate that current host entity is set (by default, host entity is a last created entity)
- Validate that provided field exists on the entity and can accept a bundle of current paragraphs_item.
- If validation fails - move up in the stack of host entity types and try to resolve with a parent.
- If an valid host entity item is found in the stack - set this item as current parent host entity and proceed below.
- If a valid host entity is not found - throw an exception notifying developer that steps in scenario are not in the expected order.
- If validation fails - move up in the stack of host entity types and try to resolve with a parent.
- Create an instance of the bundle.
- If fields for currently created paragraphs_item were provided - pupulate them.
- Save host entity (also, need to make sure that this process saves the node itself).