Show URL alias after autocomplete selection instead of internal route (e.g., /node/123)

Created on 11 May 2017, over 7 years ago
Updated 5 September 2024, 23 days ago

Recent changes, probably related to - https://www.drupal.org/node/2844466 β†’ - made the adding and editing a little less intuitive, as now the links are always show as /node/x or /media/x instead of being more friendly, say using the path alias instead.

✨ Feature request
Status

Needs work

Version

7.0

Component

Code

Created by

πŸ‡¦πŸ‡ΊAustralia singularo Adelaide, AUS

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

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.

  • First commit to issue fork.
  • @jds1 opened merge request.
  • Status changed to Needs review over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States jds1 Hudson Valley, NY

    Rerolled #41 against 6.0.x https://git.drupalcode.org/project/linkit/-/merge_requests/16. Everything passes. Patch applies cleanly locally against both 6.0.x-dev and 6.0.0-beta4. Tested locally and now I'm getting aliases instead of node URLs! Marking as "Needs Review" – thank you!

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    92 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    83 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    38 pass, 20 fail
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    Patch Failed to Apply
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    Patch Failed to Apply
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    73 pass, 12 fail
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    46 pass, 2 fail
  • First commit to issue fork.
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    83 pass
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    73 pass, 12 fail
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 9.5.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    73 pass, 12 fail
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    Composer require failure
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    83 pass
  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    The merge request in patch format.

  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.x + Environment: PHP 8.1 & MySQL 8
    last update over 1 year ago
    Composer require failure
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.0.7 + Environment: PHP 8.1 & MariaDB 10.3.22
    last update over 1 year ago
    83 pass
  • The linkit-n2877535-50.patch is successfully changed the URL Link field from "node/xxx" to URL Alias, but it still needs to work, as the saved URL is still "node/xxx" rather than saved as its path alias, 'canonical' is not active.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    It's ok that the saved value is "node/xxx" because it's converted to the correct URL when the content is rendered; it's a feature.

  • Thank you Damien, but I think once it has the path alias also it appears in the Link field, it should save as path alias with canonical active.

  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA

    That would need to be a separate discussion.

  • πŸ‡¨πŸ‡¦Canada vladt

    I'm encountering an issue after applying patch #50 to Linkit 6.0.0-rc1 (as well as Linkit 5.0-beta 13), the data-entity-substitution, data-entity-type, data-entity-uuid attributes are no longer being added to the link, so the LinkitFilter is no longer working. I can't see why this would be the case after applying this patch, removing the patch resolves this and the attributes are added but I need the functionality provided by this patch. Could anyone suggest why this may be the case?

  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States moshe weitzman Boston, MA

    Sounds like this is right status.

  • Status changed to RTBC about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States moshe weitzman Boston, MA

    In my instance, that check is preventing the values from being unset, not unsetting them. Changing status after my review.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson
  • πŸ‡ΊπŸ‡ΈUnited States R_H-L

    Testing this in 9.5, #50 breaks the a tag extra attributes. The tag gets put in as just the bare '/node/x' without any data attributes.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    Addressing the comments in #51, #53, and #59, all of which are suggesting that the URL alias is what should be saved to the database, rather than the internal route, I agree with Damien McKenna's statement in #52:

    It's ok that the saved value is "node/xxx" because it's converted to the correct URL when the content is rendered; it's a feature.

  • Status changed to Needs work about 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    It's ok that the saved value is "node/xxx" because it's converted to the correct URL when the content is rendered

    After testing to confirm, I take back my comment. Using the latest patch, the comments in #51, #52, and #59 are effectively pointing out a problem -- not that the internal URL is what is saved (that's fine), but that when the resulting page is rendering, the internal URL is what is rendered to the end-user, rather than the URL alias.

    Changing status to "Needs work" to address this. I'm also surprised that there is apparently no test coverage to catch this. I'd like to add test coverage for this going forward: the URL alias should be rendered on the page after save.

  • πŸ‡ΊπŸ‡ΈUnited States moshe weitzman Boston, MA

    This patch no longer applies to the most recent release (Sep 30) :(

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson

    This patch no longer applies to the most recent release (Sep 30)

    Here's a revised patch that replicates verbatim what was in the patch in #50, and which will apply to the current development release for 6.0.x and 6.1.x.

    Noting that the comments from #51, #52, and #59 still need to be addressed: when the resulting page is rendered, the internal URL should not be rendered to the end-user. The URL alias should. Test coverage needs to be added for this, too. Leaving status as "Needs work."

  • πŸ‡¦πŸ‡²Armenia arthur.baghdasar

    Ive removed this part from the patch everything seems to be working fine for me.
    In the code below $input variable comes with an Alias and I don't understand why we should get the $path from it.
    In My case the $path variable is converted back to the node/[nid] which is then being set to be the new input.

    +    if (!empty($input) && \Drupal::moduleHandler()->moduleExists('path_alias')) {
    +      /** @var \Drupal\path_alias\AliasManagerInterface $aliasManager */
    +      $aliasManager = \Drupal::service('path_alias.manager');
    +      $path = $aliasManager->getPathByAlias($input);
    +      if ($path !== $input) {
    +        $input = $path;
    +      }
    +    }
  • Status changed to Needs review 12 months ago
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 12 months ago
    Composer require failure
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.1.4 + Environment: PHP 8.1 & MariaDB 10.3.22
    last update 12 months ago
    Composer require failure
  • πŸ‡ΊπŸ‡ΈUnited States DamienMcKenna NH, USA
  • πŸ‡ΊπŸ‡ΈUnited States kthull Fort Wayne, Indiana

    Patch from #64 applied to 6.1.2 for me and solved the generic node path on D10.1.5

  • Status changed to Needs work 11 months ago
  • πŸ‡ΊπŸ‡ΈUnited States greenSkin

    The LinkitFilter doesn't look to work with the patch. I'm not seeing the additional attributes getting added to the tag.

  • πŸ‡³πŸ‡±Netherlands SpadXIII

    I tried to get something working, but did not succeed.

    My goal was to use the alias during editing and store the node-url in the database. The link would then show the node alias in the editor, but in the database the node/ was stored. By adding another data-property (data-entity-path) I tried adding a editorDowncast and dataDowncast in the plugin, but that didn't work correctly: it would create a new a-tag wrapping the one that was being made by the base link plugin.

    By adding a general upcast conversion, the href is changed to the alias:

    editor.conversion.for('upcast')
          .elementToAttribute( {
            view: {
              name: 'a',
              attributes: {
                href: true,
                ['data-entity-alias']: true
              }
            },
            model: {
              key: 'linkHref',
              value: viewElement => viewElement.getAttribute('data-entity-alias'),
            },
            converterPriority: 'high'
          } );

    But I failed trying to move the other way around. I tried adding a dataDowncast (which would set the path back to the href), and an editorDowncast (which would use the alias as href). This didn't work, because when creating an element with the same attribute, would add another element to the html instead of merging them (effectively overwriting the href-attribute)
    I also tried using a data downcastDispatcher, but that doesn't seem to be able to get the correct viewElement (or rather, it couldn't find any element at all).

    ps. there are some other code changes required as well to not only pass but also use the alias and path in the links.

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

    For anybody who needs a quick and dirty fix for this, I'm handling it in hook_preprocess_field() with:

    function cc_gin_preprocess_field__node__body__article(&$variables) {
      // Assuming $text is the HTML content you're preprocessing
      $text = $variables["items"][0]["content"]["#text"];
    
      // Create a new DOMDocument and load the HTML content
      $dom = new DOMDocument();
      @$dom->loadHTML(mb_convert_encoding($text, 'HTML-ENTITIES', 'UTF-8'), LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    
      $links = $dom->getElementsByTagName('a');
    
      foreach ($links as $link) {
        // Check if the link is an internal node link
        if ($link->hasAttribute('href') && str_starts_with($link->getAttribute('href'), '/node/')) {
          // Extract node ID
          $path = $link->getAttribute('href');
    
          // Fetch the alias for the node
          $alias = \Drupal::service('path_alias.manager')->getAliasByPath($path);
    
          // Replace the href attribute with the alias
          $link->setAttribute('href', $alias);
        }
      }
    
      // Save the updated HTML
      $variables["items"][0]["content"]["#text"] = $dom->saveHTML();
    }

    Note that this was generated by chatgpt, so I'm not sure that all of the args in $dom->loadHTML() are needed, but it seems to do the trick for me.

  • πŸ‡ΊπŸ‡ΈUnited States mark_fullmer Tucson
  • πŸ‡³πŸ‡ΏNew Zealand klidifia

    I updated this issue with some code that will use the entity title (already obtained via Linkit) as the default link text for a brand new inserted link without a selection range: ✨ Display node title (a text) in by default when creating link in ckeditor5 Closed: duplicate

  • πŸ‡«πŸ‡·France federiko_

    Patch from #64 applied to 7.0.0-alpha1 seems to be working well for me ; here is the generated link code :
    <a class="test-css" href="/fr/mentions-legales" target="_blank" aria-label="test-aria" id="test-idd" rel="test-rel" data-entity-type="node" data-entity-uuid="7a012594-8e2f-446f-8559-ac377b1ec0d5" data-entity-substitution="canonical" tabindex="-1" data-title="Test title">Test link</a>

Production build 0.71.5 2024