Score:0

How can I set a node's nested (address field) value in a custom submit handler?

ng flag

I need to clear any data from an address field's values (administrative area, etc.)

In my custom submit hanlder for node form, I know I can use something like below for a standard field:

<?php
  $form_state->setValue('field_alternate_phone', [['value' => 'NEW VALUE']]);
?>

But I cannot figure out how to change the value of a nested address field where the values are something like:

enter image description here

I've solutions similar to below but nothing is working:

$form_state->setValue('field_service_location', [
    ['address' => ['organization' => 'test22111']]
  ]);
Ivaylo Tsandev avatar
us flag
Have you tried with `$form_state->setValue(['field_service_location'][0]['address']['organization'], 'your value here')`. As pointed in the documentation, the key for `setValue` could be either a string for a direct field or an array for nested field. Reference here: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Form%21FormStateInterface.php/function/FormStateInterface%3A%3AsetValue/8.7.x
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.