Hook in Select Dropdown modifies option attributes

Recommended Posts

In a project I had to add the attribute 'disable' to some options of a select field (page reference) to show them but make them unselectable. Since I could not find an integrated solution, I wrote a tiny module and a hook. This could also be a POC to similar needs.

  1. Install module
  2. Add the Module to InputfieldPage in the module settings to make it selectable as Inputfield for page reference fields
  3. Create a hook in ready.php to manipulate attributes of the <option> tag for specified items


<?php namespace ProcessWire;

 * POC Inputfield Select Hook Add Option -- Replacement for InputfieldSelect
 * Selection of a single value from a select pulldown. Same like InputfieldSelect
 * except this version provides hookable addOption() function which allows to modify
 * attributes of the <option> tag (i. e. 'disabled' to show items but disallow to select them
 * @author Christoph Thelen aka @kixe

class InputfieldSelectHookAddOption extends InputfieldSelect {

	 * Return information about this module
	public static function getModuleInfo() {
		return array(
			'title' => __('Select Hookable Add Option', __FILE__), // Module Title
			'summary' => __('Selection of a single value from a select pulldown. Same like InputfieldSelect. except this version provides hookable addOption() function which allows to modify attributes of the <option> tag (e.g. \'disabled\' to show items in dropdown but disallow to select', __FILE__), // Module Summary
			'version' => 100,

	 * Hook in here to modify attributes
	public function ___addOptionAttributes($value, $label) {
		return array();

	 * @see InputfieldSelect::addOption()
	public function addOption($value, $label = null, array $attributes = null) {
		if (!is_array($attributes)) $attributes = array();
		$attributes = array_merge($attributes, $this->addOptionAttributes($value, $label));
		return parent::addOption($value, $label, $attributes);


 * This example hook modifies the attributes of the selectable options of a Pagereference field named 'test'.
 * The selectable pages have the template 'test' assigned which includes a checkbox 'disallow'.
 * The attribute 'disabled' will be added to the selectable page if the user does not have the role 'user-extended' and 'disallow' is set.
$wire->addHookAfter('InputfieldSelectHookAddOption::addOptionAttributes', function($e) {
    // quick exit
    if ($e->object->name != 'test') return;
    if ($this->wire('user')->isSuperuser()|| $this->wire('user')->hasRole('user-extended')) return;
    // disable items (pages) in select
    $restrictedPageIDs = $this->wire('pages')->find('template=test,disallow=1')->each('id');
    if (in_array($e->arguments[0], $restrictedPageIDs)) $e->return = array('disabled' => 'disabled');


  • Like 4

Share this post

Link to post
Share on other sites
31 minutes ago, zimali said:

That is where I was stuck. It is not populating the default value into the dropdown. The value remains unchanged. I checked the value of $form['egender'] using dpm() and the default_value still showing integer 1. But the dropdown value remains unchanged rather than showing "Female" What am I missing?

ShowBoxKodiLucky Patcher

@zimali. Consider this your final warning. You have been posting Drupal stuff in the ProcessWire forums. You have, presumably,  ignored my request for clarification. This has trolling written all over it. 

  • Like 1

Share this post

Link to post
Share on other sites

We have a lot of selects that are fairly similar so I'd like to have a wrapper for select that encompasses this commonality with data to change the behavior, changing the properties of an object is easier than adding/removing/manipulating html.

Share this post

Link to post
Share on other sites

Since PW >= 3.0.105 you can easily use the following hook.

    $wire->addHookBefore('InputfieldSelect::render', function($e) {
        if ($e->object->name != 'myfield') return; // quick exit if fieldname doesn't match
        $restrictedIDs = array(); // array of option values to be modified, page IDs in case of pagefield
        $optionAttributes = array_fill_keys($restrictedIDs, array('disabled' => 'disabled'));

Related commit:

  • Like 2

Share this post

Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Anssi
      A simple module to enable easy navigation between the public and the admin side of the site. After installation a green bar will appear to the upper side of the screen, containing a few navigation elements and displaying the PW version number.
      Heavily inspired by @apeisa's great AdminBar (Thanks!). I needed a bit simpler tool for my projects and as a result, this was made. Available on GitHub .

    • By NorbertH
      Is there a hook to do something right after cloning a page ?
      I want the page to be saved right after cloning it either from the button in the tree or from a lister, because saving the page triggers several calculations that are not triggered by just cloning the page.
      Thanks in advance !
    • By Sebi
      I've created a small module which lets you define a timestamp after which a page should be accessible. In addition you can define a timestamp when the release should end and the page should not be accessable any more.
      PageAccessReleasetime can be installed like every other module in ProcessWire. Check the following guide for detailed information: How-To Install or Uninstall Modules
      After that, you will find checkboxes for activating the releasetime-fields at the settings-tab of each page. You don't need to add the fields to your templates manually.
      Check e.g. the checkbox "Activate Releasetime from?" and fill in a date in the future. The page will not be accessable for your users until the given date is reached.
      If you have $config->pagefileSecure = true, the module will protect files of unreleased pages as well.
      How it works
      This module hooks into Page::viewable to prevent users to access unreleased pages:
      public function hookPageViewable($event) { $page = $event->object; $viewable = $event->return; if($viewable){ // If the page would be viewable, additionally check Releasetime and User-Permission $viewable = $this->canUserSee($page); } $event->return = $viewable; } To prevent access to the files of unreleased pages, we hook into Page::isPublic and ProcessPageView::sendFile.
      public function hookPageIsPublic($e) { $page = $e->object; if($e->return && $this->isReleaseTimeSet($page)) { $e->return = false; } } The site/assets/files/ directory of pages, which isPublic() returns false, will get a '-' as prefix. This indicates ProcessWire (with activated $config->pagefileSecure) to check the file's permissions via PHP before delivering it to the client.
      The check wether a not-public file should be accessable happens in ProcessPageView::sendFile. We throw an 404 Exception if the current user must not see the file.
      public function hookProcessPageViewSendFile($e) { $page = $e->arguments[0]; if(!$this->canUserSee($page)) { throw new Wire404Exception('File not found'); } } Additionally we hook into ProcessPageEdit::buildForm to add the PageAccessReleasetime fields to each page and move them to the settings tab.
      In the current version, releasetime-protected pages will appear in wire('pages')->find() queries. If you want to display a list of pages, where pages could be releasetime-protected, you should double-check with $page->viewable() wether the page can be accessed. $page->viewable() returns false, if the page is not released yet.
      If you have an idea how unreleased pages can be filtered out of ProcessWire selector queries, feel free to write an issue, comment or make a pull request!
    • By David Karich
      Thanks to the great Pro module "RepeaterMatrix" I have the possibility to create complex repeater items. With it I have created a quite powerful page builder. Many different content modules, with many more possible design options. The RepeaterMatrix module supports the cloning of items, but only within the same page. Now I often have the case that very design-intensive pages and items are created. If you want to use this module on a different page (e.g. in the same design), you have to rebuild each item manually every time.
      With this proof of concept I have created a module which adds the feature to copy a repeater item to the clipboard so that you can paste this item to another page with the same repeater field. The module has been developed very rudimentarily so far. It is currently not possible to copy nested items. There is also no check of Min/Max. You can also only copy items that have the same field on different pages. And surely you can solve all this more elegantly with AJAX. But personally I lack the deeper understanding of the repeaters. Also missing on the Javascript side are event triggers for the repeaters, which would make it easier. Like e.g. RepeaterItemInitReady or similar.
      it would be great if @ryan would implement this functionality in the core of RepeaterMatrix. I think he has better ways to implement this. Or what do you think, Ryan?
      Everybody is welcome to work on this module and improve it, if it should not be integrated into the matrix core. Therefore I put it for testing and as download on GitHub:
      You can best see the functionality in the screencast: 
    • By anderson
      Please take a look at this:
      The upper nav bar, including dropdowns like "pages" and "portfolios", what do you call this whole thing? At first I guess it's called "dropdown nav bar", but seems not.
      AND of course, what's the simplest way/module to achieve this in PW?
      Thanks in advance.