Problem/Motivation

Around line 1389 in \Drupal\views\Plugin\views\field\FieldPluginBase::renderAsLink and around line 1266 in \Drupal\views\Plugin\views\field\FieldPluginBase::renderText

uses base_path() with code comments asset that this is what the UrlGenerator puts at the start of a URL.

However, checking the code in the UrlGenerator we see:

    $base_url = $this->context->getBaseUrl();

Where context is the RequestContext

So the code comments and use of base_path() are not correct.

Proposed resolution

Inject the request context and use the base url from that since it correctly matches the UrlGenerator

Add a test to show the current bug.

Remaining tasks

doit

User interface changes

none

API changes

none

Data model changes

none

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pwolanin created an issue. See original summary.

pwolanin’s picture

pwolanin’s picture

Status: Active » Needs review
FileSize
2.65 KB

Partial patch, NR for bot

Status: Needs review » Needs work

The last submitted patch, 3: 2818185-3.patch, failed testing.

pwolanin’s picture

Status: Needs work » Needs review
FileSize
3.06 KB
1.63 KB

discussed with dawehner in IRC

making the constructor options to avoid imposing a change on all subclasses (especially contrib) + fallback to getting via global service container like it does for renderer.

dawehner’s picture

+1

Status: Needs review » Needs work

The last submitted patch, 5: 2818185-5.patch, failed testing.

mpdonadio’s picture

I think all of those fails are from the unit tests. I guess the question is whether to just add the mock so the tests pass using a base path of /, or use a mock with a non-/ basepath and update tests to explicitly cover this. The later is probably better.

jcloys’s picture

First pass at adding missing dependency in container.

mpdonadio’s picture

Title: Views method FieldPluginBase::renderAsLink uses base_path() when it should use $context->getBaseUrl() » Views methods FieldPluginBase::renderAsLink and renderText() uses base_path() when it should use $context->getBaseUrl()
Status: Needs work » Needs review
Issue tags: +VDC
Related issues: +#2529170: Remove DrupalKernel::initializeRequestGlobals and replace base_root, base_url and base_path with a service
FileSize
5.63 KB
2.62 KB

#9, nice patch (even though it was left at NW, the test passed). Totally forgot this test uses the ContainerBuilder.

There is another use of base_path() in that class related to links that should really go, and not increase the scope of the issue. It also allows us to get rid of the hack at the end of the test.

So the question remains, do we want to keep this as-is, or adjust the test to explicitly check for non-/ base paths?

Status: Needs review » Needs work

The last submitted patch, 10: 2818185-10.patch, failed testing.

pwolanin’s picture

Yes, we need to remove the other pase_path() call in the class.

We should try to expand the test a little also - we can add a method to set the request context if desired.

pwolanin’s picture

Status: Needs work » Needs review
FileSize
9.16 KB
4.56 KB

Some possible added test coverage - some of this is a little wonky to do in just a unit test due to all the mocking - we can't really test the use case of a link field.

Status: Needs review » Needs work

The last submitted patch, 13: 2818185-13.patch, failed testing.

mpdonadio’s picture

Status: Needs work » Needs review
mpdonadio’s picture

+++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
@@ -102,6 +104,40 @@
+   * @var \Symfony\Component\Routing\RequestContext
+   */
+  protected $context;
+
+++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php
@@ -114,6 +116,13 @@ class FieldPluginBaseTest extends UnitTestCase {
+   * @var \Drupal\Core\Routing\RequestContext
+   */
+  protected $requestContext;
+

Think I made that last change. w/o looking at the rest of the patch. For consistency sake, which is preferred `$context` or `$requestContext`? There doesn't seem to be a clear winner in core, but it looks like the later may be used more (which is why I made the change, but missed the others in the patch).

pwolanin’s picture

I don't think the naming in the test really matters in terms of consistency, but as far as I could see most other uses in core are just $this->context

pwolanin’s picture

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.

anavarre’s picture

Version: 8.3.x-dev » 8.4.x-dev
dawehner’s picture

  1. +++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
    @@ -102,6 +104,40 @@
    +    $this->context = $context;
    
    @@ -1824,6 +1862,18 @@ protected function getRenderer() {
    +   * Returns the request context.
    +   *
    +   * @return \Symfony\Component\Routing\RequestContext
    +   */
    +  protected function getRequestContext() {
    +    if (!isset($this->context)) {
    +      $this->context = \Drupal::service('router.request_context');
    +    }
    +
    +    return $this->context;
    +  }
    

    An alternative pattern is to lazy fetch it in the constructor, if it wasn't passed long. Not sure what is better to be honest.

  2. +++ b/core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php
    @@ -726,12 +797,3 @@ public function setLinkGenerator(LinkGeneratorInterface $link_generator) {
     
     }
    -
    -// @todo Remove as part of https://www.drupal.org/node/2529170.
    -namespace Drupal\views\Plugin\views\field;
    -
    -if (!function_exists('base_path')) {
    -  function base_path() {
    -    return '/';
    -  }
    -}
     
    

    It is super nice to get rid of this hack.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

jibran’s picture

Status: Needs review » Needs work
+++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
@@ -102,6 +104,40 @@
+  public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestContext $context = NULL) {
+    parent::__construct($configuration, $plugin_id, $plugin_definition);
+    $this->context = $context;

We need to add bc warning and bc test for this change.

Lendude’s picture

+++ b/core/modules/views/src/Plugin/views/field/FieldPluginBase.php
@@ -1824,6 +1862,18 @@ protected function getRenderer() {
+  protected function getRequestContext() {
+    if (!isset($this->context)) {
+      $this->context = \Drupal::service('router.request_context');
+    }

We usually set this in the constructor for new dependencies now, so let's follow that pattern here too and we can probably remove this method.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

beatrizrodrigues’s picture

Assigned: Unassigned » beatrizrodrigues
beatrizrodrigues’s picture

I did the reroll of the patch and I addressed comment #31 too.

beatrizrodrigues’s picture

Status: Needs work » Needs review
ankithashetty’s picture

Fixed the custom command error in #34, thanks!

ankithashetty’s picture

FileSize
1.15 KB

Sorry, messed up the interdiff file.

The last submitted patch, 36: 2818185-36.patch, failed testing. View results

lucienchalom’s picture

Status: Needs review » Needs work

I run the phpcs and in the test files and the core/modules/views/tests/src/Unit/Plugin/field/FieldPluginBaseTest.php returned this errors in the added lines:

 401 | ERROR   | [ ] Missing short description in doc comment
 405 | ERROR   | [x] Parameter comment must end with a full stop
 415 | ERROR   | [ ] The array declaration extends to column 93 (the limit is 80). The array content should be split up over multiple lines
 445 | ERROR   | [ ] The array declaration extends to column 99 (the limit is 80). The array content should be split up over multiple lines
 447 | ERROR   | [ ] The array declaration extends to column 83 (the limit is 80). The array content should be split up over multiple lines
 449 | ERROR   | [ ] The array declaration extends to column 104 (the limit is 80). The array content should be split up over multiple lines
 

also the test is returning getBaseUrl() as NULL in all the assertions related.

I will try to work on this but as I am not 100% sure how to fix it, if someone wants to step in is welcome.

sourabhjain’s picture

Status: Needs work » Needs review
FileSize
11 KB
4.12 KB

Fixed the issues which is mentioned in #39. Please review.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

bruno.bicudo’s picture

I reviewed #40, worked on the errors and also worked on what #39 pointed out.

Kindly review it :)

Status: Needs review » Needs work

The last submitted patch, 42: 2818185-42.patch, failed testing. View results

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.