How to pull extra Bynder metadata into Drupal?

Created on 12 August 2024, 3 months ago

Problem/Motivation

I'm trying to pull in a few metadata fields into Drupal from Bynder, and I noticed I'm not able to pull in certain fields.

For instance, I created a test field field_test_tags on my media type which I want to use to pull in all the tags on an image. On the media type edit page, I selected my custom 'tags' field in the 'field mapping' section:

Yet when I pull in a new asset from Bynder, that field is not populated. Even though I know the image I selected has multiple tags. I noticed that Bynder does store certain metadata in the database when a Bynder image asset is created, however 'tags' is not one of these options. From my research, the only fields that are saved here are: description, id, type, height, width, name, extension, dateCreated, propertyOptions, dateModified, and thumbnails.

I created test fields ('width', 'id', and 'type') on my media type in Drupal and linked them to their metadata fields in the 'field mapping' section. Those were populated correctly when I pulled in a new image. How do I do this for 'tags'? That option exists on the 'field mapping' section so I'm confused as to why it's not working.

Also, I'm also trying to pull in some other metadata fields that don't exist on the 'field mapping' section. For instance, some of these fields from Bynder:

How would I go about pulling in that data? I created a 'topic sub-category' field on my media type in Drupal, however I don't know how to populate that Drupal field with the value that is set in Bynder.

Is this possible? If so, how would I go about doing that?

Thanks!

💬 Support request
Status

Active

Version

4.0

Component

Miscellaneous

Created by

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

Comments & Activities

  • Issue created by @tlo405
  • 🇧🇪Belgium mikell

    The mapping logic and list of properties that can be mapped is defined in the source plugin Bynder.php.
    I created a custom media source plugin which extends the Bynder one.
    In the hook_media_source_info_alter I have set my custom source plugin for the Bynder one.
    In hook_bynder_media_update_alter you can fix the field mapping.

    Code can probably be better but this worked for me.

    <?php
    use Drupal\custom_module\Plugin\media\Source\CustomBynder;
    use Drupal\media\MediaInterface;
    
    /**
     * Implements hook_media_source_info_alter().
     */
    function custom_module_media_source_info_alter(array &$sources) {
      $sources['bynder']['class'] = CustomBynder::class;
    }
    
    /**
     * Implements hook_bynder_media_update_alter().
     *
     * Save mapped metadata to the media entity.
     */
    function custom_module_bynder_media_update_alter(MediaInterface $media, array $item, &$has_changed) {
      /** @var \Drupal\media\MediaTypeInterface $bundle */
      $bundle = $media->get($media->getEntityType()->getKey('bundle'))->entity;
    
      $mapping = [
        'field_property_usage_rights' => 'property_Usage_Rights',
        'field_property_category' => 'property_Category',
        'field_bynder_type' => 'property_Asset_Type',
        'field_property_asset_sub' => 'property_Asset_Sub',
      ];
    
      foreach ($bundle->getFieldMap() as $metadata_attribute_name => $entity_field_name) {
        if (array_key_exists($metadata_attribute_name, $mapping)) {
          $metadata_attribute_name = $mapping[$entity_field_name];
        }
        if (array_key_exists($metadata_attribute_name, $item) && $media->hasField($entity_field_name)) {
          $has_changed = TRUE;
          $media->set($entity_field_name, $item[$metadata_attribute_name]);
        }
      }
    
      $media->updateQueuedThumbnail();
    }
    
    <?php
    declare(strict_types=1);
    
    namespace Drupal\custom_module\Plugin\media\Source;
    
    use Drupal\bynder\Plugin\media\Source\Bynder;
    
    /**
     * Plugin overwrite for 'bynder'
     *
     * Extends the plugin to provide the extra properties.
     *
     */
    final class CustomBynder extends Bynder {
    
      /**
       * {@inheritdoc}
       */
      public function getMetadataAttributes(): array {
        $attributes = parent::getMetadataAttributes();
    
        return array_merge($attributes, [
          'property_usage_rights' => $this->t('Property Usage Rights'),
          'property_category' => $this->t('Property Category'),
          'property_asset_type' => $this->t('Property Asset Type'),
          'property_asset_sub' => $this->t('Property Asset Sub'),
        ]);
      }
    
      /**
       * {@inheritdoc}
       */
      public function getRemoteMetadataProperties(): array {
        $properties = parent::getRemoteMetadataProperties();
    
        return array_merge($properties, [
          'property_Usage_Rights',
          'property_Category',
          'property_Asset_Type',
          'property_Asset_Sub',
    
        ]);
      }
    
    }
    
    
  • First commit to issue fork.
  • 🇨🇭Switzerland berdir Switzerland

    We're currently looking into improving this, specifically also for multilingual situations

  • @berdir opened merge request.
  • 🇨🇭Switzerland berdir Switzerland

    This merge request now makes the following changes:

    * No longer filter stored metadata so all is available through getMetadata()
    * Allow to map metaproperties
    * Allow to enforce and always set mapped field values every time the entity is saved
    * Set up per language overrides for mapped fields
    * Allow to directly use metadata from the image formatter (other formatters not yet updated, doesn't directly support per-language metadata yet).

  • 🇧🇪Belgium mikell

    Can confirm this works, Thx!

  • Hmm...I created a static patch of this MR locally and doesn't apply correctly (using version 4.2.1 of this module).

    I will say though, I manually went in and made the updates from the MR in the correct spots just to test it out. It works great! I now see all other Bynder fields getting pulled in (and not the hardcoded list from before).

  • 🇧🇪Belgium mikell

    There is a small difference between version 4.x-dev and 4.2.1, should we perhaps open a separate issue for this? In the meantime, I’ve already added a patch for version 4.2.1

  • Status changed to Needs work 26 days ago
  • 🇨🇭Switzerland berdir Switzerland

    Thanks for testing this. We're implementing this together with Bynder and are in the process of letting them review and test it. I have one more change I want to make related to better suppport for multilingual projects but then I'll go ahead and commit this.

    Note that merge requests and patches are always against the development version, that's what they are committed against. That's not a problem and not something that needs an issue.

  • 🇨🇭Switzerland berdir Switzerland
  • 🇧🇪Belgium mikell

    Mapping is way easier, thanks for great work!
    Can confirm this patch applies and works for me.

  • 🇨🇭Switzerland berdir Switzerland

    Merged, thanks for testing and confirming.

  • Thanks @berdir! I have also tested this patch and it works great.

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

Production build 0.71.5 2024