Created
May 4, 2014 20:37
-
-
Save stevector/42c8f98a424fc0a0e1ee to your computer and use it in GitHub Desktop.
Directly placing Drupal blocks in a Twig file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{# | |
I suspect Drupal site builders will eventually to be able | |
to put block instances into twig files without being limited | |
to a theme's regions. Calling a function to print them | |
anywhere is farirly simple. | |
#} | |
{{ drupal_block_placement('bartik_breadcrumbs') }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/core/lib/Drupal/Core/Template/TwigExtension.php b/core/lib/Drupal/Core/Template/TwigExtension.php | |
index 4cb31ca..47618a0 100644 | |
--- a/core/lib/Drupal/Core/Template/TwigExtension.php | |
+++ b/core/lib/Drupal/Core/Template/TwigExtension.php | |
@@ -27,6 +27,7 @@ class TwigExtension extends \Twig_Extension { | |
public function getFunctions() { | |
return array( | |
new \Twig_SimpleFunction('url', 'url'), | |
+ new \Twig_SimpleFunction('drupal_block_placement', 'block_get_render_array_by_machine_name'), | |
// This function will receive a renderable array, if an array is detected. | |
new \Twig_SimpleFunction('render_var', 'twig_render_var'), | |
); | |
diff --git a/core/modules/block/block.module b/core/modules/block/block.module | |
index 5190436..abd45f8 100644 | |
--- a/core/modules/block/block.module | |
+++ b/core/modules/block/block.module | |
@@ -449,3 +449,27 @@ function block_language_entity_delete(Language $language) { | |
} | |
} | |
} | |
+ | |
+/** | |
+ * Return the render array for a block instance. | |
+ * | |
+ * The function is used in conjunction with Twig to be able to call in a Twig | |
+ * file something like {{ drupal_block_placement('machine_name_of_block') }} | |
+ * | |
+ * @todo, this code should probably go into a service rather than adding another | |
+ * procedural function. | |
+ * | |
+ * @param string $machine_name | |
+ * The machine name of a block instance like 'poweredbydrupal_2' or | |
+ * 'views_block__content_recent_block_1' | |
+ * @return array | |
+ * A render array from entity_view() | |
+ */ | |
+function block_get_render_array_by_machine_name($block_machine_name) { | |
+ | |
+ $entity = entity_load('block', $block_machine_name); | |
+ $view = entity_view($entity, 'default'); | |
+ | |
+ return $view; | |
+} | |
+ | |
diff --git a/core/themes/bartik/templates/page.html.twig b/core/themes/bartik/templates/page.html.twig | |
index b075295..22bd591 100644 | |
--- a/core/themes/bartik/templates/page.html.twig | |
+++ b/core/themes/bartik/templates/page.html.twig | |
@@ -162,6 +162,13 @@ | |
{{ action_links }} | |
</ul> | |
{% endif %} | |
+ | |
+ {# | |
+ Directly place the breadcrumb block in a twig file. This technique | |
+ skips over placing the block in a region. | |
+ #} | |
+ {{ drupal_block_placement('bartik_breadcrumbs') }} | |
+ | |
{{ page.content }} | |
{{ feed_icons }} | |
</section></main> <!-- /.section, /#content --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment