Exception in Activity stream notifications for anonymous users

Created on 4 November 2024, 2 months ago

Problem/Motivation

Now I see a PHP Exception in Activity stream notifications for anonymous users when I go by URL - /activities/streams/notifications

Drupal\\Core\\Database\\DatabaseExceptionWrapper: Exception in Activity stream notifications[activity_stream_notifications]: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'node_field_data.entity_id'

Steps to reproduce

1) Create the node
2) Create a notification related to the node (activity)
3) Go to /activities/streams/notifications from anonymous user, and you will see the error

Proposed resolution

The error says: Column not found: 1054 Unknown column 'node_field_data.entity_id' in 'on clause'
If I perform DESCRIBE node_field_data for my local install then I get next:

<code>MariaDB [main]> DESCRIBE node_field_data;
+-------------------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------------------+------------------+------+-----+---------+-------+
| nid | int(10) unsigned | NO | PRI | NULL | |
| vid | int(10) unsigned | NO | MUL | NULL | |
| type | varchar(32) | NO | MUL | NULL | |
| langcode | varchar(12) | NO | PRI | NULL | |
| status | tinyint(4) | NO | MUL | NULL | |
| uid | int(10) unsigned | NO | MUL | NULL | |
| title | varchar(255) | NO | MUL | NULL | |
| created | int(11) | NO | MUL | NULL | |
| changed | int(11) | NO | MUL | NULL | |
| promote | tinyint(4) | NO | MUL | NULL | |
| sticky | tinyint(4) | NO | | NULL | |
| default_langcode | tinyint(4) | NO | | NULL | |
| revision_translation_affected | tinyint(4) | YES | | NULL | |
| content_translation_source | varchar(12) | YES | | NULL | |
| content_translation_outdated | tinyint(4) | YES | | NULL | |
+-------------------------------+------------------+------+-----+---------+-------+
15 rows in set (0.001 sec)

You can see the entity_id column indeed doesn’t exist on this install either, but the table does look exactly like expected.
The field that the query is actually looking for is nid. So the question is why it’s trying to select entity_id and not nid. That’s usually caused by some code not respecting the entity key mapping but hardcoding entity_id. That’s controlled by the following part of the entity annotations

 *   entity_keys = {
 *     "id" = "nid",
 *     "revision" = "vid",
 *     "bundle" = "type",
 *     "label" = "title",
 *     "langcode" = "langcode",
 *     "uuid" = "uuid",
 *     "status" = "status",
 *     "published" = "status",
 *     "uid" = "uid",
 *     "owner" = "uid",
 *   },

The problem in ActivityNotificationVisibilityAccess.php

  $configuration = [
      'left_table' => 'post',
      'left_field' => 'id',
      'table' => 'post__field_visibility',
      'field' => 'entity_id',
      'operator' => '=',
    ];
    /** @var \Drupal\views\Plugin\views\join\JoinPluginBase $join */
    $join = Views::pluginManager('join')->createInstance('standard', $configuration);
    $query->addRelationship('post__field_visibility', $join, 'post__field_visibility');

    if ($account->isAnonymous()) {
      $configuration['table'] = 'node_field_data';
      /** @var \Drupal\views\Plugin\views\join\JoinPluginBase $join */
      $join = Views::pluginManager('join')->createInstance('standard', $configuration);
      $query->addRelationship('node_field_data', $join, 'node_field_data');
    }

As you can see we try to join to node_field_data table, but field in $configuration is entity_id which not exist in node_field_data table. So, we need to add the new field to $configuration , so I should be:

if ($account->isAnonymous()) {
      $configuration['table'] = 'node_field_data';
      $configuration['field'] = 'nid';
      /** @var \Drupal\views\Plugin\views\join\JoinPluginBase $join */
      $join = Views::pluginManager('join')->createInstance('standard', $configuration);
      $query->addRelationship('node_field_data', $join, 'node_field_data');
    }

So, now we join to table by correct field, and error should be disseapear

🐛 Bug report
Status

Needs work

Version

13.0

Component

Code (back-end)

Created by

