finnito - 3 months ago

Hi all!

I want to be able to pass an argument to a relationship-field_type options handler from the frontend, I guess! Let me explain my setup a little bit:

I am writing a little module to make management of user groups a little easier. In a twig view I am iterating over each user group, displaying the users in the group and a little button to submit a form to remove them from that group. Great, that's easy, I can do that!

What I want to do now is to show a relationship-field_type input to add a user to each group as I iterate through them. Now, I've got it setup in search mode which works well, but I would love to add an options handler so that I can only display those users who are not already in that group, as options. I get that this might be a little tricky (or impossible) because I'm not sure at what point the handler is called.

Let me show you:

uumBRBX.png

My form looks like this:

{% set addForm = form({
    "handler": "Finnito\\GroupManagerModule\\Form\\AddToGroupForm@handle",
    "fields": {
        "user": {
            "type": "relationship",
            "required": true,
            "config": {
                "related": "Anomaly\\UsersModule\\User\\UserModel",
                "mode": "search"
            }
        },
        "role": {
            "type": "text",
            "required": true
        }
    },
    "actions": {
        "add": {
            "type": "success",
            "text": "Add"
        }
    }
}).get() %}

And this is how I render out my list of groups/roles

{% for group in groups %}
    <div class="group">
        <div class="group-header">
            <div class="name">
                <h2>{{ group.name }}</h2>
            </div>
            <div class="add">
                {{ addForm.open({"class": "add-form"})|raw }}
                {{ addForm.actions|raw }}
                {{ addForm.fields.user|raw }}
                {{ addForm.fields.role.setValue(group.slug)|raw }}
                {{ addForm.close|raw }}
            </div>
        </div>
        <!-- Omitted code -->
    </div>
{% endfor %}

What I would love to be able to do is to, when I render each relationship field in Twig, give it a variable (somehow) which is the slug of the role that they would be added to, and that way, the options can be set for each role and users already with that role, omitted.

Any thoughts would be appreciated!

Finn