Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

media objects disappear after a reload #1545

Closed
andrewscofield opened this issue Jun 27, 2017 · 9 comments
Closed

media objects disappear after a reload #1545

andrewscofield opened this issue Jun 27, 2017 · 9 comments
Assignees
Labels

Comments

@andrewscofield
Copy link

So, I recently learned about the global media object and tried to use it to pull images from my theme and found a bug in it. I'm not sure if I'm using it wrong or what the deal is. But here are repeatable steps you can do to reproduce it, I just tested this out on a clean develop branch.

  1. clone develop branch
  2. run bin/grav install
  3. on the antimatter theme add a media object to templates/default.html.twig like so:
{% extends 'partials/base.html.twig' %}

{% block content %}
	{{ media['theme://images/logo.png'].html() }}
	{{ page.content }}
{% endblock %}
  1. Load page of the fresh install - notice the logo on top.
  2. Reload the page - logo is gone.
  3. Clear cache in admin
  4. Reload the page - logo is back.
@rhukster
Copy link
Member

rhukster commented Jun 27, 2017

Ok, a point of confusion here that is probably to blame. There's a media twig variable that is made available in the processPage() and processSite(). This is a convenience variable that simply points to $page->media() which returns an array of the page's media.

So if you access media['somefile.jpg] you are using that.

However, there's also a a media() twig function that lets you use streams such as the example above. This is not directly conflicting with the media variable, as the variable is an array and this is a function, but if you use [] you are using the variable, and if you use () you are using the function. I think the solution to your issues is to use () like:

{{ media('theme://images/logo.png').html() }}

@andrewscofield
Copy link
Author

Ooh, ok this make sense. But I am unable to get the image to show up at all if I change it to a media function though.

And I did get my info and that snippet from the docs:

https://learn.getgrav.org/themes/theme-vars#media-object

So maybe its wrong there too?

@rhukster
Copy link
Member

Well, i can reproduce the issue so i'm going to dig in!

@rhukster rhukster reopened this Jun 27, 2017
@rhukster
Copy link
Member

@mahagr any ideas on this one.. seems the Twig is just losing all reference to this once cached...

@mahagr
Copy link
Member

mahagr commented Jun 28, 2017

This seems to be a hard one. The entry that causes this issue is the doctrine cached item for Page\Media.

@mahagr
Copy link
Member

mahagr commented Jun 28, 2017

Found it out. Media object was serialized, but unserialization was incomplete.

@andrewscofield
Copy link
Author

Awesome that you guys were able to find the bug!

Getting back to my question about the docs, is it correct to use media['theme://images/logo.png'] like the docs say, or is it supposed to be media('theme://images/logo.png') ? From the first reply by @rhukster it seems like the docs are wrong, but currently media() doesn't work for me at all

@rhukster
Copy link
Member

The docs are correct. I renamed the media() twig function which actually works on a directory to media_directory() to avoid confusion.

@klihelp
Copy link

klihelp commented Mar 2, 2018

How media_directory() works? Gives me this error

 Twig_Error_Runtime
An exception has been thrown during the rendering of a template ("Object of class Grav\Common\Page\Media could not be converted to string").

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants