Cannot set metadata on update

Created on 15 March 2021, almost 4 years ago
Updated 29 October 2024, about 2 months ago

Problem/Motivation

On asset update from the DAM there is no ability to hook and change 'alt' text for an image file

A DAM should manage aspects of a file including alt text. hook_entity_type_presave() triggers on first file import and creation but not on update of the file.

Motivation
Alt text is important for accessibility.

It is double work to edit a file in both the DAM and in Drupal.

Steps to reproduce

First. Based on https://www.drupal.org/project/media_acquiadam/issues/3048225 πŸ’¬ Missing alt and title HTML image properties Closed: won't fix I created a simple module to set the alternate text of an image.

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
function REDACTED_media_presave(MediaInterface $entity) {
  /** @var \Drupal\media\MediaInterface $source */
  $source = $entity->getSource();
  if ('acquiadam_asset' == $source->getPluginId()) {
    /** @var \Drupal\media_acquiadam\MediaEntityHelper $media_helper */
    $media_helper = \Drupal::service('media_acquiadam.asset_media.factory')
      ->get($entity);

    $asset = $media_helper->getAsset();
    // Set the image alt attribute based on available Asset metadata.
    if (!empty($asset)) {
      /** @var \Drupal\image\Plugin\Field\FieldType\ImageItem $field */
            $field = $entity->get('field_asset')->first();
            $field->set('alt', $asset->description);
    }
  }
}

Next, link a new file from the dam and trigger import.

Third, update the description field in the DAM for the image and trigger asset update with cron run. Presave hook does not trigger

πŸ’¬ Support request
Status

Closed: won't fix

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States cpierce42

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

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024