Jump to content

Dynamic Options


Robin S
 Share

Recommended Posts

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);
    }

});
  • Like 22
Link to comment
Share on other sites

v0.1.3 released. This version adds options to set a limit to the number of items that may be selected. When the limit is reached the supported core "multiple" inputfields become disabled. It  adds an option for the formatted value to be Pagefile/Pageimage object(s) where that would be relevant. And it integrates with the newly released Select Images module to create a kind of "image reference" field. See the updated readme for more detail.

 

  • Like 3
Link to comment
Share on other sites

  • 1 year later...

Thanks for this great module!  I was wondering if it would be possible to add support for making it work with custom image/file fields templates?

At the moment the option to enable it is crossed out in FieldtypeFile modeule settings (in PW 3.0.211 dev).

Link to comment
Share on other sites

8 hours ago, LMD said:

At the moment the option to enable it is crossed out in FieldtypeFile modeule settings (in PW 3.0.211 dev).

Probably it applies to the strikethrough to any non-core fieldtypes because it can't guarantee 100% compatibility. Did you try ticking the checkbox anyway? It seems to work when I did some quick testing.

Link to comment
Share on other sites

13 hours ago, Robin S said:

Probably it applies to the strikethrough to any non-core fieldtypes because it can't guarantee 100% compatibility. Did you try ticking the checkbox anyway? It seems to work when I did some quick testing.

Well, this is embarassing, I did actually try it yesterday and it didn't work, but I tried again this morning and it works just fine now.  I don't know what changed  - maybe I forgot to save something before.

Sorry about that!

  • Like 1
Link to comment
Share on other sites

  • 1 month later...

Update:  SOLVED!

Never mind, I had moved a bunch of hooks to admin.php, including the DynamicOptions hook, which is why it stopped working on the frontend.  I have put the hook back in ready.php and works as expected.  Of course that makes perfect sense in retrospect.

[--- Snipped description of the problem, it was a wild goose chase! --]

Edited by LMD
I solved the issue, it was me... I was the problem!
  • Like 1
Link to comment
Share on other sites

Not a problem, this time, but an FYI and a solution in case anybody needs it.

When using a Dynamic Options field as an images/files custom field, the 'page' value of 'arguments(0)' is not the page being edited, but instead it's a dummy page used for the 'field-fieldname' template.

If the actual page is required for some logic (a need I had), then this poses an issue... but there is a solution!

In your 'getSelectableOptions' hook, do the following:

$wire->addHookAfter('FieldtypeDynamicOptions::getSelectableOptions', function(HookEvent $event) {
    $page = $event->arguments(0); // Page data (which will be wrong for our specific use case)
    $field = $event->arguments(1); // Field data

    // 'foo' is an images custom field
    if ($field->name === 'foo') {
		/* Get the page -- where we get page from will be different in the admin (edit) and frontend.*/
        $page = $this->page; // the current page route (sufficient for the frontend)

		/* However, in the admin edit page (check the process) */
        if ($page->process === 'ProcessPageEdit') {
			// Get the id of page being edited from $input and then use that to fetch the page
            $page = $this->pages->get($this->input->get('id', 'int'));
        }

        $options = []; // empty array to return if no valid page is found

		// If it is a valid id... continue as usual, using the $page var
        if ($page->id) {
            if ($page->template->name === 'gallery') {
                $options = ['foo' => "Foo", 'bar' => "Bar"];
            } else {
                $options = ['lorem' => "Lorem", 'ipsum' => "Ipsum"];
            }
        }

        $event->return = $options;
    }

	// ... other fields as required can use the $page from args
});

No idea if there is a different way, but this method is fairly simple, besides, it's not going to be a common issue.

Hope this helps somebody.

Here's the Tracy dump of the custom field "page" for the curious:

ProcessWire\DefaultPage
	id: 0
	name: ''
	parent: ''
	status: 'corrupted'
	template: 'field-images'
	caption: ''
    foo: => ''
	data: array
    	'caption' => ''
		'foo' => ''

 

  • Like 1
