Score:0

Why a form doesn't keep the value created during construction

sa flag

Here is a form using DI for $this->entity_type_manager

There is also a radio-button with Ajax (I have added the code as it is maybe the reason of my problem)

class myForm extends FormBase {

  private ?EntityTypeManagerInterface $entity_type_manager=NULL;

  public function __construct(Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager) {

    $this->entity_type_manager=$entity_type_manager;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_type.manager'),
    );
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    // keep the value in $form_state to be used in submit
    $form_state->set('entity_type_manager', $this->entity_type_manager);
    ...
    // Maybe the Ajax component is the problem
    $form['container'] = [
      '#type' => 'container',
      '#prefix' => '<div id="ajax-wrapper">',
      '#suffix' => '</div>',
      'my_radio' => [
        '#type' => 'radios',
        '#options' => ["option1","option2"],
        '#required' => TRUE,
        '#default_value' => $type_of_bo,
        '#ajax' => [
          'callback' => [$this, 'ajaxGetInvestmentHandler'],
          'wrapper' => 'ajax-wrapper',
          'event' => 'click input',
        ],
      ],
      ...
    ];
  }

  static public function ajaxGetInvestmentHandler(array $form, FormStateInterface $form_state) {
    return $form['container'];
  }
  
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // $this->entity_type_manager is NULL
    // $form_state->get('entity_type_manager') is also NULL
    ...
  }

  ...
  
}

As you can see in the submit, $this->entity_type_manager is NULL and $form_state->get('entity_type_manager') is also NULL.

What is wrong?

Jaypan avatar
de flag
"As you can see" - how can we see this? There isn't any reason why it should be null, how are you determining it is?
Baud avatar
sa flag
Sorry to not be clear enough. I meant that I have made some tests on those values and they are NULL (as showed by the somments in the submit). I agree this should not be NULL but it is... maybe it is coming from the AJAX?
Jaypan avatar
de flag
Based on the code shown, it's not possible for `$this->entity_type_manager` to be NULL. The submitForm function cannot be called without instantiating the form, and the form requires the variable be passed to it to construct the form. That's why I'm asking how you diagnosed it to be NULL.
Baud avatar
sa flag
$this->entity_type_manager is also NULL in the validate function...
No Sssweat avatar
ua flag
Because you're setting it to null... `$entity_type_manager=NULL;`
Baud avatar
sa flag
If I don't set it to NULL as a default value, I got `$entity_type_manager must not be accessed before initialization`
Jaypan avatar
de flag
Then that's a different problem.
Score:2
cn flag

As you can see in the submit, $this->entity_type_manager is NULL

You need to declare the service property protected. Private properties can't be restored. See https://www.drupal.org/docs/drupal-apis/services-and-dependency-injection/dependency-injection-for-a-form#s-considerations-for-dependency-injection

.. and $form_state->get('entity_type_manager') is also NULL

The form_state is not cached between the first form build and the submit request. See buildForm: how differentiate between page reload and ajax callback?. And when you run the form build in later Ajax requests you overwrite the form state value with NULL.

Baud avatar
sa flag
This is magic. You save my day!!!
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.