From 4a0c3588118304ba81cb45ea9710ff78d4f13532 Mon Sep 17 00:00:00 2001
From: Colan Schwartz <colan@58704.no-reply.drupal.org>
Date: Thu, 25 Apr 2019 15:24:56 -0400
Subject: [PATCH] Issue #483150 by colan: Reformatted internal menu links as
 internal if entered as external.

---
 .../src/Form/MenuLinkContentForm.php          | 22 +++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php
index a7525c0ddc..3b708b9e77 100644
--- a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php
+++ b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php
@@ -117,10 +117,32 @@ public function buildEntity(array $form, FormStateInterface $form_state) {
     $entity->menu_name->value = $menu_name;
     $entity->enabled->value = (!$form_state->isValueEmpty(['enabled', 'value']));
     $entity->expanded->value = (!$form_state->isValueEmpty(['expanded', 'value']));
+    $entity->link->uri = $this->reformatInternalLinkIfEnteredAsExternal($entity->link->uri);
 
     return $entity;
   }
 
+  /**
+   * Reformat an internal link if it was entered as an external one.
+   *
+   * @param string $uri
+   *   The URI to possibly reformat.
+   *
+   * @return string
+   *   The possibly reformatted link.
+   */
+  protected function reformatInternalLinkIfEnteredAsExternal($uri) {
+    if (parse_url($uri, PHP_URL_SCHEME) !== 'internal') {
+      $base_url = $this->getRequest()->getSchemeAndHttpHost() . $this->getRequest()->getBasePath();
+      if (strpos($uri, $base_url) === 0) {
+        // The entered base URL matches the current request so we can drop it.
+        $path = substr($uri, strlen($base_url));
+        return 'internal:' . ($path ?: '/');
+      }
+    }
+    return $uri;
+  }
+
   /**
    * {@inheritdoc}
    */
-- 
2.19.1

