View source
<?php
namespace Drupal\menu_link_clone\Form;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_clone\Form\EntityCloneForm;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\Core\Url;
use Drupal\Component\Uuid\Php;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\TranslationManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Drupal\Core\Messenger\Messenger;
class EntityMenuLinkCloneForm extends EntityCloneForm {
protected $uuidinterface;
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('current_route_match'), $container
->get('string_translation'), $container
->get('event_dispatcher'), $container
->get('messenger'), $container
->get('uuid'));
}
public function __construct(EntityTypeManagerInterface $entity_type_manager, RouteMatchInterface $route_match, TranslationManager $string_translation, EventDispatcherInterface $eventDispatcher, Messenger $messenger, Php $uuid_interface) {
parent::__construct($entity_type_manager, $route_match, $string_translation, $eventDispatcher, $messenger);
$this->uuidinterface = $uuid_interface;
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['clone_links'] = [
'#type' => 'checkbox',
'#title' => t('Clone with Links'),
'#required' => FALSE,
'#default_value' => FALSE,
'#weight' => 0,
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$cloneLink = $form_state
->getValue('clone_links');
if ($cloneLink) {
$sourceMenuId = $this->entity
->id();
$destMenuId = $form_state
->getValue('id');
$sourceMenuExistence = $this
->menuLinksAvailabilityCheck($sourceMenuId);
if (!$sourceMenuExistence) {
$this->messenger
->addMessage($this->stringTranslationManager
->translate('Self(Admin) created menu links are not available in ' . $this->entity
->label() . ' menu.'));
}
else {
$result = $this
->cloneMenuLinks($sourceMenuId, $destMenuId);
if ($result) {
$this->messenger
->addMessage($this->stringTranslationManager
->translate('Self(Admin) created Links are cloned successfully for ' . $form_state
->getValue('label') . ' menu.'));
}
else {
$this->messenger
->addMessage($this->stringTranslationManager
->translate('Unsuccessfull to clone links for ' . $form_state
->getValue('label') . ', Please try again or contact to site admin.'));
}
}
}
$response = Url::fromUserInput('/admin/structure/menu');
$form_state
->setRedirectUrl($response);
}
protected function cloneMenuLinks($source_menu_name, $target_menu_name) {
$result = FALSE;
$menuLinkItems = $this
->getMenuItems($source_menu_name);
if ($menuLinkItems['status']) {
$data = $this
->resetLinkItems($menuLinkItems['items']);
$data = $this
->setUuidForMenuItems($data, $target_menu_name);
$data = $this
->createMenuLinkClone($data);
if ($data) {
$result = TRUE;
}
}
return $result;
}
protected function getMenuItems($menu_id) {
$result = [];
$storage = $this->entityTypeManager
->getStorage('menu_link_content');
$menuLinkItems = $storage
->loadByProperties([
'menu_name' => $menu_id,
]);
if (!empty($menuLinkItems)) {
$result['status'] = TRUE;
$result['items'] = $menuLinkItems;
}
else {
$result['status'] = FALSE;
$result['items'] = [];
}
return $result;
}
protected function menuLinksAvailabilityCheck($source_menu_id) {
$result = FALSE;
if (!empty($source_menu_id)) {
$menuLinkItems = $this
->getMenuItems($source_menu_id);
if ($menuLinkItems['status']) {
$result = TRUE;
}
}
return $result;
}
protected function resetLinkItems($menu_links_object_multiple) {
$result = [];
foreach ($menu_links_object_multiple as $link) {
if (!empty($link)) {
$linkArray = $link
->toArray();
$linkData = [];
foreach ($linkArray as $key => $linkArrayItem) {
$linkData[$key] = reset($linkArrayItem);
}
$result[$link
->id()] = $linkData;
}
}
return $result;
}
protected function setUuidForMenuItems($menu_links_object_multiple, $target_menu_name) {
$uuid_map = [];
foreach ($menu_links_object_multiple as $id => $menu) {
$uuid = $menu['uuid']['value'];
$new_uuid = $new_uuid = $this->uuidinterface
->generate();
$uuid_map['menu_link_content:' . $uuid] = 'menu_link_content:' . $new_uuid;
$menu_links_object_multiple[$id]['uuid'] = $new_uuid;
unset($menu_links_object_multiple[$id]['id']);
$menu_links_object_multiple[$id]['menu_name'] = $target_menu_name;
if (isset($menu_links_object_multiple[$id]['parent']['value']) && !empty($menu_links_object_multiple[$id]['parent']['value'])) {
$menu_links_object_multiple[$id]['parent']['value'] = $uuid_map[$menu_links_object_multiple[$id]['parent']['value']];
}
}
return $menu_links_object_multiple;
}
protected function createMenuLinkClone($menu_links_object_multiple) {
$result = FALSE;
foreach ($menu_links_object_multiple as $id => $menu) {
if (isset($id) && !empty($id)) {
unset($menu['revision_id']);
unset($menu['bundle']);
$save_menu = MenuLinkContent::create($menu);
$save_menu
->save();
if ($save_menu) {
$result = TRUE;
}
}
}
return $result;
}
}