If I type in a link with no anchor it gets rendered as an internal link.
Example:

www.google.com -> myDrupalSitewww.google.com

To solve this issue I overrode the LinkitEditorDialog:

public function submitForm(array &$form, FormStateInterface $form_state) {

    // determine if external link need protocol prepended
    $attributes = $form_state->getValues()['attributes'];
    // If Url is valid, not recognized as external,
    // and does not start with a slash '/'
    if (UrlHelper::isValid($attributes['href']) && !UrlHelper::isExternal($attributes['href']) && $attributes['href'][0] != "/") {
      // add generic protocol so url is recognized
      // as external
      $attributes['href'] = "//".$attributes['href'];
      $form_state->setValue('attributes', $attributes);
    }

    // Generate response.
    return parent::submitForm($form, $form_state);
  }

And then to get rid of the relative protocol on the front end I overrode plugin.js in the saveCallback:

             // Use link URL as text with a collapsed cursor.
              if (range.collapsed) {
                // Shorten mailto URLs to just the email address.
                // remove relative protocols
                var raw_text = returnValues.attributes.href.replace(/^mailto:/, '').replace(/^\/\//, '');
                var text = new CKEDITOR.dom.text(raw_text, editor.document);
                range.insertNode(text);
                range.selectNodeContents(text);
              }

I'm hoping you can integrate some version of this into your module so that I can remove my custom code.

Thanks!

Comments

lee.cocklin created an issue. See original summary.

lee.cocklin’s picture

Issue summary: View changes
anon’s picture

Project: Linkit - Enriched linking experience » Drupal core
Version: 8.x-5.0-beta5 » 8.4.x-dev
Component: Code » ckeditor.module
Category: Bug report » Feature request

This is rather an issue of the default Drupal Link plugin. Linkit only extends that.

Wim Leers’s picture

Title: Links without protocol set as internal » URLs with top-level domain but without protocol should get a protocol added automatically
Component: ckeditor.module » editor.module
Category: Feature request » Task
Issue tags: +Usability

Thanks, @anon, for moving this to Drupal core's issue queue.

This is clearly a usability problem. We could add some detection logic in \Drupal\editor\Form\EditorLinkDialog::submitForm(), to automatically prefix the URL with a protocol: http:// or https://. But which of the two? How can we reliably guess? Should we perform a request?

The developer answer would be "just type the protocol", but that's of course not very usable. Not sure yet how to proceed here.

anon’s picture

I didn't even think about the http vs https. That is a hard one.

Just to perform a request wouldn't be a 100% solution. Both protocol can exists at the same time, and even link to two different targets.

To force the user to add the protocol if the "link" seems like a URI is not an option either in my mind, as entity URIs actually can be something like /www.something.com as in (http:example.com/www.something.com)

Wim Leers’s picture

Agreed with every one of your points!

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.