Skip to content
Snippets Groups Projects
Commit cb434ac3 authored by Ruslan Piskarov's avatar Ruslan Piskarov Committed by Louis Cuny
Browse files

Issue #3073810 by DYdave, mecmartini, louis-cuny: Support link options on menu item

parent 5c8f309b
No related branches found
No related tags found
1 merge request!16Issue #3073810: Support link options on menu item
Pipeline #81032 passed
...@@ -48,6 +48,13 @@ structure_sync_menu_item: ...@@ -48,6 +48,13 @@ structure_sync_menu_item:
type: string type: string
uuid: uuid:
type: string type: string
options:
type: sequence
label: 'Link options'
# Ignore the type of the items in the sequence, since we're not sure what
# they could be: integer, mapping, sequence, string, etc...
sequence:
type: ignore
# Define a data type for taxonomies captured by structure_sync. # Define a data type for taxonomies captured by structure_sync.
structure_sync_taxonomy: structure_sync_taxonomy:
......
...@@ -104,6 +104,7 @@ public function exportMenuLinks(array $form = NULL, FormStateInterface $form_sta ...@@ -104,6 +104,7 @@ public function exportMenuLinks(array $form = NULL, FormStateInterface $form_sta
'weight' => $menuLink->weight->value, 'weight' => $menuLink->weight->value,
'langcode' => $menuLink->langcode->value, 'langcode' => $menuLink->langcode->value,
'uuid' => $menuLink->uuid(), 'uuid' => $menuLink->uuid(),
'options' => $menuLink->link->options,
]; ];
StructureSyncHelper::logMessage('Exported "' . $menuLink->title->value . '" of menu "' . $menuLink->menu_name->value . '"'); StructureSyncHelper::logMessage('Exported "' . $menuLink->title->value . '" of menu "' . $menuLink->menu_name->value . '"');
...@@ -378,6 +379,7 @@ public static function importMenuLinksFull($menus, &$context) { ...@@ -378,6 +379,7 @@ public static function importMenuLinksFull($menus, &$context) {
'link' => [ 'link' => [
'uri' => $menuLink['uri'], 'uri' => $menuLink['uri'],
'title' => $menuLink['link_title'], 'title' => $menuLink['link_title'],
'options' => $menuLink['options'],
], ],
'menu_name' => $menuLink['menu_name'], 'menu_name' => $menuLink['menu_name'],
'expanded' => in_array($menuLink['expanded'], ['1', TRUE], TRUE), 'expanded' => in_array($menuLink['expanded'], ['1', TRUE], TRUE),
...@@ -399,6 +401,7 @@ public static function importMenuLinksFull($menus, &$context) { ...@@ -399,6 +401,7 @@ public static function importMenuLinksFull($menus, &$context) {
->set('link', [ ->set('link', [
'uri' => $menuLink['uri'], 'uri' => $menuLink['uri'],
'title' => $menuLink['link_title'], 'title' => $menuLink['link_title'],
'options' => $menuLink['options'],
]) ])
->set('expanded', in_array($menuLink['expanded'], ['1', TRUE], TRUE)) ->set('expanded', in_array($menuLink['expanded'], ['1', TRUE], TRUE))
->set('enabled', in_array($menuLink['enabled'], ['1', TRUE], TRUE)) ->set('enabled', in_array($menuLink['enabled'], ['1', TRUE], TRUE))
...@@ -461,6 +464,7 @@ public static function importMenuLinksSafe($menus, &$context) { ...@@ -461,6 +464,7 @@ public static function importMenuLinksSafe($menus, &$context) {
'link' => [ 'link' => [
'uri' => $menuLink['uri'], 'uri' => $menuLink['uri'],
'title' => $menuLink['link_title'], 'title' => $menuLink['link_title'],
'options' => $menuLink['options'],
], ],
'menu_name' => $menuLink['menu_name'], 'menu_name' => $menuLink['menu_name'],
'expanded' => in_array($menuLink['expanded'], ['1', TRUE], TRUE), 'expanded' => in_array($menuLink['expanded'], ['1', TRUE], TRUE),
...@@ -503,6 +507,7 @@ public static function importMenuLinksForce($menus, &$context) { ...@@ -503,6 +507,7 @@ public static function importMenuLinksForce($menus, &$context) {
'link' => [ 'link' => [
'uri' => $menuLink['uri'], 'uri' => $menuLink['uri'],
'title' => $menuLink['link_title'], 'title' => $menuLink['link_title'],
'options' => $menuLink['options'],
], ],
'menu_name' => $menuLink['menu_name'], 'menu_name' => $menuLink['menu_name'],
'expanded' => in_array($menuLink['expanded'], ['1', TRUE], TRUE), 'expanded' => in_array($menuLink['expanded'], ['1', TRUE], TRUE),
......
...@@ -57,6 +57,7 @@ protected function setUp(): void { ...@@ -57,6 +57,7 @@ protected function setUp(): void {
public function testMenuLinksExportImportUsingAdmin(): void { public function testMenuLinksExportImportUsingAdmin(): void {
$this->drupalGet('admin/structure/structure-sync/menu-links'); $this->drupalGet('admin/structure/structure-sync/menu-links');
// Test export of all the link items of all menus except 'Arc'.
$page = $this->getSession()->getPage(); $page = $this->getSession()->getPage();
$page->uncheckField('Arc'); $page->uncheckField('Arc');
$this->click('#edit-export-menu-links'); $this->click('#edit-export-menu-links');
...@@ -69,15 +70,15 @@ public function testMenuLinksExportImportUsingAdmin(): void { ...@@ -69,15 +70,15 @@ public function testMenuLinksExportImportUsingAdmin(): void {
$this->assertCount(5, $menus); $this->assertCount(5, $menus);
// Editing the "Oxygen" menu link. // Update the link's 'class' and 'target' attributes of the tested links.
$oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']); $updated_options = [
reset($oxygenMenuLinks)->set('description', 'Oxygen')->save(); 'attributes' => [
'class' => ['testing-class3 testing-class4'],
// Deleting the "Tig gas" menu link. 'target' => '_blank',
$tigGasMenuLinkBefore = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); ],
$this->menuLinkStorage->delete($tigGasMenuLinkBefore); ];
$this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']));
/* Test 'Safe' import. */
// Adding an "Electrode" menu link. // Adding an "Electrode" menu link.
MenuLinkContent::create([ MenuLinkContent::create([
'title' => 'Electrode', 'title' => 'Electrode',
...@@ -86,47 +87,168 @@ public function testMenuLinksExportImportUsingAdmin(): void { ...@@ -86,47 +87,168 @@ public function testMenuLinksExportImportUsingAdmin(): void {
'uuid' => '9a49342a-45e0-4d15-a251-83d704371c79', 'uuid' => '9a49342a-45e0-4d15-a251-83d704371c79',
])->save(); ])->save();
// Deleting the "Tig gas" menu link.
$tigGasMenuLinkBefore = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']);
$this->menuLinkStorage->delete($tigGasMenuLinkBefore);
$this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']));
// Editing the "Oxygen" menu link of the 'tig' menu.
$oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']);
// Update the description of the "Oxygen" menu link.
/** @var array $oxygenMenuLinks */
$oxygenMenuLink = reset($oxygenMenuLinks)->set('description', 'Oxygen');
// The link item has to be set separately.
$oxygenMenuLink->link->get(0)->set('options', $updated_options);
// Save the "Oxygen" menu link after it was updated.
$oxygenMenuLink->save();
// Editing the "Wire rod" menu link of the 'mig' menu.
$wirerodMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'a568e500-850a-4445-9e97-bf3b20aee779']);
// Update the description of the "Wire rod" menu link.
/** @var array $wirerodMenuLinks */
$wirerodMenuLink = reset($wirerodMenuLinks)->set('description', 'Wire rod');
// The link item has to be set separately.
$wirerodMenuLink->link->get(0)->set('options', $updated_options);
// Save the "Wire rod" menu link after it was updated.
$wirerodMenuLink->save();
// Importing menus using 'safe' mode.
$this->click('#edit-import-menus-safe'); $this->click('#edit-import-menus-safe');
// Wait for the import batch to complete so the buttons are visible again. // Wait for the import batch to complete so the buttons are visible again.
$this->assertSession()->waitForElementVisible('css', '#edit-import-menus-full'); $this->assertSession()->waitForElementVisible('css', '#edit-import-menus-full');
// Deleted menu item should be imported. // "Electrode" menu link should not be deleted by a 'safe' import.
$tigGasMenuLink = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); $electrodeMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '9a49342a-45e0-4d15-a251-83d704371c79']);
$this->assertNotEmpty($tigGasMenuLink); $this->assertNotEmpty($electrodeMenuLinks);
// "MIG" and "Arc" should not be deleted during the process. // Deleted "Tig gas" menu item should be imported.
$this->assertNotEmpty($this->menuLinkStorage->loadByProperties(['uuid' => 'a568e500-850a-4445-9e97-bf3b20aee779'])); $tigGasMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']);
$this->assertNotEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '73e0f490-803c-439a-a15c-2d1c3d4ceac7'])); $this->assertNotEmpty($tigGasMenuLinks);
$tigGasMenuLink = reset($tigGasMenuLinks);
// Check whether exported menu link's options were imported correctly.
$this->assertEquals('tig-argon-gas-class1 tig-argon-gas-class2', $tigGasMenuLink->link->options['attributes']['class'][0]);
$this->assertEquals('_blank', $tigGasMenuLink->link->options['attributes']['target']);
// "Oxygen" menu link should not be updated by a safe import. // "Oxygen" menu link should not be updated by a 'safe' import.
$oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']); $oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']);
$this->assertEquals('Oxygen', reset($oxygenMenuLinks)->description->value); $oxygenMenuLink = reset($oxygenMenuLinks);
$this->assertEquals('Oxygen', $oxygenMenuLink->description->value);
// Ensure the "Oxygen" menu link still has the updated options values.
$this->assertEquals($updated_options['attributes']['class'][0], $oxygenMenuLink->link->options['attributes']['class'][0]);
$this->assertEquals($updated_options['attributes']['target'], $oxygenMenuLink->link->options['attributes']['target']);
// "Wire rod" menu link should not be updated by a 'safe' import.
$wirerodMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'a568e500-850a-4445-9e97-bf3b20aee779']);
$this->assertNotEmpty($wirerodMenuLinks);
$wirerodMenuLink = reset($wirerodMenuLinks);
$this->assertEquals('Wire rod', $wirerodMenuLink->description->value);
// Ensure the "Wire rod" menu link still has the updated options values.
$this->assertEquals($updated_options['attributes']['class'][0], $wirerodMenuLink->link->options['attributes']['class'][0]);
$this->assertEquals($updated_options['attributes']['target'], $wirerodMenuLink->link->options['attributes']['target']);
// "Arc" should not be deleted during a 'safe' import.
$this->assertNotEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '73e0f490-803c-439a-a15c-2d1c3d4ceac7']));
// "Electrode" menu link should not be deleted by a safe import. /* Test 'Full' import. */
$electrodeMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '9a49342a-45e0-4d15-a251-83d704371c79']); // Deleting the "Tig gas" menu link to prepare next import ('full').
$this->assertNotEmpty($electrodeMenuLinks); $tigGasMenuLinkBefore = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']);
$this->menuLinkStorage->delete($tigGasMenuLinkBefore);
$this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']));
// Importing menus using full mode. // Importing menus using 'full' mode.
$this->click('#edit-import-menus-full'); $this->click('#edit-import-menus-full');
// Wait for the import batch to complete so the buttons are visible again. // Wait for the import batch to complete so the buttons are visible again.
$this->assertSession()->waitForElementVisible('css', '#edit-import-menus-force'); $this->assertSession()->waitForElementVisible('css', '#edit-import-menus-force');
// "Electrode" menu link should be deleted by full import. // "Electrode" menu link should be deleted by 'full' import.
$electrodeMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '9a49342a-45e0-4d15-a251-83d704371c79']); $electrodeMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '9a49342a-45e0-4d15-a251-83d704371c79']);
$this->assertEmpty($electrodeMenuLinks); $this->assertEmpty($electrodeMenuLinks);
// "Tig gas" should be correctly imported. // "Tig gas" should be correctly imported.
$tigGasMenuLink = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']); $tigGasMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']);
$this->assertNotEmpty($tigGasMenuLink); $this->assertNotEmpty($tigGasMenuLinks);
$tigGasMenuLink = reset($tigGasMenuLinks);
// Check whether exported menu link's options were imported correctly.
$this->assertEquals('tig-argon-gas-class1 tig-argon-gas-class2', $tigGasMenuLink->link->options['attributes']['class'][0]);
$this->assertEquals('_blank', $tigGasMenuLink->link->options['attributes']['target']);
// "Oxygen" menu link should not be updated by a 'full' import.
$oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']);
$oxygenMenuLink = reset($oxygenMenuLinks);
$this->assertEquals('Oxygen', $oxygenMenuLink->description->value);
// Ensure the "Oxygen" menu link still has the updated options values.
$this->assertEquals($updated_options['attributes']['class'][0], $oxygenMenuLink->link->options['attributes']['class'][0]);
$this->assertEquals($updated_options['attributes']['target'], $oxygenMenuLink->link->options['attributes']['target']);
// "Wire rod" menu link should not be updated by a 'full' import.
$wirerodMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'a568e500-850a-4445-9e97-bf3b20aee779']);
$this->assertNotEmpty($wirerodMenuLinks);
$wirerodMenuLink = reset($wirerodMenuLinks);
$this->assertEquals('Wire rod', $wirerodMenuLink->description->value);
// Ensure the "Wire rod" menu link still has the updated options values.
$this->assertEquals($updated_options['attributes']['class'][0], $wirerodMenuLink->link->options['attributes']['class'][0]);
$this->assertEquals($updated_options['attributes']['target'], $wirerodMenuLink->link->options['attributes']['target']);
// "Arc" should be deleted during a 'full' import.
$this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '73e0f490-803c-439a-a15c-2d1c3d4ceac7']));
/* Test 'Force' import. */
// Adding an "Electrode" menu link back again for the 'force' tests.
MenuLinkContent::create([
'title' => 'Electrode',
'link' => 'https://en.wikipedia.org/wiki/Electrode',
'menu_name' => 'arc',
'uuid' => '9a49342a-45e0-4d15-a251-83d704371c79',
])->save();
// Deleting the "Tig gas" menu link to prepare next import ('force').
$tigGasMenuLinkBefore = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']);
$this->menuLinkStorage->delete($tigGasMenuLinkBefore);
$this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']));
// Adding an "Arc" menu link back again for the 'force' tests.
MenuLinkContent::create([
'title' => 'SMAW',
'link' => 'https://en.wikipedia.org/wiki/Shielded_metal_arc_welding',
'menu_name' => 'arc',
'uuid' => '73e0f490-803c-439a-a15c-2d1c3d4ceac7',
])->save();
// Importing menus using full mode. // Importing menus using 'force' mode.
$this->click('#edit-import-menus-force'); $this->click('#edit-import-menus-force');
// Wait for the import batch to complete so the buttons are visible again. // Wait for the import batch to complete so the buttons are visible again.
$this->assertSession()->waitForElementVisible('css', '#edit-import-menus-force'); $this->assertSession()->waitForElementVisible('css', '#edit-import-menus-force');
// "Oxygen" menu link should be updated by a force import. // "Electrode" menu link should be deleted by 'force' import.
$electrodeMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '9a49342a-45e0-4d15-a251-83d704371c79']);
$this->assertEmpty($electrodeMenuLinks);
// "Tig gas" should be correctly imported.
$tigGasMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4']);
$this->assertNotEmpty($tigGasMenuLinks);
$tigGasMenuLink = reset($tigGasMenuLinks);
// Check whether exported menu link's options were imported correctly.
$this->assertEquals('tig-argon-gas-class1 tig-argon-gas-class2', $tigGasMenuLink->link->options['attributes']['class'][0]);
$this->assertEquals('_blank', $tigGasMenuLink->link->options['attributes']['target']);
// "Oxygen" menu link should be updated by a 'force' import.
$oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']); $oxygenMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac']);
$this->assertEquals('Oxygen gas', reset($oxygenMenuLinks)->description->value); $oxygenMenuLink = reset($oxygenMenuLinks);
$this->assertEquals('Oxygen gas', $oxygenMenuLink->description->value);
// Ensure the "Oxygen" menu link options were reset to their initial values.
$this->assertEquals('tig-oxygen-gas-class1 tig-oxygen-gas-class2', $oxygenMenuLink->link->options['attributes']['class'][0]);
$this->assertEquals('_self', $oxygenMenuLink->link->options['attributes']['target']);
// "Wire rod" menu link should be updated by a 'force' import.
$wirerodMenuLinks = $this->menuLinkStorage->loadByProperties(['uuid' => 'a568e500-850a-4445-9e97-bf3b20aee779']);
$this->assertNotEmpty($wirerodMenuLinks);
$wirerodMenuLink = reset($wirerodMenuLinks);
$this->assertEquals('Machine-fed wire rod', $wirerodMenuLink->description->value);
// Ensure "Wire rod" menu link options were reset to their initial values.
$this->assertEmpty($wirerodMenuLink->link->options);
// "Arc" should be deleted during a 'force' import.
$this->assertEmpty($this->menuLinkStorage->loadByProperties(['uuid' => '73e0f490-803c-439a-a15c-2d1c3d4ceac7']));
} }
} }
...@@ -73,7 +73,16 @@ public function createSetUpContent(): void { ...@@ -73,7 +73,16 @@ public function createSetUpContent(): void {
MenuLinkContent::create([ MenuLinkContent::create([
'title' => 'Argon', 'title' => 'Argon',
'description' => 'Argon gas', 'description' => 'Argon gas',
'link' => 'https://en.wikipedia.org/wiki/Argon', 'link' => [
'uri' => 'https://en.wikipedia.org/wiki/Argon',
// Set the menu link 'class' and 'target' attributes.
'options' => [
'attributes' => [
'class' => ['tig-argon-gas-class1 tig-argon-gas-class2'],
'target' => '_blank',
],
],
],
'weight' => 1, 'weight' => 1,
'menu_name' => 'tig', 'menu_name' => 'tig',
'uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4', 'uuid' => '48062c1c-132b-476e-bc3c-8511fb8896e4',
...@@ -85,7 +94,16 @@ public function createSetUpContent(): void { ...@@ -85,7 +94,16 @@ public function createSetUpContent(): void {
MenuLinkContent::create([ MenuLinkContent::create([
'title' => 'Oxygen', 'title' => 'Oxygen',
'description' => 'Oxygen gas', 'description' => 'Oxygen gas',
'link' => 'https://en.wikipedia.org/wiki/Oxygen', 'link' => [
'uri' => 'https://en.wikipedia.org/wiki/Oxygen',
// Set the menu link 'class' and 'target' attributes.
'options' => [
'attributes' => [
'class' => ['tig-oxygen-gas-class1 tig-oxygen-gas-class2'],
'target' => '_self',
],
],
],
'weight' => 2, 'weight' => 2,
'menu_name' => 'tig', 'menu_name' => 'tig',
'uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac', 'uuid' => 'd2385dc8-e70d-490f-8ec7-d95c828c98ac',
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment