Hello,

On a view with a result summary, the result summary is not displayed when there is no result. Even if the checkbox "Display even if view has no result" is checked.

Steps to reproduce :

  1. Make a view with a result summary in the view header.
  2. Check "Display even if view has no result" on the result summary.
  3. Add an exposed filter to get no result in your view.
  4. When there is no result, the summary is not displayed.

I will attach a patch.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Grimreaper created an issue. See original summary.

Grimreaper’s picture

Status: Active » Needs review
FileSize
2.09 KB

Here is the patch.

Thanks for the review.

dawehner’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thank you for working on this issue! The fix itself looks totally fine.

Do you mind writing a test for this bug as well?

+++ b/core/modules/views/src/Plugin/views/area/Result.php
@@ -98,6 +97,10 @@ public function render($empty = FALSE) {
+    // Special treatment if the view is empty.
+    if (empty($total)) {
+      $start = 0;
+    }

Ideally this comment should answer "why" we need this. The code itself already kinda explains that :)

Grimreaper’s picture

Hello,

Thanks for the review.

Ok, I will make a patch rewording the comment.

About the test, no problem, it is an opportunity for me to learn. But I never wrote test for the core so I don't know exactly how to do it.

Do you want an unit, kernel or functional test? (no more simpletest?)

I also have a problem with automated tests. I tried to follow the documentation https://www.drupal.org/node/2116263, https://www.drupal.org/node/2288559 and an article from commerceguys https://drupalcommerce.org/blog/45322/commerce-2x-unit-kernel-and-functi...

But I can't figure out the step I missed because in PhpStorm when I try to execute the core tests, I have the following error: "PHP Fatal error: Class 'Drupal\Tests\TestSuites\TestSuiteBase' not found in core/tests/Drupal/Tests/TestSuites/TestSuiteBaseTest.php on line 101" but when executing tests in command line, I have no problem.

Thanks for any help.

Grimreaper’s picture

Hello,

I find out a way to add an automated test. I based myself on the AreaMessageTest.

Here are two patches. One adds the test and should fail. The other one adds the tezst and the fix.

Thanks for the review.

Status: Needs review » Needs work

The last submitted patch, 5: drupal-result_summary_not_displayed_ADD_TEST-2782221-5.patch, failed testing.

Grimreaper’s picture

Status: Needs work » Needs review

As expected the patch that only adds the test has failed.

dawehner’s picture

Issue tags: -Needs tests

The tests are looking great. Here is a quick comment about the comment.

  1. +++ b/core/modules/views/src/Plugin/views/area/Result.php
    @@ -98,6 +97,10 @@ public function render($empty = FALSE) {
    +    // If the view is empty, start = 1 instead of 0.
    +    if (empty($total)) {
    +      $start = 0;
    +    }
    

    Is there a reason why this comment doesn't match to the code? Note: it would be even better, if the comment would describe why we need to use a different start.

  2. +++ b/core/modules/views/src/Plugin/views/area/Result.php
    @@ -108,14 +111,14 @@ public function render($empty = FALSE) {
    -    // Send the output.
    -    if (!empty($total)) {
    -      $output .= Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format));
    -    }
         // Return as render array.
    -    return array(
    -      '#markup' => $output,
    -    );
    +    if (!empty($total) || !empty($this->options['empty'])) {
    +      return array(
    +        '#markup' => Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format)),
    +      );
    +    }
    +
    +    return array();
    

    its good to fix that!

  3. +++ b/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php
    @@ -0,0 +1,62 @@
    +    $output = \Drupal::service('renderer')->renderRoot($output);
    +    $this->setRawContent($output);
    +    $this->assertText('start: 0 | end: 0 | total: 0 | label: test_area_result | per page: 0 | current page: 1 | current record count: 0 | page count: 1');
    +  }
    

    Nice!

Grimreaper’s picture

Hello,

Thanks for the review.

About the $start = 0 if the view is empty, I have changed the code a little bit. I don't know if it is more explicit.

I also found that when using a mini pager, if it is not the last page that is displayed, the total count is broken, instead of 54 for example for my manual tests, it displays 4.6116860184274E+18.

I will open another issue for that and try to find from where it is from.

dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Well mini pagers are special :)

alexpott’s picture

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

I think we need to add to the testResultEmpty test case. We need to test when the view has the empty option set to FALSE - to check nothing displays.

Grimreaper’s picture

dawehner’s picture

@Grimreaper do you mind posting an interdiff for it?

Grimreaper’s picture

Sorry, I don't have the habit of making interdiff.

Here it is.

Grimreaper’s picture

Issue tags: -Needs tests

Removing the "Needs tests" tag as there is a patch with the automated test.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Grimreaper’s picture

Hello,

I will be at the Drupal Developer Days 2017 in Seville.

Any chance to meet a core commiter there and get it reviewed and merged (after rerolling the patch)?

dawehner’s picture

@Grimreaper
The problem is not a missing core committer, its about a missing reviewer

