|
|
|
|
@ -86,6 +86,8 @@ class TwigTweakExtension extends AbstractExtension {
|
|
|
|
|
new TwigFunction('drupal_breakpoint', [self::class, 'drupalBreakpoint'], $all_options), |
|
|
|
|
// @phpcs:ignore Drupal.Arrays.Array.LongLineDeclaration |
|
|
|
|
new TwigFunction('drupal_contextual_links', [self::class, 'drupalContextualLinks']), |
|
|
|
|
// @phpcs:ignore Drupal.Arrays.Array.LongLineDeclaration |
|
|
|
|
new TwigFunction('drupal_condition_evaluate', [self::class, 'drupalConditionEvaluate']), |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$this->moduleHandler->alter('twig_tweak_functions', $functions); |
|
|
|
|
@ -420,6 +422,37 @@ class TwigTweakExtension extends AbstractExtension {
|
|
|
|
|
return $build; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Use a condition plugin to evaluate based on the context. |
|
|
|
|
* |
|
|
|
|
* @param string $plugin_id |
|
|
|
|
* The plugin ID. |
|
|
|
|
* @param array $configuration |
|
|
|
|
* Configuration for the plugin. |
|
|
|
|
* |
|
|
|
|
* @return bool |
|
|
|
|
* The evaluation of the plugin. |
|
|
|
|
*/ |
|
|
|
|
public static function drupalConditionEvaluate(string $plugin_id, array $configuration = []): bool { |
|
|
|
|
$pluginInstance = \Drupal::service('plugin.manager.condition')->createInstance($plugin_id); |
|
|
|
|
$pluginInstance->setConfiguration($configuration); |
|
|
|
|
$contextRepository = \Drupal::service('context.repository'); |
|
|
|
|
$availableContexts = $contextRepository->getAvailableContexts(); |
|
|
|
|
|
|
|
|
|
// Ensure that the contexts have data by getting corresponding runtime contexts. |
|
|
|
|
$availableRuntimeContexts = $contextRepository->getRuntimeContexts(array_keys($availableContexts)); |
|
|
|
|
$pluginContextDefinitions = $pluginInstance->getContextDefinitions(); |
|
|
|
|
foreach ($pluginContextDefinitions as $name => $pluginContextDefinition) { |
|
|
|
|
// Identify and fetch the matching runtime context, with the plugin's context definition. |
|
|
|
|
$matches = \Drupal::service('context.handler')->getMatchingContexts($availableRuntimeContexts, $pluginContextDefinition); |
|
|
|
|
$matchingContext = reset($matches); |
|
|
|
|
|
|
|
|
|
// Set the value to the plugin's context, from runtime context value. |
|
|
|
|
$pluginInstance->setContextValue($name, $matchingContext->getContextValue()); |
|
|
|
|
} |
|
|
|
|
return $pluginInstance->evaluate(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Emits a breakpoint to the debug client. |
|
|
|
|
* |
|
|
|
|
|