Browse Source

Merge branch '3380764-drupalblock-would-be' into '3.x'

Issue #3380764: drupal_block would be easier to use just with the id

See merge request project/twig_tweak!37
merge-requests/37/merge
Juan Olalla 2 years ago
parent
commit
dd4a9bb333
  1. 13
      src/TwigTweakExtension.php

13
src/TwigTweakExtension.php

@ -66,6 +66,7 @@ class TwigTweakExtension extends AbstractExtension {
new TwigFunction('drupal_view', 'views_embed_view'), new TwigFunction('drupal_view', 'views_embed_view'),
new TwigFunction('drupal_view_result', 'views_get_view_result'), new TwigFunction('drupal_view_result', 'views_get_view_result'),
new TwigFunction('drupal_block', [self::class, 'drupalBlock']), new TwigFunction('drupal_block', [self::class, 'drupalBlock']),
new TwigFunction('drupal_block_by_id', [self::class, 'drupalBlockById']),
new TwigFunction('drupal_region', [self::class, 'drupalRegion']), new TwigFunction('drupal_region', [self::class, 'drupalRegion']),
new TwigFunction('drupal_entity', [self::class, 'drupalEntity']), new TwigFunction('drupal_entity', [self::class, 'drupalEntity']),
new TwigFunction('drupal_entity_form', [self::class, 'drupalEntityForm']), new TwigFunction('drupal_entity_form', [self::class, 'drupalEntityForm']),
@ -143,12 +144,22 @@ class TwigTweakExtension extends AbstractExtension {
} }
/** /**
* Builds the render array for a block. * Builds the render array for a block identified by plugin.
*/ */
public static function drupalBlock(string $id, array $configuration = [], bool $wrapper = TRUE): array { public static function drupalBlock(string $id, array $configuration = [], bool $wrapper = TRUE): array {
return \Drupal::service('twig_tweak.block_view_builder')->build($id, $configuration, $wrapper); return \Drupal::service('twig_tweak.block_view_builder')->build($id, $configuration, $wrapper);
} }
/**
* Builds the render array for the block identified by id.
*/
public static function drupalBlockById($id, array $configuration = [], bool $wrapper = TRUE) {
$storage = \Drupal::entityTypeManager()->getStorage('block');
$block = $storage->load($id)->toArray();
$configuration = array_merge($block['settings'], $configuration);
return \Drupal::service('twig_tweak.block_view_builder')->build($block['plugin'], $configuration, $wrapper);
}
/** /**
* Builds the render array of a given region. * Builds the render array of a given region.
*/ */

Loading…
Cancel
Save