1

We have a custom form mymoduleForm that has mymodule.settings which stores some values that we need to run some logic when calling mymodule_preprocess_node() hook. The form just has a text area where we insert URIs and use that in the mymodule_preprocess_node hook to perform some action.

We can store the values in a Config entity as expected (see below). When we make changes to the Config entity, we want to clear cache for the config entity as well. How can we clear the Config Entity's Cache after making changes?

Goal:

  • When there are updates to the Config entity, clear the cache for the entity so that the latest entity is used in mymodule_preprocess_node hook.

Issue:

  • When we make changes to the Config entity using the custom form, the logic that we need to run is not running with the latest settings.

What we have tried:

  1. Tried adding addCacheableDependency($form, $config); to the buildForm(), but it is not working.

mymoduleForm.php

class mymoduleForm extends ConfigFormBase {
  public function buildForm(array $form, FormStateInterface $form_state) {
    if (!$this->entity->isNew()) {
        \Drupal::service('renderer')->addCacheableDependency($form, $this->entity);
      }

    $config = $this->config('mymodule.settings');

    $form['some_field'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Some field title'),
      '#description' => $this->t('some description'),
      '#default_value' => $config->get('some_field'),
    ];

    return parent::buildForm($form, $form_state);
  }

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('mymodule.settings'),
      ->set('some_field', $form_state->getValue('some_fields'))
      ->save();
  }
}

mymodule_preprocess_node

function mymodule_preprocess_node(&$variables) {
  $config = \Drupal::config('mymodule.settings');
  $config_value = $config->get('some_field');

  $library = 'mymodule/some-library';

  if($config_value == 'some_value') {
    $variables['#attached']['library'][] = $library;
  }
}
3

Yes, you have to add the cacheable dependencies of the config object, not only to the form, but also to the render array in preprocess, so that the rendered output gets the correct cache data:

function mymodule_preprocess_node(&$variables) {
  $config = \Drupal::config('mymodule.settings');
  $config_value = $config->get('some_field');
  // add the cache tag, so that the output gets invalidated when the config is saved
  \Drupal::service('renderer')->addCacheableDependency($variables, $config);

  $library = 'mymodule/some-library';

  if($config_value == 'some_value') {
    $variables['#attached']['library'][] = $library;
  }
}
  • I think the dependencies from the form should work without this. See eg. Drupal\search\Form\SearchBlockForm::buildForm(). – Andy Jul 26 '18 at 13:47
  • My bad, didn't read the question closely enough. +1! – Andy Jul 26 '18 at 14:15
  • @Andy, yes, this is the same problem I had while answering. Normally you don't need to add cache data to an administrative config form. But it is unclear what kind of form this is, so I edited the answer, because generally it is not a bad idea to add the cache data of the object to the form on which the form is based on. – 4k4 Jul 26 '18 at 15:33
  • @4k4 you have $variables instead of $form. was that intentional? I am trying to figure out if this addCacheableDependency() needs to happen after the $config = $this->config('mymodule.settings'); – usernameabc Jul 26 '18 at 18:40
  • If this is a config form you probably don't need to add cache data to $form at all. But you need to do this in preprocess. I've edited the answer with the complete preprocess hook. – 4k4 Jul 26 '18 at 19:02

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.