Problem/Motivation

If you have an entity with a link field that contains a root relative path to a managed file, the file usage is not tracked. E.g.: If a link field has a value like '/sites/default/files/foobar.pdf'. The file usage page for foobar.pdf will not show the entity with the link field as using the file.

This can be a common issue where content people need to link to a file in a standard link field with the default field widget. To do this, they cut and paste just the file path into the link field because that makes it site domain independent (works on dev, etc. sites).

Steps to reproduce

  • Create a node type with a standard link field using the default link field widget.
  • Upload or use an existing file to get the path part of the url to the file.
  • Create a new node of the type from above
  • Enter the root relative path in to the link field
  • Save
  • Check the file's entity usage info /admin/content/entity-usage/file/####
  • The new node will not be listed as using the file

Proposed resolution

The problem is that the link plugin only tracks route based internal urls and does not handle the non route case.

The internal url handling in the link plugin's getTargetEntities method should go from:

      $url = $link->getUrl();
       $entity_info = $this->urlToEntity->findEntityIdByRoutedUrl($url);
      $url = $link->getUrl();
      if ($url->isRouted()) {
        $entity_info = $this->urlToEntity->findEntityIdByRoutedUrl($url);
      }
      else {
        $entity_info = $this->urlToEntity->findEntityIdByUrl($url->toString());
      }

FYI - if the url used in the link field is a fully qualified url with a domain listed in the entity_usage config setting, it is tracked.

Remaining tasks

Put together patch.

User interface changes

n/a

API changes

n/a

Data model changes

n/a

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

cgmonroe created an issue. See original summary.

cgmonroe’s picture

Status: Active » Needs review
StatusFileSize
new653 bytes

Here's a quick patch to fix this. Sorry don't have the time to write the tests that probably should be included.

alexpott made their first commit to this issue’s fork.

alexpott’s picture

Status: Needs review » Reviewed & tested by the community

I've added test coverage. @cgmonroe thanks for the bug fix.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Now that this issue is closed, review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, credit people who helped resolve this issue.

  • alexpott committed 4cd5d336 on 5.x
    fix: #3592109 Link plugin does not track root relative files
    
    By:...

  • alexpott committed d1abe6be on 8.x-2.x
    fix: #3592109 Link plugin does not track root relative files
    
    By:...

Status: Fixed » Closed (fixed)

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