You are here

public function TwigExtension::getFunctions in Twig Tweak 8.2

Same name and namespace in other branches
  1. 8 src/TwigExtension.php \Drupal\twig_tweak\TwigExtension::getFunctions()

File

src/TwigExtension.php, line 43

Class

TwigExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public function getFunctions() {
  $context_options = [
    'needs_context' => TRUE,
  ];
  $all_options = [
    'needs_environment' => TRUE,
    'needs_context' => TRUE,
  ];
  return [
    // - Drupal View -
    //
    // @code
    //   {{ drupal_view('who_s_new', 'block_1') }}
    // @endcode
    new TwigFunction('drupal_view', 'views_embed_view'),
    // - Drupal View Result -
    //
    // @code
    //   {{ drupal_view('who_s_new', 'block_1') }}
    // @endcode
    new TwigFunction('drupal_view_result', 'views_get_view_result'),
    // - Drupal Block -
    //
    // In order to list all registered plugin IDs fetch them with block plugin
    // manager. With Drush it can be done like follows:
    // @code
    //   drush ev "print_r(array_keys(\Drupal::service('plugin.manager.block')->getDefinitions()));"
    // @endcode
    //
    // @code
    //   {# Print block using default configuration. #}
    //   {{ drupal_block('system_branding_block') }}
    //
    //   {# Print block using custom configuration. #}
    //   {{ drupal_block('system_branding_block', {label: 'Branding', use_site_name: false})
    //
    //   {# Bypass block.html.twig theming. #}
    //   {{ drupal_block('system_branding_block', wrapper=false) }}
    // @endcode
    //
    // @see https://www.drupal.org/node/2964457#block-plugin
    new TwigFunction('drupal_block', [
      $this,
      'drupalBlock',
    ]),
    // - Drupal Region -
    //
    // @code
    //   {# Print 'Sidebar First' region of the default site theme. #}
    //   {{ drupal_region('sidebar_first') }}
    //
    //   {# Print 'Sidebar First' region of Bartik theme. #}
    //   {{ drupal_region('sidebar_first', 'bartik') }}
    // @endcode
    new TwigFunction('drupal_region', [
      $this,
      'drupalRegion',
    ]),
    // - Drupal Entity -
    //
    // @code
    //   {# Print a content block which ID is 1. #}
    //   {{ drupal_entity('block_content', 1) }}
    //
    //   {# Print a node's teaser. #}
    //   {{ drupal_entity('node', 123, 'teaser') }}
    //
    //   {# Print Branding block which was previously disabled on #}
    //   {# admin/structure/block page. #}
    //   {{ drupal_entity('block', 'bartik_branding', check_access=false) }}
    // @endcode
    new TwigFunction('drupal_entity', [
      $this,
      'drupalEntity',
    ]),
    // - Drupal Entity Form -
    //
    // @code
    //   {# Print edit form for node 1. #}
    //   {{ drupal_entity_form('node', 1) }}
    //
    //   {# Print add form for Article content type. #}
    //   {{ drupal_entity_form('node', values={type: 'article'}) }}
    //
    //   {# Print user register form. #}
    //   {{ drupal_entity_form('user', NULL, 'register', check_access=false) }}
    // @endcode
    new TwigFunction('drupal_entity_form', [
      $this,
      'drupalEntityForm',
    ]),
    // - Drupal Field -
    //
    // @code
    //   {{ drupal_field('field_image', 'node', 1) }}
    //   {{ drupal_field('field_image', 'node', 1, 'teaser') }}
    //   {{ drupal_field('field_image', 'node', 1, {type: 'image_url', settings: {image_style: 'large'}}) }}
    // @endcode
    new TwigFunction('drupal_field', [
      $this,
      'drupalField',
    ]),
    // - Drupal Menu -
    //
    // @code
    //   {{ drupal_menu('main') }}
    // @endcode
    new TwigFunction('drupal_menu', [
      $this,
      'drupalMenu',
    ]),
    // - Drupal Form -
    //
    // @code
    //   {{ drupal_form('Drupal\\search\\Form\\SearchBlockForm') }}
    // @endcode
    new TwigFunction('drupal_form', [
      $this,
      'drupalForm',
    ]),
    // - Drupal Image -
    //
    // @code
    //   {# Render image specified by file ID. #}
    //   {{ drupal_image(123) }}
    //
    //   {# Render image specified by file UUID. #}
    //   {{ drupal_image('9bb27144-e6b2-4847-bd24-adcc59613ec0') }}
    //
    //   {# Render image specified by file URI. #}
    //   {{ drupal_image('public://ocean.jpg') }}
    //
    //   {# Render image using 'thumbnail' image style and custom attributes. #}
    //   {{ drupal_image('public://ocean.jpg', 'thumbnail', {alt: 'The alternative text'|t, title: 'The title text'|t}) }}
    //
    //   {# Render responsive image. #}
    //   {{ drupal_image('public://ocean.jpg', 'wide', responsive=true) }}
    // @endcode
    new TwigFunction('drupal_image', [
      $this,
      'drupalImage',
    ]),
    // - Drupal Token -
    //
    // @code
    //   {{ drupal_token('site:name') }}
    // @endcode
    new TwigFunction('drupal_token', [
      $this,
      'drupalToken',
    ]),
    // - Drupal Config -
    //
    // @code
    //   {{ drupal_config('system.site', 'name') }}
    // @endcode
    new TwigFunction('drupal_config', [
      $this,
      'drupalConfig',
    ]),
    // - Drupal Dump -
    //
    // @code
    //   {# Basic usage. #}
    //   {{ drupal_dump(var) }}
    //
    //   {# Same as above but shorter. #}
    //   {{ dd(var) }}
    //
    //   {# Dump all available variables for the current template. #}
    //   {{ dd() }}
    // @endcode
    new TwigFunction('drupal_dump', [
      $this,
      'drupalDump',
    ], $context_options),
    new TwigFunction('dd', [
      $this,
      'drupalDump',
    ], $context_options),
    // - Drupal Title -
    new TwigFunction('drupal_title', [
      $this,
      'drupalTitle',
    ]),
    // - Drupal URL -
    //
    // @code
    //   {# Basic usage. #}
    //   {{ drupal_url('node/1) }}
    //
    //   {# Complex URL. #}
    //   {{ drupal_url('node/1', {query: {foo: 'bar'}, fragment: 'example', absolute: true}) }}
    // @endcode
    new TwigFunction('drupal_url', [
      $this,
      'drupalUrl',
    ]),
    // - Drupal Link -
    //
    // @code
    //   {# It supports the same options as drupal_url(), plus attributes. #}
    //   {{ drupal_link('View'|t, 'node/1', {attributes: {target: '_blank'}}) }}
    //
    //   {# This link will only be shown for privileged users. #}
    //   {{ drupal_link('Example'|t, '/admin', check_access=true) }}
    // @endcode
    new TwigFunction('drupal_link', [
      $this,
      'drupalLink',
    ]),
    // - Drupal Messages -
    new TwigFunction('drupal_messages', [
      $this,
      'drupalMessages',
    ]),
    // - Drupal Breadcrumb -
    new TwigFunction('drupal_breadcrumb', [
      $this,
      'drupalBreadcrumb',
    ]),
    // - Drupal Breakpoint -
    new TwigFunction('drupal_breakpoint', [
      $this,
      'drupalBreakpoint',
    ], $all_options),
    // - Contextual Links -
    //
    // @code
    //   {# Basic usage. #}
    //   <div class="contextual-region">
    //     {{ contextual_links('entity.view.edit_form:view=frontpage&display_id=feed_1') }}
    //     {{ drupal_view('frontpage') }}
    //   </div>
    //   {# Multiple links. #}
    //   <div class="contextual-region">
    //     {{ contextual_links('node:node=123|block_content:block_content=123') }}
    //     {{ content }}
    //   </div>
    // @endcode
    new TwigFunction('contextual_links', [
      $this,
      'contextualLinks',
    ]),
  ];
}