Failed to start the session on view with flag content

Created on 22 November 2022, about 2 years ago
Updated 8 September 2024, 3 months ago

Problem/Motivation

We have a view that uses Search_API SOLR with content that uses the flag field, when it is indexed on save an error is triggered.

RuntimeException while trying to render item entity:node/19:en with view mode full for search index default: Failed to start the session because headers have already been sent by "/var/www/html/vendor/symfony/http-foundation/Response.php" at line 384. in Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage->start() (line 152 of /var/www/html/vendor/symfony/http-foundation/Session/Storage/NativeSessionStorage.php).

After debug it I saw that the hook flag_views_query_substitutions is triggered, and the code from FlagService::getAnonymousSessionId triggered the error.
$request->getSession()->get('flag.session_id')

Why is it necessary the query substitution for flag session? I saw that is used on the plugin https://git.drupalcode.org/project/flag/-/blob/8.x-4.x/src/Plugin/views/... but I do not understand if it is relevant.

$flag_roles = user_roles(FALSE, "flag " . $flag->id());
      if (isset($flag_roles[RoleInterface::ANONYMOUS_ID]) && $this->currentUser->isAnonymous()) {
        // Disable page caching for anonymous users.
        $this->pageCacheKillSwitch->trigger();

        // Add a condition to the join on the PHP session id for anonymous users.
        $this->definition['extra'][] = [
          'field' => 'session_id',
          'value' => '***FLAG_CURRENT_USER_SID***',
        ];
      }

Steps to reproduce

  • Create new content with a flag field.
  • Index it on a search_api view
  • The error will appear on the log

Proposed resolution

Check that the session is already started on method FlagService::getAnonymousSessionId()

    $session_id = $request->hasSession() && $request->getSession()->isStarted()
      ? $request->getSession()->get('flag.session_id')
      : NULL;
πŸ› Bug report
Status

Fixed

Version

4.0

Component

Flag core

Created by

πŸ‡ͺπŸ‡ΈSpain eduardo morales alberti Spain, πŸ‡ͺπŸ‡Ί

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

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024