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

Example: Populate with sibling matrix block IDs. #20

Open
swthate opened this issue Jun 28, 2018 · 3 comments
Open

Example: Populate with sibling matrix block IDs. #20

swthate opened this issue Jun 28, 2018 · 3 comments

Comments

@swthate
Copy link

swthate commented Jun 28, 2018

Not an issue, but excited to share I finally figured out how to make a dropdown inside a matrix field that gets populated with the IDs of sibling blocks:

{# Get ID of entry currently editing #}
{% set segment = craft.app.request.getSegment(3)|split('-') %}
{% set theEntryId = segment[0] %}

{# Query the entry and loop through its matrix field, if populated #}
{% set theEntry = craft.entries.id(theEntryId).one() %}
{% if theEntry.matrixField|length %}
    {% for block in theEntry.matrixField %}
        {{ loop.index > 1 ? ',' }} {
            "value":"{{block.id}}",
            "label":"{{block.siblingBlockLabelFriendlyField}}"
        }
    {% endfor %}
{% endif %}

This isn't much of an issue, but it took me a little bit to figure out. I'm not sure if it is mentioned explicitly anywhere in your readme, but I was only able to fix an "invalid foreach argument" error by including the {{ loop.index > 1 ? ',' }} bit before the value/label block inside a for loop that I finally noticed in some of your examples. Maybe a nod to that for dynamic fields within a loop?

Anyway, awesome plugin!!

@swthate
Copy link
Author

swthate commented Jun 28, 2018

Something I just thought of: how can I include an option at the top of the list with a NULL value?

I placed this ahead of my for loop:

{
    "value":null,
    "label":"None"
},

I tried both null and "null" but is not null doesn't work on it. I suppose I could just test against the presence of a particular string value?

@lewisjenkins
Copy link
Owner

Have you tried:

{
    "value":"",
    "label":"Please select..."
}

@swthate
Copy link
Author

swthate commented Jun 28, 2018

This is weird, when I print the field, {{ block.replaces }}, it returns the length of the stored string, which after dumping it I can tell is the correct block id.

Why is it printing the length of the string and not the string itself? Hmm

Update:

Wow, I don't know what's going on, but I deleted my dump() stuff and now it's printing out the actual id of the selected block... No idea what I did. I think it's time for a break, lol!

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

No branches or pull requests

2 participants