Problem/Motivation

In #2950107: Add plugin for tracking non-LinkIt links in WYSIWYG fields a plugin was added to track non-linkit fields in WYSIWYG field. This seems to work really good. It would be really nice to track absolute URL's in link fields in the same way.

Proposed resolution

Parse absolute URLs for link fields and add tracking for them as well. We probably need to add a helper method to EntityUsageTrackBase to fetch a target type and target ID based on an absolute URL so we don't duplicate code.

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#68 2954973-68.patch23.49 KBricovandevin
#66 2954973-66.patch23.5 KBricovandevin
#63 2954973-63.patch22.89 KBseanb
#62 interdiff_61-62.txt5.76 KBOscaner
#62 2954973-62.patch23.4 KBOscaner
#61 interdiff-59-61.txt407 bytesweynhamz
#61 2954973-61.patch20 KBweynhamz
#59 interdiff-45-59.txt5.01 KBweynhamz
#59 interdiff-53-59.txt1.17 KBweynhamz
#59 2954973-59.patch19.95 KBweynhamz
#58 interdiff-53-58.txt1.65 KBweynhamz
#58 interdiff-57-58.txt723 bytesweynhamz
#58 2954973-58-debug.patch20.35 KBweynhamz
#57 interdiff-53-57.txt1.65 KBweynhamz
#57 2954973-57-debug.patch20.35 KBweynhamz
#55 interdiff-53-54.txt1.17 KBweynhamz
#54 2954973-54-debug.patch20 KBweynhamz
#53 interdiff-45-53.txt4.72 KBweynhamz
#53 interdiff-52-53.txt1012 bytesweynhamz
#53 2954973-53.patch19.66 KBweynhamz
#52 interdiff-51-52.txt504 bytesweynhamz
#52 interdiff-45-52.txt4.06 KBweynhamz
#52 2954973-52.patch19.51 KBweynhamz
#51 interdiff-45-51.txt4.05 KBweynhamz
#51 interdiff-49-51.txt1.14 KBweynhamz
#51 2954973-51.patch19.5 KBweynhamz
#50 interdiff-45-49.txt3.52 KBweynhamz
#49 interdiff.2954973.48-49.txt2.3 KBweynhamz
#49 2954973-49.patch19.4 KBweynhamz
#48 interdiff-47-48.txt1 KBweynhamz
#48 interdiff-45-48.txt4.41 KBweynhamz
#48 2954973-48.patch19.7 KBweynhamz
#47 interdiff-45-47.txt3.88 KBweynhamz
#47 2954973-47.patch19.18 KBweynhamz
#46 interdiff-40-45.txt5.63 KBweynhamz
#45 2954973-45.patch18.96 KBweynhamz
#44 2954973-44.patch18.95 KBweynhamz
#42 2954973-42-debug.patch19.16 KBweynhamz
#41 2954973-41-debug.patch19.07 KBweynhamz
#40 2954973-40.patch18.87 KBweynhamz
#39 2954973-39.patch18.78 KBweynhamz
#37 2954973-37.patch18.93 KBweynhamz
#36 2954973-36.patch22.61 KBweynhamz
#35 2954973-35.patch17.36 KBweynhamz
#34 2954973-34.patch15.31 KBseanb
#33 interdiff-31-33.txt977 bytesseanb
#33 2954973-33.patch14.16 KBseanb
#31 interdiff-29-31.txt1.36 KBseanb
#31 2954973-31.patch14.18 KBseanb
#29 2954973-29.patch14.71 KBidebr
#26 interdiff-24-26.txt3 KBmarcoscano
#26 2954973-26.patch15.08 KBmarcoscano
#24 interdiff-22-24.txt2.33 KBmarcoscano
#24 2954973-24.patch13.92 KBmarcoscano
#22 interdiff-20-22.txt756 bytesmarcoscano
#22 2954973-22.patch14.93 KBmarcoscano
#20 2954973-20.patch14.45 KBmarcoscano
#19 2954973-19.patch25.95 KBmarcoscano
#17 2954973-17-debug-fail.patch27.05 KBmarcoscano
#15 2954973-15-debug-fail.patch26.74 KBmarcoscano
#12 interdiff-10-12.txt1.23 KBmarcoscano
#12 2954973-12.patch25.94 KBmarcoscano
#12 2954973-debug-fail.patch26.11 KBmarcoscano
#10 interdiff-7-10.txt1.57 KBmarcoscano
#10 2954973-10.patch24.96 KBmarcoscano
#7 interdiff-5-7.txt3.45 KBseanb
#7 2954973-7.patch24.61 KBseanb
#5 interdiff-2-5.txt10.46 KBseanb
#5 2954973-5.patch21.16 KBseanb
#2 2954973-2.patch21.17 KBbryandenijs
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