+++ b/core/modules/views/src/Plugin/views/area/Result.php
@@ -83,9 +82,11 @@ public function render($empty = FALSE) {
+    // Offset for start calculation.
+    $start_offset = empty($total) ? 0 : 1;

It would be nice to explain why this line is needed. The current documentation is about what is done.

Lendude’s picture

Status: Needs review » Needs work

Needs a reroll for #2776975: March 3, 2017: Convert core to array syntax coding standards for Drupal 8.3.x RC phase

some other things:

  1. +++ b/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php
    @@ -0,0 +1,86 @@
    +  /**
    +   * Views used by this test.
    +   *
    +   * @var array
    +   */
    

    This can just be an {@inheritdoc}

  2. +++ b/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php
    @@ -0,0 +1,86 @@
    +  /**
    +   * Tests the messages area handler.
    +   */
    

    Copy/paste leftover, needs to be updated to the current use case.

  3. +++ b/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php
    @@ -0,0 +1,86 @@
    +  /**
    +   * Tests the messages area handler.
    +   */
    

    Copy/paste leftover, needs an update.

  4. +++ b/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php
    @@ -0,0 +1,86 @@
    +    // Add a filtering to have no result.
    

    'Add a filter that will make the result set empty.' Something like that?

  5. +++ b/core/modules/views/tests/src/Kernel/Handler/AreaResultTest.php
    @@ -0,0 +1,86 @@
    +    $view->setDisplay('page_1');
    +
    +    // Add a filtering to have no result.
    +    $view->displayHandlers->get('page_1')->overrideOption('filters', array(
    +      'name' => array(
    +        'id' => 'name',
    +        'table' => 'views_test_data',
    +        'field' => 'name',
    +        'relationship' => 'none',
    +        'operator' => '=',
    +        'value' => 'non-existing-name',
    +      ),
    +    ));
    

    Since the page_1 display doesn't have overridden filters, adding this again shouldn't be necessary right? It should already have this filter from adding it to the default display?

Grimreaper’s picture

Assigned: Unassigned » Grimreaper

Hello,

@dawehner and @Lendude, thanks for the review and reply.

I will update my patch next week.

Grimreaper’s picture

Status: Needs work » Needs review
Issue tags: +DevDaysSeville
FileSize
8.56 KB

Hello,

I have rerolled the patch and applied your comment review.

Sorry, I haven't made the interdiff, the previous patch did not apply anymore. I changed the comment that @davehner pointed at and the points 1, 2, 3, 4 pointed by @Lendude.

@Lendude, about point 5, the default display and the page_1 display have a small difference.

On the default display, the result header is always displayed "empty: true" whereas on the page_1 display is it displayed only if there is a result "empty: false".

Thanks for the review.

Grimreaper’s picture

Thanks @Lendude for the live review.

Here is an update patch that remove the filter override as you suggested and I also remembered to convert array in short array syntax.

Haza’s picture

I can't reproduce the issue on the 8.4.x branch. Does this have been fixed in any way with 8.4 ?

Lendude’s picture

Status: Needs review » Reviewed & tested by the community

I also remembered to convert array in short array syntax.

Good catch! This looks good now.

Manually tested this in 8.4.x again and still fails to show the summary for me without the patch applied. Don't know what's different for @Haza

Haza’s picture

Ok, got it. I added the wrong "field" type for the header (I added "Global : Text area")

Well, at least it made clear that the issue is very specific to the "issue summary" field.

Manually tested the patch with 8.x and using the correct field (this time), I can confirm it solves the issue.

Grimreaper’s picture

Assigned: Grimreaper » Unassigned
Lendude’s picture

Overlaps with #2569381: Drupal\views\Plugin\views\area\Result does an unnecessary XSS::adminFilter() , if that lands first we need to combine the tests from that with the tests added here.

iMiksu’s picture

Issue tags: +Needs screenshots

Can we have some screenshots about this fix for making it easier for core committer to review?

Grimreaper’s picture

Assigned: Unassigned » Grimreaper

I am making the screenshots.

Grimreaper’s picture

The important screenshot is:

Now the result area, that is configured to be displayed even if there is no result, is actually displayed and with the right values for the replacement.

Grimreaper’s picture

Assigned: Grimreaper » Unassigned
mavillalba’s picture

Assigned: Unassigned » mavillalba
mavillalba’s picture

Assigned: mavillalba » Unassigned
alexpott’s picture

Status: Reviewed & tested by the community » Needs work

Nice, fix, tests and screenshots everyone. Just one minor point around BC...

+++ b/core/modules/views/src/Plugin/views/area/Result.php
@@ -74,7 +74,6 @@ public function render($empty = FALSE) {
-    $output = '';

@@ -108,14 +111,14 @@ public function render($empty = FALSE) {
-    // Send the output.
-    if (!empty($total)) {
-      $output .= Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format));
-    }
     // Return as render array.
-    return [
-      '#markup' => $output,
-    ];
+    if (!empty($total) || !empty($this->options['empty'])) {
+      return [
+        '#markup' => Xss::filterAdmin(str_replace(array_keys($replacements), array_values($replacements), $format)),
+      ];
+    }
+
+    return [];

Whilst adding || !empty($this->options['empty'] is required for the fix. I'm not sure that we should change the return for empty result from ['#markup' => ''] to []. As far as I can see it's not necessary for the fix and potentially could introduce PHP notices/errors in custom code where people are relying on #markup being set even when there is nothing to render.

Grimreaper’s picture

Status: Needs work » Needs review
FileSize
7.55 KB
1.31 KB

Hello,

Here is reworked patch to take @alexpott suggestions into account.

Thanks for the review.

Lendude’s picture

Status: Needs review » Reviewed & tested by the community

@alexpotts feedback has been addressed, still looks good, tests are still green.

alexpott’s picture

Status: Reviewed & tested by the community » Patch (to be ported)

Committed 2be2910 and pushed to 8.4.x. Thanks!

As we're in the run up to 8.3.0 setting to patch to be ported for commit to 8.3.1 as I think this is BC safe.

  • alexpott committed 2be2910 on 8.4.x
    Issue #2782221 by Grimreaper, dawehner, Lendude, alexpott: Result...
alexpott’s picture

Status: Patch (to be ported) » Fixed

Committed b32e30b and pushed to 8.3.x. Thanks!

  • alexpott committed b32e30b on 8.3.x
    Issue #2782221 by Grimreaper, dawehner, Lendude, alexpott: Result...
Grimreaper’s picture

Thanks :)

Status: Fixed » Closed (fixed)

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