lewisjenkins / craft-dynamic-fields

Populate Craft fields with dynamic data using the power of Twig.
Other
147 stars 10 forks source link

Template error: Variable "entry" does not exist #6

Closed siebird closed 8 years ago

siebird commented 8 years ago

Hey Lewis,

Thanks for sharing this plugin. Pretty slick!

So, this was working for me the other day and now it's not. The only thing different is that I added a 3rd custom field that is using this plugin. I'm currently getting the following error while editing an entry: Variable "entry" does not exist. All templates are currently using entry variable (the current entry being edited) to pull into related content to build the dropdown menu.

{% set settings = craft.entries.section('surveySettings').relatedTo(entry).limit(1).first %}
{% for row in settings.factors %}
{ "value" : "{{ row.factor|kebab }}", "label" : "{{ row.factor | raw }}" }{% if not loop.last %},{% endif %}
{% endfor %}
lewisjenkins commented 8 years ago

I'm away from my laptop at the moment so this is a very short response via mobile. I think it's because the current 'entry' variable isn't available in the control panel - it's a front end template thing. Please see my answer on the following thread for a possible workaround:

http://craftcms.stackexchange.com/questions/5377/building-a-website-containing-restaurant-menus-from-multiple-vendors-need-som/5403#5403

siebird commented 8 years ago

Lewis, thanks for the reply. This worked. I ended up reorganizing the content under one section and added notes to the user to save the form in steps to load in correct settings.

{% set thisEntry = craft.entries.id(craft.request.getSegment(3) | split("-") | first).first %}
{% if thisEntry.factors is defined %}
{% for row in thisEntry.factors %}
{ "value" : "{{ row.factor|kebab }}", "label" : "{{ row.factor|raw }}" }{% if not loop.last %},{% endif %}
{% endfor %}
{% endif %}
lewisjenkins commented 8 years ago

OK, good to hear. I think this may also work:

{% set thisEntry = craft.entries.id(element.id).first() %}

It's been a while since this plugin was last updated so I'll see if any improvements can be made. Posting this for future reference:

https://plus.google.com/107469985815708452956/posts/fBuQ2k5iDTB