Jump to content
Orkun

Default Values and Select Options Fieldtype

Recommended Posts

Am I right, that when I attempt to change the default value of a Select Options Fieldtype,
that it will only change the Field Value (or better said the displaying of the selected Radio on the Page Editor) of Pages that were created back in the days but were never saved afterwards?

Every Page that was saved back in the days with the old default value will remain as it is now am I right?

649172053_Bildschirmfoto2021-02-24um09_33_34.thumb.png.923374c40fcfe7ece5b410cf997391b7.png
 

Share this post


Link to post
Share on other sites

 Yes, that's correct. The default value is only used for the interface, it's not applied to any existing pages. You can fix that in two ways:

1. Anywhere you use the value in your code, assume the default if no value is set. Something like this:

// default to behavior option one
$behaviorField = $fields->get('speciality_text_behavior');
$textBehavior = $page->speciality_text_behavior->id ?: $behaviorField->type->getOptions($behaviorField)->get('id=1')->id;

You could also put that in a hook to get that behaviour anywhere you use this field.

2. Write a script to manually change the value of all pages with an empty value in that field to your new default:

$valueMissing = $pages->find('speciality_text_behavior=""');
foreach ($valueMissing as $p) {
	$p->of(false);
	$p->speciality_text_behavior = 1;
	$p->save();
}

You can quickly execute that snippet in the Tracy Debugger console module.

BTW am I the only one who's annoyed by the field name? "speciality" in British English but "behavior" in American English, who does that 😅

  • Like 1
  • Thanks 1

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.

×
×
  • Create New...