Setting Subfield Values Programatically

Created on 28 February 2023, over 1 year ago
Updated 1 March 2023, over 1 year ago

Hi!

I want to calculate and set the fourth ("points") value of a datafield programatically on node edit submit.

In the submit function, how can I set the values of the points field within the foreach loop?

function add_points_settings_validate(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
	 $duration = 0.6;
	 $node = $form_state->getFormObject()->getEntity()->toArray();
	//  dpm($node);
	 // dpm($node['field_player_stats']);
	  $allPlayers = $node['field_player_stats'];
	 foreach ($allPlayers as $player)	{
		//first subfield dpm($player["player"]);
		//second subfield dpm($player["wins"]);
               //third subfield dpm($player["non_wins"]);
              //fourth subfield dpm($player["points"]);
		if (isset($player["player"])) {
			$points = intval($player["wins"]) * $duration * 2 + intval($player["non_wins"]) * $duration;
			\Drupal::messenger()->addStatus(
				t(htmlentities('Points' .  $points))
			  ); 
		}
/* How can i set the fourth (points) subfield with the calculated $points variable? */

		 $form_state->setValue('field_player_stats', [['points' => $points]]);
	 }
}

Thanks for any thoughts on this.

💬 Support request
Status

Closed: works as designed

Version

1.0

Component

Code

Created by

🇦🇹Austria coreteamvn

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024