The url of "Your cookie policy" is mandatory absolute, I would like to have the possibility to use a relative URL.

I have tried changing "url" to "path" into the dev version of the module:

    $form['cookie_policy'] = [
      '#type' => 'url',
      '#title' => $this->t('Your cookie policy'),
      '#description' => $this->t('If you already have a cookie policy, link to it here.'),
      '#maxlength' => 255,
      '#size' => 64,
      '#default_value' => $config->get('cookie_policy'),
    ];

To

    $form['cookie_policy'] = [
      '#type' => 'url',
      '#title' => $this->t('Your cookie policy'),
      '#description' => $this->t('If you already have a cookie policy, link to it here.'),
      '#maxlength' => 255,
      '#size' => 64,
      '#default_value' => $config->get('cookie_policy'),
    ];

But I get an error that said:
"This path does not exist or you do not have permission to link to"

And it is not possible any more link with an absolute URL.

Has someone any other idea?

Comments

dimr created an issue. See original summary.

dimr’s picture

Issue summary: View changes
Issue tags: +EU Cookie, +absolute url
StatusFileSize
new497 bytes
ckaotik’s picture

Normally, you can enter a relative url in an url field (which contains an uri string) by prefixing it, e.g. internal:/node/1. However, this is not yet handled by the module. To fix this properly, we'd have to convert the uri to a proper Url object using Url::fromUri() and set this for the relevant variables. Twig can handle url objects nicely and automatically calls toString during render. The JS however needs the plain text url. Probably something along the lines of this:

use Drupal\Core\Url;
// in cookieconsent_page_attachments():
$url = !empty($config->get('cookie_policy')) ? Url::fromUri($config->get('cookie_policy')) : NULL;
$attachments['#attached']['drupalSettings']['cookieconsent']['link'] = $url ? $url->toString() : NULL;
// ...
$element['#link'] = $url;

As a workaround for now, you can change the #type of the cookie_policy to textfield, as that value is directly passed along to the template.

hewok’s picture

Hi,
you can override the module method with:

function THEMENAME_page_attachments_alter(array &$attachments) {

  $config = \Drupal::config('cookieconsent.settings');

  // Get the URL of Cookie Policy from node ID
  $options = ['absolute' => TRUE];
  $cookie_policy_url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => NODE_ID], $options);
  $cookie_policy_url = $cookie_policy_url->toString();

  // Update Cookie Policy URL - CookieConsent module
  $attachments['#attached']['drupalSettings']['cookieconsent']['link'] = $cookie_policy_url;

  // Render the template and pass it over to our javascript.
  /** @var Drupal\Core\Render\Renderer $renderer */
  $renderer = \Drupal::service('renderer');
  $element = [
    '#theme' => 'cookieconsent',
    '#dismiss' => $config->get('accept_button_text'),
    '#message' => $config->get('headline_text'),
    '#target' => $config->get('target'),
    '#link' => $cookie_policy_url,
    '#learn_more' => $config->get('read_more_button_text'),
  ];
  /** @var Drupal\Core\Render\Markup $markup */
  $markup = $renderer->renderRoot($element);

  // Cast to string (Uses the magic __toString method).
  $attachments['#attached']['drupalSettings']['cookieconsent']['markup'] = (string) $markup;
}
matt b’s picture

This needs to be fixed in the module, not with a workaround.

nitebreed’s picture

Status: Active » Closed (duplicate)

Closing this because of the patch committed in #2910802: Update the policy link field type to a node entity autocomplete. I feel that functionality is more suited.