2

I'm working on a custom module that has a block. This block has a form in configuration where user input the relative data, so far it works its saving the data and it display it but without a template (twig).

I was able to make it work with a twig template but it works when the twig file is at the templates directory of the main theme. I need it to work when the template is located at the custom module directory. At the end I'll show the file structure of the module.

I've read a lot of documentation but haven't been able to make it work. Not really sure what's the problem or what I'm missing. I've tried different possible solutions but haven't been able to make it work.

This is the .module file onyx_experienicia.module

function onyx_experiencia_theme($existing, $type, $theme, $path) {
  return [
    'onyxex' => [
      'template' => 'onyxex',
      'variables' => [
        'featured' => [],
        'events' => [],
      ],
    ],
  ];
}

This the block onyx_experiencia.php

namespace Drupal\onyx_experiencia\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\file\Entity\File;
use Druap\image\Entity\ImageStyle;
class onyx_experiencia extends BlockBase implements BlockPluginInterface {

 /**
   * {@inheritdoc}
   */  
  public function build() {
      return [
            '#theme' => 'onyxex',
            '#events' => ['event1', 'event2'],
            '#featured' => ['featured1', 'featured2'],
        ];

  }

This is the html.twig file onyxex.html.twig As I said before, this file is located at modules/custom/onyx_experiencia/templates

{#
/**
 * @file
 * Profile for onyx_experiencia block.
 */
#}
<h3>Featured Events</h3>
<ul>
    {% for featured_event in featured %}
    <li>{{ featured_event }}</li>
    {% endfor %}
</ul>

<h3>Events</h3>
<ul>
    {% for event in events %}
    <li>{{ event }}</li>
    {% endfor %}
</ul>

And this is the file structure of the module

   module
    ------custom
    ------------onyx_experiencia
    ----------------------------onyx_experiencia.info.yml
    ----------------------------onyx_experiencia.libraries.yml
    ----------------------------onyx_experiencia.module
    ----------------------------css
    -------------------------------onyx_serv_css.css
    ----------------------------templates
    -------------------------------------onyx-experiencia.html.twig
    ----------------------------src
    -------------------------------Plugin
    -------------------------------------Block
    ------------------------------------------onyx_experiencia.php
PhönixGeist
  • 309
  • 3
  • 16
  • 1
    Haven't been able to make it work, I've tried adding 'render element', 'template' and 'base hook' at the .module file... but doesn't seem to work either. Any ideas? – PhönixGeist Apr 30 '19 at 02:23
  • 1
    I just ran into the same issue with a custom template in a module. I solved it by explicitly setting a theme suggestion hook even though I had the theme function like you do above. So for example, my theme function returns `menu_local_tasks__entity_user` and then I set a matching suggestion hook. ` `$suggestions[] = 'menu_local_tasks__entity_user';` and that fixed it, my module template now worked and showed up fine. – Danny Englander May 02 '19 at 15:10
  • @DannyEnglander Where did you put $suggestions[] ??? – PhönixGeist May 03 '19 at 19:36
  • 2
    You need a separate function. e.g. `function my_module_theme_suggestions_hook_alter(&$suggestions, array $variables) {` – Danny Englander May 08 '19 at 12:47
  • @DannyEnglander Should put that comment as the answer. 2 hours of getting nowhere and that fixed it. – tobynew Jul 26 '21 at 11:28

0 Answers0