seanB created an issue. See original summary.

bryandenijs’s picture

Status: Active » Needs review
StatusFileSize
new21.17 KB

I moved the logic that finds an entity by a URL from the HtmlLink plugin to the EntityUsageTrackBase and refactored the HtmlLink and Link plugins.

Unfortunately I don't have the time and skills to create/modify the tests, so if anyone else could do that for me, that would be great!

Status: Needs review » Needs work

The last submitted patch, 2: 2954973-2.patch, failed testing. View results

bryandenijs’s picture

I see the tests are failing. Don't have the time at the moment to review if it is because of the non-updated tests, or because something is really broken. Will check tomorrow.

seanb’s picture

Status: Needs work » Needs review
StatusFileSize
new21.16 KB
new10.46 KB

Updated the patch a bit to fix small things I found and hopefully fixes the test..

Status: Needs review » Needs work

The last submitted patch, 5: 2954973-5.patch, failed testing. View results

seanb’s picture

Status: Needs work » Needs review
StatusFileSize
new24.61 KB
new3.45 KB

Tests pass locally for me, let's try again. Added new tests while I was at it.

Status: Needs review » Needs work

The last submitted patch, 7: 2954973-7.patch, failed testing. View results

marcoscano’s picture

  1. +++ b/tests/src/FunctionalJavascript/EntityUsageJavascriptTestBase.php
    @@ -33,6 +33,11 @@ abstract class EntityUsageJavascriptTestBase extends JavascriptTestBase {
    +    $current_request = \Drupal::request();
    +    $config = \Drupal::configFactory()->getEditable('entity_usage.settings');
    +    $config->set('site_domains', [$current_request->getHttpHost()]);
    +    $config->save();
    +
    
  2. +++ b/tests/src/FunctionalJavascript/IntegrationTest.php
    @@ -315,6 +315,65 @@ class IntegrationTest extends EntityUsageJavascriptTestBase {
    +    $page->fillField('field_link1[0][uri]', $node1->toUrl()->setAbsolute()->toString());
    

Couldn't test this locally, but reviewing the code, maybe the issue comes because if I recall correctly, the testbot runs the tests with a path prefix, so maybe there is a mismatch between the full URL you are saving into the field, and the one you are whitelisting in config?

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new24.96 KB
new1.57 KB

Let's see if this improves anything.

Status: Needs review » Needs work

The last submitted patch, 10: 2954973-10.patch, failed testing. View results

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new26.11 KB
new25.94 KB
new1.23 KB

The last submitted patch, 12: 2954973-debug-fail.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Needs review » Needs work

The last submitted patch, 12: 2954973-12.patch, failed testing. View results

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new26.74 KB

Status: Needs review » Needs work

The last submitted patch, 15: 2954973-15-debug-fail.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new27.05 KB

Status: Needs review » Needs work

The last submitted patch, 17: 2954973-17-debug-fail.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

marcoscano’s picture

StatusFileSize
new25.95 KB

OK, giving up for now.
This is how I think it should work (and passes for me), but the testbot doesn't appear to be wanting to collaborate.

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new14.45 KB

Re-rolled against recent head and (hopefully) found the problem with the test.

Status: Needs review » Needs work

The last submitted patch, 20: 2954973-20.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new14.93 KB
new756 bytes

Are you kidding me

Status: Needs review » Needs work

The last submitted patch, 22: 2954973-22.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new13.92 KB
new2.33 KB

Status: Needs review » Needs work

The last submitted patch, 24: 2954973-24.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

marcoscano’s picture

Status: Needs work » Needs review
StatusFileSize
new15.08 KB
new3 KB

Just realized that the failure comes in a part of the test that was already passing on d.org testbot before, so I wonder if there is indeed a regression somewhere...

Let's try again with less black magic in the regex.

Status: Needs review » Needs work

The last submitted patch, 26: 2954973-26.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

marcoscano’s picture

Interestingly, the fact that https://www.drupal.org/project/entity_usage/issues/2952008#comment-12550349 is green demonstrates that our logic to whitelist the domain inside the test with:

+    // Whitelist the local hostname so we can test absolute URLs.
+    $current_request = \Drupal::request();
+    $config = \Drupal::configFactory()->getEditable('entity_usage.settings');
+    $config->set('site_domains', [$current_request->getHttpHost() . $current_request->getBasePath()]);
+    $config->save();

works, so there must be indeed a bug in ::findEntityByUrl() when used in Link fields, in some scenarios (probably when the installation uses a subdirectory?)

So we definitely need to fix that before committing this.

idebr’s picture

Status: Needs work » Needs review
StatusFileSize
new14.71 KB

Reroll after #2952008: Automated tests: Refactor existing and add missing coverage was committed.

Removed these conflicting lines as these were already changed in the related issue above:

+++ b/tests/src/FunctionalJavascript/IntegrationTest.php
@@ -313,8 +313,8 @@ class IntegrationTest extends EntityUsageJavascriptTestBase {
     $session->wait(500);
-    $assert_session->pageTextContains('eu_test_ct Node 1 has been created.');
     $this->saveHtmlOutput();
+    $assert_session->pageTextContains('eu_test_ct Node 1 has been created.');
     /** @var \Drupal\node\NodeInterface $node1 */

@@ -351,8 +351,8 @@ class IntegrationTest extends EntityUsageJavascriptTestBase {
     $session->wait(500);
-    $assert_session->pageTextContains('eu_test_ct Node 2 has been updated.');
     $this->saveHtmlOutput();
+    $assert_session->pageTextContains('eu_test_ct Node 2 has been updated.');
     // Verify the usage was released.

Status: Needs review » Needs work

The last submitted patch, 29: 2954973-29.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

seanb’s picture

StatusFileSize
new14.18 KB
new1.36 KB

The regex to find a entity URL seemed to break for translation routes (for example linking to node/165356/translations/add/nl/en) since the route parameters for this route are different.

I updated the regex to find entity routes and added a check for the entity type. Didn't have to to fix the test, so leaving to needs work for that.

bryandenijs’s picture

+++ b/src/EntityUsageTrackBase.php
@@ -296,14 +296,16 @@ abstract class EntityUsageTrackBase extends PluginBase implements EntityUsageTra
+      if (isset($matches[1]) && $target_entity_type = $this->entityTypeManager->getDefinition($matches[1])) {

The isset($matches[1]) is unnecessary since the preg_match() will return 0 (false) when no match is found. With the current pattern you can assume $matches[0-2] are always set inside this if statement.

seanb’s picture

StatusFileSize
new14.16 KB
new977 bytes

Fixed #32. Also changed the matching pattern.

Ran into an issue with the groups module. When adding content in a group, the URL is group/1/content/create/group_node:article. When linking to a URL such as this the pattern matches, but since we are creating content we don't have a ID in the route parameters.

I suggest we change the pattern to work only for entity.*.canonical routes and not worry about all kinds of other entity related routes.

seanb’s picture

StatusFileSize
new15.31 KB

Rerolled #33.

weynhamz’s picture

StatusFileSize
new17.36 KB

Rerolled #34.

weynhamz’s picture

StatusFileSize
new22.61 KB

#3060687: Support Layout Builder relationships landed recently, it needs to update the __construct signature accordingly.

weynhamz’s picture

StatusFileSize
new18.93 KB

The tests run fine locally, why failed on D.O.

marcoscano’s picture

@weynhamz thanks for your contribution!

I haven't reviewed the patch, but see #26 and #28 for more info. At that time, there seemed to be legitimate concerns about the failures on d.o indicating real issues with the logic being introduced here.

It would be helpful if you could provide interdiffs between patches, it makes it easier to identify what changed between two versions of the same patch. You can find more information about creating such a file here. Thanks!

weynhamz’s picture

StatusFileSize
new18.78 KB

@marcoscano Thanks for point out internal diff, I will provide one for the final patch.

Meanwhile, I am just submitting a patch that just moved the code around without introducing any new changes, let's see how the test bot says about it.

weynhamz’s picture

StatusFileSize
new18.87 KB
weynhamz’s picture

StatusFileSize
new19.07 KB
weynhamz’s picture

StatusFileSize
new19.16 KB
weynhamz’s picture

Finally, manage to find why the `testLinkTracking()` is failing on test bot.

I printed out the $url = $link->getUrl()->toString(); in \Drupal\entity_usage\Plugin\EntityUsage\Track\Link::getTargetEntities, and the path is has a `/subdirectory` prefix, which later $this->pathValidator->getUrlIfValidWithoutAccessCheck($url) would not be able to handle.

https://dispatcher.drupalci.org/job/drupal8_contrib_patches/17534/artifact/jenkins-drupal8_contrib_patches-17534/artifacts/run_tests.js/simpletest_html/Drupal_Tests_entity_usage_FunctionalJavascript_IntegrationTest-32-90569414.html

Status message
/subdirectory/node/1 |||
Failed to get url object |||
Can not get a valid entity |||
eu_test_ct Node 2 has been created.

I miss my bed now, continue on this tomorrow.

weynhamz’s picture

StatusFileSize
new18.95 KB

Since `$link->getUrl()->toString()` will return a path with `subdirectory` prefix because of D.O. testbot runs in the subdirectory, and we only what to handle the situation that `$link` is actually an external absolute link, let's handle things differently then.

I will provide an interdiff if testbot is happy with this patch.

weynhamz’s picture

StatusFileSize
new18.96 KB

It shall pass.

weynhamz’s picture

StatusFileSize
new5.63 KB

Patch in #40 can be considered as a reroll of #24, without any new change introduced later in #26, #29, #31, #33, #34.

Update interdiff between #40 and #45 that we know why testbot is happy with #45.

weynhamz’s picture

StatusFileSize
new19.18 KB
new3.88 KB

Add back new changes introduced in #26, #29, #31, #33, #34.

Expect test failing.

weynhamz’s picture

StatusFileSize
new19.7 KB
new4.41 KB
new1 KB

Add back new changes introduced in #26, #29, #31, #33, #34.

Also, add back below code that got removed in #34.

+          if ($element->hasAttribute('data-entity-uuid')) {
+            // Normally the Linkit plugin handles when a element has this
+            // attribute, but sometimes users may change the HREF manually and
+            // leave behind the wrong UUID.
+            $data_uuid = $element->getAttribute('data-entity-uuid');
+            // If the UUID is the same as found in HREF, then skip it because
+            // it's LinkIt's job to register this usage.
+            if ($data_uuid == $entity->uuid()) {
+              continue;
+          }

Fix a stupid mistake in #47.

Now it shall pass the tests.

weynhamz’s picture

StatusFileSize
new19.4 KB
new2.3 KB

Let's introduce these new changes in a safe way, step by step.

weynhamz’s picture

StatusFileSize
new3.52 KB

I am using #45 as baseline now, add interdiff of 45-49.

weynhamz’s picture

StatusFileSize
new19.5 KB
new1.14 KB
new4.05 KB

Introduce changes in #31 and #33, but keep the original entity match pattern.

I think the proposal in #33 about use `entity.*.canonical` route pattern would be better to handle and discuss in a separate issue because we might need to have more flexibility about the entity pattern. For example, I have a case where I am using media_entity_download and the entity pattern for that is media_entity_download.download, maybe, have it configurable somehow?

weynhamz’s picture

StatusFileSize
new19.51 KB
new4.06 KB
new504 bytes

Fix stupid error in the last patch.

weynhamz’s picture

StatusFileSize
new19.66 KB
new1012 bytes
new4.72 KB

Add changes in #26.

weynhamz’s picture

StatusFileSize
new20 KB
weynhamz’s picture

StatusFileSize
new1.17 KB

https://dispatcher.drupalci.org/job/drupal8_contrib_patches/17724/artifact/jenkins-drupal8_contrib_patches-17724/artifacts/run_tests.js/simpletest_html/Drupal_Tests_entity_usage_FunctionalJavascript_IntegrationTest-38-49315841.html

1. php-apache-jenkins-drupal8-contrib-patches-17724/subdirectory
2. php-apache-jenkins-drupal8-contrib-patches-17724/subdirectory/
3. /php-apache-jenkins-drupal8-contrib-patches-17724\/subdirectory\//
4. /subdirectory/node/1
5. /subdirectory/node/1
eu_test_ct Node 3 has been created.

Domain handling in the new code introduced in #26 does not get along with the testbot subdirectory setup.

weynhamz’s picture

Looks like the domain used by testbot is `php-apache-jenkins-drupal8-contrib-patches-17724/subdirectory`, we need to to support subdirectory suffixed domain.

weynhamz’s picture

StatusFileSize
new20.35 KB
new1.65 KB
weynhamz’s picture

StatusFileSize
new20.35 KB
new723 bytes
new1.65 KB
weynhamz’s picture

StatusFileSize
new19.95 KB
new1.17 KB
new5.01 KB

Fixed testbot failure in #53.

The only thing that is not included in this patch, is the proposal in #33 about use `entity.*.canonical` route pattern, which I think should be in a separate issue to discuss.

We have a few options now.

#45: this patch just tried to move the logic to handle HTML link to the base abstract class and make the Link plugin support absolute URL as well, does not introduce any other new code changes, just as this issue's original purpose.

#49: this patch can be think as a reroll of #5, with some safe code improvements.

#52: this patch added back the changes in #31 and #33, but left behind the `entity.*.canonical` route pattern change.

And finally, this one added back the regex pattern change in #26.

Of course, testbot is happy about all of these.

weynhamz’s picture

Status: Needs work » Needs review
weynhamz’s picture

StatusFileSize
new20 KB
new407 bytes
Oscaner’s picture

StatusFileSize
new23.4 KB
new5.76 KB

Improvement for EntityUsage type hint to EntityUsageInterface

seanb’s picture

StatusFileSize
new22.89 KB

Rerolled for beta4.

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

ricovandevin’s picture

StatusFileSize
new23.5 KB

Rerolled for beta7.

Status: Needs review » Needs work

The last submitted patch, 66: 2954973-66.patch, failed testing. View results

ricovandevin’s picture

Status: Needs work » Needs review
StatusFileSize
new23.49 KB

Fixed a copy/paste error.

  • marcoscano committed 22901f1 on 8.x-2.x
    Issue #2954973 by weynhamz, marcoscano, seanB, ricovandevin, Oscaner,...
marcoscano’s picture

Status: Needs review » Fixed

Sorry all for my late response on this. Committed to 8.x-2.x.
Thanks all for contributing!

Status: Fixed » Closed (fixed)

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