Problem/Motivation
It looks like the #2993086: Message related object entity type is empty issue didn't fix all the case where the target type of the message related object field might be empty. One of the user on our open social installation couldn't access the website and php logs were reporting
Drupal\Component\Plugin\Exception\PluginNotFoundException: The "" entity type does not exist. in Drupal\Core\Entity\EntityTypeManager->getDefinition() (line 133 of /code/web/core/lib/Drupal/Core/Entity/EntityTypeManager.php).
To fix this issue I went ahead and grep for all occurrences of ->getStorage($target_type) preceded by \Drupal::entityTypeManager() in all the open social distro and made the local patch below, but it felt ugly:
diff --git a/web/profiles/contrib/open_social/modules/custom/activity_logger/activity_logger.tokens.inc b/web/profiles/contrib/open_social/modules/custom/activity_logger/activity_logger.tokens.inc
index 94794a7..8aa6042 100644
--- a/web/profiles/contrib/open_social/modules/custom/activity_logger/activity_logger.tokens.inc
+++ b/web/profiles/contrib/open_social/modules/custom/activity_logger/activity_logger.tokens.inc
@@ -145,7 +145,7 @@ function activity_logger_tokens($type, $tokens, array $data, array $options, Bub
}
}
- if ($name === 'pmt-url') {
+ if ($name === 'pmt-url' && !empty($message->field_message_related_object->target_type)) {
// Get the related message.
$target_type = $message->field_message_related_object->target_type;
$target_id = $message->field_message_related_object->target_id;
diff --git a/web/profiles/contrib/open_social/modules/social_features/social_comment/social_comment.tokens.inc b/web/profiles/contrib/open_social/modules/social_features/social_comment/social_comment.tokens.inc
index d35cab7..133488e 100644
--- a/web/profiles/contrib/open_social/modules/social_features/social_comment/social_comment.tokens.inc
+++ b/web/profiles/contrib/open_social/modules/social_features/social_comment/social_comment.tokens.inc
@@ -59,7 +59,7 @@ function social_comment_tokens($type, $tokens, array $data, array $options, Bubb
case 'parent_entity_author':
- if (isset($message->field_message_related_object)) {
+ if (isset($message->field_message_related_object) && !empty($message->field_message_related_object->target_type)) {
$target_type = $message->field_message_related_object->target_type;
$target_id = $message->field_message_related_object->target_id;
@@ -90,7 +90,7 @@ function social_comment_tokens($type, $tokens, array $data, array $options, Bubb
case 'commented_content_type':
case 'commented_entity_link_html':
- if (isset($message->field_message_related_object)) {
+ if (isset($message->field_message_related_object) && !empty($message->field_message_related_object->target_type)) {
$target_type = $message->field_message_related_object->target_type;
$target_id = $message->field_message_related_object->target_id;
diff --git a/web/profiles/contrib/open_social/modules/social_features/social_group/social_group.tokens.inc b/web/profiles/contrib/open_social/modules/social_features/social_group/social_group.tokens.inc
index 534e79d..e8eeeb6 100644
--- a/web/profiles/contrib/open_social/modules/social_features/social_group/social_group.tokens.inc
+++ b/web/profiles/contrib/open_social/modules/social_features/social_group/social_group.tokens.inc
@@ -61,7 +61,7 @@ function social_group_tokens($type, $tokens, array $data, array $options, Bubble
case 'created_entity_link_html':
// Get the related entity.
- if (isset($message->field_message_related_object)) {
+ if (isset($message->field_message_related_object) && !empty($message->field_message_related_object->target_type)) {
$target_type = $message->field_message_related_object->target_type;
$target_id = $message->field_message_related_object->target_id;
$entity = \Drupal::entityTypeManager()
diff --git a/web/profiles/contrib/open_social/modules/social_features/social_mentions/social_mentions.tokens.inc b/web/profiles/contrib/open_social/modules/social_features/social_mentions/social_mentions.tokens.inc
index 1f6938b..b5f4cf1 100644
--- a/web/profiles/contrib/open_social/modules/social_features/social_mentions/social_mentions.tokens.inc
+++ b/web/profiles/contrib/open_social/modules/social_features/social_mentions/social_mentions.tokens.inc
@@ -84,7 +84,7 @@ function social_mentions_tokens($type, $tokens, array $data, array $options, Bub
case 'mentioned_user':
if ($name === 'mentioned_user') {
- if (isset($message->field_message_related_object)) {
+ if (isset($message->field_message_related_object) && !empty($message->field_message_related_object->target_type)) {
$target_type = $message->field_message_related_object->target_type;
$target_id = $message->field_message_related_object->target_id;
$mention = \Drupal::entityTypeManager()
@@ -104,7 +104,7 @@ function social_mentions_tokens($type, $tokens, array $data, array $options, Bub
case 'commented_entity_link_html':
- if (isset($message->field_message_related_object)) {
+ if (isset($message->field_message_related_object) && !empty($message->field_message_related_object->target_type)) {
$target_type = $message->field_message_related_object->target_type;
$target_id = $message->field_message_related_object->target_id;
/** @var \Drupal\mentions\Entity\Mentions $mention */
Proposed resolution
Avoid the use of entity type manager and use fields entity api to get the entities. Is there a way we can use something like
<?php
if ($message->hasField('field_message_related_object') && !$message->get('field_message_related_object')->isEmpty()) {
$entity = $message->get('field_message_related_object')->entity;
}
?>instead since this field is of type dynamic_entity_reference and reduce the calls to \Drupal::entityTypeManager()?
You will notice that the isEmpty() method at https://git.drupalcode.org/project/dynamic_entity_reference/blob/8.x-1.x... take care of doing the necessary if statement checks.
Remaining tasks
Implement the proposed resolution
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | social-object_entity_type_randomly_empty-3045872-5.patch | 1.15 KB | colan |
Comments
Comment #2
nikathoneFixed some typo.
Comment #3
socialnicheguru commentedcan you create a patch to test. I think I might be coming up against this same issue.
Comment #4
kingdutchHey nikathone,
Thanks for the report! Yes, using the proper field methods to check if it's empty and to load the entity is the right way to go and should improve the code.
I think it may be a good idea here to add a debug level message when the related object field is empty. Perhaps with some other identifying info about the message. That may help us to track down what is causing this scenario to occur in the first place (and perhaps discover a different bug) while also making our code more robust.
Thanks,
~ Kingdutch
Comment #5
colanHere's what I'm getting on a site at https://example.com/fr/notifications:
Here's a patch to get things started. I prefer guard clauses because they prevent
if-style nesting, which is a barrier to clean code.Comment #6
colanForgot to update the branch.
Comment #7
ressinelHi, @colan
Thanks for your work!
I have created PR with your changes: https://github.com/goalgorilla/open_social/pull/2881
Comment #8
tbsiqueiraHi @colan, thank. you so much for your contribution!
We have a slightly different way of contributing for Open Social, if you would like to do it using pull requests instead of patches ;)
Please find at the following link how:
https://www.drupal.org/docs/drupal-distributions/open-social/contribute-...
Have a great day!
Comment #9
colanWill do from now on!
Comment #10
tbsiqueiraComment #11
ressinelThis will be released in Open Social 11.1.6 and above.