I encountered an error:
Error message
Notice: Undefined index: render element in Drupal\Core\Theme\ThemeManager->render() (line 200 of core/lib/Drupal/Core/Theme/ThemeManager.php).
...
I found the source of the problem, in my THEME.theme
file, I have this hook:
function THEME_preprocess_paragraph__header(&$variables) {
$blockInstance = Block::load('searchblock');
$variables['searchBlock'] = \Drupal::entityManager()
->getViewBuilder('block')
->view($blockInstance);
}
In my twig file, I have this code
{% block paragraph %}
{{ searchBlock }}
{% endblock paragraph %}
Why is it showing Undefined index: render element
?
I tried to put print_r($variables['searchBlock'])
, and this is the output:
Array
(
[#cache] => Array
(
[keys] => Array
(
[0] => entity_view
[1] => block
[2] => searchblock
)
[contexts] => Array
(
[0] => languages:language_interface
)
[tags] => Array
(
[0] => block_view
[1] => config:block.block.searchblock
)
[max-age] => -1
)
[#weight] => -6
[#lazy_builder] => Array
(
[0] => Drupal\block\BlockViewBuilder::lazyBuilder
[1] => Array
(
[0] => searchblock
[1] => full
[2] =>
)
)
)
In the twig file, I tried to put dump(searchBlock)
, and this is the output:
array(3) {
["#cache"]=>
array(4) {
["keys"]=>
array(3) {
[0]=>
string(11) "entity_view"
[1]=>
string(5) "block"
[2]=>
string(11) "searchblock"
}
["contexts"]=>
array(1) {
[0]=>
string(28) "languages:language_interface"
}
["tags"]=>
array(2) {
[0]=>
string(10) "block_view"
[1]=>
string(30) "config:block.block.searchblock"
}
["max-age"]=>
int(-1)
}
["#weight"]=>
int(-6)
["#lazy_builder"]=>
array(2) {
[0]=>
string(42) "Drupal\block\BlockViewBuilder::lazyBuilder"
[1]=>
array(3) {
[0]=>
string(11) "searchblock"
[1]=>
string(4) "full"
[2]=>
NULL
}
}
}
Seems that the problem occurs in my custom block SearchBlock
plugin. Here's the content on the SearchBlock.php
:
<?php
namespace Drupal\site_core\Plugin\Block;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a block with search information.
*
* @Block(
* id = "site_search_block",
* admin_label = @Translation("Custom Search Block"),
* )
*/
class SearchBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function build() {
return [
'#theme' => 'site_search_block',
'#attached' => [
'library' => [
'site_core/autocomplete'
]
],
'#cache' => [
'max-age' => 0
]
];
}
/**
* {@inheritdoc}
*/
protected function blockAccess(AccountInterface $account) {
return AccessResult::allowedIfHasPermission($account, 'access content');
}
}
Here's my hook_theme
function:
function site_core_theme() {
return [
'site_search_block' => [
'template' => 'site-search-block',
]
];
}
Here's my site-search-block.html.twig
:
<div class="search-bar">
<select id="source" style="height: 0px; width: 0px; position: absolute; border: none; padding: 0px; margin: 0px;">
<option value="type1">Type1</option>
<option value="type2">Type2</option>
</select>
<div class="term">
<div class="box-input">
<input autocomplete="off" class="search-term ui-autocomplete-input"
data-type1-autocomplete="true" id="searchTerm" name="searchTerm" onkeypress="return searchOnEnter(event)"
placeholder="Search" />
</div>
<div class="box-remove"><button class="btn-remove" onclick="jQuery('#searchTerm').val('');">X</button></div>
<ul class="ui-menu ui-widget ui-widget-content ui-autocomplete ui-front" id="ui-id-1" style="display: none;"
tabindex="0">
</ul>
</div>
<input class="btn-submit" onclick="search()" type="button" value="Go" />
</div>
Can anyone help me with this?
{{ searchBlock }}
with{{ dump(searchBlock) }}
? – Isaiah Nixon Sep 6 '19 at 19:29{{ dump(searchBlock) }}
– aceraven777 Sep 9 '19 at 2:52build()
method of whichever plugin is providing thesearchblock
block. A render array somewhere is expecting arender element
to be provided, and it's not. If you've debugged your issue correctly, that's probably inside thebuild()
method. – Jaypan Sep 9 '19 at 4:46SearchBlock.php
in my question. I don't have much experience in developing drupal custom block. I'm just maintaining a project that was given to me. – aceraven777 Sep 9 '19 at 5:10site_search_block
? I'm not finding it in core, is it part of a custom module you've created? That theme hook is expecting a 'render element', which is not being set in the code you showed. You'll need to set some entity to that field. You can do a search on your code base to find it. – Jaypan Sep 9 '19 at 5:14