0

I'm using Bootstrap theme on Drupal 8. How can I print the default search block form in page.html.twig?

Nov 22,17 @9am Update my question for @No Sssweat and @Ramdas Gaikar

I have tired this before my post. I’m not sure is it because the bootstrap theme. I do receive an error message when using this method

"The website encountered an unexpected error. Please try again later."

In MYTHEME.theme

function MYTHEMENAME_preprocess_page(&$variables) {

    $block = \Drupal\block\Entity\Block::load('search-block-form');
    $block_content = \Drupal::entityTypeManager()
      ->getViewBuilder('block')
      ->view($block);
    $variables['CUSTOMENAME'] = $block_content;
}

In page.html.twig

{{ CUSTOMENAME }}

Update error message @ 10:30am in Nov 22

Shortened error:

Recoverable fatal error: Argument 1 passed to Drupal\block\BlockViewBuilder::view() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /Users/useraccount/Sites/devdesktop/test-drupal-8/themes/testtheme/testtheme.theme on line 23

Full message with stack trace:

Recoverable fatal error: Argument 1 passed to Drupal\block\BlockViewBuilder::view() must implement interface Drupal\Core\Entity\EntityInterface, null given, called in /Users/useraccount/Sites/devdesktop/test-drupal-8/themes/testtheme/testtheme.theme on line 23 and defined in Drupal\block\BlockViewBuilder->view() (line 70 of /Users/useraccount/Sites/devdesktop/test-drupal-8/core/modules/block/src/BlockViewBuilder.php) #0 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/includes/bootstrap.inc(566): _drupal_error_handler_real(4096, 'Argument 1 pass...', '/Users/useraccount/Si...', 70, Array) #1 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/modules/block/src/BlockViewBuilder.php(70): _drupal_error_handler(4096, 'Argument 1 pass...', '/Users/useraccount/Si...', 70, Array) #2 /Users/useraccount/Sites/devdesktop/test-drupal-8/themes/testtheme/testtheme.theme(23): Drupal\block\BlockViewBuilder->view(NULL) #3 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/Theme/ThemeManager.php(287): testtheme_preprocess_page(Array, 'page', Array) #4 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/Render/Renderer.php(437): Drupal\Core\Theme\ThemeManager->render('page', Array) #5 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/Render/Renderer.php(195): Drupal\Core\Render\Renderer->doRender(Array, false) #6 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/Template/TwigExtension.php(490): Drupal\Core\Render\Renderer->render(Array) #7 /Users/useraccount/Sites/devdesktop/test-drupal-8/sites/default/files/php/twig/5a0f2c582d265_html.html.twig_1tSCfq69xi0uPcf7-7MUaQqQN/xi3VbzQaML3prijWzrpd_WRrzwIGH4jFi_SeWD3krj8.php(108): Drupal\Core\Template\TwigExtension->escapeFilter(Object(Drupal\Core\Template\TwigEnvironment), Array, 'html', NULL, true) #8 /Users/useraccount/Sites/devdesktop/test-drupal-8/vendor/twig/twig/lib/Twig/Template.php(432): __TwigTemplate_37eb0decd5b0d9ff7b62920677f1e56389764cee16a2b3161919a7109301c109->doDisplay(Array, Array) #9 /Users/useraccount/Sites/devdesktop/test-drupal-8/vendor/twig/twig/lib/Twig/Template.php(403): Twig_Template->displayWithErrorHandling(Array, Array) #10 /Users/useraccount/Sites/devdesktop/test-drupal-8/vendor/twig/twig/lib/Twig/Template.php(411): Twig_Template->display(Array) #11 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/themes/engines/twig/twig.engine(64): Twig_Template->render(Array) #12 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/Theme/ThemeManager.php(384): twig_render_template('themes/testtheme/tem...', Array) #13 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/Render/Renderer.php(437): Drupal\Core\Theme\ThemeManager->render('html', Array) #14 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/Render/Renderer.php(195): Drupal\Core\Render\Renderer->doRender(Array, false) #15 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(147): Drupal\Core\Render\Renderer->render(Array) #16 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/Render/Renderer.php(576): Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() #17 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/Render/MainContent/HtmlRenderer.php(148): Drupal\Core\Render\Renderer->executeInRenderContext(Object(Drupal\Core\Render\RenderContext), Object(Closure)) #18 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/EventSubscriber/MainContentViewSubscriber.php(90): Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object(Symfony\Component\HttpFoundation\Request), Object(Drupal\Core\Routing\CurrentRouteMatch)) #19 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Component/EventDispatcher/ContainerAwareEventDispatcher.php(108): Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent), 'kernel.view', Object(Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher)) #20 /Users/useraccount/Sites/devdesktop/test-drupal-8/vendor/symfony/http-kernel/HttpKernel.php(158): Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object(Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent)) #21 /Users/useraccount/Sites/devdesktop/test-drupal-8/vendor/symfony/http-kernel/HttpKernel.php(68): Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object(Symfony\Component\HttpFoundation\Request), 1) #22 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/StackMiddleware/Session.php(57): Symfony\Component\HttpKernel\HttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #23 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/StackMiddleware/KernelPreHandle.php(47): Drupal\Core\StackMiddleware\Session->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #24 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/modules/page_cache/src/StackMiddleware/PageCache.php(99): Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #25 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/modules/page_cache/src/StackMiddleware/PageCache.php(78): Drupal\page_cache\StackMiddleware\PageCache->pass(Object(Symfony\Component\HttpFoundation\Request), 1, true) #26 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/StackMiddleware/ReverseProxyMiddleware.php(47): Drupal\page_cache\StackMiddleware\PageCache->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #27 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/StackMiddleware/NegotiationMiddleware.php(50): Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #28 /Users/useraccount/Sites/devdesktop/test-drupal-8/vendor/stack/builder/src/Stack/StackedHttpKernel.php(23): Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #29 /Users/useraccount/Sites/devdesktop/test-drupal-8/core/lib/Drupal/Core/DrupalKernel.php(657): Stack\StackedHttpKernel->handle(Object(Symfony\Component\HttpFoundation\Request), 1, true) #30 /Users/useraccount/Sites/devdesktop/test-drupal-8/index.php(19): Drupal\Core\DrupalKernel->handle(Object(Symfony\Component\HttpFoundation\Request)) #31 {main}.
  • I have tried that, it does not work on search form block. – CocoSkin Nov 21 '17 at 23:26
  • 1
    Provide the actual error you are getting from the logs so people can assist. – Kevin Nov 22 '17 at 15:12
  • "The website encountered an unexpected error. Please try again later." is just a plain message for a 500 error. You need to put your PHP into "development" mode (display_errors = On) to get a detailed output of the error (which file, which line). Or just copy and replace your normal php.ini with the one in /usr/lib/php/7.1/php.ini-development or 7.0 if you're not on Drupal 8.4 (And using ubuntu). Otherwise we're all just playing a guessing game here. Or as @Kevin mentions, also you can just paste the log output here. – Beebee Nov 22 '17 at 15:13
  • I just added the log message, its super long – CocoSkin Nov 22 '17 at 15:35
  • 1
    @Beebee I found the issue. In order to print "searchform", the "Search form" block must place in "Block layout". I place the search form block in a region and leave it disable. – CocoSkin Dec 7 '17 at 15:04
0

To render contributor module block's

function theme_preprocess_page(&$variables) {
  $block = \Drupal\block\Entity\Block::load('search-block-form');
  $block_content = \Drupal::entityTypeManager()
    ->getViewBuilder('block')
    ->view($block);
  $variables['youblockname'] = $block_content;
}

and in your theme file just use

{{ youblockname }}

and rebuild the cache.

Similar to this question

| improve this answer | |
  • I have tired this before my post. I’m not sure is it because the bootstrap theme. I didnt get the search form block but i do receive an error message "The website encountered an unexpected error. Please try again later." In MYTHEME.theme function MYTHEMENAME_preprocess_page(&$variables) { $block = \Drupal\block\Entity\Block::load('search-block-form'); $block_content = \Drupal::entityTypeManager() ->getViewBuilder('block') ->view($block); $variables[‘CUSTOMENAME’] = $block_content; } In page.html.twig {{ CUSTOMENAME }} – CocoSkin Nov 22 '17 at 13:35
  • You need to find the search block id from admin user interface. Maybe it has got changed. – Ramdas Gaikar Nov 23 '17 at 4:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.