Update documentation for drupal_view_result

Created on 18 November 2023, 12 months ago
Updated 19 January 2024, 10 months ago

Problem/Motivation

I am getting this Error when I try to use "drupal_view_result('frontpage)", in one of my twig files.
User error: "1" is an invalid render array key in Drupal\Core\Render\Element::children()

I am not sure If I am using this correctly, I want to be get the total number of the view results as a number to display in my navbar.

Steps to reproduce

Using Drupal 10.1.6,
Twig tweak: 3.2.1,

1 - Go to any twig tempalte
2 - use the drupal_view_result() to display all count of a view.

🐛 Bug report
Status

Fixed

Version

3.0

Component

Documentation

Created by

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

Comments & Activities

  • Issue created by @KhaledAmer
  • 🇷🇺Russia Chi

    I want to be get the total number of the view results

    Then you need to calculate the results.

    Check out Twig length filter.

  • 🇩🇪Germany sunlix Wesel

    I have the same question. :)
    The Twig-Function use the Drupal callable views_get_view_result()
    I have attached a Screenshot with the Details from xdebug.

    It cant render correctly by Twig. Do you have any advice?

    The error is for "0" and "1" the same (In my case there a two view rows)

    User error: "0" is an invalid render array key in Drupal\Core\Render\Element::children() (line 98 of core\lib\Drupal\Core\Render\Element.php)

    The Result of Element::children(views_get_view_result('view_name', 'view_display')) is empty.

  • 🇷🇺Russia Chi

    It cant render correctly by Twig.

    What do you expect to be printed? The function returns array of objects and has nothing printable. It's only useful to check if a view has results or not.

  • 🇩🇪Germany sunlix Wesel

    Yeah you are right. I did not know the function before I saw it here in Twig Tweak.
    I think my expectation was mislead by the documentation, where only {{ drupal_view_result('who_s_new', 'block_1') }} was written down without any further clarification.
    So sorry for that. Now it is more clear to me.

  • 🇷🇺Russia Chi

    Agreed. The documentation is definitely needs to be updated.

  • Ah, same as Sunlix, based on the documentation I was expecting the total result of the page.

    Thank alot for the help Chi

  • A small update about this, I managed to get the total of my views using the following code in my twig template

    {{ drupal_view_result('frontpage')|length }}

    The reason I want to be able to render it like this is because I want the views count to be visible in my Navbar menu and not in page content.

    • Chi committed 271e723d on 3.x
      Issue #3402615: Update documentation for drupal_view_result
      
  • Status changed to Fixed 10 months ago
  • 🇷🇺Russia Chi

    Thank you.

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

Production build 0.71.5 2024