Skip to content

Instantly share code, notes, and snippets.

@junaidpv
Created September 20, 2022 15:39
Show Gist options
  • Save junaidpv/378f3fb5562ddc1e58603fbe96a3e1fc to your computer and use it in GitHub Desktop.
Save junaidpv/378f3fb5562ddc1e58603fbe96a3e1fc to your computer and use it in GitHub Desktop.
Patch for the menu_export module to include path aliases of menu link internal path in exported data.
diff --git a/src/Form/MenuExportForm.php b/src/Form/MenuExportForm.php
index 2b9a2ac..04a3bb4 100644
--- a/src/Form/MenuExportForm.php
+++ b/src/Form/MenuExportForm.php
@@ -6,6 +6,8 @@ use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use \Drupal\system\Entity\Menu;
+use Drupal\Core\Url;
+use Drupal\path_alias\Entity\PathAlias;
/**
* Configure Menu Export settings.
@@ -88,6 +90,24 @@ class MenuExportForm extends ConfigFormBase {
foreach($linkArray as $key=>$linkArrayItem){
$linkData[$key] = reset($linkArrayItem);
}
+
+ $url = Url::fromUri($link->link->uri);
+ if ($url->isRouted()) {
+ $internal_path = $url->getInternalPath();
+ // Get all path aliases
+ $path_aliases = \Drupal::entityQuery('path_alias')
+ ->condition('path', '/' . $internal_path)
+ ->execute();
+ $aliases = [];
+ foreach ($path_aliases as $alias_id) {
+ $alias = PathAlias::load($alias_id);
+ if ($alias) {
+ $aliases[] = $alias->alias->value;
+ }
+ }
+ }
+ $linkData['aliases'] = $aliases;
+
//$data[$link->id()] = $linkData;
$config->set($link->id(),$linkData);
unset($linkData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment