Normally, when one or more of the widgets in the exposed form of a view has errors, Views exits the build process for the view and "empties" it.

However, when such a view with exposed widgets is embedded on a page that has one or more unrelated forms "built" before the view is rendered, the same thing happens again. Views is unable to make out the difference between errors caused by its exposed form and by other forms built on the page before it.

I have a sandbox project that illustrates this issue. On downloading and enabling the project, go to /views-example.

http://drupal.org/sandbox/amarnus/1863012
http://drupalcode.org/sandbox/amarnus/1863012.git

CommentFileSizeAuthor
#76 1863020-76.patch6.44 KBLendude
#76 interdiff-1863020-65-76.txt1.21 KBLendude
#65 interdiff-62-65.txt2.43 KBpaulmckibben
#65 1863020-65.patch6.43 KBpaulmckibben
#62 interdiff-60-62.txt2.05 KBpaulmckibben
#62 1863020-62.patch6.55 KBpaulmckibben
#60 1863020-60-without-tests.patch1.62 KBpaulmckibben
#60 1863020-60.patch4.09 KBpaulmckibben
#53 1863020-52.patch7.1 KBcatch
#53 1863020-52-tests.patch4.93 KBcatch
#52 1863020-tests.patch4.79 KBcatch
#50 1863020-tests.patch7.17 KBcatch
#48 1863020-48-tests.patch4.67 KBcatch
#45 1863020-45.patch761 bytesdaceej
#41 1863020-41.patch8.33 KBrpayanm
#36 interdiff-1863020-35-CORRECT.txt8.22 KBdamiankloip
#35 interdiff-1863020-35.txt6.58 KBdamiankloip
#35 1863020-35.patch8.29 KBdamiankloip
#33 interdiff.txt4.76 KBalansaviolobo
#33 view_s_build_fails_when-1863020-33.patch6.7 KBalansaviolobo
#28 drupal-1863020-28.patch6.8 KBsunset_bill
#27 drupal-1863020.patch6.8 KBsunset_bill
#24 drupal-1863020-24.patch7.02 KBdeveshpal9
#18 drupal-1863020-18.patch7 KBjlbellido
#12 drupal-1863020-12.patch6.8 KBdawehner
#9 drupal-1863020-9.patch6.9 KBdawehner
#7 drupal-1863020-7.patch6.75 KBdawehner
#5 drupal-1863020-4-tests-only.patch6.31 KBdawehner
#5 drupal-1863020-4.patch8.62 KBdawehner
#1 exposed-form-build-fail-1863020-1.patch1.13 KBamarnus
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

amarnus’s picture

This patch seems to fix the issue.

dawehner’s picture

Status: Active » Needs review

Thank you for providing such helpful information and a patch, so set it to needs review.

Do you currently understand why this behavior is happening, because just by thinking about FAPI etc.
I don't really see what could cause this behavior. Your patch seems to fix the symptoms not the real reason, but yeah sometimes you can't choose.

amarnus’s picture

From what I see, there's nothing wrong with the Form API, really. So, Views assumes that if validation fails in a page with a view having an exposed form in it, then it is probably because of an issue with one or more exposed widgets and cancels the build.

Typically, this assumption holds good for a view with the "page" display type because the content region can just house a view and if there were other forms, they would have to be through blocks in sidebars/other regions. In Drupal by default though, the content is built first before other regions - so it doesn't matter.

However, this assumption is no longer valid when:

  1. The view is embedded on a page with other forms rendered before it.

In this case, it is no longer clear where the errors are from. If the static error collector - form_set_error() was keyed by the form ID or something, we could have done a more elegant check; something along the lines of:

  $errors = form_set_error();
  if (!empty($errors['my_form_id'])) {
    // Panic
  }

Unfortunately though, it doesn't work that way. So, we may have to stick to the crude method as used in the patch.

merlinofchaos’s picture

This patch makes sense to me.

dawehner’s picture

Project: Views (for Drupal 7) » Drupal core
Version: 7.x-3.5 » 8.x-dev
Component: Code » views.module
Assigned: amarnus » Unassigned
Issue tags: +VDC
FileSize
8.62 KB
6.31 KB

