class EntityMenuLinkCloneForm in Menu Link Clone 8.3
Same name and namespace in other branches
- 8 src/Form/EntityMenuLinkCloneForm.php \Drupal\menu_link_clone\Form\EntityMenuLinkCloneForm
- 8.2 src/Form/EntityMenuLinkCloneForm.php \Drupal\menu_link_clone\Form\EntityMenuLinkCloneForm
Provides a menu link clone form.
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\entity_clone\Form\EntityCloneForm
- class \Drupal\menu_link_clone\Form\EntityMenuLinkCloneForm uses StringTranslationTrait
- class \Drupal\entity_clone\Form\EntityCloneForm
Expanded class hierarchy of EntityMenuLinkCloneForm
File
- src/
Form/ EntityMenuLinkCloneForm.php, line 21
Namespace
Drupal\menu_link_clone\FormView source
class EntityMenuLinkCloneForm extends EntityCloneForm {
use StringTranslationTrait;
/**
* Generate unique id(uuid).
*
* @var \Drupal\administerusersbyrole\Services\AccessManagerInterface
*/
protected $uuidinterface;
/**
* {@inheritdoc}
*/
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'));
}
/**
* Constructs a clone menu lines with parent construct.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match service.
* @param \Drupal\Core\StringTranslation\TranslationManager $string_translation
* The string translation manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* The event dispatcher service.
* @param \Drupal\Core\Messenger\Messenger $messenger
* The messenger service.
* @param \Drupal\Component\Uuid\Php $uuid_interface
* Generate unique id(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;
}
/**
* {@inheritdoc}
*
* @return array
* A render form array for a page.
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = parent::buildForm($form, $form_state);
$form['clone_links'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Clone with Links'),
'#required' => FALSE,
'#default_value' => FALSE,
'#weight' => 0,
];
return $form;
}
/**
* {@inheritdoc}
*/
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
->t('Self(Admin) created menu links are not available in @label menu.', [
'@label' => $this->entity
->label(),
]));
}
else {
$result = $this
->cloneMenuLinks($sourceMenuId, $destMenuId);
if ($result) {
$this->messenger
->addMessage($this
->t('Self(Admin) created Links are cloned successfully for @label menu.', [
'@label' => $form_state
->getValue('label'),
]));
}
else {
$this->messenger
->addMessage($this
->t('Unsuccessfull to clone links for @label, Please try again or contact to site admin.', [
'@label' => $form_state
->getValue('label'),
]));
}
}
}
$response = Url::fromRoute('entity.menu.collection');
$form_state
->setRedirectUrl($response);
}
/**
* Clone menu items.
*
* @param object $source_menu_name
* Source menu name from we need to clone the menu items.
* @param string $target_menu_name
* Destination menu name to clone the menu items.
*
* @return bool
* Return the True or False.
*/
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;
}
/**
* Get menu items ids.
*
* @param string $menu_id
* Menu name for which we can get there items.
*
* @return array
* Retrun array with there menu items tree.
*/
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;
}
/**
* Check Menu Link items are availabe inside the menu.
*
* @param string $source_menu_id
* Menu name for which we need to check their items.
*
* @return bool
* Return True and False.
*/
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;
}
/**
* Reset elements in menu item object.
*
* @param object $menu_links_object_multiple
* Menu Items Object.
*
* @return array
* Retrun array with there menu items tree.
*/
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;
}
/**
* Set UUID for menu items.
*
* @param object $menu_links_object_multiple
* Menu Items Object.
* @param string $target_menu_name
* Menu Name for which we need to set UUID.
*
* @return array
* Retrun array with there menu items tree.
*/
protected function setUuidForMenuItems($menu_links_object_multiple, $target_menu_name) {
$uuid_map = [];
// Create an uuid mapping table.
foreach ($menu_links_object_multiple as $id => $menu) {
$uuid = $menu['uuid']['value'];
// Assume uuid is not duplicated here.
$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;
}
/**
* Create menu links.
*
* @param object $menu_links_object_multiple
* Menu Items Object.
*
* @return array
* Retrun array with there menu items tree.
*/
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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EntityCloneForm:: |
protected | property | The current user. | |
EntityCloneForm:: |
protected | property | The entity ready to clone. | |
EntityCloneForm:: |
protected | property | The entity clone settings manager service. | |
EntityCloneForm:: |
protected | property | The entity type définition. | |
EntityCloneForm:: |
protected | property | The entity type manager. | |
EntityCloneForm:: |
protected | property | Event dispatcher service. | |
EntityCloneForm:: |
protected | property |
The messenger service. Overrides MessengerTrait:: |
|
EntityCloneForm:: |
protected | property | The Service Provider that verifies if entity has ownership. | |
EntityCloneForm:: |
protected | property | The string translation manager. | |
EntityCloneForm:: |
public | function | Cancel form handler. | |
EntityCloneForm:: |
protected | function | Sets a redirect on form state. | |
EntityCloneForm:: |
public | function | Gets the entity of this form. | |
EntityCloneForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EntityCloneForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
EntityMenuLinkCloneForm:: |
protected | property | Generate unique id(uuid). | |
EntityMenuLinkCloneForm:: |
public | function |
Overrides EntityCloneForm:: |
|
EntityMenuLinkCloneForm:: |
protected | function | Clone menu items. | |
EntityMenuLinkCloneForm:: |
public static | function |
Instantiates a new instance of this class. Overrides EntityCloneForm:: |
|
EntityMenuLinkCloneForm:: |
protected | function | Create menu links. | |
EntityMenuLinkCloneForm:: |
protected | function | Get menu items ids. | |
EntityMenuLinkCloneForm:: |
protected | function | Check Menu Link items are availabe inside the menu. | |
EntityMenuLinkCloneForm:: |
protected | function | Reset elements in menu item object. | |
EntityMenuLinkCloneForm:: |
protected | function | Set UUID for menu items. | |
EntityMenuLinkCloneForm:: |
public | function |
Form submission handler. Overrides EntityCloneForm:: |
|
EntityMenuLinkCloneForm:: |
public | function |
Constructs a clone menu lines with parent construct. Overrides EntityCloneForm:: |
|
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |