0

I am struggling to get my custom module template to get use by by block plugin.

I have followed the solution given at: Twig Template for a Custom Block Plugin but continually get this error:

The website encountered an unexpected error. Please try again later. Twig_Error_Loader: Template "modules/custom/lrg_osm/templates/block--osmnhd.html.twig" is not defined in Twig_Loader_Chain->getCacheKey() (line 64 of themes/contrib/bootstrap/templates/block/block.html.twig). Drupal\Core\Template\TwigEnvironment-->getTemplateClass("modules/custom/lrg_osm/templates/block--osmnhd.html.twig") (Line: 424)

I have tried to also fiddle the name of the twig file in various ways to not avail.

My module file structure is shown below with relevant code if anyone is able to point out the error of my ways please:

File and folder structure

src/Plugin/Block/osmnhdblock.php

<?php
declare(strict_types=1);

namespace Drupal\lrg_osm\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;

/**
 * Provides a 'OSM Block for Home development Landing page' block.
 *
 * @Block(
 *   id = "osmnhdblock",
 *   admin_label = @Translation("OSM New Homes Development (NHD) block"),
 *   category = @Translation("LRG")
 * )
 */
class OsmNhdBlock extends BlockBase implements BlockPluginInterface
{
    /**
     * {@inheritdoc}
     *
     */
    public function build()
    {
        return array(
            '#theme' => 'osmtheme',
            '#jsonDataLocations' => 'nhd some data',
            '#title' => 'nhd some title',
        );

        /*
         '#attach' => [
                'css' => '/css/openstreetmap.css',
                'js' => '/js/openstreetmap.js',
            ],
         */
    }
}

lrg_osm.module

<?php
declare(strict_types=1);

function lrg_osm_theme($existing, $type, $theme, $path) {

    return array('osmtheme' =>
        array(
            'variables' => array(
                'title' => null,
                'jsonDataLocations' => null
            ),
            'template' => 'block--osmnhd'
        )
    );
}

templates/block--osmnhd.html.twig

{#
/**
 * @file
 * Some description
 */
#}
<div class="col-md-12 block--osmnhdmap">
    <h1>{{ title }}</h1>
    <h2>{{ jsonDataLocations }}</h2>
</div>
0

The templates directory must always be a direct descendent of the module or theme root directory, in your case /modules/custom/lrg_osm/templates/ (not modules/custom/lrg_osm/src/templates/).

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.