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.

Issue fork acquia_dam-3443757

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

Nikhilesh Gupta created an issue. See original summary.

nikhilesh gupta’s picture

Title: Mapping metadata to reference entities » Mapping metadata to reference field
agentrickard’s picture

Version: 1.0.12 » 1.1.x-dev
Category: Bug report » Feature request
Priority: Critical » Major

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

agentrickard’s picture

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:

  1. Move the existing Trait to a Service for greater flexibility
  2. Write the Event handler and a default implementation, allowing other modules / sites to also respond.
agentrickard’s picture

agentrickard’s picture

Status: Active » Needs review
StatusFileSize
new12.75 KB

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.

mahesh bandhiya’s picture

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

mahesh bandhiya’s picture

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.

japerry’s picture

Status: Needs review » Needs work
Issue tags: +undefined

before pushing this up, we need this to be put into an MR and corresponding tests added.

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

rajeshreeputra’s picture

Assigned: Unassigned » rajeshreeputra
Status: Needs work » Needs review

Created MR, please review. Code coverage will be added soon.

rajeshreeputra’s picture

Assigned: rajeshreeputra » Unassigned
Status: Needs review » Fixed

MR merged!

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.