diff --git a/core/modules/menu_link_content/src/Entity/MenuLinkContent.php b/core/modules/menu_link_content/src/Entity/MenuLinkContent.php index 7ab8a8e..4e2b513 100644 --- a/core/modules/menu_link_content/src/Entity/MenuLinkContent.php +++ b/core/modules/menu_link_content/src/Entity/MenuLinkContent.php @@ -378,4 +378,29 @@ public function setRequiresRediscovery($rediscovery) { return $this; } + /** + * Disallow translation of internal links, and add a validation to + * avoid changing an internal to an external link and vice versa. + */ + public function setTranslationConstraints() { + if (!$this->link->isEmpty()) { + // If the source link is internal we don't allow the translation. + $link_definition = $this->getFieldDefinition('link'); + + if (!$this->getUrlObject()->isExternal()) { + $link_definition->setTranslatable(FALSE); + // If a link is external we cannot use a internal one. + $link_definition->setSetting('link_type', LinkItemInterface::LINK_INTERNAL); + } + else { + $link_definition->setSetting('link_type', LinkItemInterface::LINK_EXTERNAL); + } + $item_definition = $link_definition->getItemDefinition(); + $item_definition->addConstraint('LinkType'); + $link_definition->setItemDefinition($item_definition); + + $this->fieldDefinitions['link'] = $link_definition; + } + } + } diff --git a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php index ed372a9..9b67bc1 100644 --- a/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php +++ b/core/modules/menu_link_content/src/Form/MenuLinkContentForm.php @@ -73,6 +73,7 @@ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $default = $this->entity->getMenuName() . ':' . $this->entity->getParentId(); + $this->entity->setTranslationConstraints(); $id = $this->entity->isNew() ? '' : $this->entity->getPluginId(); $form['menu_parent'] = $this->menuParentSelector->parentSelectElement($default, $id); $form['menu_parent']['#weight'] = 10;