For more information about this repository, visit the project page at https://www.drupal.org/project/twig_tweak
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.9 KiB

8 years ago
<?php
/**
* @file
* Primary module hooks for Twig Tweak test module.
*/
use Drupal\block\BlockInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\file\FileInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
8 years ago
/**
* Implements hook_page_bottom().
*/
function twig_tweak_test_page_bottom(): void {
Drupal::service('messenger')->addMessage('Hello world!');
8 years ago
}
/**
* Implements hook_theme().
*/
function twig_tweak_test_theme(): array {
8 years ago
return ['twig_tweak_test' => ['variables' => []]];
}
/**
* Prepares variables for twig-tweak-test template.
*/
function template_preprocess_twig_tweak_test(array &$vars): void {
$vars['node'] = Node::load(1);
}
/**
* Implements hook_block_access().
*
* @see \Drupal\Tests\twig_tweak\Kernel\RegionViewBuilderTest
*/
function twig_tweak_test_block_access(BlockInterface $block): AccessResultInterface {
$result = AccessResult::forbiddenIf($block->id() == 'private_block');
$result->cachePerUser();
$result->addCacheTags(['tag_for_' . $block->id()]);
$result->setCacheMaxAge(123);
return $result;
}
/**
* Implements hook_node_access().
*
* @see \Drupal\Tests\twig_tweak\Kernel\EntityViewBuilderTest
*/
function twig_tweak_test_node_access(NodeInterface $node): AccessResultInterface {
$result = AccessResult::forbiddenIf($node->getTitle() == 'Private node');
$result->addCacheTags(['tag_from_' . __FUNCTION__]);
$result->cachePerUser();
$result->setCacheMaxAge(50);
return $result;
}
/**
* Implements hook_file_access().
*
* @see \Drupal\Tests\twig_tweak\Kernel\ImageViewBuilderTest
*/
function twig_tweak_test_file_access(FileInterface $file): AccessResultInterface {
$is_public = parse_url($file->getFileUri(), PHP_URL_SCHEME) == 'public';
$result = AccessResult::allowedIf($is_public);
$result->cachePerUser();
$result->addCacheTags(['tag_for_' . $file->getFileUri()]);
$result->setCacheMaxAge(70);
return $result;
}