Jump to content

Recommended Posts

Page Reference Default Value

Most ProcessWire core inputfield types that can be used with a Page Reference field support a "Default value" setting. This module extends support for default values to the following core inputfield types:

  • Page List Select
  • Page List Select Multiple
  • Page Autocomplete (single and multiple)

Seeing as these inputfield types only support the selection of pages a Page List Select / Page List Select Multiple is used for defining the default value instead of the Text / Textarea field used by the core for other inputfield types. This makes defining a default value a bit more user-friendly.

Note that as per the core "Default value" setting, the Page Reference field must be set to "required" in order for the default value to be used.

Screenshot

default-value

 

https://github.com/Toutouwai/PageReferenceDefaultValue
https://modules.processwire.com/modules/page-reference-default-value/

  • Like 12
Link to comment
Share on other sites

  • 8 months later...

I wanted to do something a little different but the code for this module helped me figure out how to achieve it. I had an existing page with a page reference field with a value that I wanted as the default value for the same page reference field when used on another template, ie I wanted to be able to allow admin but non-super users set a default themselves on a site settings page that they could change periodically.

I ended up doing it as a hook in ready.php, but I guess with some more work it could be made into a module. It would need to allow selection of both source template and field, and templates the default should apply to (to avoid circular references of the field to itself).

/**
 * Check for default value and populate when appropriate
 */
$wire->addHookBefore('InputfieldPage::render', function(HookEvent $event) {
		/** @var InputfieldPage $if */
		$if = $event->object;
		$inputfield = $if->getInputfield();
		//$default_value = $this->getDefaultValue($inputfield);
		if (!($if->hasPage->template == 'ProductionRole' && $if->hasField->name == 'pageShow'))
				return;
		$default_value = $event->pages->get('name=settings')->pageShow;
		if (!$if->required || !$default_value || !$if->isEmpty())
				return;
		$inputfield->value = $default_value;
});

 

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

×
×
  • Create New...