- Issue created by @wesleymusgrove
- Merge request !6Save images permanently and ensure the file id references the media entity... β (Open) created by wesleymusgrove
I have a content type called "Blog Post" that has an image field called `field_media`, which is an Entity Reference field type that can reference media types of "Brandfolder" and "Image".
After updating from Brandfolder 2.0.0-beta1 to 5.0.1, I noticed that when I use the media picker to browse for a Brandfolder image that hasn't been downloaded to my site yet, that when I pick that image and "Insert selected" into the host/parent entity type and save that host/parent node, the brandfolder.module code saves the image as "Temporary" instead of "Permanent" and it doesn't create a row on the `file_usage` table, which links the managed file to the media entity reference.
I confirmed that by looking at the `file_managed` table and verifying the `status` was `0` for recent Brandfolder images I have been trying to insert into blog posts that hadn't been downloaded and stored as a managed file into Drupal yet. Also when I save the parent node entity, it does not create a row on the `file_usage` table for the new `fid` that it created on the `file_managed` table as a "Temporary" image. So there is no link between the managed file and the media entity.
I'm attaching screenshots to show my debugging process and the proposed solution.
I reproduced this by stepping through the brandfolder.module code in `brandfolder_map_attachment_to_file` around this comment on line 1111, where it starts out with a temporary status, but never changes it to a permanent status.
$file_data = [
// Note: Start with 0 (temporary) file status, which will be changed to
// 1 (permanent) by the File module if/when the host entity is saved.
// File module will also create a file_usage record.
// @todo: If the file is never made permanent, remove its brandfolder_file record? Or see if we can just jump straight to permanent status? That might run afoul of file/entity reference validation.
'status' => 0,
'uid' => \Drupal::currentUser()->id(),
];
And where it saves a new managed file around line 1174:
try {
$file = File::Create($file_data);
$file->save();
$fid = $bf_file_data['fid'] = $file->id();
I was able to get it to create the files as "Permanent" by adding the `$file->setPermanent()` function before file is saved like this:
@@ -1270,6 +1270,7 @@ function brandfolder_map_attachment_to_file($attachment, $create_new_file = TRUE
try {
$file = File::Create($file_data);
+ $file->setPermanent();
$file->save();
$fid = $file->id();
However that was not enough because when the host/parent entity was saved, it still did not create a row on the `file_usage` table for that "Permanent" fid.
After clicking "Insert selected" and before I've saved the host/parenty entity node, while I'm stepping through the `brandfolder_media_presave` hook I think this `$image_field_data`'s `target_id` is not supposed to be `false`...
On line 1563, the `empty($image_field_data)` check isn't an empty object... it's `target_id` is `false`. So when I add this code to the conditional check, it ends up being able to set a valid bf_image media entity later on in the function.
@@ -1559,7 +1560,7 @@ function brandfolder_media_presave(MediaInterface $media) {
$image_field_data = ($imageItem && $imageItem->count() > 0) ? $imageItem->first()->getValue() : [];
$is_image_update_required = FALSE;
$is_metadata_fetch_required = FALSE;
- if (empty($image_field_data)) {
+ if (empty($image_field_data) || $imageItem->first()->getValue()['target_id'] === false) {
if (!empty($bf_attachment_id)) {
// @todo: If changed. Dupe reconciliation, etc.
if ($fid = brandfolder_map_attachment_to_file($bf_attachment_id)) {
That adds both a `file_managed` row and a corresponding `field_usage` row, correctly linking the "Permanent" file to it's referenced media entity.
I confirmed this patch resolves the issue for me, but curious if anyone else has experienced this issue or can reproduce?
Active
5.0
Code