Jump to content

Select Input Field with opt Groups?


prestoav
 Share

Recommended Posts

Hi all,

Does anyone know of a field type that allows the generation of a <select> but with the ability to add <options> within <optgroups>?

I'm basically looking for a field like the commonly used FeildtypeSelect but with optgroups.

Many thanks in advance.

Geoff.

Link to comment
Share on other sites

Hi prestoav, welcome to processwire.

Does anyone know of a field type that allows the generation of a <select> but with the ability to add <options> within <optgroups>?

Check these threads in the forum:

https://processwire.com/talk/topic/2010-fieldtype-for-storing-tags/

https://processwire.com/talk/topic/3942-help-for-creating-tags-please/

Link to comment
Share on other sites

I really like the idea of modifying the FieldtypeSelect module and have gone that way. I can pick up the '-' conditionally and process differently but I'm stuck with finding an alternative to this line:

$inputfield->addOption((string) trim($value),(string) trim($label));

Is there an alternative that adds an <optgroup> rather than an <option>?

Link to comment
Share on other sites

Just so you are aware, the InputfieldSelect that soma mentioned has nothing to do with the third party FieldtypeSelect module from Hani which is what I think you are talking about in your first post. If you want to specify optgroups with InputfieldSelect, you want to use a Page field, or the Options field type (which is in the core, but not installed by default).

  • Like 2
Link to comment
Share on other sites

Hi Adrian, that's a real help as I was getting confused exactly as you described!

One question though, I've added a new field using the Options field type and tried some values. Single option values are working fine but I'm having no luck with Optgroups.

I've had a look at the code in InputfieldSelect.module and found these comments:

* If you want to add an optgroup, use the $value param as the label, and the label param as an array of options. 
* Note that optgroups may not be applicable to other Inputfields that descend from InputfieldSelect.
 
However, even trying to back-engineer the code later in that file I can't understand the syntax for adding the array within the Detail tab of the created field in admin.
 
Has anyone tried this and could share an example syntax?
 
I really appreciate everyone's help!
Link to comment
Share on other sites

  • 4 weeks later...
  • 4 months later...

I had this problem today and solved it.

In my case I had multiple pages with children, and want to show parent pages titles as group labels, and their child pages as group options.

    
// $f = $this->modules->get('InputfieldSelect');

/* here we have some array of pages and foreach loops */
/* inside inner foreach fill $group_options array */
 
$f->addOption(
                      $group_label, // $parentpage->title (I had troubles here, use var_dump to check that value!)
                      $group_options, // $group_options[$childpage->id] = $childpage->title
                      $opt_attr // array for option attributes
                 );

If this is confused, here is part from some my custom field module where parent pages use "blank" template:

$f = $this->modules->get('InputfieldSelect');
$opts = wire('pages')->get(1078)->children();
foreach($opts as $opt){
    if($opt->template->name == 'blank' && $opt->numChildren()){
        $group_options = array();
        foreach($opt->children as $option){
          $group_options[$option->id] = $option->title; // <= main part to get group options
        }
        $group_label = $opt->title . '';
        $f->addOption($group_label, $group_options, array());
    }
}
$f->attr('name', $name . '_widget[]');
$f->attr('value', $widget);

Regards.

  • Like 2
Link to comment
Share on other sites

A other possibility for ProcessPageEdit would be to abuse the method addOptionsString.

The default InputfieldSelect (the page one:-)) has a method 'addOptionsString' that methode takes a string as argument. To create options from there you could create a text like the text below (There must be at least 3 spaces before the options, 3 is recommended)

A
   1234=Alfa Romeo
   1235=Aston Martin
B
   1236=BMW
   1237=Bugatti
Example in the spoiler.
/**
 * In this example we use on every option an alphabetic character to 'indentify' a group.
 * In the liste below I put an example how the options should be named.
 *
 * A Alfa Romeo
 * A Aston Martin
 * A Audi
 * B BMW
 * B Bugatti 
 * B Cadillac 
 * C Citroën
 *
 */

wire()->addHookBefore('InputfieldSelect::render', function($event) {

    $select = $event->object;
    if (strpos($select->name, 'name-of-your-field') !== 0) return;

    // Get all options from the select
    $options = $select->getOptions();
    // Unique option groups
    $groups = array();
    // Build addOptionsString String
    $string = '';

    foreach($options as $value => $label) {
        // remove the options, addOptionsString will replace it 
        $select->removeOption($value);
        
        /**
         * This is the important part, and it is hacky 
         *
         * Every group should be indentified with the alphabetic character of the option. 
         * Later on we remove this letter from the option. 
         *
         */
        
        $group = mb_substr($label, 0, 1);
        if (!isset($groups[$group])) {
            $string .= "Group $group\n";
            $groups[$group] = $group;
        }
        
        // Replace first letter from the option
        $label = str_replace($group . ' ', '', $label);
        $string .= "   $value=$label\n";
    }

    $select->addOptionsString(trim($string));
});
  • Like 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...