Yeah this makes sense now! It's interesting that you are the first one who see that problem.
Thanks for the code, committed and pushed to 7.x-3.x

Yet another reason why global state can be problematic.

Here are patches for d8 which also includes tests.

aspilicious’s picture

Status: Needs review » Needs work
+++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/ExposedFormTest.phpundefined
@@ -51,7 +51,7 @@ protected function setUp() {
+  public function _testResetButton() {

Some tests are disabled ;)

dawehner’s picture

Status: Needs work » Needs review
FileSize
6.75 KB

Ups right.

Also rerolled against recent changes.

Status: Needs review » Needs work

The last submitted patch, drupal-1863020-7.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
6.9 KB

This time without fails.

dawehner’s picture

Issue tags: -VDC

#9: drupal-1863020-9.patch queued for re-testing.

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

The last submitted patch, drupal-1863020-9.patch, failed testing.

dawehner’s picture

Status: Needs work » Needs review
FileSize
6.8 KB

Just a rerole.

dawehner’s picture

Issue tags: -VDC

#12: drupal-1863020-12.patch queued for re-testing.

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

The last submitted patch, drupal-1863020-12.patch, failed testing.

dawehner’s picture

Issue tags: +Needs reroll

.

dermario’s picture

Issue summary: View changes

I will try to reroll.

dermario’s picture

Ok i am "unassigning" from that issues. The reroll is to heavy for me cause of the long time.

jlbellido’s picture

Status: Needs work » Needs review
Issue tags: +SprintWeekend 2014, +D8SVQ
FileSize
7 KB

Rerolled #12 and run test.

Status: Needs review » Needs work

The last submitted patch, 18: drupal-1863020-18.patch, failed testing.

The last submitted patch, 18: drupal-1863020-18.patch, failed testing.

YesCT’s picture

Issue tags: -SprintWeekend 2014 +SprintWeekend2014

fixing tag. sorry for noise.

dawehner’s picture

Status: Needs work » Needs review

18: drupal-1863020-18.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 18: drupal-1863020-18.patch, failed testing.

deveshpal9’s picture

Status: Needs work » Needs review
FileSize
7.02 KB

Rerolling patch from #18

Status: Needs review » Needs work

The last submitted patch, 24: drupal-1863020-24.patch, failed testing.

sunset_bill’s picture

Assigned: Unassigned » sunset_bill

working on a reroll

sunset_bill’s picture

FileSize
6.8 KB

Reroll from #24 (my bad on the patch name)

sunset_bill’s picture

FileSize
6.8 KB

Renamed reroll from patch #24

dawehner’s picture

Status: Needs work » Needs review

.

Status: Needs review » Needs work

The last submitted patch, 28: drupal-1863020-28.patch, failed testing.

alansaviolobo’s picture

Status: Needs work » Needs review

28: drupal-1863020-28.patch queued for re-testing.

Status: Needs review » Needs work

The last submitted patch, 28: drupal-1863020-28.patch, failed testing.

alansaviolobo’s picture

Status: Needs work » Needs review
FileSize
6.7 KB
4.76 KB

re-rolling the patch

Status: Needs review » Needs work

The last submitted patch, 33: view_s_build_fails_when-1863020-33.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
8.29 KB
6.58 KB

Thanks for the re-roll, and the reminder this issue existed :)

It needed a bit of fixing to bring it up to date, mainly:

- Added a little more to the test assertions
- Created a FormInterface form that has the form error
- Use the #view element to render it page
- Converted the page to a route and controller class

I also rolled the callbacks etc.. into the views_test_data module instead of a new views_test_form module.

damiankloip’s picture

Sorry - with the correct interdiff.

damiankloip’s picture

Priority: Normal » Major

I also think this is major. This will cause some pretty unexpected behaviour for people.

dawehner queued 35: 1863020-35.patch for re-testing.

Status: Needs review » Needs work

The last submitted patch, 35: 1863020-35.patch, failed testing.

heddn’s picture

