Pass method as parameter to class in form.md

Hello, all.

I made a plugin to solve my problem of generating fields (or form attributes) retrieving data from other pages. It works fine and it is almost generic. But, there is a but. Here is how I call my plugin within the form:

form:
    name: voting
    fields:
        -
            name: fieldset
            type: fieldset
            label: Fieldset
            data-fields@: [\Grav\Plugin\DynamicFormPlugin::mapPropertyToFormAttribute, [01.home/07._nomeados, header.categories]]

The first argument passed to DynamicFormPlugin::field is the page path. The second argument is the property that I want to use to populate my fields.

The problem: I need to pass a method (or a path to the method, I couldn’t find out how that works with grav, yaml nor php) to DynamicFormPlugin::field, so it could use that function to map the property to the form attribute.

I thought about passing class::method as a third argument to DynamicFormPlugin::field. But I don’t know how to call the method later.

I found the answer :slight_smile:

Actually, I did what I thought. Now the form.md is like this:

form:
    name: voting
    fields:
        -
            name: fieldset
            type: fieldset
            label: Fieldset
            data-fields@: [\Grav\Plugin\DynamicFormPlugin::mapPropertyToFormAttribute, [01.home/07._nomeados, header.categories]]

And I use call_user_func in DynamicFormPlugin::mapPropertyToFormAttribute to execute the function that does the mapping, passing the property to it:

/**
 * Call the function that converts the property to a form attribute,
 * passing the property as a parameter. The function needs to return the attribute info.
 */
return call_user_func($map_func_path, $selected_property);

I will publish this plugin so others can use.

@matepaiva, did you publish this anywhere? I could not see an applicable repository on your GitHub-profile. I’m particularly interested in whether data-fields@ worked for you, and would love to see any example of it in use.

Hey @matepaiva,
I think your plugin could be of great use for my volunteer team management intranet.
Could you somehow share it here or elsewhere ?
(take care and thank you for your contribution !)

Also imagine the NEXT LEVEL project management website crafted help to @OleVik’s Project Space theme !!

Hello @medi and @OleVik! I didn’t think my plugin would be useful for others so I didn’t publish it yet. But I will do it this weekend and I come back here with the link. Thank you!

1 Like

Super great @matepaiva !
Thanks a lot !

Hey there @matepaiva !
Any updates ?

Take care
Medi

Hi there. Sorry I am late.

I didn’t uploaded the plugin to the grav market place because I thought it didn’t worth the time. I just uploaded it to github.

I think you can understand what is happening reading the README, but if you have any question, just shut it :slight_smile:

I hope it to be helpful.

1 Like

Thank you @matepaiva !!