lewisjenkins / craft-dynamic-fields

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

Possible to use this to dynamically fetch matrix blocks selected by another sibling field in the same entry? #29

Closed signorekai closed 5 years ago

signorekai commented 5 years ago

I have another field that is a Entries type, selecting another specific Entry. Is it possible to use this to dynamically populate a dropdown based on that selected Entry?

lewisjenkins commented 5 years ago

Yes, it is possible to populate a dynamic field from other fields within the same entry, but the entry would need to be saved first. So, the user would make their selection from your Entries field, and then press the 'Save and Continue Editing' button. Once the entry has been saved at least once, it's possible to access other fields.

signorekai commented 5 years ago

Would it be possible for you to show me an example of how to refer to the current entry in Twig?

On 13 June 2019 at 8:41:46 PM, Lewis Jenkins (notifications@github.com) wrote:

Yes, it is possible to populate a dynamic field from other fields within the same entry, but the entry would need to be saved first. So, the user would make their selection from your Entries field, and then press the 'Save and Continue Editing' button. Once the entry has been saved at least once, it's possible to access other fields.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/lewisjenkins/craft-dynamic-fields/issues/29?email_source=notifications&email_token=AC42ZV2AA4OLAT6PYDQAYEDP2I6AVA5CNFSM4HXZBZWKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODXTR4WA#issuecomment-501685848, or mute the thread https://github.com/notifications/unsubscribe-auth/AC42ZV66JPIYHAK5BLWHNFLP2I6AVANCNFSM4HXZBZWA .

lewisjenkins commented 5 years ago

A basic example with a single Plain Text Field and a single Dynamic Dropdown Field would be something like this.

{% if element.myPlainTextField is defined and element.myPlainTextField != '' %}
{ "value":"foo", "label":"{{ element.myPlainTextField }}" }
{% else %}
{ "value":"", "label":"Please populate 'My Plain Text Field' above and save the entry to continue" }
{% endif %}
signorekai commented 5 years ago

Thank you so much. For the benefit of other users, this is what I did to dynamically populate the dropdown based on in the other field. I only need one Relation, hence the .one().

{% if element.<EntryField> is defined and element.<EntryField>.exists() %}
  {% set theEntry = element.<EntryField>.one() %}
  {% if theEntry.<Matrix>|length %}
    {% for block in theEntry.divisions %}
      {{ loop.index > 1 ? ',' }}{
        "value":"{{block.id}}",
        "label":"{{block.<Field>}}"
      }
    {% endfor %}
  {% else %}
    {"value": "", "label": "No <Matrix> found, please add <Matrix> to the <EntryField>!"}
  {% endif %}

{% else %}
  { "value":"", "label":"No <EntryField> selected" }
{% endif %}