I have a personal flag setup for AUTHENTICATED USER. no problem for authenticated users, but I got the error below when anonymous when they go to the node page. please advise. ( I noticed the problem is only happened when I set the link type to "Count link", if I change to ajax link then the problem gone )

The website encountered an unexpected error. Please try again later.
TypeError: Argument 1 passed to Drupal\flag\TwigExtension\FlagCount::count() must implement interface Drupal\flag\FlagInterface, null given, called in /home/xx/public_html/web/sites/default/files/php/twig/62c74d7e62e5c_flag-count.html.twig_o3BzpAD5mRLvllwl8oGhKAnyG/A5Gd-5PDinRR3LxnxaP8i2DBUcSxPpAS6ewEtqQRXHs.php on line 67 in Drupal\flag\TwigExtension\FlagCount->count() (line 64 of modules/flag/src/TwigExtension/FlagCount.php).

Drupal\flag\TwigExtension\FlagCount->count(NULL, NULL) (Line: 67)
__TwigTemplate_9154b0721101147f4324558e3fceea7b24e25cd62199bb2f377a344aefa8b0d9->doDisplay(Array, Array) (Line: 405)
Twig\Template->displayWithErrorHandling(Array, Array) (Line: 378)
Twig\Template->display(Array) (Line: 390)
Twig\Template->render(Array) (Line: 55)
twig_render_template('modules/flag/modules/flag_count/templates/flag-count.html.twig', Array) (Line: 384)
Drupal\Core\Theme\ThemeManager->render('flag_count', Array) (Line: 422)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 201)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 157)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 158)
Drupal\Core\Render\Renderer->renderPlain(Array) (Line: 172)
Drupal\Core\Render\Renderer->renderPlaceholder('', Array) (Line: 649)
Drupal\Core\Render\Renderer->replacePlaceholders(Array) (Line: 534)
Drupal\Core\Render\Renderer->doRender(Array, 1) (Line: 201)
Drupal\Core\Render\Renderer->render(Array, 1) (Line: 145)
Drupal\Core\Render\Renderer->Drupal\Core\Render\{closure}() (Line: 564)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 146)
Drupal\Core\Render\Renderer->renderRoot(Array) (Line: 279)
Drupal\Core\Render\HtmlResponseAttachmentsProcessor->renderPlaceholders(Object) (Line: 71)
Drupal\big_pipe\Render\BigPipeResponseAttachmentsProcessor->processAttachments(Object) (Line: 45)
Drupal\Core\EventSubscriber\HtmlResponseSubscriber->onRespond(Object, 'kernel.response', Object)
call_user_func(Array, Object, 'kernel.response', Object) (Line: 142)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch(Object, 'kernel.response') (Line: 191)
Symfony\Component\HttpKernel\HttpKernel->filterResponse(Object, Object, 1) (Line: 179)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 80)
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: 191)
Drupal\page_cache\StackMiddleware\PageCache->fetch(Object, 1, 1) (Line: 128)
Drupal\page_cache\StackMiddleware\PageCache->lookup(Object, 1, 1) (Line: 82)
Drupal\page_cache\StackMiddleware\PageCache->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: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 709)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)
nt.php).

Issue fork flag-3294851

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

freelylw created an issue. See original summary.

freelylw’s picture

Issue summary: View changes
rik.scholten’s picture

StatusFileSize
new890 bytes

I added two checks that make sure the params are set before you try to access them. This removes the white page errors for anonymous users.

jaydev bhatt’s picture

@rik.scholten if you have generated this page can we move this to Needs Review?

rik.scholten’s picture

Status: Active » Needs review

You are correct, I forgot to change the status. Thanks!

berdir’s picture

Title: The website encountered an unexpected error » flag-count twig template calls flagcount() whithout checking if there is a flag
Status: Needs review » Needs work

That seems like a hack. the twig template should check if there is a flag, I'd assume this results in displaying empty [].

rik.scholten’s picture

StatusFileSize
new867 bytes

Hi Berdir, you are 100% correct. This patch checks the flag and flaggable in the twig template, if one of these is NULL the twig template will not render the count. I tested it and it seems to get the desired outcome.

Please let me know if this is better, thanks in advance!

rik.scholten’s picture

Status: Needs work » Needs review
StatusFileSize
new865 bytes

Remove one line of whitespace.

berdir’s picture

Status: Needs review » Needs work

Makes sense, but you need to reroll this against 8.x-4.x-dev because I made a conflicting change there for D10 compatibility.

rik.scholten’s picture

Status: Needs work » Needs review
StatusFileSize
new861 bytes

Rerolled patch against 8.x-4.x-dev. Thanks!

B1’s picture

D9.5
PHP 8.1.7
Flag 8.x-4.0-beta3

A flag count of 0 is shown to users.

Apply patch to twig template.
If flag is NULL the twig template successfully hides 0 count to users.

No known errors occur from patch and patch deemed successful in this particular use case.

rik.scholten’s picture

Status: Needs review » Reviewed & tested by the community
tree2009’s picture

RTBC+1

ivnish’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll, +Needs tests

Needs reroll to MR, needs tests how it works

manishsaharan’s picture

Version: 8.x-4.0-beta3 » 5.0.0
Status: Needs work » Needs review
StatusFileSize
new864 bytes

Rerolled patch against 5.x branch

ivnish’s picture

Status: Needs review » Needs work

@manishsaharan needs reroll to MR. Also needs tests to show that the template is not broken after this patch

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

deaom’s picture

Version: 5.0.0 » 5.x-dev
Status: Needs work » Needs review

I think the tests currently present already cover this scenario, they are added in the main module, so setting status to Needs review if additional test are needed, please provide information on what needs to be tested.

  • ivnish committed a560998b on 5.x authored by deaom
    [#3294851] feat: flag-count twig template calls flagcount() whithout...
ivnish’s picture

Status: Needs review » Fixed
Issue tags: -Needs reroll, -Needs tests

Merged! thanks!

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.

Status: Fixed » Closed (fixed)

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