You change content with messages and EnableFlushStamp. With partial updates always send template, and read fields such as seo beforehand. Only publish what was published before. With several languages, clear the entity manager before every change, and look for duplicate rows afterwards.
Series 5: Sulu for developers Chapter 8 of 16
Writing content in code
In short
Changing content in code means doing the same as the admin: sending a message that Sulu processes. Three traps lie in wait, which became visible in the website bertrams-media: a missing template, publishing too eagerly and the state of the entity manager with several languages.
Helpful first: 5.1 Sulu 3 at a glance
The pattern
The classes and commands in this chapter come from the example website; they are not Sulu standard. The class App\Content\TemplateFieldWriter sets a field of a page or an article just as the admin does. It shows all the rules of this chapter in 20 lines. The message goes to the bus sulu_message_bus, each time with EnableFlushStamp (see “Sulu 3 at a glance”):
// Like an admin request per change: clear the entity manager
$this->entityManager->clear();
$identifier = ['uuid' => $uuid];
// Sulu maps the template on every change; articles have no default
$data = ['template' => $template, $field => $value, 'locale' => $locale];
$this->dispatch('page' === $type
? new ModifyPageMessage($identifier, $data)
: new ModifyArticleMessage($identifier, $data));
if (self::PUBLISHED === $result) {
$this->dispatch('page' === $type
? new ApplyWorkflowTransitionPageMessage($identifier, $locale, WorkflowInterface::WORKFLOW_TRANSITION_PUBLISH)
: new ApplyWorkflowTransitionArticleMessage($identifier, $locale, WorkflowInterface::WORKFLOW_TRANSITION_PUBLISH));
}
Partial updates: send the template along
A message with only locale and one field is not enough. The TemplateDataMapper calls getDefaultType() before it checks the fields. Articles have no default type, so a TypeError occurs. Always send template along, the current templateKey of the draft row. The admin does the same. Consequence: the top-level keys of templateData are re-sorted, the content stays the same.
Sulu replaces fields such as seo as a whole. Read the current seoData beforehand and merge your change into it. That is what App\Seo\SeoWriter does with SeoChange::mergeInto().
Only publish what was published
A draft can carry open changes that nobody has approved. If your code simply publishes after every change, those go live too. The writers of the example website therefore decide by the workflowPlace of the draft row:
public static function result(?string $workflowPlace, bool $publish): string
{
return match (true) {
WorkflowInterface::WORKFLOW_PLACE_PUBLISHED === $workflowPlace && $publish => self::PUBLISHED,
\in_array($workflowPlace, [WorkflowInterface::WORKFLOW_PLACE_DRAFT, WorkflowInterface::WORKFLOW_PLACE_REVIEW_DRAFT], true) => self::DRAFT_PENDING,
default => self::DRAFT,
};
}
It is only published if the draft was published before. With draft or review_draft the content stays a draft, and the command reports that.
Several languages: clear the entity manager
After the German version has been changed, the article sits in Doctrine's identity map, with a collection that contains only the German and the structural rows. If you change the English version in the same process, Sulu finds no English row there. The DimensionContentCollectionFactory creates a new, empty draft row (version = 0, unpublished), and the subsequent publishing fails. The admin does not have the problem, because every change is a request of its own. Your code recreates this separation with EntityManager::clear() before every change.
This applies to every loop over languages or content that sends messages. SeoWriter and TemplateFieldWriter clear the entity manager before every change.
In the example website, a command that writes content gets a check for duplicate rows before it counts as done. The query is below.
Finding duplicate dimension rows
Sometimes there are two rows with different content in ar_article_dimension_contents for the same article, stage, language and version. The website and the preview then do not always pick the same one. The symptom: the admin shows the new state, the website the old one. We found this in the example website after several rounds of editing through MCP and through the language bug above. This query finds the groups:
SELECT articleUuid, stage, locale, version, COUNT(*) AS duplicates
FROM ar_article_dimension_contents
WHERE locale IS NOT NULL
GROUP BY articleUuid, stage, locale, version
HAVING COUNT(*) > 1;
Two notes on this:
WHERE locale IS NOT NULLis important. Every article also has rows without a language for the fields that are not translated. That is not an error, do not touch them.- Do not delete the second row. Categories and tags hang on it in tables of their own. Instead, the example website aligns
templateDataandtitleof all rows of the group with the row that has the most recentchanged. Afterwards the rows are still duplicated, but identical.
After aligning, clear the cache of the website: php bin/websiteconsole cache:clear.
As a command, not a script
The example website writes one-off data work as an app: command with a service in src/, with --dry-run and a test. Examples: app:seo:apply, app:content:set-field, app:content:publish and app:content:delete. Deletion only happens with --force; without it the command shows what it would do. The metadata of the templates exists only in the admin context, so such commands run with bin/adminconsole.
- 5.15Opening Sulu to AI assistants (MCP) Applies to: official Sulu extension “MCP bundle”