Problem/Motivation

Using the 2.2.x-dev branch, I can export file and media entities and the output JSON files look correct. But when I import them, the media entity field_media_image field doesn't populate. And the thumbnail field values on all media entities created have the same reference to a broken file entity. When I add a logger to FileItemNormalizer::normalize() it never gets called.

I wonder if we need to support Drupal\image\Plugin\Field\FieldType\ImageItem, since right now it normalization only seems to support Drupal\file\Plugin\Field\FieldType\FileItem.

CommentFileSizeAuthor
#3 dcd-media-import-fix-3519163-3.patch1.38 KBsmulvih2

Comments

smulvih2 created an issue. See original summary.

smulvih2’s picture

Here is my exported media item:

{
    "_links": {
        "type": {
            "href": "media\/image"
        },
        "self": {
            "href": "_dcd\/media\/22"
        }
    },
    "mid": [
        {
            "value": 22
        }
    ],
    "uuid": [
        {
            "value": "07a67070-ca0f-48c1-a971-ac14397b9468"
        }
    ],
    "langcode": [
        {
            "value": "fr",
            "lang": "fr"
        }
    ],
    "bundle": [
        {
            "target_id": "image"
        }
    ],
    "revision_created": [
        {
            "value": "2025-03-25T22:38:47+00:00",
            "format": "Y-m-d\\TH:i:sP"
        }
    ],
    "status": [
        {
            "value": true,
            "lang": "fr"
        }
    ],
    "_embedded": {
        "relation\/media\/image\/uid": [
            {
                "_links": {
                    "type": {
                        "href": "user\/user"
                    },
                    "self": {
                        "href": "_dcd\/user\/4"
                    }
                },
                "uuid": [
                    {
                        "value": "13d57196-00c3-488b-afda-1b43fa6066df"
                    }
                ],
                "lang": "fr"
            }
        ],
        "relation\/media\/image\/thumbnail": [
            {
                "_links": {
                    "type": {
                        "href": "file\/file"
                    },
                    "self": {
                        "href": "_dcd\/file\/28"
                    }
                },
                "uuid": [
                    {
                        "value": "c0c1c5fc-3a29-492b-b1b2-715f136d5aae"
                    }
                ],
                "lang": "fr",
                "alt": null,
                "title": null,
                "width": "128",
                "height": "128"
            }
        ],
        "relation\/media\/image\/field_media_image": [
            {
                "_links": {
                    "type": {
                        "href": "file\/file"
                    },
                    "self": {
                        "href": "_dcd\/file\/28"
                    }
                },
                "uuid": [
                    {
                        "value": "c0c1c5fc-3a29-492b-b1b2-715f136d5aae"
                    }
                ],
                "alt": null,
                "title": null,
                "width": "128",
                "height": "128"
            }
        ]
    },
    "name": [
        {
            "value": "icon-subject-17.png",
            "lang": "fr"
        }
    ],
    "created": [
        {
            "value": "2025-03-25T22:38:47+00:00",
            "lang": "fr",
            "format": "Y-m-d\\TH:i:sP"
        }
    ],
    "changed": [
        {
            "value": "2025-03-25T22:38:47+00:00",
            "lang": "fr",
            "format": "Y-m-d\\TH:i:sP"
        }
    ],
    "default_langcode": [
        {
            "value": true,
            "lang": "fr"
        }
    ],
    "revision_translation_affected": [
        {
            "value": true,
            "lang": "fr"
        }
    ],
    "metatag": [
        {
            "value": {
                "canonical_url": "http:\/\/default\/fr\/bienvenue",
                "dcterms_language": "fr"
            }
        }
    ],
    "path": [
        {
            "alias": "\/media\/image\/22",
            "pid": 1457,
            "langcode": "fr",
            "lang": "fr"
        }
    ],
    "content_translation_source": [
        {
            "value": "und",
            "lang": "fr"
        }
    ],
    "content_translation_outdated": [
        {
            "value": false,
            "lang": "fr"
        }
    ],
    "field_media_in_library": [
        {
            "value": true
        }
    ],
    "_dcd_metadata": {
        "export_timestamp": 1744399835
    }
}

Comparing this against another project using an older DCD version, this is missing items from _links, specifically uid, thumbnail and image.

smulvih2’s picture

StatusFileSize
new1.38 KB

So from the JSON code above, it's missing this outside of _embedded:

  "field_media_image": [
        {
          "uuid": "c0c1c5fc-3a29-492b-b1b2-715f136d5aae",
          "lang": "en"
        }
    ],
    "field_media_image": [
        {
          "uuid": "c0c1c5fc-3a29-492b-b1b2-715f136d5aae",
          "lang": "fr"
        }
    ],

The patch attached gets media items importing for me, although not a real solution, just to show what is needed for the fix to $data.

smulvih2’s picture

Hmmm not sure actually, looks like with 2.1.x the field_media_image is actually under _links and not as it's own root-level array. Not sure the proper approach here.

smulvih2’s picture

Title: Issue with media entity import » Issue with media entity export

So after looing at this in more detail, seems to be an export issue, not import. The export is missing the _links to the embedded content.

mkalkbrenner’s picture

The format changed. Most of _links at top level is not need for our use-case.

So from the JSON code above, it's missing this outside of _embedded:

No, this is correct. Entity Reference fields are only in _embedded.

It seems to be an import related issue.

  • mkalkbrenner committed 44a7eaca on 2.2.x
    Issue #3519163 by smulvih2, mkalkbrenner: Issue with media entity export
    
mkalkbrenner’s picture

Priority: Normal » Major
Status: Active » Needs review

I was able to reproduce the issue if a module is installed that reacts to media entity updates. In our case, image_replace isn't fault tolerant. So default_content_deploy can't import the entity or fix the file ID later.

A quick fix is to handle files at the beginning of the import.

BTW that issue was hidden in previous versions. Meaning, it existed as well but didn't hurt because of the more verbose export format.

mkalkbrenner’s picture

Status: Needs review » Fixed

Status: Fixed » Closed (fixed)

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