Answers.Body: <p>@crawf
  This is how I used your code to get and set fields 3 layers deep Supertable-&gt;Matrix-&gt;supertable</p>
  <p>This is on submit in a freeForm form.</p>
  <p>Thank you so much!!</p>
  <pre><code>&lt;?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-&gt;getForm();
  
                  if ($form-&gt;getHandle() !== 'events') {
                      return;
                  }
  
                  // Get the store that's selected
                  $store = $form-&gt;get('dynamicRecipient');
  
                  // Get all fields needed to set the booking
                  $emailAdres = $store-&gt;getActualValue($store-&gt;getValue());
                  $selectedDate = $form-&gt;get('eventDate')-&gt;getValue();
                  $selectedTime = $form-&gt;get('eventTime')-&gt;getValue();
                  $bookerFirstname = $form-&gt;get('firstName')-&gt;getValue();
                  $bookerLastname = $form-&gt;get('lastName')-&gt;getValue();
                  $bookerEmail = $form-&gt;get('email')-&gt;getValue()[0];
                  $bookerPhone = $form-&gt;get('phoneNumber')-&gt;getValue();
  
                  // Get current Entry
                  $entry = Entry::find()-&gt;one();
  
                  // loop tru the locations that are connected to this event
                  foreach($entry-&gt;eventsLocations as $location) {
                      // If this location is the chosen store, get the dates
                      if ($location-&gt;location-&gt;one()-&gt;establishmentEMailAddress === $emailAdres[0]) {
                          $matrixBlocks = $location-&gt;eventDate-&gt;all();
                          // Loop tru the dates for this location
                          foreach($matrixBlocks as $matrixBlock) {
                              $currentDate = Craft::$app-&gt;getFormatter()-&gt;asDate(
                                  $matrixBlock-&gt;date,
                                  'short'
                              );
  
                              // If selected date is current loop date, get available timeslots
                              if ($currentDate === $selectedDate) {
                                  $superTableBlocks = $matrixBlock-&gt;timeslots-&gt;all();
                                  foreach($superTableBlocks as $superTableBlock) {
                                      $currentDate = Craft::$app-&gt;getFormatter()-&gt;asTime(
                                          $superTableBlock-&gt;start,
                                          'short'
                                      );
                                      // If selected timeslot is loop start time, set this timeslot to false and fill in the credentials
                                      if ($currentDate === $selectedTime) {
                                          $superTableBlock-&gt;setFieldValues(['slot_status' =&gt; false]);
                                          $superTableBlock-&gt;setFieldValues(['booker_name' =&gt; $bookerFirstname . ' ' . $bookerLastname]);
                                          $superTableBlock-&gt;setFieldValues(['booker_email' =&gt; $bookerEmail]);
                                          $superTableBlock-&gt;setFieldValues(['booker_phone' =&gt; $bookerPhone]);
                                          // save and we are done.
                                          Craft::$app-&gt;getElements()-&gt;saveElement($superTableBlock);
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
          );
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-15T09:21:58.467Z
Answers.LastActivityDate: 2022-06-15T09:21:58.467Z
Answers.UserDisplayName: Dimitri Hilverda
Answers.Body: <p>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.</p>
  <pre><code>$matrixBlocks = $entry-&gt;matrixField-&gt;all();
  
  foreach ($matrixBlocks as $matrixBlock) {
      $superTableBlocks = $matrixBlock-&gt;superTableField-&gt;all();
  
      foreach ($superTableBlocks as $superTableBlock) {
          $superTableBlock-&gt;setFieldValues(['lightswitchField' =&gt; true]);
  
          Craft::$app-&gt;getElements()-&gt;saveElement($superTableBlock);
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-02-02T21:49:21.257Z
Answers.LastActivityDate: 2021-02-02T21:49:21.257Z
Answers.UserDisplayName: crawf
Body: <p>I have a matrix field set up as global (<code>strategies</code>).</p>
  <p>Inside each matrix block there is a SuperTable (<code>pairs</code>)</p>
  <p>On each row in the SuperTable there is a lightswitch (<code>active</code>)</p>
  <p>I need to toggle the lightswitch programatically for each row.</p>
  <p>Looking at the SuperTable and Craft docs, I still can't make heads or tails of this. Can anyone help?</p>
  <p>Edit</p>
  <p>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 &quot;properly, though.</p>
  <pre><code>$disable = StcThreePairs::find()
      -&gt;where(['elementId' =&gt; $pair-&gt;id])
      -&gt;one();
  
  $disable-&gt;field_active = 0;
  $disable-&gt;save();
  </code></pre>
  
Tags: <plugin-development><plugin-supertable>
Title: Editing SuperTable inside Matrix block
ThreadText: # Editing SuperTable inside Matrix block
  
  (Asked by: username_0 on 2021-02-02)
  
  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();
  
  ```
  
  **Tags:** plugin-development, plugin-supertable
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2021-02-02. Score: 4 | Accepted answer
  
  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);
      }
  }
  
  ```
  
  
  ---
  
  ### Answer by username_1 on 2022-06-15. Score: 1
  
  @username_2
  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);
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
          );
      }
  }
  
  ```
  
  
  ---
  
  