0

I'm on Drupal 10.2.x.

I'm writing a form_alter for a node add/edit form, where I am putting some extra markup data depending on terms of a vocabulary licenses.

function my_module_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if(//checks to execute code only for the right form) {
    $licenses = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
      ->loadTree('licenses', 0, NULL, TRUE);
    $build = [];
    foreach ($licenses as $license) {
     $build[$license->id()]['name'] = [
        '#type' => 'html_tag',
        '#tag' => 'div',
        '#value' => $license->getName(),
      ]; 
      // add other elements to $build
     }
    $form['field_license']['#suffix'] = \Drupal::service('renderer')
      ->renderPlain($build);
 }
}

If I try to add the relative cache context, taxonomy_term_list:licenses, either doing:

 $form['#cache']['contexts'] = Cache::mergeContexts( $form['#cache']['contexts'], ['taxonomy_term_list:licenses']);

or

$form['#cache']['contexts'][] = 'taxonomy_term_list:licenses';

I get the error

The website encountered an unexpected error. Try again later.

AssertionError: Failed to assert that "languages:language_interface, user.roles:authenticated, taxonomy_term_list:licenses, languages:language_url, user" are valid cache contexts. in assert() (line 31 of core/lib/Drupal/Core/Cache/Cache.php).

Debugging inside CacheContextsManager::validateTokens() I've seen that effectively the array that lists the "valid" cache contexts doesn't have that context, therefore the error is thrown.

$validContextTokens = [
  'cookies' => 0,
  'headers' => 1,
  'ip' => 2,
  'languages' => 3,
  'protocol_version' => 4,
  'request_format' => 5,
  'route' => 6,
  'route.is_admin' => 7,
  'route.menu_active_trails' => 8,
  'route.name' => 9,
  'session' => 10,
  'session.exists' => 11,
  'session.is_masquerading' => 12,
  'theme' => 13,
  'timezone' => 14,
  'url' => 15,
  'url.path' => 16,
  'url.path.is_front' => 17,
  'url.path.parent' => 18,
  'url.query_args' => 19,
  'url.query_args.pagers' => 20,
  'url.site' => 21,
  'user' => 22,
  'user.is_super_user' => 23,
  'user.node_grants' => 24,
  'user.permissions' => 25,
  'user.roles' => 26,
  'languages:language_interface' => TRUE,
  'languages:language_url' => TRUE,
  'user.roles:authenticated' => TRUE,
];

I guess I'm doing something wrong\missing something, because I've already used the above code for adding the cache context in other parts - e.g. preprocess - but it's first time trying to add into a form_alter. However I could not find anything specific on internet.

Any idea why I should do?

2
  • 3
    This is not a valid cache context because it is a cache tag.
    – 4uk4
    May 15 at 10:07
  • Whups, very silly error :facepalm. I don't remember the expression, but it's the perfect case where I was looking into a specific point of the code instead of the big picture. Thanks!
    – Giuseppe
    May 16 at 7:24

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.