I'm trying to create a block that will pass a variable to its template. For now just some text but it will be an array later.
The default block variables such as label are working but I'm unable to get the value to show up in template.
Any ideas ?
FrontAppsBlock.php
namespace Drupal\custom_blocks\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Block\BlockPluginInterface;
/**
* @Block(
* id = "front_apps_block",
* admin_label = @Translation("Front Apps Block"),
* )
*/
class FrontAppsBlock extends BlockBase implements BlockPluginInterface {
public function build() {
return array(
'#theme' => 'front_apps_block',
'#app' => 'test value',
);
}
}
custom_blocks.module
function custom_blocks_theme($existing, $type, $theme, $path) {
return [
'front_apps_block' => [
'variables' => [
'app' => null
]
]
];
}
block--front-apps-block.html.twig
<p>test: {{ app }}</p>
<p>label: {{ label }}</p>
{{ app }}
?{{ app }}
in my template. And it is not displaying anything.content()
function for your block? see drupal.org/docs/8/theming/twig/…content()
function is not needed since it uses thebuild()
function.