TwigFunction example

public function __construct(AccessManagerInterface $access_manager, HelpTopicPluginManagerInterface $plugin_manager, TranslationInterface $string_translation) {
    $this->accessManager = $access_manager;
    $this->pluginManager = $plugin_manager;
    $this->stringTranslation = $string_translation;
  }

  /** * {@inheritdoc} */
  public function getFunctions() {
    return [
      new TwigFunction('help_route_link', [$this, 'getRouteLink']),
      new TwigFunction('help_topic_link', [$this, 'getTopicLink']),
    ];
  }

  /** * Returns a link or plain text, given text, route name, and parameters. * * @param string $text * The link text. * @param string $route * The name of the route. * @param array $parameters * (optional) An associative array of route parameter names and values. * @param array $options * (optional) An associative array of additional options. The 'absolute' * option is forced to be TRUE. * * @return array * A render array with a generated absolute link to the given route. If * the user does not have permission for the route, or an exception occurs, * such as a missing route or missing parameters, the render array is for * the link text as a plain string instead. * * @see \Drupal\Core\Template\TwigExtension::getUrl() */
$this->themeManager = $theme_manager;
    $this->dateFormatter = $date_formatter;
    $this->fileUrlGenerator = $file_url_generator;
  }

  /** * {@inheritdoc} */
  public function getFunctions() {
    return [
      // This function will receive a renderable array, if an array is detected.       new TwigFunction('render_var', [$this, 'renderVar']),
      // The URL and path function are defined in close parallel to those found       // in \Symfony\Bridge\Twig\Extension\RoutingExtension       new TwigFunction('url', [$this, 'getUrl']['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
      new TwigFunction('path', [$this, 'getPath']['is_safe_callback' => [$this, 'isUrlGenerationSafe']]),
      new TwigFunction('link', [$this, 'getLink']),
      new TwigFunction('file_url', [$this, 'getFileUrl']),
      new TwigFunction('attach_library', [$this, 'attachLibrary']),
      new TwigFunction('active_theme_path', [$this, 'getActiveThemePath']),
      new TwigFunction('active_theme', [$this, 'getActiveTheme']),
      new TwigFunction('create_attribute', [$this, 'createAttribute']),
    ];
  }

  public function getFunctions() {
    return [
      'testfunc' => new TwigFunction('testfunc', ['Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFunction']),
    ];
  }

  /** * Generates a list of all Twig filters that this extension defines. * * @return array * A key/value array that defines custom Twig filters. The key denotes the * filter name used in the tag, e.g.: * @code * {{ foo|testfilter }} * @endcode * * The value is a standard PHP callback that defines what the filter does. */
/** * {@inheritdoc} */
  public function getFunctions(): array {
    // Override Twig built in debugger when Symfony VarDumper is available to     // improve developer experience.     // @see \Twig\Extension\DebugExtension     // @see \Symfony\Component\VarDumper\VarDumper     if (class_exists(self::SYMFONY_VAR_DUMPER_CLASS)) {
      return [
        new TwigFunction('dump', [self::class, 'dump']['needs_context' => TRUE, 'needs_environment' => TRUE, 'is_variadic' => TRUE]),
      ];
    }

    return [];
  }

  /** * Dumps information about variables using Symfony VarDumper. * * @param \Twig\Environment $env * The Twig environment. * @param array $context * Variables from the Twig template. * @param array $variables * (optional) Variable(s) to dump. */


namespace Twig\Extension {
use Twig\TwigFunction;

final class StringLoaderExtension extends AbstractExtension
{
    public function getFunctions(): array
    {
        return [
            new TwigFunction('template_from_string', 'twig_template_from_string', ['needs_environment' => true]),
        ];
    }
}
}

namespace {
use Twig\Environment;
use Twig\TemplateWrapper;

/** * Loads a template from a string. * * {{ include(template_from_string("Hello {{ name }}")) }} * * @param string $template A template as a string or object implementing __toString() * @param string $name An optional name of the template to be used in error messages */

  public function getNodeVisitors(): array {
    return [new ComponentNodeVisitor($this->pluginManager)];
  }

  /** * {@inheritdoc} */
  public function getFunctions(): array {
    return [
      new TwigFunction(
        'sdc_additional_context',
        [$this, 'addAdditionalContext'],
        ['needs_context' => TRUE]
      ),
      new TwigFunction(
        'sdc_validate_props',
        [$this, 'validateProps'],
        ['needs_context' => TRUE]
      ),
    ];
  }

  
// dump is safe if var_dump is overridden by xdebug         $isDumpOutputHtmlSafe = \extension_loaded('xdebug')
            // false means that it was not set (and the default is on) or it explicitly enabled             && (false === ini_get('xdebug.overload_var_dump') || ini_get('xdebug.overload_var_dump'))
            // false means that it was not set (and the default is on) or it explicitly enabled             // xdebug.overload_var_dump produces HTML only when html_errors is also enabled             && (false === ini_get('html_errors') || ini_get('html_errors'))
            || 'cli' === \PHP_SAPI
        ;

        return [
            new TwigFunction('dump', 'twig_var_dump', ['is_safe' => $isDumpOutputHtmlSafe ? ['html'] : [], 'needs_context' => true, 'needs_environment' => true, 'is_variadic' => true]),
        ];
    }
}
}

namespace {
use Twig\Environment;
use Twig\Template;
use Twig\TemplateWrapper;

function twig_var_dump(Environment $env$context, ...$vars)
{
new TwigFilter('last', 'twig_last', ['needs_environment' => true]),

            // iteration and runtime             new TwigFilter('default', '_twig_default_filter', ['node_class' => DefaultFilter::class]),
            new TwigFilter('keys', 'twig_get_array_keys_filter'),
        ];
    }

    public function getFunctions(): array
    {
        return [
            new TwigFunction('max', 'max'),
            new TwigFunction('min', 'min'),
            new TwigFunction('range', 'range'),
            new TwigFunction('constant', 'twig_constant'),
            new TwigFunction('cycle', 'twig_cycle'),
            new TwigFunction('random', 'twig_random', ['needs_environment' => true]),
            new TwigFunction('date', 'twig_date_converter', ['needs_environment' => true]),
            new TwigFunction('include', 'twig_include', ['needs_environment' => true, 'needs_context' => true, 'is_safe' => ['all']]),
            new TwigFunction('source', 'twig_source', ['needs_environment' => true, 'is_safe' => ['all']]),
        ];
    }

    
Home | Imprint | This part of the site doesn't use cookies.