Needs a re-roll. Before that though, it might not hurt to validate this is still an issue since it has been a little while since things were worked on here. I'm pretty sure this also needs a backport to D7.

rpayanm’s picture

Status: Needs review » Needs work

The last submitted patch, 41: 1863020-41.patch, failed testing.

Nikolay Shapovalov’s picture

Issue tags: -Needs reroll
mgifford’s picture

Assigned: sunset_bill » Unassigned
daceej’s picture

I also faced this issue, and came up with the following patch.

daceej’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 45: 1863020-45.patch, failed testing.

catch’s picture

Version: 8.0.x-dev » 8.1.x-dev
Status: Needs work » Needs review
FileSize
4.67 KB

First of all re-rolling with just the tests from #48.

Status: Needs review » Needs work

The last submitted patch, 48: 1863020-48-tests.patch, failed testing.

catch’s picture

Status: Needs work » Needs review
FileSize
7.17 KB

Patch was very out of date, slightly better re-roll.

Status: Needs review » Needs work

The last submitted patch, 50: 1863020-tests.patch, failed testing.

catch’s picture

Status: Needs work » Needs review
FileSize
4.79 KB

Test actually runs properly now, but doesn't fail for me either via simpletest or manually testing with the test module.

catch’s picture

OK test should now fail and updated version of that patch that makes that test pass.

Didn't check other views tests though so it could break something else.

The FormState:hasAnyErrors() check in HEAD is completely wrong, and there's no way to get the errors without a proper FormState object, so had to add an optional $form_state parameter to ExposedFormPluginBase::renderForm().

The last submitted patch, 53: 1863020-52-tests.patch, failed testing.

alexpott’s picture

Issue tags: -Triaged core major

My previous comment was about another issue. Sorry for the noise.

alexpott’s picture

Issue tags: +Triaged core major

Discussed with @xjm, @tim.plunkett and @dawehner - we decided that this issue is a major issue which you are able to configure a view through the UI that renders the site unusable.

dawehner’s picture

