Problem/Motivation

Exposed form is rendered in cases, it doesn't needs. When exposed form is in block, it also is rendered for view results in content block. When there are attached displays to main one - the form is also rendered for them.

Proposed resolution

In file plugins/views_plugin_exposed_form.inc code from line 162 move into if construct body:

    $form = drupal_build_form('views_exposed_form', $form_state);
    $output = drupal_render($form);

    if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) {
      return "";
    }
    else {
      return $output;
    }
    $form = drupal_build_form('views_exposed_form', $form_state);

    if (!$this->view->display_handler->displays_exposed() || (!$block && $this->view->display_handler->get_option('exposed_block'))) {
      return "";
    }
    else {
      return drupal_render($form);
    }

Comments

pjcdawkins’s picture

StatusFileSize
new1.14 KB

Hm yes that does seem to be unnecessary. I had a look and perhaps there's a further improvement - see attached patch.

dewalt’s picture

The last improvement has a defect - form should be processed, to get values from user's input. Only render operation is
unnecessary.

pjcdawkins’s picture

Status: Active » Needs work
StatusFileSize
new689 bytes

OK, well here's a patch for the change you suggest.

But since I don't understand what's going on in this code, I think we at least need some comments.

dewalt’s picture

Status: Needs work » Patch (to be ported)
StatusFileSize
new689 bytes

I'll insert you patch with correct issue status.

dewalt’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new689 bytes

Oops, I'll try one more time :)

chris matthews’s picture

Issue summary: View changes

The 5 year old patch in #5 to views_plugin_exposed_form.inc applied cleanly to the latest views 7.x-3.x-dev and if still applicable need review.

tonytheferg’s picture

We have a similar problem in drupal 8/9. Anyone have a suggestion as to where to start with this? drupal_render is deprecated in D8,

In ExposedFormPluginBase

@ namespace Drupal\views\Plugin\views\exposed_form;

$form = \Drupal::formBuilder()->buildForm('\Drupal\views\Form\ViewsExposedForm', $form_state);
  $errors = $form_state->getErrors();

  // If the exposed form had errors, do not build the view.
  if (!empty($errors)) {
    $this->view->build_info['abort'] = TRUE;
  }

  if (!$this->view->display_handler->displaysExposed() || (!$block && $this->view->display_handler->getOption('exposed_block'))) {
    return [];
  }
  else {
    // this didn't work:
    // return \Drupal::service('renderer')->render($form);
    return $form;
  }
}

I tried as a stab in the dark, replacing return $form with return \Drupal::service('renderer')->render($form);

Got the following error.

Error: Cannot use object of type Drupal\Core\Form\FormState as array in Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase->renderExposedForm() (line 134 of *\web\core\modules\views\src\Plugin\views\exposed_form\ExposedFormPluginBase.php)

renatog’s picture

Status: Needs review » Reviewed & tested by the community

It really makes sense

@tonytheferg
As this project is for D7 only on this case for D8 and D9 you can move your question here
https://www.drupal.org/project/issues/drupal?version=any_9.&component=vi...

  • RenatoG committed d4181e8 on 7.x-3.x authored by dewalt
    Issue #2088549 by dewalt, pjcdawkins, Chris Matthews, tonytheferg,...
renatog’s picture

Status: Reviewed & tested by the community » Fixed

Moved to the dev branch

tonytheferg’s picture

@RenatoG, I marked this as a parent of a similar issue for 8/9.

https://www.drupal.org/project/drupal/issues/3031259

damienmckenna’s picture

Status: Fixed » Closed (fixed)

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