I noticed some changes to core/modules/block_content/src/Plugin/Derivative/BlockContent.php in Drupal core 10.3.2. As a result, I had to update an existing patch. Without this patch, I continue to encounter the following error:
Error: Call to a member function label() on null in Drupal\block_content\Plugin\Derivative\BlockContent->getDerivativeDefinitions() (line 52 of core/modules/block_content/src/Plugin/Derivative/BlockContent.php).
Could someone assist with submitting my changes to Drupal core? Any guidance on how to proceed would be greatly appreciated.
bigbabert β credited mario.elias β .
I have created and attached a patch that addresses the null reference error in BlockContent.php. I have thoroughly tested it in my environment, and it resolves the issue effectively.
Please find the patch attached for review. I welcome any feedback or additional testing from the community to ensure the change meets all necessary criteria and does not introduce any regressions.
Thank you for reviewing this patch.
I would like to propose changes to the BlockContent.php file within the block_content module to handle potential null entity issues that have been causing errors in our environment.
Issue Description:
We have encountered a recurring error when the label() function is called on a null block_content entity. This results in a PHP exception "Call to a member function label() on null."
Proposed Changes:
To address this issue, I've implemented a null check before accessing the block_content entity's methods. Below is the diff of the changes made:
diff --git a/docroot/core/modules/block_content/src/Plugin/Derivative/BlockContent.php b/docroot/core/modules/block_content/src/Plugin/Derivative/BlockContent.php
index 5ba836f1a8..73a99af376 100644
--- a/docroot/core/modules/block_content/src/Plugin/Derivative/BlockContent.php
+++ b/docroot/core/modules/block_content/src/Plugin/Derivative/BlockContent.php
@@ -48,13 +48,19 @@ public function getDerivativeDefinitions($base_plugin_definition) {
$this->derivatives = [];
/** @var \Drupal\block_content\Entity\BlockContent $block_content */
foreach ($block_contents as $block_content) {
- $this->derivatives[$block_content->uuid()] = $base_plugin_definition;
- $this->derivatives[$block_content->uuid()]['admin_label'] = $block_content->label();
- $this->derivatives[$block_content->uuid()]['config_dependencies']['content'] = [
- $block_content->getConfigDependencyName(),
- ];
+ if ($block_content !== null) {
+ $uuid = $block_content->uuid();
+ $this->derivatives[$uuid] = $base_plugin_definition;
+ // Use null coalescing operator to safely attempt to call label()
+ $this->derivatives[$uuid]['admin_label'] = $block_content->label() ?? 'Default Label';
+ $this->derivatives[$uuid]['config_dependencies']['content'] = [
+ $block_content->getConfigDependencyName(),
+ ];
+ } else {
+ // Optionally log or handle the case when block_content is unexpectedly null
+ \Drupal::logger('block_content')->warning('Encountered a null block content entity.');
+ }
}
return parent::getDerivativeDefinitions($base_plugin_definition);
}
-
}
I am looking forward a feedback on this patch. If deemed suitable, I would appreciate guidance on the formal submission process for these changes to the Drupal community.
Thank you for your attention and support.
Mario
I found additional information regarding the error while traced through our server logs.
Error Log Entry:
WARNING: [pool www] child 7000 said into stderr: "NOTICE: PHP message: Uncaught PHP Exception Error: "Call to a member function label() on null" at /var/www/html/docroot/core/modules/block_content/src/Plugin/Derivative/BlockContent.php line 52"
2024/05/08 15:42:46 [error] 2069#2069: *227 FastCGI sent in stderr: "PHP message: Uncaught PHP Exception Error: "Call to a member function label() on null" at /var/www/html/docroot/core/modules/block_content/src/Plugin/Derivative/BlockContent.php line 52" while reading response header from upstream, client: 172.18.0.5, server: , request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm.sock:", host: "www.mydomain.com"
Code Location:
The error is consistently pointing to line 52 in /var/www/html/docroot/core/modules/block_content/src/Plugin/Derivative/BlockContent.php, where there seems to be a null object reference issue when attempting to access the label() function.
1 <?php
2
3 namespace Drupal\block_content\Plugin\Derivative;
4
5 use Drupal\Component\Plugin\Derivative\DeriverBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11 * Retrieves block plugin definitions for all content blocks.
12 */
13 class BlockContent extends DeriverBase implements ContainerDeriverInterface {
14
15 /**
16 * The content block storage.
17 *
18 * @var \Drupal\Core\Entity\EntityStorageInterface
19 */
20 protected $blockContentStorage;
21
22 /**
23 * Constructs a BlockContent object.
24 *
25 * @param \Drupal\Core\Entity\EntityStorageInterface $block_content_storage
26 * The content block storage.
27 */
28 public function __construct(EntityStorageInterface $block_content_storage) {
29 $this->blockContentStorage = $block_content_storage;
30 }
31
32 /**
33 * {@inheritdoc}
34 */
35 public static function create(ContainerInterface $container, $base_plugin_id) {
36 $entity_type_manager = $container->get('entity_type.manager');
37 return new static(
38 $entity_type_manager->getStorage('block_content')
39 );
40 }
41
42 /**
43 * {@inheritdoc}
44 */
45 public function getDerivativeDefinitions($base_plugin_definition) {
46 $block_contents = $this->blockContentStorage->loadByProperties(['reusable' => TRUE]);
47 // Reset the discovered definitions.
48 $this->derivatives = [];
49 /** @var \Drupal\block_content\Entity\BlockContent $block_content */
50 foreach ($block_contents as $block_content) {
51 $this->derivatives[$block_content->uuid()] = $base_plugin_definition;
52 $this->derivatives[$block_content->uuid()]['admin_label'] = $block_content->label() ?? ($block_content->type->entity->label() . ': ' . $block_cont 52 ent->id());
53 $this->derivatives[$block_content->uuid()]['config_dependencies']['content'] = [
54 $block_content->getConfigDependencyName(),
55 ];
56 }
57 return parent::getDerivativeDefinitions($base_plugin_definition);
58 }
59
60 }
I am adding complete error message here, including line numbers. Thank you @cilefen for your feedback.
The website encountered an unexpected error. Try again later.
Error: Call to a member function label() on null in Drupal\block_content\Plugin\Derivative\BlockContent->getDerivativeDefinitions() (line 52 of core/modules/block_content/src/Plugin/Derivative/BlockContent.php).
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDerivatives(Array) (Line: 87)
Drupal\Component\Plugin\Discovery\DerivativeDiscoveryDecorator->getDefinitions() (Line: 323)
Drupal\Core\Plugin\DefaultPluginManager->findDefinitions() (Line: 205)
Drupal\Core\Plugin\DefaultPluginManager->getDefinitions() (Line: 22)
Drupal\Core\Plugin\DefaultPluginManager->getDefinition('block_content:2f516acc-dd9d-44cf-b764-ac9c80821252') (Line: 16)
Drupal\Core\Plugin\Factory\ContainerFactory->createInstance('block_content:2f516acc-dd9d-44cf-b764-ac9c80821252', Array) (Line: 76)
Drupal\Component\Plugin\PluginManagerBase->createInstance('block_content:2f516acc-dd9d-44cf-b764-ac9c80821252', Array) (Line: 62)
Drupal\Core\Plugin\DefaultSingleLazyPluginCollection->initializePlugin('block_content:2f516acc-dd9d-44cf-b764-ac9c80821252') (Line: 57)
Drupal\block\BlockPluginCollection->initializePlugin('block_content:2f516acc-dd9d-44cf-b764-ac9c80821252') (Line: 80)
Drupal\Component\Plugin\LazyPluginCollection->get('block_content:2f516acc-dd9d-44cf-b764-ac9c80821252') (Line: 45)
Drupal\block\BlockPluginCollection->get('block_content:2f516acc-dd9d-44cf-b764-ac9c80821252') (Line: 83)
Drupal\Core\Plugin\DefaultSingleLazyPluginCollection->setConfiguration(Array) (Line: 99)
Drupal\Core\Plugin\DefaultSingleLazyPluginCollection->addInstanceId('block_content:2f516acc-dd9d-44cf-b764-ac9c80821252', Array) (Line: 55)
Drupal\Core\Plugin\DefaultSingleLazyPluginCollection->__construct(Object, 'block_content:2f516acc-dd9d-44cf-b764-ac9c80821252', Array) (Line: 34)
Drupal\block\BlockPluginCollection->__construct(Object, 'block_content:2f516acc-dd9d-44cf-b764-ac9c80821252', Array, 'aboutusmenuheader') (Line: 156)
Drupal\block\Entity\Block->getPluginCollection() (Line: 145)
Drupal\block\Entity\Block->getPlugin() (Line: 118)
Drupal\block\BlockAccessControlHandler->checkAccess(Object, 'view', Object) (Line: 109)
Drupal\Core\Entity\EntityAccessControlHandler->access(Object, 'view', Object, 1) (Line: 329)
Drupal\Core\Entity\EntityBase->access('view', NULL, 1) (Line: 63)
Drupal\block\BlockRepository->getVisibleBlocksPerRegion(Array) (Line: 137)
Drupal\block\Plugin\DisplayVariant\BlockPageVariant->build() (Line: 270)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.view') (Line: 186)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 53)
Asm89\Stack\Cors->handle(Object, 1, 1) (Line: 50)
Drupal\ban\BanMiddleware->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
mario.elias β created an issue.
I try updating TB Mega Menu JavaScript code for triggering dropdowns with click events instead of hover.
I have noticed the file modules/contrib/tb_megamenu/js/plugin.js has a hover Event Listeners for Desktop:
Below appear to be where the original hover functionality for non-touch devices is implemented.
this.navParent.querySelectorAll('.tbm-item').forEach((element) => {
element.addEventListener('mouseenter', (event) => {
if (!_this.isMobile && !_this.hasArrows) {
_this.showMenu(element, _this.mm_timeout);
}
});
element.addEventListener('mouseleave', (event) => {
if (!_this.isMobile && !_this.hasArrows) {
_this.hideMenu(element, _this.mm_timeout);
}
});
});
I tried replacing with Click Event Listeners by replace these mouseenter and mouseleave event listeners with a click event listener. See my change below
this.navParent.querySelectorAll('.tbm-item').forEach((element) => {
element.addEventListener('click', (event) => {
if (!_this.isMobile && !_this.hasArrows) {
if (element.classList.contains('open')) {
_this.hideMenu(element, _this.mm_timeout);
} else {
_this.showMenu(element, _this.mm_timeout);
}
}
});
});
can someone familiar with the code assist.
Can someone guide me on setting up TB Mega Menu to open dropdowns on click instead of hover?
I am drupal/tb_megamenu Versions : * 3.0.0-alpha2
Thank you Mario
mario.elias β created an issue.
i found a documentation to uninstalling Web Page Archive
drush web-page-archive-prepare-uninstall
# or
drush wpa-pu
https://www.drupal.org/docs/8/modules/web-page-archive/uninstalling-web-... β
mario.elias β created an issue.
I'm encountering the same issue with utilizing multiple tags. In version 1.6, we employed tags for all pages and a secondary tag for a select few. This is necessary as we are using two distinct analytics accounts.. Please advise what should do.
mario.elias β created an issue.
mario.elias β created an issue.