grav icon indicating copy to clipboard operation
grav copied to clipboard

frontmatter twig variable substitution not working in file field destination attribute

Open GavinTomlins opened this issue 4 years ago • 4 comments

Given the scenario of user JohnCitizen logged into Grav and to uploading a file the destination: field attribute of file does not do twig variable substitution correctly.

Example:

---
title: Home

access:
  site.login: true
  admin.login: true

process:
  twig: true
  twig_first: true

form:
    name: upload

    fields:
        upload:
          label: upload Label
          type: file
          destination: 'user/{{ grav.user.username }}'
          multiple: true
          accept:
           - image/*
    buttons:
        upload:
          type: upload
          value: Upload
    process: 
        userinfo: true 
---

# Testing File Upload
## Storing file uniquely in logged in user. Making life a little easier...

With twig processing enabled in system.yaml

   frontmatter:
    process_twig: true
    ignore_fields:
      - form 
      - forms

placing frontmatter.yaml in the same folder with configuration

frontmatter:
    process_twig: true

If JohnCitizen uploads a file called logo.png

{{grav.user.username}} is not substituted correctly for the current logged in user with the file and folder created /user/JohnCitizen/logo.png ie. Expected correct behaviour

Event though using {{dump(grav.user.username)}} the debugger bar outputs the correct result of logged in user..

The incorrect behaviour that occurs is the following: /user/{{grav.user.username}}/logo.png

GavinTomlins avatar Aug 10 '20 09:08 GavinTomlins

i wouldnt recommend putting random folders directly into user/ folder you probably want /user/data/

ricardo118 avatar Aug 10 '20 12:08 ricardo118

Actually i thought this was related to folder not being created, but i think it does. I never really use twig in frontmatter, and generally think it's a bad idea. As @ricardo118 points out, you really should not be putting stuff directly into user/ folder, probably should put them into the user/data/ folder at the very least.

This could be an issue with the validation of the path and the twig in it, which we do for security. I'll have to think about a possible solution.

rhukster avatar Aug 21 '20 18:08 rhukster

@rhukster you are correct, the issue is around folder creation and variable substitution. As per @ricardo118 suggestion, I have moved the path to user/data but the behaviour is still the same. ie. twig variable substitution does not occur with /user/data/{{grav.user.username}}/filename.ext

GavinTomlins avatar Aug 26 '20 03:08 GavinTomlins

I had the same problem (twig not processed in frontmatter) with a common page (no form). In that case, I could resolve the issue with these settings in sytem.yaml:

pages:
  frontmatter:
    process_twig: true

CorinnaH7 avatar Apr 12 '22 21:04 CorinnaH7