FieldtypeDynamicOptions by Robin S

A Fieldtype for dynamic options that are generated at runtime via a hook.

Dynamic Options

A module for ProcessWire CMS/CMF. A Fieldtype for dynamic options that are generated at runtime via a hook.

Configuration


Inputfield type

You can choose from a range of inputfield types on the Details tab of a Dynamic Options field. Your field will return a string or an array depending on if the selected input field type is for "single item selection" or "multiple item selection".

Maximum number of items

You can define a maximum number of items the field is allowed to contain. The core inputfields supported by this module will become disabled once the limit is reached. This option is only applicable if you have selected an inputfield type that is for multiple item selection.

Format as Pagefile/Pageimage object(s)

If the field will store paths/URLs to Pagefiles/Pageimages then you can enable this option to have the formatted value be a Pagefile/Pageimage object for "single" fields or an array of Pagefile/Pageimage objects for "multiple" fields.

There is a related Select Images inputfield module that allows you to visually select image thumbnails.

Defining selectable options

Selectable options for a Dynamic Options field should be set in a FieldtypeDynamicOptions::getSelectableOptions hook in /site/ready.php. The hook should return an array of options as 'value' => 'label'.

An example hook is shown on the Details tab of a Dynamic Options field:

$wire->addHookAfter('FieldtypeDynamicOptions::getSelectableOptions', function(HookEvent $event) {
    // The page being edited
    $page = $event->arguments(0);
    // The Dynamic Options field
    $field = $event->arguments(1);
    if($field->name === 'your_field_name') {
        $event->return = [
            'red' => 'Red',
            'green' => 'Green',
            'blue' => 'Blue',
        ];
    }
});

Formatted value


If a Dynamic Options field uses a "single" input type then its formatted value is a string, and if it uses a "multiple" input type then its formatted value is an array. The unformatted value of a Dynamic Options field is always an array.

Also see the Configuration section above for description of an option to have the formatted value be Pagefile/Pageimage object(s).

Examples of possible uses


dynamic-options

$wire->addHookAfter('FieldtypeDynamicOptions::getSelectableOptions', function(HookEvent $event) {
    // The page being edited
    $page = $event->arguments(0);
    // The Dynamic Options field
    $field = $event->arguments(1);

    // Select from the "files" field on the page
    if($field->name === 'select_files') {
        $options = [];
        foreach($page->files as $file) {
            // Value is basename, label is description if one exists
            $options[$file->basename] = $file->get('description|basename');
        }
        $event->return = $options;
    }

    // Select from files in a folder
    if($field->name === 'select_folder_files') {
        $options = [];
        $path = $event->wire()->config->paths->root . 'my-folder/';
        $files = $event->wire()->files->find($path);
        foreach($files as $file) {
            // Value is full path, label is basename
            $options[$file] = str_replace($path, '', $file);
        }
        $event->return = $options;
    }

    // Select from non-system templates
    if($field->name === 'select_template') {
        $options = [];
        foreach($event->wire()->templates as $template) {
            if($template->flags & Template::flagSystem) continue;
            $options[$template->id] = $template->name;
        }
        $event->return = $options;
    }

    // Select from non-system fields
    if($field->name === 'select_field') {
        $options = [];
        foreach($event->wire()->fields as $field) {
            if($field->flags & Field::flagSystem) continue;
            $options[$field->id] = $field->name;
        }
        $event->return = $options;
    }

    // Select from FormBuilder forms
    if($field->name === 'select_formbuilder_form') {
        $form_names = $event->wire()->forms->getFormNames();
        // Use form names as both keys and values
        $event->return = array_combine($form_names, $form_names);
    }

});

Install and use modules at your own risk. Always have a site and database backup before installing new modules.

Twitter updates

  • ProcessWire 3.0.185 (dev) core updates, plus new Session Allow module— More
    17 September 2021
  • Three new ProcessWire Textformatter modules: Find/Replace, Markdown in Markup, and Emoji— More
    3 September 2021
  • This week we have a new master version released after a year in the making. With nearly 40 pull requests, hundreds of new additions and more than 100 issue reports resolved, this new version has a ton of great new stuff— More
    27 August 2021

Latest news

  • ProcessWire Weekly #384
    In the 384th issue of ProcessWire Weekly we'll cover the latest core updates, introduce a new module called Session Allow, and highlight a new site of the week. Read on!
    Weekly.pw / 18 September 2021
  • ProcessWire 3.0.184 new master/main version
    This week we have a new master/main version released after a full year in the making. As you might imagine, this new version has a ton of great new stuff and we’ll try to cover much of it here.
    Blog / 27 August 2021
  • Subscribe to weekly ProcessWire news

“We chose ProcessWire because of its excellent architecture, modular extensibility and the internal API. The CMS offers the necessary flexibility and performance for such a complex website like superbude.de. ProcessWire offers options that are only available for larger systems, such as Drupal, and allows a much slimmer development process.” —xport communication GmbH