Score:1

Get the node title field value in the form_state

pe flag

Drupal version 9.3.9.

This is a process of Ajax callback, where the value of the title field is required. In the form node add, the title is displayed. It seems that the form_state does not apply to the title field. When I inspect the variables, the title field is also not reflected in the values.

function my_custom_module_field_widget_process($element, \Drupal\Core\Form\FormStateInterface $form_state,$form) {
  #$element['my_field']['#default_value'] =$form_state->getFormObject()->getEntity()- 
  #>label();this work, but useless in the callback

  if(!empty($form_state->getValue('title'))){
    $title = $form_state->getValue('title');
    $element['my_field']['#default_value'] = $title;
  }
    
  return $element;
}
sonfd avatar
in flag
What do you mean by `$form_state->getFormObject()->getEntity()->label();` works but is useless in the callback?
Belba avatar
pe flag
I mean, the value is stored after the node is been saving. I need the value in an Ajax callback. The user must see the value when entering the form so that they can change or edit the value.
Score:0
dz flag

This should work:

    $node = \Drupal::routeMatch()->getParameter('node');
    if($node) {
        $title = $node->label();
    }
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.