function handling_points_settings_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$duration = 0.6;
$node = $form_state->getFormObject()->getEntity()->toArray();
$allPlayers = $node['field_player_stats'];
foreach ($allPlayers as &$player) {
if (isset($player["player"])) {
$points = intval($player["wins"]) * $duration * 2 + intval($player["non_wins"]) * $duration;
$player["points"] = $points;
}
}
$node->set('field_player_stats', $allPlayers);
$node->save();
\Drupal::messenger()->addStatus(t('Points calculated and saved for all players.'));
}
In this example, we first retrieve the value of the my_datafield element from $form_state using the getValue() method. We then loop through each item in the datafield using a foreach loop, and calculate the points value using the second and third subfields. Finally, we set the fourth subfield value to the calculated points value, and update the datafield value in $form_state using the setValue() method.
Note that in the code above, we are using the & reference operator to modify the $item variable directly, rather than creating a copy of it. This allows us to modify the actual value of the datafield subfield instead of just changing the value of the $item variable.