Mapping metadata to reference entities

Created on 26 April 2024, 8 months ago

Problem/Motivation

Is it possible to map DAM metadata to Drupal entity reference field or mapping is supported only for text or number fields ? Currently it is working only for text fields but not for entity reference fields for me.

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇮🇳India nikhilesh gupta Hyderabad

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @nikhilesh gupta
  • 🇺🇸United States agentrickard Georgia (US)

    We are also looking at this and it does not appear to be supported, so marking as a Feature Request.

  • 🇺🇸United States agentrickard Georgia (US)

    So the current metadata refresh looks pretty simple. See MetadataRefreshTrait::forceMappedFieldRefresh.

    We should be able to adapt that method a bit to account for entity reference fields. The problem here is that we have to register field lookups.

    Consider this case:

    • We have a Drupal taxonomy - "File type" that we want to sync from the DAM.
    • File type contains "pdf", "doc", "image"
    • In Drupal, these are referenced by entity ID
    • Acquia DAM doesn't know the entity ID, it probably stores a string
    • So we would need a means to lookup the proper entity reference by string

    This is all pretty simple if we assume that the Entity Reference is a taxonomy term, but I am not sure that is a safe assumption.

    What I would propose is adding an event to the trait which allows for a data lookup on the Drupal side before we resolve the call to $translation->set($entity_field_name, $media_source->getMetadata($translation, $metadata_attribute_name));

    Something like this:

      protected function forceMappedFieldRefresh(MediaInterface $media): void {
        $media_source = $media->getSource();
        $translations = $media->getTranslationLanguages();
        foreach ($translations as $langcode => $data) {
          if ($media->hasTranslation($langcode)) {
            $translation = $media->getTranslation($langcode);
            // Try to set fields provided by the media source and mapped in
            // media type config.
            foreach ($translation->bundle->entity->getFieldMap() as $metadata_attribute_name => $entity_field_name) {
              if ($translation->hasField($entity_field_name)) {
                // New pseudo-code here.
                $metadata = $media_source->getMetadata($translation, $metadata_attribute_name);
                // We have to write this event handler, which can use $translation->getFieldDefinition($entity_field_name) to get the target type
                $metadata = $this->eventDispatcher->dispatch(new MappedFieldEvent($translation, $metadata, $entity_field_name));
                $translation->set($entity_field_name, $metadata);
              }
            }
          }
        }
      }
    

    That should provide enough metadata to derive the entity lookup.

    I would propose that we write the Event handler and a default implementation, allowing other modules / sites to also respond.

  • 🇺🇸United States agentrickard Georgia (US)
  • Status changed to Needs review 7 months ago
  • 🇺🇸United States agentrickard Georgia (US)

    I am not going to have immediate time to pursue this, but here is a patch for testing. It does the following:

    1. Moves the metadata update trait to a service
    2. Introduces an event that fires after metadata has been mapped

    I started to write default event handlers, but that gets tricky, so it seems "good enough" to allow developers to write their own Event handlers to respond to the metadata update.

  • 🇮🇳India bmahesh03121

    Rerolled the patch to update according to the latest changes in AssetUpdateChecker QueueWorker.

  • 🇮🇳India bmahesh03121

    There might be a possibility when custom implementation where we require the metadata attribute name in the event handler, i have added an extra argument for this.

Production build 0.71.5 2024