Currently, when we are doing mapping of Images/Documents from gathercontent into Drupal we can see these fields can be mapped with only Image and File field.

So this module should provide support for the media entity reference field as well.

Comments

dishabhadra created an issue. See original summary.

Kova101’s picture

This feature is in development for 8.x-5.x alongside with a refactored paragraph implementation.
Currently we did not plan to implement it into the 8.x-4.x but we will think about it.

Kova101’s picture

Assigned: Unassigned » Kova101
mathilde_dumond’s picture

Version: 8.x-4.3 » 8.x-5.x-dev
Assigned: Kova101 » Unassigned
Status: Active » Needs review
StatusFileSize
new15.09 KB

I added support for media reference field (not for paragraphs)

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/gathercontent.links.menu.yml
    @@ -1,7 +1,7 @@
    -  parent: system.admin_config
    +  parent: system.admin_config_services
       description: 'GatherContent module configuration settings.'
       weight: 0
    

    looks like you included the other patch in your diff.

  2. +++ b/gathercontent_ui/src/Form/MappingEditSteps/MappingSteps.php
    @@ -328,15 +328,21 @@ abstract class MappingSteps {
        *   Bundle label string.
    +   * @param int $counter
    +   *   Field depth, to avoid infinite recursion.
        *
        * @return array
    

    all of them :)

  3. +++ b/src/MigrationDefinitionCreator.php
    @@ -512,21 +513,48 @@ class MigrationDefinitionCreator {
             }
             else {
               $handler_settings = $fieldInfo->getSetting('handler_settings');
    -          $handler_settings = reset($handler_settings);
    -          $bundle = array_shift($handler_settings);
    +          $bundle = array_values($handler_settings['target_bundles'])[0];
             }
    

    use reset(), that will return the first element in the array without changing it like array_shift() does.

    Also, above here is the bug we discussed with the auto-create. You must check if auto-create is enabled before using the auto-create bundle setting. Basically duplicate \Drupal\Core\Field\Plugin\Field\FieldWidget\EntityReferenceAutocompleteWidget::getAutocreateBundle(). as you can see there, unfortunately the setting really is only used if there is more than one enabled bundle, with just one, it's likely wrong.

  4. +++ b/src/MigrationDefinitionCreator.php
    @@ -512,21 +513,48 @@ class MigrationDefinitionCreator {
    +          $media_image_field = $source->getSourceFieldDefinition($media_type);
    +          $media_image_field_type = $media_image_field->getType();
    +
    +          if (!in_array($media_image_field_type, ['image', 'file'])) {
    +            break;
    

    make sure you really get a field back from that first call, that's not guaranteed, it can return null.

    Also, I do wonder if we should loop over the enabled bundles instead of just picking the first like taxonomy, because you can't really control the order. then we'd use the first compatible type, not just the first and fail if that doesn't work.

    that would mean duplicating the bundle selection logic for taxonomy and media. for media, we'd still respect the auto-create setting if enabled, but if not, we'd loop over them, so always make it an array and this would be a continue instead of a break, the break would be when we found one.

  5. +++ b/src/Plugin/migrate/process/GatherContentMedia.php
    @@ -0,0 +1,119 @@
    + *
    + * @\Drupal\migrate\Annotation\MigrateProcessPlugin(
    + *   id = "gather_content_media"
    + * )
    + *
    + * @code
    + * file:
    + *   plugin: gather_content_media
    + *   source: file
    + *   uri_scheme: string
    + *   file_dir: string
    + *   language: string
    + * @endcode
    + */
    +class GatherContentMedia extends ProcessPluginBase implements ContainerFactoryPluginInterface {
    

    the process plugin should have config schema, but the existing ones don't have that either. See migrate_plus.process.schema.yml for examples.

  6. +++ b/src/Plugin/migrate/process/GatherContentMedia.php
    @@ -0,0 +1,119 @@
    +   * {@inheritdoc}
    +   */
    +  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    +    if (empty($value)) {
    +      return NULL;
    +    }
    

    ok, so the big topic now is reusing of files/media.

    I'm pretty sure, but first, can you confirm my assumption that every time you import, it creates duplicate files and medias?

    It uses file_save_data() internally which defaults to FileSystemInterface::EXISTS_RENAME, so you should see the usual _0, _1 files and so on pop up as well as duplicates in the media list.

    Lets have a look at that together and discuss this, that should be easier.

  7. +++ b/src/Plugin/migrate/process/GatherContentMedia.php
    @@ -0,0 +1,119 @@
    +    $media_type = MediaType::load($this->configuration['bundle']);
    +    foreach ($value as $key => $item) {
    +
    

    this is a bit of a mix of single and multi-support now.

    If you are going for a loop and supporting multiple values, don't bother with first id. Just return a list of $result, it's field values, so you can just return multiple and it should work fine.

  8. +++ b/src/Plugin/migrate/process/GatherContentMedia.php
    @@ -0,0 +1,119 @@
    +      # $fileId[$key] is the id of the file in drupal. we then need to put that in an image file
    

    there is no such thing as an image file, only an image filed.

    The structure:

    host entity -> entity reference field -> media entity -> image/file field -> file entity.

  9. +++ b/src/Plugin/migrate/process/GatherContentMedia.php
    @@ -0,0 +1,119 @@
    +          'alt' => $item->altText,
    

    a bit concerned that we can't rely on the keys. if you look at downloadFiles(), it calls array_values() on it. I'm unsure why it does that, I would expect that they are numbered keys, no?

mathilde_dumond’s picture

Status: Needs work » Needs review
StatusFileSize
new8.51 KB

So, here is a new version of this patch:

1 &2: hopefully there is nothing left anymore

3: used reset, and fixed the auto generate bundle thing

4: I am not sure that I do the existence check too late?

5: added a schema

6&7. Multiple media work. However, multiple files do not work, and I do not know yet why

9. I am not sure. I am confused about the downloadFiles function, and how the order of the files is. they call a ksort at some poitn so I guess they get shuffled at some point?

mathilde_dumond’s picture

StatusFileSize
new9.31 KB

Now with the small improvement that because we don't support multiple values in the file and media plugin, we do not need to support these cases.

berdir’s picture

Version: 8.x-5.x-dev » 6.x-dev

  • Berdir committed 42302da on 6.x
    Issue #3136910 by mathilde_dumond: Provide field mapping support for...
berdir’s picture

Status: Needs review » Fixed

Committed.

Status: Fixed » Closed (fixed)

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