Skip to content

Commit

Permalink
WIP: Dynamic Navigation Link URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Aug 18, 2020
1 parent 685a77a commit a057ca4
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 22 deletions.
2 changes: 2 additions & 0 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ function NavigationLinkEdit( {
title: newTitle = '',
url: newURL = '',
opensInNewTab: newOpensInNewTab,
type,
id,
} = {} ) =>
setAttributes( {
Expand Down Expand Up @@ -265,6 +266,7 @@ function NavigationLinkEdit( {
return escape( normalizedURL );
} )(),
opensInNewTab: newOpensInNewTab,
type,
id,
} )
}
Expand Down
18 changes: 15 additions & 3 deletions packages/block-library/src/navigation-link/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,27 @@ function render_block_core_navigation_link( $attributes, $content, $block ) {
'<a class="wp-block-navigation-link__content" ';

// Start appending HTML attributes to anchor tag.
if ( isset( $attributes['url'] ) ) {
$html .= ' href="' . esc_url( $attributes['url'] ) . '"';

if ( isset( $attributes['type'] ) && isset( $attributes['id'] ) ) {
if ( 'post_type' === $attributes['type'] ) {
$url = get_permalink( $attributes['id'] );
} elseif ( 'taxonomy' === $attributes['type'] ) {
$url = get_term_link( $attributes['id'] );
}
}

if ( ! isset( $url ) && isset( $attributes['url'] ) ) {
$url = $attributes['url'];
}

if ( isset( $url ) ) {
$html .= ' href="' . esc_url( $url ) . '"';
}

if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
$html .= ' target="_blank" ';
}

// Start appending HTML attributes to anchor tag.
if ( isset( $attributes['rel'] ) ) {
$html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
} elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
Expand Down
73 changes: 54 additions & 19 deletions packages/block-library/src/navigation/placeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/

import { escape } from 'lodash';
import { escape, some } from 'lodash';
import classnames from 'classnames';

/**
Expand Down Expand Up @@ -78,29 +78,64 @@ function getSelectedMenu( selectedCreateOption ) {
/**
* A recursive function that maps menu item nodes to blocks.
*
* @param {Object[]} nodes An array of menu items.
*
* @param {Object[]} menuItems An array of menu items.
* @return {WPBlock[]} An array of blocks.
*/
function mapMenuItemsToBlocks( nodes ) {
return nodes.map( ( { title, type, link: url, id, children } ) => {
const innerBlocks =
children && children.length ? mapMenuItemsToBlocks( children ) : [];

return createBlock(
'core/navigation-link',
{
type,
id,
url,
function mapMenuItemsToBlocks( menuItems ) {
return menuItems.map(
( {
title,
type,
description,
xfn,
object_id: objectId,
target,
url,
classes,
children,
} ) => {
const attributes = {
label: ! title.rendered
? __( '(no title)' )
: escape( title.rendered ),
opensInNewTab: false,
},
innerBlocks
);
} );
opensInNewTab: target === '_blank',
};

if ( type ) {
attributes.type = type;
}

if ( description ) {
attributes.description = description;
}

if ( xfn?.length && some( xfn ) ) {
attributes.rel = xfn.join( ' ' );
}

if ( objectId ) {
attributes.id = objectId;
}

if ( classes?.length && some( classes ) ) {
attributes.className = classes.join( ' ' );
}

if ( url ) {
attributes.url = url;
}

const innerBlocks = children?.length
? mapMenuItemsToBlocks( children )
: [];

return createBlock(
'core/navigation-link',
attributes,
innerBlocks
);
}
);
}

/**
Expand Down

0 comments on commit a057ca4

Please sign in to comment.