lewisjenkins / craft-dynamic-fields

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

Has `element` disappeared in v3.2? #30

Closed davebeesley closed 5 years ago

davebeesley commented 5 years ago

I deployed this plugin on a site built on Craft 3.1 and the following twig worked fine

{% set thisEntry = craft.entries.id(element.id).one() %}
{% if thisEntry.[fieldNameRedacted] is defined %}
    {% for location in thisEntry.[fieldNameRedacted] %}
        {{ loop.index > 1 ? ',' }} {    
            "value":"{{ location.id }}",
            "label":"{{ location.title }}",
            "default":{{ 'false' }}
        }
    {% endfor %}
{% endif %}

Since upgrading to 3.2 - element appears to be coming through as null or nothing Has element disappeared/been replaced?

lewisjenkins commented 5 years ago

Craft 3.2 appeared with some major changes which have introduced some bugs into a few plugins (not just mine!). Just to check, are you on the latest versions of Craft and Dynamic Fields?

davebeesley commented 5 years ago

Hey Lewis, Thanks for getting back to me so quickly Plugin "version": "3.0.9", Craft "version": "3.2.4.1",

Copied from composer.lock

davebeesley commented 5 years ago

Just bumping Craft to the latest version

davebeesley commented 5 years ago

All up to date. Now on 3.2.6

lewisjenkins commented 5 years ago

OK, I can see the problem. I'll try and release a fix in the next few hours. Craft 3.2 and subsequent minor releases have been causing a few issues with existing plugins. To quote another dev on Discord, "3.2 was released too early with lots of problems". I'll update this thread shortly.

davebeesley commented 5 years ago

Awesome thank you. Is this something you pipe through then, and Craft have removed the variable/method that you hook in to? I was really struggling to find any Craft Release Notes. I hate raising issues until I have exhausted all other avenues

lewisjenkins commented 5 years ago

Just to check, is [fieldNameRedacted] another field within the same entry? And what type of field is it? An entries field?

davebeesley commented 5 years ago

Yeah on all counts :)

lewisjenkins commented 5 years ago

OK, can you please try this:

{% if element is defined %}
    {% if element.[fieldNameRedacted] is defined %} 
        {% set myLocationsField = element.[fieldNameRedacted].all() %}
        {% if myLocationsField | length %}
            {% for location in myLocationsField %}
                {{ loop.index > 1 ? ',' }} {    
                    "value":"{{ location.id }}",
                    "label":"{{ location.title }}"
                }
            {% endfor %}
        {% else %}
            { "value":"", "label":"Please add some locations and save entry to continue..." }
        {% endif %}
    {% endif %}
{% endif %}
davebeesley commented 5 years ago

No dice. I'm not getting any options displayed, or the fallback message, so I presume either element or element.[field] isn't coming through. There isn't an update to the plugin I need to pull through is there? The repo doesn't seem to have updated and element appears to be null still

lewisjenkins commented 5 years ago

Please let me know the result of this.

{% if element is defined %}
    { "value":"", "label":"Element exists" }
{% else %}
    { "value":"", "label":"Element does not exist." }
{% endif %}
davebeesley commented 5 years ago

Interestingly. Element does exist, but the field definitions on it don't

Screenshot 2019-07-24 at 10 56 38
lewisjenkins commented 5 years ago

Now try this:

{% if element is defined and element.featuredLocations is defined %}
    { "value":"", "label":"{{ element.featuredLocations.count() }}" }
{% else %}
    { "value":"", "label":"Nope." }
{% endif %}
davebeesley commented 5 years ago

Featured Locations as an example

{% if element is defined %}
    {% if element.practitionerPracticesAt is defined %} 
        {% set myLocationsField = element.practitionerPracticesAt.all() %}
        {% if myLocationsField | length %}
            {% for location in myLocationsField %}
                {{ loop.index > 1 ? ',' }} {    
                    "value":"{{ location.id }}",
                    "label":"{{ location.title }}"
                }
            {% endfor %}
        {% else %}
            { "value":"", "label":"Please add some locations and save entry to continue..." }
        {% endif %}
        {% else %}
             { "value":"", "label":"Field 'practitionerPracticesAt' is not defined" }
    {% endif %}
{% else %}
    { "value":"", "label":"Element is not defined" }
{% endif %}
davebeesley commented 5 years ago

Sorry, commented at the same time there

lewisjenkins commented 5 years ago
{% if element is defined and element.practitionerPracticesAt is defined %}
    { "value":"", "label":"{{ element.practitionerPracticesAt.count() }}" }
{% else %}
    { "value":"", "label":"Nope." }
{% endif %}
davebeesley commented 5 years ago

Cool, implemented what you have put there (practitionerPracticesAt)

Screenshot 2019-07-24 at 11 04 55
lewisjenkins commented 5 years ago

OK, looks like a bug. I was using a dropdown field and it was working, however you were using a checkboxes field and it's not working (now also confirmed here). I'll take a look now...

davebeesley commented 5 years ago

Cool. Nice one man. Thanks for being so responsive it is appreciated

lewisjenkins commented 5 years ago

I've fixed the issue. I'm going to do some further bug testing before I do an official release later today, but in the meantime can you replace /vendor/lewisjenkins/craft-dynamic-fields/src/fields/Checkboxes.php with this, and try the following once again:

{% if element is defined %}
    {% if element.practitionerPracticesAt is defined %} 
        {% set myLocationsField = element.practitionerPracticesAt.all() %}
        {% if myLocationsField | length %}
            {% for location in myLocationsField %}
                {{ loop.index > 1 ? ',' }} {    
                    "value":"{{ location.id }}",
                    "label":"{{ location.title }}"
                }
            {% endfor %}
        {% else %}
            { "value":"", "label":"Please add some locations and save entry to continue..." }
        {% endif %}
    {% else %}
         { "value":"", "label":"Field 'practitionerPracticesAt' is not defined" }
    {% endif %}
{% else %}
    { "value":"", "label":"Element is not defined" }
{% endif %}
davebeesley commented 5 years ago
Screenshot 2019-07-24 at 13 14 41

Legend! Thank you

lewisjenkins commented 5 years ago

Cool, thanks for bringing it to my attention. I'll push a proper release later today because this also affects the other fieldtypes. The element feature is undocumented, but if you use element.owner rather than element then you can also access the current entry from within Matrix fields as well.

davebeesley commented 5 years ago

Cool. I've got quite a big roll out of this fix now, as this field is used quite extensively on this site

lewisjenkins commented 5 years ago

I tend to use {% include 'somefile.json' ignore missing %} for my dynamic field settings because it's so much easier to test and make quick changes to field behaviour.

davebeesley commented 5 years ago

Good call! I may just do that, then it will also be source controlled across environments (assuming this doesn't get propagated by Crafts yml file)

davebeesley commented 5 years ago

I'll just add a couple of screenshots to both sanity check what I am doing, and to help anybody who stumbles across this in future

Screenshot 2019-07-24 at 13 42 26 Screenshot 2019-07-24 at 13 42 41
lewisjenkins commented 5 years ago

Many thanks. Also, I've just pushed the latest release to the Plugin Store.