Jump to content

Get value of custom attribute via PW API


Harmen
 Share

Recommended Posts

Hi all,

I do have a form in the 'backend' of my website which has a fieldtype of ASMSelect. I gave all the options of that field a custom attribute with a value:

                foreach ($feature_group['features'] as $feature_id => $feature) {
                    $field->addOption($feature_id, $feature['name'], array('data-feature_group' => $feature_group_id));
                }

How can I read out the value of that custom attribute named data-feature group?

I've tried it with

$selected_feature_groups = $form->get($category_id)->getAttribute('data-feature_group');

//AND

selected_feature_groups = $form->get($category_id)->feature_group;

but that doesn't work.

How can I get this to work?

~Harmen

Link to comment
Share on other sites

I doubt if ProcessWire will do that for you (unless ASMSelect's JS actually does this, which would be news to me). Data Attributes are not form elements so will not be submitted with the form. You will need JS to grab those attributes and submit them along with the form, e.g. as hidden elements created on the fly on submit.

  • Like 1
Link to comment
Share on other sites

Could you work out the $feature_group_id of a given option in the code that processes the form submission?

If that is difficult then another possibility is to use both the $feature_id and the $feature_group_id in the option value - that is, combine them into a single string with some character such as a pipe as a delimiter. When you process the form submission, explode() on the delimiter to get the two ID variables.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...