When an entity is created via a JSON API POST route, the returned response contains empty path values. You need to do a new GET request to retrieve the correct path values.

Issue fork pathauto-3223746

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

cgoffin created an issue. See original summary.

cgoffin’s picture

I created a fix that sets the generated path onto the entity.

cgoffin’s picture

Status: Active » Needs review
bramdriesen’s picture

Status: Needs review » Reviewed & tested by the community
berdir’s picture

Status: Reviewed & tested by the community » Needs work

I could be wrong, but setting the path like that I think overwrites the pathauto flag. The return value documentation around these methods is messy, but it passes through from \Drupal\pathauto\AliasStorageHelper::save(), which kinda matches the path item values, but also not fully (there is no source, for example). Maybe we should set the values explicitly, with $entity->get('path')->alias = $result['alias'] and so on?

mably made their first commit to this issue’s fork.

mably’s picture

Updated the MR to reset the path field computed value instead.

Any feedback welcome.

mably’s picture

Status: Needs work » Needs review
mably’s picture

Assigned: Unassigned » berdir

Summary

After Pathauto generates or updates an alias, the entity's in-memory path field still returns NULL or the old alias value, causing JSON:API (and other serializers) to return stale/empty path data in responses.

Problem

Drupal's path field on content entities is a computed field — its value is lazily loaded from the database and then cached in memory for the lifetime of the entity object. When Pathauto generates a new alias during entity->save(), the alias is written to the database but the entity's computed path field still holds the previously cached value. Any code that reads $entity->path->alias after save (e.g. JSON:API serializing the entity in a POST/PATCH response) gets NULL or the old alias instead of the freshly generated one.

Changes

  • PathautoFieldItemList.php: Added a resetComputedValue() method that clears the internal item list and resets the valueComputed flag, forcing the field to re-read from the database on next access.
  • PathautoGenerator.php: After createEntityAlias() returns a non-null result (meaning an alias was created or updated), resetComputedValue() is called on the entity's path field so the in-memory representation stays in sync.
  • PathautoFieldItemListTest.php (new): Kernel test with two scenarios — testResetComputedValueRefreshesAlias() verifies that after an external alias change, calling resetComputedValue() causes the field to return the updated alias; testPathFieldUpdatedAfterAliasGeneration() verifies that after a node title change and save, the path field automatically reflects the new alias without manual intervention.