0

bI made a custom module and made a custom block inside of it.

In the build () I have this.

return array (
  '#theme' => 'no_athlete_match_block',
  '#title' => 'Athletes With No Matching Name in the Database',
  '#description' => 'Displays the athletes with no matching name in the database for a given sporting event.',
);

In the .module file I have this.

function no_athlete_match_theme($existing, $type, $theme, $path) {
  return array(
    'no_athlete_match_block' => array(
      'variables' => array(
        'description' => NULL,
        'title' => NULL
      ),
      'template' => 'block--no_athlete_match',
    ),
  );
}

my twig file named block--no-athlete-match.html.twig

<div>
node: {{title}} {{description}}

Only Node: shows up. If I do dump(title) I get NULL to show up. Not sure what I'm missing. I've copied other blocks and still nothing.

0

In your module file the suggestion of template will be only with underscores.

block__no_athelete_match

and in templates folder template name needs to be only with dashes.

block--no-athelete-match.html.twig

  • That makes sense and I made the change but it still doesn't work after rebuilding cache. – Tyler Oct 8 '18 at 16:05
0

Because you've specified the template parameter in the hook_theme function the template file name should be exactly as specified block--no_athelete_match.html.twig. The underscores are converted to dashes only in the case where the template parameter is not specified in the hook_theme function.

  • .tpl is for drupal 7. Do you mean .twig? – Tyler Oct 8 '18 at 16:54
  • yeah, sorry about that. I updated the file name. – ec0g Oct 8 '18 at 18:30
0

So having playing around with it for two days I ended up commenting out template and listening to the twig debug recommendations. I then added the path so that I could have the template in my module folder. It might not be the best way to do this but it's working.

My module function

function no_athlete_match_theme($existing, $type, $theme, $path) {
return array(
 'no_athlete_match_block' => array(
          'variables' => array(
              'dsp' => NULL,
              'title' => NULL,
          ),
       //  'template' => 'no-athlete-match',
         'path' => drupal_get_path('module', 'no_athlete_match') . '/templates'
      )
);

}

My block file

 return array (
            '#theme' => 'no_athlete_match_block',
            '#title' => 'Athletes With No Matching Name in the Database',
            '#description' => 'Displays the athletes with no matching name in the database for a given sporting event.',
            '#dsp' => $output,
        );

My Twig file.

<div>
    <table>
        <tr>
            <th>Player</th>
            <th>Query</th>
        </tr>
        <tr>
            <th>{{dsp.player}}</th>
            <th><small>{{dsp.notes}}</small></th>
        </tr>
    </table>
</div>

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.