0

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 \Drupal\system\ConfigFormBase object.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $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');
    }
0
1

The difference could be that managed_file is using Ajax which is caching the form object.

If you want to have this property cached you would need to declare it as protected property:

class CustomForm extends ConfigFormBase {

  protected $config;

Normally $this->config() is the method FormBase::config as the main entry point to the config API, not a property you populate when constructing the form object. Using the same name could be quite confusing and it's also recommended to use the mentioned method to retrieve the config and declare the config in ConfigFormBase::getEditableConfigNames() if you intend to save it.

1
  • Yes, thank you, using a protected property did the trick. I changed to property name to avoid confusion. I'm using the ̀config($key)` method to load the settings, with the proper declaration of the key in getEditableConfigNames(), the property is just a way to avoid calling the function each time and cache the module config in the object. Maybe it's useless, I'm not sure. – Simon Vart May 28 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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