1

I'd like to know the node title within the file link template. How can I get it?

For the current example I use the bootstrap file-link.html.twig template. In it, I have the following variables:

{#
/**
 * @file
 * Theme override for a link to a file.
 *
 * Available variables:
 * - attributes: The HTML attributes for the containing element.
 * - link: A link to the file.
 * - icon: An icon.
 * - icon_only: Flag to display only the icon and not the label.
 * - icon_position: Where an icon should be displayed.
 *
 * @see \Drupal\bootstrap\Plugin\Preprocess\FileLink::preprocessVariables
 */
#}

I dont know if a preprocess function is the correct way to go. I didnt even found a respective hook for drupal 8 for that. I dont even know if this is a field template. But still, the question would also be interesting for other templates than file-link.

0

2 Answers 2

4

He wants to get the node title in the file link template (I assume that is file-link.html.twig). You can implement the hook template_preprocess_file_link() in your mytheme.theme file, get the current node and pass its title to twig, something like:

function mytheme_preprocess_file_link(&$variables) {
   $node = \Drupal::request()->attributes->get('node');
   $variables['node_title'] = $node->getTitle();
}

Then, in file-link.html.twig:

{{ node_title }}
5
  • does not work some reason, but I will continue testing
    – Alex
    Commented Jun 9, 2016 at 8:59
  • @Alex did you try the last edit? the initial answer had typos
    – No Sssweat
    Commented Jun 9, 2016 at 9:15
  • @Alex I think that it should work. Did you try some debugging in php and in twig? Commented Jun 9, 2016 at 9:19
  • wouldnt it be cleaner to get the node from the variables?
    – Alex
    Commented Jun 9, 2016 at 9:42
  • 1
    @Alex I didn't tested exactly the code. If you have the node in $variables, use it. I f not, use $node = \Drupal::request()->attributes->get('node'); to get the current node. Commented Jun 9, 2016 at 9:47
-2

I assume, you are using Drupal 7 and we're talking about a node.tpl.php:

<h2<?php print $title_attributes; ?>>
 <a href="<?php print $node_url; ?>">
  <?php print $title; ?>
 </a>
</h2>

*Edit: Just realized, you're using Drupal 8:

For a node.html.twig you could try:

  {{ title_prefix }}
   {% if not page %}
    <h2{{ title_attributes }}>
     <a href="{{ url }}" rel="bookmark">{{ label }}</a>
    </h2>
   {% endif %}
 {{ title_suffix }}
3
  • 4
    I assume, you are using Drupal 7 he is using Drupal 8
    – No Sssweat
    Commented Jun 9, 2016 at 8:19
  • 1
    Sorry, I just saw the small 8!
    – Paul Max
    Commented Jun 9, 2016 at 8:20
  • 1
    the question was how I can access the title of node inside the field template, not the node template
    – Alex
    Commented Jun 9, 2016 at 8:38

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.