2

I have a matrix field set up as global (strategies).

Inside each matrix block there is a SuperTable (pairs)

On each row in the SuperTable there is a lightswitch (active)

I need to toggle the lightswitch programatically for each row.

Looking at the SuperTable and Craft docs, I still can't make heads or tails of this. Can anyone help?

Edit

Ended up with a poor man's solution by going directly to the table and editing the column. Would still love to know how to do this "properly, though.

$disable = StcThreePairs::find()
    ->where(['elementId' => $pair->id])
    ->one();

$disable->field_active = 0;
$disable->save();
4

So you'll want to update the Super Table Block's content and to do that when in a Matrix field is a little tricky, but it can be done.

$matrixBlocks = $entry->matrixField->all();

foreach ($matrixBlocks as $matrixBlock) {
    $superTableBlocks = $matrixBlock->superTableField->all();

    foreach ($superTableBlocks as $superTableBlock) {
        $superTableBlock->setFieldValues(['lightswitchField' => true]);

        Craft::$app->getElements()->saveElement($superTableBlock);
    }
}

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.