Problem/Motivation

There is an event dispatched when content is exported, however this occurs after serialization and thus does not allow the exported entity to be altered.

Proposed resolution

I would like an event after the entity is loaded, but before it is serialized so that I can alter the exported entity.

Remaining tasks

  • Post patch
  • review
  • tests

User interface changes

None

API changes

New event dispatched.

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

yanniboi created an issue. See original summary.

yanniboi’s picture

Issue summary: View changes
Status: Active » Needs review
StatusFileSize
new1.69 KB
yanniboi’s picture

Issue summary: View changes
rlmumford’s picture

Status: Needs review » Reviewed & tested by the community

Code here looks good to me. A simple but very useful patch.

jibran’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Need tests

Let's add some tests.

larowlan’s picture

Just me or is ExportEvent missing from the patch?

Not 100% certain we need this. If you need to alter the output, can't you add your own normalizer?

Either way, no harm in adding it though.

yanniboi’s picture

Just me or is ExportEvent missing from the patch?

@larowlan ExportEvent already exists. (http://cgit.drupalcode.org/default_content/tree/src/Event/ExportEvent.php)

Not 100% certain we need this.

Our main use case is that for some entities you may want to remove fields before the export. Also, they are purely fields that we want excluded from the export not excluded from serialization in general. Eg. entity reference to entities that are themselves not being exported.

Will have a look at tests when I get a chance.

yanniboi’s picture

Issue summary: View changes
andrewbelcher’s picture

I've taken a look at the normalizer approach, e.g. \Drupal\default_content\Normalizer\TermEntityNormalizer. It looks to me like this approach actually replaces the existing normalizer with the one default content provides, which feels like it has a couple drawbacks:

  • If the entity type provider adds it's own specific normalizer, it will not be used (unless it happens to be higher priority than default content's and will therefore stop default content's being used).
  • Changes you make to the normalization will affect both REST requests and exports. That may not be desired (such as our case where we only want to affect export).
andypost’s picture

I agree that having an event is fine here instead of a tons of normalizers

r-mo’s picture

Rerolling for current version, still need tests.

berdir’s picture

Version: 8.x-1.x-dev » 2.0.x-dev

I've recently created the 2.0.x branch, see the project page on all the improvements in the 2.0.x branch. Testing that and providing feedback would be very welcome. The 1.x branch isn't actively maintained and won't receive new features anymore, so I'm closing this and other issues as won't fix.

2.0.x currently doesn't have an extension mechanism, and I think we should move an event that is similar to that for export and import in the new \Drupal\default_content\Normalizer\ContentEntityNormalizer class, e.g. denormalize() and normalizeTranslation(). API changes are OK there, so we can remove the current events.

berdir’s picture

Title: Need an event before serialization » Add export/import events
andypost’s picture

On other hand the \Drupal\default_content\Normalizer\ContentEntityNormalizer using Entity api under the hood

So for each create already 2 hooks fired
- for getStorage($entity_type->id())->create($values); hook_entity_create from \Drupal\Core\Entity\EntityStorageBase::create
- for $entity->addTranslation() hook_translation_create from \Drupal\Core\Entity\ContentEntityStorageBase::createTranslation()

SO i'm +1 to just add documentation and maybe resurrect core issue about "third_party properties" for entities (like node has in_preview) to indicate that entity created while DC importing data (like config inSync)

Marios Anagnostopoulos made their first commit to this issue’s fork.

marios anagnostopoulos’s picture

@andypost in your suggestion from #14, are you referring to
https://www.drupal.org/project/drupal/issues/3052115 and https://www.drupal.org/project/drupal/issues/2329253 ?

Using the entity API might work for importing, but does not cover export cases.

I made an initial commit with the most basic solution to the issue, meaning I just added two new events, one for serialization and one for de-serialization.
I think the following points are things that need consideration.

  • I did not remove the existing events yet, will do, if it is decided that this is the way to go, but I agree with @berdir that hey are not needed.
  • I did NOT dispatch a separate event at ContentEntityNormalizer::normalizeTranslation, since I thought that the normalized translations could be processed in the default entity's event if needed.
marios anagnostopoulos’s picture

Status: Needs work » Needs review
marios anagnostopoulos’s picture

Status: Needs review » Needs work

Changing it to needs work, since we probably want to also allow adding dependencies via events, and this is not supported with the current pr

marios anagnostopoulos’s picture

Status: Needs work » Needs review

I removed the evend name from the dispatching since we are using separate events for the import/export processes and added a brief documentation for the import event.

chr.fritsch made their first commit to this issue’s fork.

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

sickness29’s picture

Updated fork with recent changes from dev branch and also added unit test for new events

phenaproxima’s picture

It is worth noting that core now has a default content import/export system (the import part has been around since 10.3, and the exporter will be in 11.3.0). The core system dispatches a PreExportEvent and PreImportEvent, so it might worth integrating with the core system instead of adding these events to Default Content.

See https://www.drupal.org/node/3533854 for more info on the export system, and https://www.drupal.org/node/3445169 for more on the recipe system (which is where the importer and its event were added).