8 – Why ConfigFormBase does not goes through custom construct() when using managed_file?

First I want to state that this is a question. My Form is working, but I had, strangely, to add clauses for the case when my CustomForm extending ConfigFormBase did not goes as expected throught the __construct()

I created a config property in the __construct(). Then, I could access it from any function, and it worked fine.

As soon as I added a managed_file, things went wrong. Specifically, $this->config was null in validateForm and submitForm. I could restore the property easily by calling the factory again (and that’s what I end up doing), but I’m confused.

What I see is the managed_file module send the form in XHR when updating the managed_file input. So when I’m validating the form, maybe there is a cache version or something that gets loaded, or the callback_func works differently. Anyway, the __construct() is executed on the XHR request (I checked with xdebug) but not after when I’m (re) submitting the form with the button.

I compared the stack when $this->config is null and when it’s not, and did not spot any difference.

Maybe it’s part of a multi-part Form functionality ?

I’m sure I’m getting it wrong somewhere.

Relevant parts as follow :

  /**
   * Constructs a DrupalsystemConfigFormBase object.
   *
   * @param DrupalCoreConfigConfigFactoryInterface $config_factory
   *   The factory for configuration objects.
   */
  public function __construct(ConfigFactoryInterface $config_factory) {
    $this->setConfigFactory($config_factory);
    $this->config = $this->config('custom.settings');

    parent::__construct($config_factory);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('config.factory'),
    );


     }

/**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state): array {

    $form('section5')(self::TAB_IMAGE) = (
      '#type' => 'managed_file',
      '#title' => $this->t("Image de l'onglet"),
      '#default_value' => ($this->config->get(self::TAB_IMAGE)) ? ($this->config->get(self::TAB_IMAGE)) : NULL,
      '#upload_location' => 'public://',
      '#upload_validators' => (
        'file_validate_extensions' => ('gif png jpg jpeg'),
        'file_validate_size' => (Bytes::toInt(Environment::getUploadMaxSize())),
        'file_validate_image_resolution' => ('300x300'),
      ),
    );
}


  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    // When submitting a Form with managed_file, weirdly no __construct().
    if (!$this->config) {
      $this->config = $this->config('custom.settings');
    }