Problem/Motivation

More and more code in views plugins just get the request directly from the view object, though this is just available when you have a page request at the moment.

Proposed resolution

Remaining tasks

User interface changes

API changes

Comments

dawehner’s picture

Status: Active » Needs review
StatusFileSize
new4.23 KB

Here is a patch.

tim.plunkett’s picture

Any reason to not pass it the request stack itself? Just curious.

dawehner’s picture

I kinda prefer to use the stack for service which live longer and the request for classes which will be destroyed relative early.

damiankloip’s picture

StatusFileSize
new6.89 KB
new2.65 KB

Yes, I think I agree with that concept too. Makes sense.

How about a unit test for the factory whilst we are here.

dawehner’s picture

If you want to extend the test coverage it would be cool to push a new request onto the stack and ensure it is reflecting in new views.

damiankloip’s picture

StatusFileSize
new7.53 KB
new1.85 KB

Sure, let's do that.

dawehner’s picture

Awesome!

znerol’s picture

  1. +++ b/core/modules/views/tests/Drupal/views/Tests/ViewExecutableFactoryTest.php
    @@ -0,0 +1,108 @@
    + * Contains \Drupal\views\Tests\VIewExecutableFactoryTest.
    

    s/VIew/View/

  2. +++ b/core/modules/views/tests/Drupal/views/Tests/ViewExecutableFactoryTest.php
    @@ -0,0 +1,108 @@
    +    $request_1 = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
    +      ->disableOriginalConstructor()
    +      ->getMock();
    +
    +    $request_2 = $this->getMockBuilder('Symfony\Component\HttpFoundation\Request')
    +      ->disableOriginalConstructor()
    +      ->getMock();
    +
    +    $this->requestStack->expects($this->at(0))
    +      ->method('getCurrentRequest')
    +      ->will($this->returnValue($request_1));
    +    $this->requestStack->expects($this->at(1))
    +      ->method('getCurrentRequest')
    +      ->will($this->returnValue($request_2));
    +
    

    In my opinion it is not necessary to mock Request and RequestStack.

Generally looks good to me. Given that it touches ViewsExecutable, I suggest to also update getExposedInput() and use $this->request instead of \Drupal::request(). Also do not forget to update the documentation of setExposedInput().

It seems that this issue will allow us to remove a great deal of request-service dependencies from views handlers. The candidates (including a couple of false positives):

$ find . -path '*/Plugin/views/*' -type f | xargs git grep -l request
core/modules/node/lib/Drupal/node/Plugin/views/argument_default/Node.php
core/modules/rest/lib/Drupal/rest/Plugin/views/display/RestExport.php
core/modules/rest/lib/Drupal/rest/Plugin/views/style/Serializer.php
core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/views/argument_default/Tid.php
core/modules/user/lib/Drupal/user/Plugin/views/argument_default/User.php
core/modules/views/lib/Drupal/views/Plugin/views/HandlerBase.php
core/modules/views/lib/Drupal/views/Plugin/views/argument/Date.php
core/modules/views/lib/Drupal/views/Plugin/views/argument_default/Raw.php
core/modules/views/lib/Drupal/views/Plugin/views/cache/CachePluginBase.php
core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
core/modules/views/lib/Drupal/views/Plugin/views/display/PathPluginBase.php
core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php
core/modules/views/lib/Drupal/views/Plugin/views/pager/SqlBase.php
core/modules/views/lib/Drupal/views/Plugin/views/query/QueryPluginBase.php
core/modules/views/lib/Drupal/views/Plugin/views/style/Table.php
core/modules/views/lib/Drupal/views/Plugin/views/wizard/WizardPluginBase.php

For the reference Drupal\views\Plugin\views\area\HTTPStatusCode is an example of how the request is accessed properly.

dawehner’s picture

StatusFileSize
new29.2 KB
new24.53 KB

Continued to work on that.

Status: Needs review » Needs work

The last submitted patch, 10: vdc-2261181-10.patch, failed testing.

damiankloip’s picture

You might want to rename this issue then because you guys just blew up the scope massively.

For the reference Drupal\views\Plugin\views\area\HTTPStatusCode is an example of how the request is accessed properly.

Yeah, we added that class so we are familiar with it.. :)

Also, I think we should still mock the request stack etc... The view executable factory is the class under test here.

znerol’s picture

You might want to rename this issue then because you guys just blew up the scope massively.

Sorry for that. The last part of my comment was meant as a pointer for a follow-up, but I failed to state that explicitely.

we should still mock the request stack etc...

I disagree. We do not have to expect any side effects from using them directly.

damiankloip’s picture

Well you mock things so you can set up expectations etc... too, but whatever.

Looks like we are going with the bigger issue now :)

znerol’s picture

+++ b/core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php
@@ -893,8 +893,7 @@ public function getHandlers($type) {
+          if ($this->view->getRequest()->request->get('form_id') && isset($this->view->temporary_options[$type][$id])) {

getRequest()->request

Well you mock things so you can set up expectations etc... too, but whatever.

I agree in general. In this case the expectations do not contribute to the test coverage. On the other hand using Request and RequestStack directly improves readability a lot.

dawehner’s picture

@damian
Would you recommend to split up the issue and not convert all the request instances?

I kind of like to not use mocking for request/request-stack as they are kind of a value object, not external dependencies.

damiankloip’s picture

OK, let's move on from the test issue. It was just meant to be a small/quick test.

We might as well just carry on with this conversion now I guess!

damiankloip’s picture

10: vdc-2261181-10.patch queued for re-testing.

The last submitted patch, 10: vdc-2261181-10.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new29.21 KB

Reroll.

znerol’s picture

There is still $this->view->getRequest()->request->get('form_id') in core/modules/views/lib/Drupal/views/Plugin/views/display/DisplayPluginBase.php (see #15). It should be $this->view->getRequest()->get('form_id').

znerol’s picture

Oh, ignore #21, I'm blind.

Status: Needs review » Needs work

The last submitted patch, 20: 2261181-20.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new29.95 KB
new755 bytes

I think we have a problem with cloned views. No request gets set on those.

Status: Needs review » Needs work

The last submitted patch, 24: 2261181-24.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new30.4 KB
new733 bytes

Ah! destroy.

Status: Needs review » Needs work

The last submitted patch, 26: 2261181-26.patch, failed testing.

damiankloip’s picture

Status: Needs work » Needs review
StatusFileSize
new32.44 KB
new2.04 KB
dawehner’s picture

Status: Needs review » Reviewed & tested by the community

Awesome!!

catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 8.x, thanks!

  • Commit 2d3d33b on 8.x by catch:
    Issue #2261181 by damiankloip, dawehner: Fixed Always pass in the...

Status: Fixed » Closed (fixed)

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