Insert + Image Styles + File (Field) Path settings + Pathauto

Created on 17 January 2025, 4 months ago

Problem/Motivation

Drupal 11.

The problem is - if I add the original image and insert it - it works fine.
If I use image style, when the image is processed, it does work in the editor. But not in the node.

When I edit the node and re-insert the same image it works fine.

Exemple:
I add the file and Insert it (image style version), the URL:
http://mysite.com/sites/default/files/styles/article_600/public/test.jpg...
It works in the editor.

I save the node and the image is broken, the URL is the same (File (Field) Path settings + Pathauto doesnt apply for some reason):
http://mysite.com/sites/default/files/styles/article_600/public/test.jpg...

I edit the node and re-insert the image, save the node and it's works, the URL now is (File (Field) Path settings apply correctly):
http://mysite.com/sites/default/files/styles/article_600/public/screensh...

File (Field) Path settings path:
screenshots/[date:custom:Y]-[date:custom:m]

File (Field) Path settings name:
[node:title].[file:ffp-extension-original]

Any idea how to fix that?

🐛 Bug report
Status

Active

Version

3.0

Component

Code

Created by

🇨🇦Canada superfedya

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

Comments & Activities

  • Issue created by @superfedya
  • Thanks for filing the issue. For clarification: When you edit the node again, after inserting the image again, is the path in the editor the one with or without the file field path setting applied?

    The place where the variables for the template are aggregated is insert_insert_variables. The image URL is generated per InsertUtility::buildDerivativeUrl. Maybe there is something missing to respect the relevant field settings.

    What is the exact involvement of Pathauto in the configuration? The image path is to be taken from the field settings if I understand correctly. As for Pathauto, maybe the path is generated only at the end of the process saving the node. So, when reinserting the image, the auto-generated path would be available as it had been generated when saving the node previously. I'm not sure something like that can be fixed, at least not without implementing Pathauto specific code in the Insert module. That would be more of a feature. But the file field path settings should be certainly be respected.

  • 🇨🇦Canada superfedya

    >What is the exact involvement of Pathauto in the configuration?

    For the path: screenshots/[date:custom:Y]-[date:custom:m]
    Filename: [node:title].[file:ffp-extension-original]

    >For clarification: When you edit the node again, after inserting the image again, is the path in the editor the one with or without the file field path setting applied?

    Add the image and insert it to CKEditor5:
    https://mysite.com/system/files/styles/article_600/private/filefield_pat...
    It works.

    Save the node and the path is:
    https://mysite.com/system/files/filefield_paths/fe6ade1d-efe4-40d2-8ef1-...
    Doesn't works. Pathauto doesn't apply.

    Re-insert the image in the CKEditor again and it works, the path is now correct:
    https://mysite.com/sites/default/files/styles/article_600/public/screens...

  • Looking at this again, it's not a bug in Insert, but an incompatibility between Insert and the File (Field) Paths module.

    Short answer

    My suggestion is to have an option implemented in the File (Field) Paths module that would allow generating paths to all image style derivatives at the time of uploading an image.

    Long answer

    Having an image field with File (Field) Paths configuration activated, the image seems to first temporarily be uploaded to a filefield_paths folder, which itself is in the files folder. The generation of the path aliases is done not when uploading the image, but when saving the node. Therefore, before saving the node, Insert (which is mainly running client side) is not able to have any knowledge about these future path aliases. However, when editing the node again, the path has been generated and is available to Insert.

    Unfortunately, I do not see a simple way to resolve this in the Insert module. A nice solution would be if File (Field) Paths would generate the paths when uploading the image. However, ideally File (Field) Paths would then clean up unused paths (i.e. paths to image style derivatives that are not used/inserted) eventually. This is certainly out of scope for File (Field) Paths, since it would be functionality practically specific to Insert.

    I notice, however, that for plain file fields, the paths seems to get generated at upload time. This might be because there is only one single path for files uploaded per the file field, no matter what. While in case of the image field, paths would need to be generated for every image style. Therefore, when generating the paths when saving the node, only the path to the image style actually used would need to be created, so no cleanup is required. (Though not sure about future manipulation of the node replacing an image style derivative with a derivative of another style.)

    Anyway, maybe File (Field) Paths could still integrate an option allowing to have paths created for all image styles at the time of uploading an image (instead of at the time of saving the node). In fact, Insert generates image derivatives for each image style when uploading since there is just no proper alternative to handle inserting client-side. So, generating all paths when uploading would match the behaviour of Insert.

    Alternatively, Insert would need to process all editors' contents backend-side when saving, replacing paths with potentially auto-generated paths. While this would be a neat feature for Insert, I consider it out of scope in terms of the complexity of the module. Therefore, my suggestion would be trying find an option to solve this issue in the File (Field) Paths module.

    I will leave the issue open for another while, but will close it as "won't fix" eventually, unless there is some suggestions on resolving this.

Production build 0.71.5 2024