The LinkIt plugin in #2927857: Support for the Linkit module only tracks links added with the LinkIt module. It would be nice to also track links that are not added with the LinkIt module.

Comments

BryanDeNijs created an issue. See original summary.

bryandenijs’s picture

Issue summary: View changes
bryandenijs’s picture

Status: Active » Needs review
StatusFileSize
new5.51 KB

I created a new HtmlFieldLink plugin to track all non-LinkIt links.

Status: Needs review » Needs work

The last submitted patch, 3: 2950107-3.patch, failed testing. View results

marcoscano’s picture

@BryanDeNijs thanks! Patch looks great, just some minor remarks:

  1. +++ b/src/Plugin/EntityUsage/Track/HtmlFieldLink.php
    @@ -0,0 +1,155 @@
    + *
    + * @EntityUsageTrack(
    + *   id = "html_field_link",
    

    How about just html_link as the ID?

  2. +++ b/src/Plugin/EntityUsage/Track/HtmlFieldLink.php
    @@ -0,0 +1,155 @@
    + *   label = @Translation("HTML Fields links"),
    

    Maybe just "HTML Links" as label?

  3. +++ b/src/Plugin/EntityUsage/Track/HtmlFieldLink.php
    @@ -0,0 +1,155 @@
    + *   description = @Translation("Tracks usage of entities linked HTML (WYSIWYG)
    + *   fields."),
    

    The description inside the annotation should be in a single line, even if longer than 80chars.

    Also, maybe "Tracks usage of entities referenced from regular HTML Links" could be a bit clearer?

It would be great to have test coverage for this plugin as well. Can you work on this? Let me know if not.

Thanks!

miro_dietiker’s picture

`not(starts-with(@href, 'http://')) and not(starts-with(@href, 'https://'))`

If you want to support unmanaged links then you also should consider that unqualified users also paste links to own site incl domain. Pathologic tries to identify them by supporting a domain list. Maybe support pathologic or have own setting?

bryandenijs’s picture

Status: Needs work » Needs review
StatusFileSize
new12.89 KB

Thanks for the feedback.

Re #5: I agree. Fixed it! Also added a kernel test. It is my first test ever, so I'd like to hear your honest feedback on it, so I can learn from it.

Re #6: Yes you are right. We should be supporting absolute URL's. I now made a quick check for absolute URL's to the current domain. I think this is OK for now. If someone wants to support a list of domains, it can be fixed in another issue.

Status: Needs review » Needs work

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

bryandenijs’s picture

miro_dietiker’s picture

IMHO you should never use a current host in any processing. The host can vary based on context, for instance with a per-language domain setup or while cron run.

Adding this will cause pain as it contains risk of unstable conditions with various results.

So IMHO this is oversimplified and not ready, decision up to the maintainer. ;-)

bryandenijs’s picture

You are absolutely right. There should be a better check for this, but this has no prio for the project i'm currently working on. If anyone would like to improve this, I'll be happy to help with reviewing.

seanb’s picture

Maybe add better support for multiple hosts in a followup?

marcoscano’s picture

I'm OK with including a feature that only partly addresses the need, but I would try to avoid including something we know is going to cause issues in some scenarios.

Why don't we only support relative links for now, and create a follow-up to extend that to support absolute URLs as well in the future? As long as we document it on the project page / documentation, I believe this would be the best option.

bryandenijs’s picture

Version: 8.x-1.x-dev » 8.x-2.x-dev
Status: Needs work » Needs review
StatusFileSize
new17.03 KB

I added a new site domains field to support absolute URL's.

seanb’s picture

Status: Needs review » Needs work

Quick review.

  1. +++ b/src/Form/EntityUsageSettingsForm.php
    @@ -190,6 +190,12 @@ class EntityUsageSettingsForm extends ConfigFormBase {
    +      '#description' => $this->t('A comma or new-line seperated list of domain names that should be assumed as a domain for this website. When a user inserts an absolute URL to an entity with one of these domains, it will be processed as an internal entity usage.'),
    

    One type: seperated > separated

    Some suggestions:
    that should be assumed as a domain for this website > for this website

    When a user inserts an absolute URL to an entity with one of these domains, it will be processed as an internal entity usage. > Absolute URLs in content will be checked against these domains to allow usage tracking.

  2. +++ b/src/Form/EntityUsageSettingsForm.php
    @@ -205,8 +211,13 @@ class EntityUsageSettingsForm extends ConfigFormBase {
    +    $site_domains = preg_replace('/[\s, ]/', ',', $form_state->getValue('site_domains'));
    +    $site_domains = array_filter(explode(',', $site_domains));
    

    Can we add this above the block of code setting the config values and let it site nicely below each other for readability?

  3. +++ b/src/Plugin/EntityUsage/Track/HtmlLink.php
    @@ -0,0 +1,162 @@
    +            $href = preg_replace($host_pattern, '/', $href);
    

    I think this is missing a break;?

  4. +++ b/tests/src/Kernel/HtmlLinkKernelTest.php
    @@ -0,0 +1,234 @@
    +   * Modules to install.
    +   *
    +   * @var array
    

    {@inheritdoc}

  5. +++ b/tests/src/Kernel/HtmlLinkKernelTest.php
    @@ -0,0 +1,234 @@
    +
    

    Useless newline

bryandenijs’s picture

Status: Needs work » Needs review
StatusFileSize
new3.84 KB
new21.21 KB
seanb’s picture

Status: Needs review » Reviewed & tested by the community

I'm ok with this, @marcoscano could you do another check?

marcoscano’s picture

Status: Reviewed & tested by the community » Needs work

Great work! I think we're almost there.

  1. +++ b/config/schema/entity_usage.schema.yml
    @@ -19,8 +21,11 @@ entity_usage.settings:
    diff --git a/interdiff-14-16.txt b/interdiff-14-16.txt
    
    diff --git a/interdiff-14-16.txt b/interdiff-14-16.txt
    new file mode 100644
    
    new file mode 100644
    index 0000000..7416252
    
    index 0000000..7416252
    --- /dev/null
    
    --- /dev/null
    +++ b/interdiff-14-16.txt
    
    +++ b/interdiff-14-16.txt
    +++ b/interdiff-14-16.txt
    @@ -0,0 +1,74 @@
    

    seems like the interdiff got unadvertently into the patch :)

  2. +++ b/src/Plugin/EntityUsage/Track/HtmlLink.php
    @@ -0,0 +1,163 @@
    +   * Constructs display plugin.
    

    I would use a generic text here, or the actual name of the object we are constructing.

  3. +++ b/src/Plugin/EntityUsage/Track/HtmlLink.php
    @@ -0,0 +1,163 @@
    +    // Loop trough all the <a> elements that link to internal URLs and don't
    +    // have the LinkIt attributes.
    

    Seems like this comment is a bit obsolete now?

  4. +++ b/src/Plugin/EntityUsage/Track/HtmlLink.php
    @@ -0,0 +1,163 @@
    +        // Strip of the scheme and host, so we only get the path.
    

    s/of/off

  5. +++ b/src/Plugin/EntityUsage/Track/HtmlLink.php
    @@ -0,0 +1,163 @@
    +          if ($files) {
    +            // File entity found.
    +            $target_type = 'file';
    +            $target_id = array_keys($files)[0];
    +          }
    

    We currently don't have a usage listing for a target file entity. So this would only be useful for people using this as an API. Nothing against letting it in though.

bryandenijs’s picture

Status: Needs work » Needs review
StatusFileSize
new1.47 KB
new17.12 KB

Thanks!

1: Whoops! Fixed it :)
2: Modified the text.
3: Yes you are right. Fixed it.
4: Yup.
5: True. In the project I'm currently working on, we have our own overview where we can see the file entity usage as a media entity usage. If it does not harm, I would like to leave this in :)

marcoscano’s picture

Status: Needs review » Fixed

\o/

This is a nice one, thanks!

Status: Fixed » Closed (fixed)

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