1

I'm using a twig template for a custom block that have images to link to specific nodes. I actually have the node IDs, however generating a relative link doesn't always work. Some pages are using aliases like so 127.0.0.1/web/products and some are using nodes like 127.0.0.1/web/node/19 so using the same relative link doesn't always work. Note that my drupal instance is within a /web/ folder and not at just 127.0.0.1

My twig template looks like this

<div class="row columns">
    {% for key, image in imageurl %}
    <div class="large-4 medium-3 column">
        <a href="./node/{{ nid[key] }}">
            <img src="../sites/default/files/{{ image }}"/>
        </a>
        <div>
            {{ block_title[key] }}
        </div>
    </div>
    {% endfor %}
</div>

When it appears on 127.0.0.1/web/products the link is

http://127.0.0.1/web/node/15

When it appears on 127.0.0.1/web/node/19 the link is

http://127.0.0.1/web/node/node/9

The image has the same issue, since 'image' only contains the filename. Is there a way I can automatically generate a link based on a node ID and a file that I know the path to, to account for URLs that may contain aliases?

1 Answer 1

3

You should use Drupal 8 classes to generate the link. If you get the nid from an existing node object you can do the following in the .theme-file of your theme:

function yourtheme_preprocess_block(&$vars) {
  $node = Drupal::request()->attributes->get('node');
  $vars['link'] = $node->toUrl()->setAbsolute();
}

In twig you can output:

<div class="row columns">
    {% for key, image in imageurl %}
    <div class="large-4 medium-3 column">
        <a href="{{ link }}">
            <img src="../sites/default/files/{{ image }}"/>
        </a>
        <div>
            {{ block_title[key] }}
        </div>
    </div>
    {% endfor %}
</div>

To generate the URL of your node just knowing the nid (here in variable $nid) you can use the following in your .theme-file:

function yourtheme_preprocess_block(&$vars) {
  $options = array('absolute' => TRUE);
  $url = \Drupal\Core\Url::fromRoute('entity.node.canonical', ['node' => $nid], $options);
  $vars['link'] = $url->toString();
}

Again you can use {{ link }} to output the URL where ever you need in yout twig template.

3
  • 1
    I would also advise against writing the image paths like that. See drupal.stackexchange.com/q/185572/10729 for information on how to safely get the path to the public files directory.
    – rooby
    Commented Jul 11, 2016 at 14:59
  • I can get the path to show up on a page template when using the reprocess_page hook but for some reason when I use it in the preprocess_block it doesn't appear in the block template. Could that have something to do with the custom block being created within a custom module?
    – Amy
    Commented Jul 11, 2016 at 15:23
  • I ended up getting the path by using your code within the Block plugin file.
    – Amy
    Commented Jul 11, 2016 at 17:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.