Skip to content

Instantly share code, notes, and snippets.

@d0t15t
Created July 21, 2021 11:32
Show Gist options
  • Save d0t15t/92f2a799c74c1fb42ecf709e5d011c9d to your computer and use it in GitHub Desktop.
Save d0t15t/92f2a799c74c1fb42ecf709e5d011c9d to your computer and use it in GitHub Desktop.
<?php
namespace Drupal\test_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
/**
* Provides a 'Test block' Block.
*
* @Block(
* id = "test_block",
* admin_label = @Translation("Test block"),
* category = @Translation("Test block"),
* )
*
* Drupal Cache Contexti
* https://www.drupal.org/docs/8/api/cache-api/cache-contexts
*/
class TestBlock extends BlockBase {
public function build() {
$node = \Drupal\node\Entity\Node::load(1);
$node_title = $node->getTitle();
return [
'#markup' => $this->t('The title of node #1 is: ') . $node_title,
'#cache' => [
'contexts' => [
'url.path',
],
],
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment