Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Support

Display image url from file custom field

Solved by pamtbaau View solution

Started by Michael Garcia 8 years ago · 10 replies · 3556 views
8 years ago

Hi, I don't manage to display an image from a file custom field.

I don't understand why this doesn't work :

{{ page.media[header.mainImage].url }}

(mainImage is the name of my file field)

Thank you very much for your help.

8 years ago

@Michael, On first sight I would guess you have gotten the reference to the frontmatter wrong. It should be page.header.mainImage. The entire code becomes:

TWIG
{{ page.media[page.header.mainImage].url }}
8 years ago

On closer look...

Grav defines the header object which is a shortcut to page.header(). Therefor header.mainImage could return the right value in the code provided in the initial post.

Could, because it depends on the context. According the docs:

The header object is an alias for page.header() of the original page. It's a convenient way to access the original page headers when you are looping through other page objects of child pages or collections.

This means that if variable page is assigned another page, header does not return the header of the new page. but still the header of the original page.

A simple example (not per se showing the best coding practice...):

TWIG
{% set page = page.children|first %}
{{ header.mainImage }}       -> shows the value of the original 'page'
{{ page.header.mainImage }}  -> shows the value of the new value of 'page'
last edited 07/28/18 by pamtbaau
8 years ago

Thank you for your time and for the explanations,
but still I can't manage to get the url of the image of my custom field

@pamtbaau:
{{ page.media[page.header.mainImage].url }}

I tried this line but it creates an error :

An exception has been thrown during the rendering of a template ("Illegal offset type in isset or empty").

Here's my yaml file :

YAML
  fields:
    content:
      type: tab

      fields:

        header.decalage:
          type: text
          label: Simple text

        header.mainImage:
          type: file
          label: Image
          destination: 'user/themes/myTheme/assets'
          filesize: 1
          accept:
            - image/*

My first custom field works and I can display its value. I don't see what I'm missing ...

8 years ago

Shouldn't you be using a field with type filepicker instead of file?

  • File
    in Admin, File is specialized to be used in plugin and theme configurations (blueprints). Handles uploading a file to a location and deleting it, and removing it from the theme / plugin configuration|
  • Filepicker
    Filepicker allows to choose files from a location in the web server filesystem.|

I use the following to set the frontmatter in my page:

YAML
header.theme.headerImage:
  type: filepicker
  folder: 'theme@:/images/headers'
  label: Select an image as header
  preview_images: true
  accept:
    - .png
    - .jpg
8 years ago

I also find this in the doc :
display an image uploaded in a file field

So I tried this after adding another image :

TWIG
{% for imagesuploaded in page.header.mainImage %}
    {{ page.media[imagesuploaded.name] }}
    yo
{% endfor %}

I can see "yo" display twice but no image name :(

👍 1
8 years ago

Some questions:

  • Are you sure the page contains the frontmatter mainImage and it has a value?
  • What value do you expect it to be? Would you mind sharing the frontmatter?
  • Where do you store your images? In same folder as *.md or in /user/pages/images or /user/themes/mytheme/images, or ...

To see what the values are in Twig you can 'dump' them. When you set in system.yaml:

YAML
debugger:
  enabled: true

You could dump the variable using:

TWIG
{{ dump(page.header) }}           {# see if frontmatter contains mainImage #}
{{ dump(page.header.mainImage) }} {# show value of mainImage #}
👍 1
last edited 07/28/18 by pamtbaau
8 years ago

I do store my images in another folder than the .md file. In an "assets" folder I created in my theme. Is it a bad way to process ?

Also I enabled the debugger but it doesn't show up in my front, I have this error message in my console :

PhpDebugBar is not defined

On the other hand I tried to use filepicker and I managed to finally get "myimage.jpg" displayed on my front. This is a good point !

But it brings on one last question : If I drop files on the "page media" area, my files are in the same folder than my page. So should I change the "folder" attribute of my filepicker to "self" or is there a way to upload files in my general "assets" folder ?

Thank you

8 years ago

Thank you for your help and explanations @dimitrilongo and @pamtbaau, I choosed to use the filepicker after uploaded my images in the page media area and it works pretty good !

8 years ago Solution

Only realised later what you meant by below question and forgot to reply...

@Michael:
But it brings on one last question : If I drop files on the “page media” area, my files are in the same folder than my page.

If you drop files on the 'Page Media' field in the 'Content' tab of the page in Admin, by design, the files are saved in the same folder as the page. That's way the field is called 'Page Media'....

If you use the filepicker as shown in post #5, you can choose where to save the images using the folder attribute. You define that field in one of the blueprints in /mytheme/blueprints/ eg. default.yaml.

👍 2

Suggested topics

Topic Participants Replies Views Activity
Support · by Thomas, 1 week ago
2 60 16 hours ago
Support · by Anna, 3 days ago
2 66 19 hours ago
Support · by Justin Young, 20 hours ago
1 34 20 hours ago
Support · by Duc , 1 week ago
2 70 6 days ago
Support · by Colin Hume, 1 week ago
2 61 6 days ago