1

maybe this is a weird question but I have a dropdown field with values from 1 to 10, and set 4 as a default value.

The thing is: I edit my entry and when I see my dropdown the value is number 1, but I set 4 as a default.

enter image description here

But if I move the value to the top (above option 1) the value shows in my entry. But this shouldn't be right because there is a default value. enter image description here

Do am I missing something? Thanks for your help.

3
  • 1
    Hi, just a simple question, is the field is created after the entries is created? if that so, then yes, the craft will use the first item as the value. but if you create a new entries after the field is created then the default value will be selected by default.
    – aodihis
    Dec 11, 2021 at 11:05
  • ooh, you're right!! (I have already a lot of entries) That's weird. So I think the only solution for old entries is to move my default option at the top of the list right? Thanks!! @aodihis
    – suhkha
    Dec 11, 2021 at 13:51
  • Sorry,just read your reply. I don't think so if you move the default is the best way, because if you just do like that, the already created entries is actually don't have the value until you re-save the entries. So the answer by MoritzLost is the best way I think. Or If you don't want to write a code you can try using feed me plugin to update the data.
    – aodihis
    Dec 14, 2021 at 3:20

1 Answer 1

2

The default value will only apply to newly created entries. Existing entries won't have any saved value for this field. You're only seeing the first value as selected in your entry edit screen because that's how select fields work (in general, not only in Craft). If no option is selected, the first option is selected automatically. Once you save the entry, the selected value will be saved for that entry.

To address the comment:

So I think the only solution for old entries is to move my default option at the top of the list right?

This doesn't solve the problem because you would have to save every entry manually to actually save the selected value. The proper way to add a new field and add a default value to all existing entries is a migration. You can create a new migration using console commands:

php craft migrate/create add_default_field_value

In the migration, you need to find all entries with that field (for example, based on the section it's used in), set the default value for each one and save it.

For example:

use Craft;
use craft\elements\Entry;

$entries = Entry::find()->section('my_section')->anyStatus()->all();
$elementsService = Craft::$app->getElements();
foreach ($entries as $entry) {
    $entry->setFieldValue('my_select_field', 4);
    $elementsService->saveElement($entry);
}

Replace my_section and my_select_field with the handles of your section and field, respectively.

If you have a lot of elements, you might want to batch the element query to avoid hitting memory limits.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.