Reputation: 319
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
Upvotes: 2
Views: 1616