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

Make addOptions extendible or hookable. #738

Closed
netcarver opened this issue Oct 15, 2014 · 4 comments
Closed

Make addOptions extendible or hookable. #738

netcarver opened this issue Oct 15, 2014 · 4 comments

Comments

@netcarver
Copy link

It would be nice to be able to extend the html options in selects with additional attributes in certain situations. I've posted one possible way to do this here. My solution focused purely on extending the attributes as options are added to a multiple select, but there might be a case for making the entire addOptions() method hookable in InputfieldSelect.

@ryancramerdesign
Copy link
Owner

I'm not sure that I understand, because addOption() already accepts user defined attributes? For instance, when I'm rendering asmSelects, I often add data-status and data-desc attributes on my options.
https://github.com/ryancramerdesign/ProcessWire/blob/master/wire/modules/Inputfield/InputfieldSelect.module#L46 – What am I missing? :) That base addOption() method is inherited by all of the select select and multi-select inputfields.

@netcarver
Copy link
Author

Ryan,

When an application is in control of adding options then you are, of course, totally correct and the additional attributes can be added via application code.

However, in the situations referred to in the links above, it's when the InputfieldSelect/SelectMultiple/AsmSelect etc are being populated by the core modules by virtue of them representing a FieldtypePage field to which I'm referring. I would like to be able to extend the attributes on select-derived options that the core added to an Inputfield.

I think this could be done by adding a hookable method to InputfieldSelect and having addOption() call it;

public function addOption($value, $label = null, array $attributes = null) {
    if(is_null($label)) $label = $value;
    $this->options[$value] = $label; 
+    if (null === $attributes) $attributes = array();
+    $extra_atts = $this->extendAttributes($value, $label);
+    $attributes = array_merge($attributes, $extra_atts);
!     if(!empty($attributes)) $this->optionAttributes[$value] = $attributes;
    return $this;
}

+public function ___extendAttributes($id, $value) {
+    $atts = array();
+    return $atts;
+}

By default this would leave the attributes for each option unchanged but would allow the application to hook the method to add whatever attributes it needs.

@netcarver
Copy link
Author

Hi Ryan,

This came up again recently in the forum (will post a link when I re-find it.) Would be great if you would reconsider adding it to the next release.

@netcarver
Copy link
Author

Moved to the new repository, here. Closing this here.

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

No branches or pull requests

2 participants