Error: Trying to access array offset on value of type null in Drupal\civicrm\Plugin\Block\CivicrmBlock->build
Error:
Notice: Trying to access array offset on value of type null in Drupal\civicrm\Plugin\Block\CivicrmBlock->build() (line 57 of modules/contrib/civicrm/src/Plugin/Block/CivicrmBlock.php).
How to recreate
- Add the block Recent Items to a blocks layout. I don't think it matters if it's the front or admin side. And this may be an issue with other blocks too.
- Log in as a user without the
access CiviCRM
permission and visit a page where that block is supposed to appear.
Background
CivicrmBlock::build()
has this line $content = \CRM_Core_Block::getContent($block_id)['content'];
. But getContent()
can return NULL
(despite it claiming it only returns arrays). It'll return NULL if various access checks fail.
What should happen
CivicrmBlock::build()
should first check the result is an array before trying to access an offset.
/**
* {@inheritdoc}
*/
public function build() {
$block_id = $this->getDerivativeId();
$content = \CRM_Core_Block::getContent($block_id);
// Bypass Drupal SafeString escaping by setting output as already escaped.
if (!empty($content['content']) {
return [
'#markup' => Markup::create($content['content']),
'#cache' => ['max-age' => 0],
];
}
return [];
}