Link to comment
Share on other sites

  • 9 months later...
6 hours ago, eydun said:

Have you been able to make it work in a combo-field?

No, this module is a fieldtype, and Combo is also a fieldtype. There isn't such a thing as one fieldtype module working inside another fieldtype module.

But you can easily add both a Dynamic Options field and a Combo field to your template so that you can use them together. You can even have the Dynamic Options inputfield adjacent to any particular Combo subfield in Page Edit thanks to the "Custom form placement" option for Combo subfields:

image.png.7f6395605d11cb00f39ef1cd4a2e5746.png

  • Thanks 1
Link to comment
Share on other sites

  • 6 months later...

I have a strange issue where a Dynamic Options field returns empty when called from a page parser within the AppApi in ProcessWire.

When I do the same call using Tracy Debugger, it works perfectly. I'm simply using $page->field_name to output a single value.

I've tried some debugging and found that in the AppApi context, the ___formatValue method returns an empty $value. However, I don't fully understand the underlying structure of how field data is retrieved in ProcessWire.

Has anyone encountered this issue or can explain why this might be happening? Any help on understanding or resolving this would be appreciated.

Link to comment
Share on other sites

@ttttim, as a test you could try $page->getUnformatted('field_name') and see if that is also empty. That will tell you if the issue relates to the formatValue() method or not.

It's just a guess, but the saved value might be failing the test at FieldtypeDynamicOptions::sanitizeValue(). This checks the saved value against the allowed values as defined in your hook to FieldtypeDynamicOptions::getSelectableOptions(). If you temporarily change that method so that it doesn't do any sanitizing...

public function sanitizeValue(Page $page, Field $field, $value) {
	return $value;
}

...and find that you are then able to get the field value via AppApi then it means that something about AppApi is causing your hook to not return the expected selectable options. Like, maybe $page and $field are not what you would expect them to be inside the hook. You should be able to use Tracy to debug what is going on inside your hook when AppApi is involved.

Link to comment
Share on other sites

  • 1 month later...
2 hours ago, elabx said:

Does setting the default value in the field setting help you to set the default selection option? (I am specifically using Radios input)

I haven't tried that before, but I just tried it now and it does work. For "Default value" you have to enter the value of the dynamic option rather than the label, and the field has to be "Required" as per the default value note.

P.S. I just released v0.1.8 which fixes a minor config fields bug, so you might like to upgrade.

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Hey Robin,
Thanks for your work. It looks like this module would fit my need for something – could you confirm please?
I need to retrieve data from an external database (I’m okay with thay), something like an array of id => value.
I’d like the select options to show the value, and then store the pair id / value in the field.
Can I do that with this module?

Link to comment
Share on other sites

10 hours ago, TomPich said:

I need to retrieve data from an external database (I’m okay with thay), something like an array of id => value.
I’d like the select options to show the value, and then store the pair id / value in the field.
Can I do that with this module?

Yes, you could do that. The fieldtype just needs an array of options returned from a hook to FieldtypeDynamicOptions::getSelectableOptions() and there are no constraints on how that array is generated.

The fieldtype only stores the value, but if you needed to look up the label that goes with a value you could just run the same code that generates the options array and then get the label for any value. One way you might do this is via a method for a custom Page class that returns the selectable options - that way you don't need to repeat any code. So you would add a method like getDynamicOptions() to the Page class that the Dynamic Options field is used with.

Your hook would then be:

$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') {
		/*
		 * Get an array of options as $value => $label from the custom Page class method
		 *
		 * If you have more than one Dynamic Options field on the page that you need
		 * to distinguish between then you could pass the field name as an argument to the method
		 * e.g. $page->getDynamicOptions('your_field_name')
		 * */
		$options = $page->getDynamicOptions();
		// Set that options array as the event return
		$event->return = $options;
	}
});

And to look up the label for the selected option in the template file:

// Get the label for the selected option
$options = $page->getDynamicOptions();
$label = $options[$page->your_field_name];

This assumes your field is for single options. If it's for multiple options then you would loop over the field value and look up each label from the $options array.

  • 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...