Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for advanced menu item properties to Navigation screen #25062

Merged
merged 2 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/edit-navigation/src/store/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ function convertMenuItemToBlock( menuItem, innerBlocks = [] ) {
const attributes = {
label: menuItem.title.rendered,
url: menuItem.url,
title: menuItem.attr_title,
className: menuItem.classes.join( ' ' ),
description: menuItem.description,
rel: menuItem.xfn.join( ' ' ),
};

return createBlock( 'core/navigation-link', attributes, innerBlocks );
Expand Down
32 changes: 32 additions & 0 deletions packages/edit-navigation/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ describe( 'saveNavigationPost', () => {
label: 'wp.org',
opensInNewTab: false,
url: 'http://wp.org',
className: '',
rel: '',
description: '',
title: '',
},
clientId: 'navigation-link-block-client-id-1',
innerBlocks: [],
Expand All @@ -255,6 +259,10 @@ describe( 'saveNavigationPost', () => {
label: 'wp.com',
opensInNewTab: false,
url: 'http://wp.com',
className: '',
rel: '',
description: '',
title: '',
},
clientId: 'navigation-link-block-client-id-2',
innerBlocks: [],
Expand All @@ -278,6 +286,10 @@ describe( 'saveNavigationPost', () => {
url: 'http://wp.com',
menu_order: 1,
menus: [ 1 ],
classes: [],
xfn: [],
description: '',
attr_title: '',
},
{
id: 101,
Expand All @@ -288,6 +300,10 @@ describe( 'saveNavigationPost', () => {
url: 'http://wp.org',
menu_order: 2,
menus: [ 1 ],
classes: [],
xfn: [],
description: '',
attr_title: '',
},
];

Expand Down Expand Up @@ -358,6 +374,10 @@ describe( 'saveNavigationPost', () => {
label: 'wp.org',
opensInNewTab: false,
url: 'http://wp.org',
className: '',
rel: '',
description: '',
title: '',
},
clientId: 'navigation-link-block-client-id-1',
innerBlocks: [],
Expand All @@ -369,6 +389,10 @@ describe( 'saveNavigationPost', () => {
label: 'wp.com',
opensInNewTab: false,
url: 'http://wp.com',
className: '',
rel: '',
description: '',
title: '',
},
clientId: 'navigation-link-block-client-id-2',
innerBlocks: [],
Expand All @@ -392,6 +416,10 @@ describe( 'saveNavigationPost', () => {
url: 'http://wp.com',
menu_order: 1,
menus: [ 1 ],
classes: [],
xfn: [],
description: '',
attr_title: '',
},
{
id: 101,
Expand All @@ -402,6 +430,10 @@ describe( 'saveNavigationPost', () => {
url: 'http://wp.org',
menu_order: 2,
menus: [ 1 ],
classes: [],
xfn: [],
description: '',
attr_title: '',
},
];

Expand Down
16 changes: 16 additions & 0 deletions packages/edit-navigation/src/store/test/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ describe( 'getNavigationPostForMenu', () => {
menu_order: 1,
menus: [ 1 ],
parent: 0,
classes: [ 'menu', 'classes' ],
xfn: [ 'nofollow' ],
description: 'description',
attr_title: 'link title',
},
{
id: 101,
Expand All @@ -87,6 +91,10 @@ describe( 'getNavigationPostForMenu', () => {
menu_order: 2,
menus: [ 1 ],
parent: 0,
classes: [],
xfn: [],
description: '',
attr_title: '',
},
];

Expand All @@ -113,6 +121,10 @@ describe( 'getNavigationPostForMenu', () => {
attributes: {
label: 'wp.com',
url: 'http://wp.com',
className: 'menu classes',
rel: 'nofollow',
description: 'description',
title: 'link title',
},
clientId: 'client-id-0',
innerBlocks: [],
Expand All @@ -122,6 +134,10 @@ describe( 'getNavigationPostForMenu', () => {
attributes: {
label: 'wp.org',
url: 'http://wp.org',
className: '',
rel: '',
description: '',
title: '',
},
clientId: 'client-id-1',
innerBlocks: [],
Expand Down
12 changes: 8 additions & 4 deletions packages/edit-navigation/src/store/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ describe( 'computeCustomizedAttribute', () => {
label: 'wp.org',
opensInNewTab: false,
url: 'http://wp.org',
className: 'block classnames',
rel: 'external',
},
clientId: 'navigation-link-block-client-id-1',
innerBlocks: [],
Expand All @@ -227,6 +229,8 @@ describe( 'computeCustomizedAttribute', () => {
label: 'wp.com',
opensInNewTab: false,
url: 'http://wp.com',
className: '',
rel: '',
},
clientId: 'navigation-link-block-client-id-2',
innerBlocks: [],
Expand Down Expand Up @@ -271,7 +275,7 @@ describe( 'computeCustomizedAttribute', () => {
).toEqual( {
'nav_menu_item[100]': {
_invalid: false,
classes: '',
classes: [ 'block', 'classnames' ],
id: 100,
menu_item_parent: 0,
menu_order: 1,
Expand All @@ -282,11 +286,11 @@ describe( 'computeCustomizedAttribute', () => {
title: 'wp.org',
type: 'custom',
url: 'http://wp.org',
xfn: '',
xfn: [ 'external' ],
},
'nav_menu_item[101]': {
_invalid: false,
classes: '',
classes: [ '' ],
id: 101,
menu_item_parent: 0,
menu_order: 2,
Expand All @@ -297,7 +301,7 @@ describe( 'computeCustomizedAttribute', () => {
title: 'wp.com',
type: 'custom',
url: 'http://wp.com',
xfn: '',
xfn: [ '' ],
},
} );
} );
Expand Down
6 changes: 4 additions & 2 deletions packages/edit-navigation/src/store/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ export function computeCustomizedAttribute(
title: block.attributes?.label,
original_title: '',
url: block.attributes.url,
description: block.attributes.description,
xfn: block.attributes.rel.split( ' ' ),
classes: block.attributes.className.split( ' ' ),
Comment on lines +158 to +159
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This throws an error when rel or className is undefined.

  1. Add a new link to the Navigation screen.
  2. Click Save.
  3. Note the There was an error. message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops, I'll fix it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here: #25229

attr_title: block.attributes.title,
};
} else {
attributes = {
Expand All @@ -166,8 +170,6 @@ export function computeCustomizedAttribute(
...menuItem,
...attributes,
position,
classes: ( menuItem.classes || [] ).join( ' ' ),
xfn: ( menuItem.xfn || [] ).join( ' ' ),
nav_menu_term_id: menuId,
menu_item_parent: parentId,
status: 'publish',
Expand Down