Improve behaviour of RSS view with fields if you enabled "Use absolute link"

Created on 26 October 2019, about 5 years ago
Updated 23 May 2024, 6 months ago

Problem/Motivation

Follow-up to #2673980: RSS view with fields give wrong URL from path field β†’

If you have a view providing an RSS feed and you configure it to display fields, you must specify the field you want to use for the item links. The most obvious choice is the 'Link to content' field (plugin ID: view_node).

The description of the link field says

The field that is going to be used as the RSS item link for each row. This must be a drupal relative path.

Nevertheless the 'Use absolute link' option is tempting as it seems like a reasonable choice for an RSS feed), and if you select it then your feed will be broken:

If you revert commit 69e0b9feb, your site blows up with an uncaught exception:

[25-Oct-2019 23:57:47 UTC] Uncaught PHP Exception InvalidArgumentException: "The internal path component 'http://localhost/drupal-8_9/pt-br/node/2' is external. You are not allowed to specify an external URL together with internal:/." at /Applications/MAMP-common/htdocs/drupal-8_9/core/lib/Drupal/Core/Url.php line 411

If you leave commit 69e0b9feb in (from #2673980) then your feed gets links like so:

http://localhost/http://localhost/drupal-8_9/pt-br/node/2

Broken in both cases. Needs a fix.

Proposed resolution

First we need to understand why this description was put in - maybe there is a good reason.

Option A: Remove the description requiring an relative URL and fix the logic to handle absolute URLs.
Option B: Print a clearer warning if absolute URLs are enabled and explain it's not allowed.

Remaining tasks

  1. Decide what to do
  2. Write tests
  3. Write code
  4. Review
  5. RTBC
  6. Commit

User interface changes

TBD.

API changes

None.

Data model changes

None.

Release notes snippet

TBD.

πŸ“Œ Task
Status

Active

Version

11.0 πŸ”₯

Component
ViewsΒ  β†’

Last updated about 5 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States dww

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¬πŸ‡§United Kingdom globexplorer

    Drupal should allow absolute links. At the moment I tracked the problem down to the RssFields row plugin located at
    core/modules/views/src/Plugin/views/row

    Relative urls are expected, because inside the row plugin, the relative urls will be transformed to aboslute ones. But of course limited to the drupal backend host.

    This function makes it impossible to use absolute URLs:

      /**
       * Convert a rendered URL string to an absolute URL.
       *
       * @param string $url_string
       *   The rendered field value ready for display in a normal view.
       *
       * @return string
       *   A string with an absolute URL.
       */
      protected function getAbsoluteUrl($url_string) {
        // If the given URL already starts with a leading slash, it's been processed
        // and we need to simply make it an absolute path by prepending the host.
        if (strpos($url_string, '/') === 0) {
          $host = \Drupal::request()->getSchemeAndHttpHost();
          // @todo Views should expect and store a leading /.
          // @see https://www.drupal.org/node/2423913
          return $host . $url_string;
        }
        // Otherwise, this is an unprocessed path (e.g. node/123) and we need to run
        // it through a Url object to allow outbound path processors to run (path
        // aliases, language prefixes, etc).
        else {
          return Url::fromUserInput('/' . $url_string)->setAbsolute()->toString();
        }
      }
    

    I have created a patch for the 9.5.x release of drupal.

  • πŸ‡ΊπŸ‡ΈUnited States Kasey_MK

    Thanks @globexplorer! The patch in #14 didn't quite work for me (I'm sometimes using absolute URLs to external sites from a link field instead of linking to the stub pages on the current domain, where 'Use absolute link' would apply), but you pointed me in the direction of a change that does seem to do the trick for how I'm using it.

    Instead of using UrlHelper to decide whether or not to send the $url_string to the getAbsoluteUrl function, I used it within that function itself - if the $url_string is already valid (absolute) then it can be sent right back and needs no further processing:

    @@ -207,9 +208,13 @@ public function getField($index, $field_id) {
        *   A string with an absolute URL.
        */
       protected function getAbsoluteUrl($url_string) {
    +    // If the given URL is already valid, allow it.
    +    if (UrlHelper::isValid($url_string, TRUE)) {
    +      return $url_string;
    +    }
         // If the given URL already starts with a leading slash, it's been processed
         // and we need to simply make it an absolute path by prepending the host.
    -    if (str_starts_with($url_string, '/')) {
    +    elseif (str_starts_with($url_string, '/')) {

    Attached are two patches: one that applies against 10.2, which I'm currently using, and one against 11.x.

  • πŸ‡ΊπŸ‡ΈUnited States Kasey_MK

    I think this may be a duplicate issue: Views RSS row plugin incorrectly assumes links are relative πŸ› Views RSS row plugin incorrectly assumes links are relative Needs work

Production build 0.71.5 2024