- 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). 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 9:13am 21 October 2024 - 🇨ðŸ‡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.
- 🇧🇪Belgium mikell
Mapping is way easier, thanks for great work!
Can confirm this patch applies and works for me. -
berdir →
committed 971a2a93 on 4.x
Issue #3467695 by berdir: Extend stored metadata and mapping options
-
berdir →
committed 971a2a93 on 4.x
- 🇨ðŸ‡Switzerland berdir Switzerland
Merged, thanks for testing and confirming.
Automatically closed - issue fixed for 2 weeks with no activity.