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;

Issue fork flag-3323151

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

eduardo morales alberti’s picture

Status: Active » Needs review
eduardo morales alberti’s picture

The hook also is executed on views that do not have flag. Should be changed the conditions to only execute if the view has the flag field or the FlagViewsRelationship?

https://git.drupalcode.org/project/flag/-/blob/8.x-4.x/flag.views_execut...

function flag_views_query_substitutions(ViewExecutable $view) {
  // Allow replacement of current user's session id so we can cache these
  // queries.
  $flag_service = \Drupal::service('flag');
  return [
    '***FLAG_CURRENT_USER_SID***' => $flag_service->getAnonymousSessionId(),
  ];
}

Daniel Korte made their first commit to this issue’s fork.

daniel korte’s picture

@Eduardo Morales Alberti I think so. That is the issue I am having. I added a check to the merge request. Please review, thanks.

hitchshock’s picture

Status: Needs review » Reviewed & tested by the community

RTBC from my side.
It also fixes the search-API error when I try to index a rendered field where the view mode uses a flag field.

eduardo morales alberti’s picture

Any news?

abarrio’s picture

It fixes my problem too. I think is RTBC too.

berdir made their first commit to this issue’s fork.

berdir’s picture

Status: Reviewed & tested by the community » Fixed

Merged.

berdir’s picture

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.