Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
menu_export
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
menu_export
Merge requests
!16
Issue
#3515114
: Create a drush command for menu links importation
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3515114
: Create a drush command for menu links importation
issue/menu_export-3515114:3515114-create-a-drush
into
8.x-1.x
Overview
0
Commits
1
Pipelines
0
Changes
1
Open
Vivien BARBEAU
requested to merge
issue/menu_export-3515114:3515114-create-a-drush
into
8.x-1.x
1 month ago
Overview
0
Commits
1
Pipelines
0
Changes
1
Expand
Closes
#3515114
0
0
Merge request reports
Compare
8.x-1.x
8.x-1.x (HEAD)
and
latest version
latest version
b64f0276
1 commit,
1 month ago
1 file
+
200
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
src/Drush/Commands/MenuImportCommands.php
0 → 100644
+
200
−
0
Options
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\menu_export\Drush\Commands
;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\menu_link_content
\Entity\MenuLinkContent
;
use
Drupal\system\Entity\Menu
;
use
Drush\Attributes
as
CLI
;
use
Drush\Commands\AutowireTrait
;
use
Drush\Commands\DrushCommands
;
/**
* A Drush commandfile for importing menus from Menu Export module.
*/
final
class
MenuImportCommands
extends
DrushCommands
{
use
AutowireTrait
;
/**
* Constructs a MenuImportCommands object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The configuration factory service.
*/
public
function
__construct
(
private
readonly
EntityTypeManagerInterface
$entityTypeManager
,
private
readonly
ConfigFactoryInterface
$configFactory
,
)
{
parent
::
__construct
();
}
/**
* Import menus from Menu Export configuration.
*/
#[CLI\Command(name: 'menu_export:menu-import', aliases: ['me-menu-import'])]
#[CLI\Usage(name: 'menu_export:menu-import', description: 'Import menus from Menu Export configuration.')]
public
function
importMenus
():
void
{
if
(
!
$this
->
validateMenuExportConfig
())
{
return
;
}
$this
->
displayMenusTable
();
$menu_links
=
$this
->
configFactory
->
get
(
'menu_export.export_data'
)
->
get
();
if
(
empty
(
$menu_links
))
{
$this
->
logger
()
->
warning
(
dt
(
'No menu links found to import.'
));
return
;
}
if
(
!
$this
->
confirmImport
(
count
(
$menu_links
)))
{
return
;
}
$this
->
processMenuImport
(
$menu_links
);
}
/**
* Validates that Menu Export module is properly configured.
*
* @return bool
* TRUE if configuration is valid, FALSE otherwise.
*/
private
function
validateMenuExportConfig
():
bool
{
$menus
=
$this
->
configFactory
->
get
(
'menu_export.settings'
)
->
get
(
'menus'
);
if
(
empty
(
$menus
))
{
$this
->
logger
()
->
warning
(
dt
(
'No menus configured for export/import in Menu Export module.'
));
return
FALSE
;
}
return
TRUE
;
}
/**
* Displays a table of menus configured for import.
*/
private
function
displayMenusTable
():
void
{
$menus
=
$this
->
configFactory
->
get
(
'menu_export.settings'
)
->
get
(
'menus'
);
$menu_entities
=
Menu
::
loadMultiple
(
$menus
);
$rows
=
[];
foreach
(
$menu_entities
as
$menu_name
=>
$menu_entity
)
{
$rows
[]
=
[
'name'
=>
$menu_name
,
'label'
=>
$menu_entity
->
label
(),
'description'
=>
$menu_entity
->
getDescription
(),
];
}
$this
->
io
()
->
title
(
dt
(
'Menus configured for import:'
));
$this
->
io
()
->
table
(
[
'Machine Name'
,
'Label'
,
'Description'
],
$rows
);
}
/**
* Asks for confirmation before proceeding with import.
*
* @param int $count
* The number of menu links to import.
*
* @return bool
* TRUE if confirmed, FALSE otherwise.
*/
private
function
confirmImport
(
int
$count
):
bool
{
return
$this
->
io
()
->
confirm
(
dt
(
'Do you want to proceed with the import of @count menu links?'
,
[
'@count'
=>
$count
,
]));
}
/**
* Process the menu links import.
*
* @param array $menu_links
* The menu links to import.
*/
private
function
processMenuImport
(
array
$menu_links
):
void
{
$import_count
=
0
;
$invalid_menus
=
[];
$total_links
=
count
(
$menu_links
);
// Set up progress bar.
$this
->
io
()
->
title
(
dt
(
'Importing menu links:'
));
$progress_bar
=
$this
->
io
()
->
createProgressBar
(
$total_links
);
$progress_bar
->
setFormat
(
' %current%/%max% [%bar%] %percent:3s%% %message%'
);
$progress_bar
->
setMessage
(
'Starting import...'
);
$progress_bar
->
start
();
foreach
(
$menu_links
as
$menu
)
{
$menu_name
=
$menu
[
'menu_name'
][
'value'
];
$progress_bar
->
setMessage
(
"Processing link from menu '
$menu_name
'"
);
if
(
!
$this
->
entityTypeManager
->
getStorage
(
'menu'
)
->
load
(
$menu_name
))
{
$invalid_menus
[]
=
$menu_name
;
$progress_bar
->
advance
();
continue
;
}
unset
(
$menu
[
'id'
]);
unset
(
$menu
[
'revision_id'
]);
$menu_link_entity
=
\Drupal
::
entityQuery
(
'menu_link_content'
)
->
accessCheck
(
FALSE
)
->
condition
(
'uuid'
,
(
!
empty
(
$menu
[
'uuid'
][
'value'
])
?
$menu
[
'uuid'
][
'value'
]
:
$menu
[
'uuid'
]))
->
execute
();
if
(
!
$menu_link_entity
)
{
$menu_link_entity
=
MenuLinkContent
::
create
();
}
else
{
$menu_link_entity
=
MenuLinkContent
::
load
(
reset
(
$menu_link_entity
));
}
foreach
(
$menu
as
$field_name
=>
$items
)
{
$menu_link_entity
->
set
(
$field_name
,
$items
);
}
$menu_link_entity
->
save
();
$import_count
++
;
$progress_bar
->
advance
();
}
$progress_bar
->
setMessage
(
'Import completed'
);
$progress_bar
->
finish
();
$this
->
io
()
->
newLine
(
2
);
$this
->
displayImportResults
(
$import_count
,
$invalid_menus
);
}
/**
* Displays import results.
*
* @param int $import_count
* The number of successfully imported menu links.
* @param array $invalid_menus
* The list of invalid menu names.
*/
private
function
displayImportResults
(
int
$import_count
,
array
$invalid_menus
):
void
{
if
(
count
(
$invalid_menus
))
{
$this
->
logger
()
->
warning
(
dt
(
'Menu(s) not found: @menus'
,
[
'@menus'
=>
implode
(
', '
,
$invalid_menus
),
]));
}
$this
->
logger
()
->
success
(
dt
(
'Successfully imported @count menu links.'
,
[
'@count'
=>
$import_count
,
]));
}
}
Loading