1

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;
}
2
  • What do you mean by $form_state->getFormObject()->getEntity()->label(); works but is useless in the callback?
    – sonfd
    Commented Mar 26, 2022 at 18:46
  • 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.
    – Belba
    Commented Mar 27, 2022 at 3:17

1 Answer 1

0

This should work:

    $node = \Drupal::routeMatch()->getParameter('node');
    if($node) {
        $title = $node->label();
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.