1

related to How can I populate a dropdown via table field type using the default values?

I created a table to be used for ordering a display via the order of the table, with a bunch of default values in place. When I apply the table to already existing categories, none of the default values display, but when I create a new category all the default values are there. This is expected behavior so far, but I am looking for a way to apply the default values to the field existing on pre-existing categories. A migration is currently the best approach I have come across, but I am struggling to get the values applied. The safeUp() for my current migration is

 public function safeUp()
    {

        $elementsService = Craft::$app->getElements();

        $catQuery = Category::find()
            ->groupId('not 5');

        $fieldsService = Craft::$app->fields;
        
        $defaultFilterTable = $fieldsService
            ->getFieldByHandle('filterDisplayOrder');
        
        foreach ($catQuery as $cat) {
            $cat->setFieldValue('filterDisplayOrder', $defaultFilterTable);
            $elementsService->saveElement($cat);
        }
    }

My thought process was to set the default field as the value of the field on the category but that migration completed with no errors and no default values applied on the fields on the category. Thanks in advance.

1 Answer 1

1

The reason the migration wasn't working was I was trying to apply the whole field as $defaultFilterTable and not the values. The fix was changing $defaultFilterTable to $defaultFilterTable->defaults.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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