🇺🇦Ukraine v.koval

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @v.koval
  • 🇺🇸United States SocialNicheGuru

    After I added the patch, the sql error was gone. But I did get another error.

    The path is defined here:
    /activities/streams/notifications is defined in:

    profiles/contrib/social/modules/custom/activity_creator/activity_creator.routing.yml
    activity_creator.stream.notifications:
      path: '/activities/streams/notifications'
      defaults:
        _controller: '\Drupal\activity_creator\Controller\NotificationsController::getNotificatio
    nListCallback'
      requirements:
        _permission: 'access content'

    I get this error:

    [{"command":"settings","settings":{"pluralDelimiter":"\u0003","suppressDeprecationErrors":true,"ajaxPageState":{"libraries":"eJwdxNENgCAMBcCFEEYytX2JmEJJC-r4Jt7HsTmK-BqkmS56UxhX0h3tgBS2HuhzG0qM01Tg6a54ovznZrIUH4qtGno","theme":"socialblue","theme_token":null},"ajaxTrustedUrl":[],"ckeditor5Premium":{"isMediaInstalled":true},"user":{"uid":0,"permissionsHash":"6e7d24a58633ad8283b1dbe961be324cb0f6fa09e49e347e3324c0403150b8ef"}},"merge":true},{"command":"add_css","data":[{"rel":"stylesheet","media":"all","href":"\/core\/misc\/components\/progress.module.css?so2r88"},{"rel":"stylesheet","media":"all","href":"\/profiles\/contrib\/social\/modules\/social_features\/social_embed\/assets\/css\/consent-placeholder.css?so2r88"},{"rel":"stylesheet","media":"all","href":"\/themes\/contrib\/socialbase\/assets\/css\/ajax.css?so2r88"},{"rel":"stylesheet","media":"all","href":"\/themes\/contrib\/socialbase\/assets\/css\/autocomplete.css?so2r88"}]},{"command":"add_js","selector":"body","data":[{"src":"\/core\/assets\/vendor\/jquery\/jquery.min.js?v=3.7.1"},{"src":"\/core\/misc\/jquery.passive-listener.js?v=3.7.1"},{"src":"\/core\/assets\/vendor\/once\/once.min.js?v=1.0.1"},{"src":"\/core\/misc\/drupalSettingsLoader.js?v=10.3.10"},{"src":"\/core\/misc\/drupal.js?v=10.3.10"},{"src":"\/core\/misc\/drupal.init.js?v=10.3.10"},{"src":"\/core\/misc\/debounce.js?v=10.3.10"},{"src":"\/core\/assets\/vendor\/tabbable\/index.umd.min.js?v=6.2.0"},{"src":"\/core\/misc\/jquery.form.js?v=4.3.0"},{"src":"\/core\/misc\/progress.js?v=10.3.10"},{"src":"\/themes\/contrib\/bootstrap\/js\/misc\/progress.js?so2r88"},{"src":"\/core\/assets\/vendor\/loadjs\/loadjs.min.js?v=4.3.0"},{"src":"\/core\/misc\/announce.js?v=10.3.10"},{"src":"\/core\/misc\/message.js?v=10.3.10"},{"src":"\/themes\/contrib\/bootstrap\/js\/misc\/message.js?so2r88"},{"src":"\/core\/misc\/ajax.js?v=10.3.10"},{"src":"\/themes\/contrib\/bootstrap\/js\/misc\/ajax.js?so2r88"}]},{"command":"insert","method":"html","selector":".js-notification-center-wrapper","data":"\n\n\n\u003Cdiv class=\u0022<strong>view view-activity-stream-notifications view-id-activity_stream_notifications view-display-id-block_1\u0022\u003E\n \n \n \n\n \n \n \n \n \u003Cdiv class=\u0022small card__block\u0022 role=\u0022alert\u0022\u003E\n \u003Ch4 class=\u0022sr-only\u0022\u003EInformative message\u003C\/h4\u003E\n \u003Cp\u003EYou do not have any notifications</strong>.\u003C\/p\u003E\n\n \u003C\/div\u003E\n \n \n\n \n \n\n \n \u003C\/div\u003E\n","settings":null},{"command":"insert","method":"html","selector":".notification-bell .badge","data":0,"settings":null}]

Production build 0.71.5 2024