Mapping metadata to reference field

Created on 26 April 2024, about 2 months ago
Updated 28 May 2024, 27 days 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.

✨ Feature request
Status

Needs review

Version

1.1

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 27 days 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.

Production build 0.69.0 2024