Status: Needs review » Needs work
+++ b/core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php
@@ -126,10 +126,12 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    */
-  public function renderExposedForm($block = FALSE) {
+  public function renderExposedForm($block = FALSE, FormStateInterface $form_state = NULL) {
+    if (!isset($form_state)) {
+      $form_state = new FormState();
+    }

It would be nice to document why we need this second form_state and how this fixes problems.

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

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

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

paulmckibben’s picture

I filed a duplicate issue, #2841413, and submitted a patch there. Comparing my patch to #52, my patch may be a little more straightforward, as it does not require a second form_state. Instead, it checks for form_state errors in the renderExposedForm() method and sets the "abort" flag there if it encounters an error. In the ViewExecutable build() method, this allows the removal of the global check, FormState::hasAnyErrors.

My conjecture is that FormState::hasAnyErrors is the wrong method to call, because it is a global check for errors on any form on the page. My patch eliminates the use of that method.

I've created a new patch combining the tests from #52 with my patch. I attempted an interdiff, but interdiff wouldn't work, I guess because the differences are too great. Instead, I have also uploaded a version of my patch without the tests, which is pretty simple.

I look forward to everyone's feedback. Thanks!

The last submitted patch, 60: 1863020-60.patch, failed testing.

paulmckibben’s picture

Oops, I didn't roll the patch in #60 correctly: I forgot to add the new files from the tests in #52, which is why the patch failed testing.

Here's a rerolled patch with all the tests.

Status: Needs review » Needs work

The last submitted patch, 62: 1863020-62.patch, failed testing.

Lendude’s picture

@paulmckibben fix looks good to me, makes sense and simplifies things and still passes the tests.

Bit of nitpicking:

  1. +++ b/core/modules/views/src/Plugin/views/exposed_form/ExposedFormPluginBase.php
    @@ -133,6 +133,10 @@ public function renderExposedForm($block = FALSE) {
    +    $errors = $form_state->getErrors();
    +    if (!empty($errors)) {
    

    This could use a comment as to why we are doing it at that point during the execution.

  2. +++ b/core/modules/views/src/Tests/Plugin/ExposedFormTest.php
    @@ -398,4 +400,24 @@ protected function getExpectedExposedFormId(ViewExecutable $view) {
    +
    

    extra white space line can be taken out

  3. +++ b/core/modules/views/tests/modules/views_test_data/src/Controller/ViewsTestDataController.php
    @@ -0,0 +1,32 @@
    +
    +/**
    + * @file
    + * Contains \Drupal\views_test_data\Controller\ViewsTestDataController.
    + */
    

    this can be taken out, no longer needed per coding standards.

  4. +++ b/core/modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataErrorForm.php
    @@ -0,0 +1,52 @@
    +/**
    + * @file
    + * Contains \Drupal\views_test_data\Form\ViewsTestDataErrorForm.
    + */
    

    this can be taken out too.

  5. +++ b/core/modules/views/tests/modules/views_test_data/src/Form/ViewsTestDataErrorForm.php
    @@ -0,0 +1,52 @@
    + * Simple form page callback to test the view element.
    

    Seems like a copy/paste left over, needs to be updated to something relevant to the current use case.

paulmckibben’s picture

Status: Needs work » Needs review
FileSize
6.43 KB
2.43 KB

@Lendude, thanks for the review.

I've implemented your feedback and have uploaded a new patch and interdiff.

Lendude’s picture

Status: Needs review » Reviewed & tested by the community

We have tests, we have a fix, all is green.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 65: 1863020-65.patch, failed testing.

paulmckibben’s picture

Status: Needs work » Reviewed & tested by the community

Putting this back in RTBC. I reran tests and they passed. Not sure what happened in #67.

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.

alexpott’s picture

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

Committed and pushed 4fc3756 to 8.4.x and 25d7f11 to 8.3.x. Thanks!

Leaving at patch to be ported so that the D7 views issue can be opened - once it is this issue should be closed.

diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php
index 3b614a0..991f6c3 100644
--- a/core/modules/views/src/ViewExecutable.php
+++ b/core/modules/views/src/ViewExecutable.php
@@ -5,7 +5,6 @@
 use Drupal\Component\Utility\Html;
 use Drupal\Component\Utility\Tags;
 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
-use Drupal\Core\Form\FormState;
 use Drupal\Core\Routing\RouteProviderInterface;
 use Drupal\Core\Session\AccountInterface;
 use Drupal\views\Plugin\views\display\DisplayRouterInterface;

Removed unused use on commit - nice that these two things are decoupled!

  • alexpott committed 4fc3756 on 8.4.x
    Issue #1863020 by dawehner, catch, paulmckibben, sunset_bill,...

  • alexpott committed 25d7f11 on 8.3.x
    Issue #1863020 by dawehner, catch, paulmckibben, sunset_bill,...

  • xjm committed 21ba478 on 8.4.x
    Revert "Issue #1863020 by dawehner, catch, paulmckibben, sunset_bill,...

  • xjm committed b828852 on 8.3.x
    Revert "Issue #1863020 by dawehner, catch, paulmckibben, sunset_bill,...
xjm’s picture

Status: Patch (to be ported) » Needs work
Lendude’s picture

Status: Needs work » Needs review
FileSize
1.21 KB
6.44 KB
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

+1 for the fix. Too bad we work on multiple things at the same time

alexpott’s picture

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

Second time lucky!

Committed and pushed bfa73c9 to 8.4.x and b4512ef to 8.3.x. Thanks!

Leaving at patch to be ported so that the D7 views issue can be opened - once it is this issue should be closed.

I fixed the unused use again... see #70

  • alexpott committed bfa73c9 on 8.4.x
    Issue #1863020 by dawehner, catch, paulmckibben, sunset_bill,...

  • alexpott committed b4512ef on 8.3.x
    Issue #1863020 by dawehner, catch, paulmckibben, sunset_bill,...
alexpott’s picture

Status: Patch (to be ported) » Fixed

I'm marking this fixed today because I'm triaging the patch-to-be-ported queue. There's been no view 7.x issue created in the past 2 months and the people who worked on this should be credited.

alexpott’s picture

Status: Fixed » Closed (fixed)

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