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();

2 Answers 2

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);
    }
}
1

@crawf This is how I used your code to get and set fields 3 layers deep Supertable->Matrix->supertable

This is on submit in a freeForm form.

Thank you so much!!

<?php

namespace modules\establishmentmodule\events;

use craft;
use craft\elements\Entry;
use yii\base\Event;
use Solspace\Freeform\Library\Composer\Components\Form;
use Solspace\Freeform\Events\Forms\SubmitEvent;

class EventsFormEvent {

    public static function init() {
        Event::on(
            Form::class,
            Form::EVENT_SUBMIT,
            function (SubmitEvent $event) {
                $form = $event->getForm();

                if ($form->getHandle() !== 'events') {
                    return;
                }

                // Get the store that's selected
                $store = $form->get('dynamicRecipient');

                // Get all fields needed to set the booking
                $emailAdres = $store->getActualValue($store->getValue());
                $selectedDate = $form->get('eventDate')->getValue();
                $selectedTime = $form->get('eventTime')->getValue();
                $bookerFirstname = $form->get('firstName')->getValue();
                $bookerLastname = $form->get('lastName')->getValue();
                $bookerEmail = $form->get('email')->getValue()[0];
                $bookerPhone = $form->get('phoneNumber')->getValue();

                // Get current Entry
                $entry = Entry::find()->one();

                // loop tru the locations that are connected to this event
                foreach($entry->eventsLocations as $location) {
                    // If this location is the chosen store, get the dates
                    if ($location->location->one()->establishmentEMailAddress === $emailAdres[0]) {
                        $matrixBlocks = $location->eventDate->all();
                        // Loop tru the dates for this location
                        foreach($matrixBlocks as $matrixBlock) {
                            $currentDate = Craft::$app->getFormatter()->asDate(
                                $matrixBlock->date,
                                'short'
                            );

                            // If selected date is current loop date, get available timeslots
                            if ($currentDate === $selectedDate) {
                                $superTableBlocks = $matrixBlock->timeslots->all();
                                foreach($superTableBlocks as $superTableBlock) {
                                    $currentDate = Craft::$app->getFormatter()->asTime(
                                        $superTableBlock->start,
                                        'short'
                                    );
                                    // If selected timeslot is loop start time, set this timeslot to false and fill in the credentials
                                    if ($currentDate === $selectedTime) {
                                        $superTableBlock->setFieldValues(['slot_status' => false]);
                                        $superTableBlock->setFieldValues(['booker_name' => $bookerFirstname . ' ' . $bookerLastname]);
                                        $superTableBlock->setFieldValues(['booker_email' => $bookerEmail]);
                                        $superTableBlock->setFieldValues(['booker_phone' => $bookerPhone]);
                                        // save and we are done.
                                        Craft::$app->getElements()->saveElement($superTableBlock);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        );
    }
}

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.