1

I have created a form using which I want to make changes in the configuration.Currently I am submitting form doing something like this:

 public function submitForm(array &$form, FormStateInterface $form_state) {
  \Drupal::service('config.factory')
    ->getEditable('module.settings')
    ->set('module_entities', $form_state->getValue('module_entities'))
    ->save();
 }

But the problem is when I have submitted the form changes in the configuration are not reflected.But when I clear the cache after submitting the form the changes are reflected.what changes should I make so that when user submits form the changes are reflected thereafter?

Update: I tried to find my mistake and found out that configuration is getting updated but somehow the changes getting lost and not being reflected in the block which I have created. In the block creation code I am creating blocks for different entities depending on the entities present in the configuration.But the configuration is not showing correct values so block for required entities is not being created.Surprisingly when I clear cache the code works(which means configuration becomes updated).Does this mean for block creation I need to clear cache? I am currently using this code:

 `public function getDerivativeDefinitions($base_plugin_definition) {
   $this->derivatives[$entity_type] = $base_plugin_definition;
   $this->derivatives[$entity_type]['admin_label'] .= ' (' . $entity_definition->getLabel() .')';
   $this->derivatives[$entity_type]['cache'] = ?;

}`

What should I add over here to make block depend on configuration without clearing cache? In the plugin I am returning:

public function build() { return array( '#theme' => 'links__entity__module', '#links' => $this->linkBuilder->buildLinks($this->request->attributes->get($entity_type)), ); }

  • You can simplify by using ConfirmFormBase and implement getEditableConfigNames() and then you can just use $this->config(). But other than that, this looks correct. Make sure that all your keys are correct.. are the form values really there, is this really the same key as the one you're using in the code using it.. Is the config schema correct, ... – Berdir May 26 '15 at 19:14
  • @Berdir, I checked for possible errors from my side but still couldn't find any.Can you suggest the reason for the form working when I have cleared the cache.Thanks. – user2750995 Jun 5 '15 at 19:33
  • I updated my problem. – user2750995 Jun 5 '15 at 22:09
2

Blocks are cached by default.

You don't need to manually clear those caches when you change your configuration, but you need to flag/tag your blocks output to tell Drupal that your block output depends on your configuration.

Read the Cacheability documentation. What you need is something like this:

$output['#cache']['tags'] = $config->getCacheTags();
  • So is I am using getDerivativeDefinitions() to add tabs for different type of entities then what changes should I make(code writen above) to tell Drupal abt dependency on configuration settings. – user2750995 Jun 10 '15 at 9:16
0

Something which I did in my form was:

    public function submitForm(array &$form, FormStateInterface $form_state) {
        $config = \Drupal::configFactory()->getEditable('module.settings');
        $userInputValues = $form_state->getUserInput();

        $config
            ->set('module_entities', $userInputValues('module_entities'))
            ->save();
        parent::submitForm($form, $form_state);
    }

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.