0

If I use template_preprocess_file_link, can I add the current url query string and append it to the link per user? Will this link be cached for all users?

Update:

This appends it, but does seem to get cached:

  if (preg_match('/private:\/\//i', $file_entity->getFileUri())) {
    $query = \Drupal::requestStack()->getCurrentRequest()->query->all();

    if (!empty($query)) {
      $options['query'] = $query;
    }
  }

  $variables['link'] = \Drupal::l($link_text, Url::fromUri($url, $options));
1

Adding this cache context seemed to do it (although I do not know if this is bulletproof):

  if (preg_match('/private:\/\//i', $file_entity->getFileUri())) {
    $query = \Drupal::requestStack()->getCurrentRequest()->query->all();

    if (!empty($query)) {
      $options['query'] = $query;
    }

    $variables['#cache']['contexts'][] = 'url.query_args';
  }

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.