diff --git a/src/TwigTweakExtension.php b/src/TwigTweakExtension.php index 7052240..b23ec21 100644 --- a/src/TwigTweakExtension.php +++ b/src/TwigTweakExtension.php @@ -6,6 +6,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Unicode; use Drupal\Component\Uuid\Uuid; use Drupal\Core\Entity\EntityInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Field\EntityReferenceFieldItemListInterface; use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldItemListInterface; @@ -14,6 +15,7 @@ use Drupal\Core\Link; use Drupal\Core\Render\Element; use Drupal\Core\Render\Markup; use Drupal\Core\Site\Settings; +use Drupal\Core\Theme\ThemeManagerInterface; use Drupal\Core\Url; use Drupal\file\Entity\File; use Drupal\file\FileInterface; @@ -35,6 +37,28 @@ use Twig\TwigFunction; */ class TwigTweakExtension extends AbstractExtension { + /** + * The module handler to invoke alter hooks. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** + * The theme manager to invoke alter hooks. + * + * @var \Drupal\Core\Theme\ThemeManagerInterface + */ + protected $themeManager; + + /** + * Constructs the TwigTweakExtension object. + */ + public function __construct(ModuleHandlerInterface $module_handler, ThemeManagerInterface $theme_manager) { + $this->moduleHandler = $module_handler; + $this->themeManager = $theme_manager; + } + /** * {@inheritdoc} */ @@ -42,7 +66,7 @@ class TwigTweakExtension extends AbstractExtension { $context_options = ['needs_context' => TRUE]; $all_options = ['needs_environment' => TRUE, 'needs_context' => TRUE]; - return [ + $functions = [ new TwigFunction('drupal_view', 'views_embed_view'), new TwigFunction('drupal_view_result', 'views_get_view_result'), new TwigFunction('drupal_block', [self::class, 'drupalBlock']), @@ -67,6 +91,11 @@ class TwigTweakExtension extends AbstractExtension { new TwigFunction('drupal_breakpoint', [self::class, 'drupalBreakpoint'], $all_options), new TwigFunction('drupal_contextual_links', [self::class, 'drupalContextualLinks']), ]; + + $this->moduleHandler->alter('twig_tweak_functions', $functions); + $this->themeManager->alter('twig_tweak_functions', $functions); + + return $functions; } /** @@ -92,9 +121,24 @@ class TwigTweakExtension extends AbstractExtension { $filters[] = new TwigFilter('php', [self::class, 'phpFilter']); } + $this->moduleHandler->alter('twig_tweak_filters', $filters); + $this->themeManager->alter('twig_tweak_filters', $filters); + return $filters; } + /** + * {@inheritdoc} + */ + public function getTests(): array { + $tests = []; + + $this->moduleHandler->alter('twig_tweak_tests', $tests); + $this->themeManager->alter('twig_tweak_tests', $tests); + + return $tests; + } + /** * Builds the render array for a block. */ diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php index 264bdb4..042a8e7 100644 --- a/tests/src/Functional/TwigTweakTest.php +++ b/tests/src/Functional/TwigTweakTest.php @@ -358,6 +358,18 @@ final class TwigTweakTest extends BrowserTestBase { // -- File URL from media field. $xpath = '//div[@class = "tt-file-url-from-media-field" and contains(text(), "/files/image-1.png")]'; $this->assertXpath($xpath); + + // -- Hook twig_tweak_functions_alter(). + $xpath = '//div[@class = "tt-functions_alter" and text() = "-=bar=-"]'; + $this->assertXpath($xpath); + + // -- Hook twig_tweak_filters_alter(). + $xpath = '//div[@class = "tt-filters_alter" and text() = "bar"]'; + $this->assertXpath($xpath); + + // -- Hook twig_tweak_tests_alter(). + $xpath = '//div[@class = "tt-tests_alter" and text() = "Yes"]'; + $this->assertXpath($xpath); } /** diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig index dcc9f28..0cca8eb 100644 --- a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -89,4 +89,7 @@