Hi

I have the need, for a menu_token replacement, that gets replaced if the node contains a specific field. If current page (this menu entry will be show on all pages) does not have a node or the node does not have the field, the token needs to be removed. Example:

menu token url:
resources/[node:field-my-specific-term:tid]

If field present replaced to:
resources/14

If field not present replaced to:
resources/%5Bnode%3Afield-my-specific-term%3Atid%5D

In the last case I would like to have it replaced to
resources/

There is an option that can be passed to token_replace called "clear". I have written a patch that adds a checkbox to the menu admin page and then utilizes the clear option on token_replace.

Please review and tell me what you think.

Comments

bitkid’s picture

Status: Active » Needs review
StatusFileSize
new5.87 KB

The patch

bitkid’s picture

Oh, by the way... I did a few other changes to the code, to have it comply with the Drupal Coding Standard. There was no actual changes to code, only to comments and empty lines.

rypit’s picture

StatusFileSize
new26.28 KB

It looks like this was added to the dev branch on the repo - seems to work great.

dealancer’s picture

Status: Needs review » Fixed

@bitkid, thanks a lot for a patch! Looks like it was committed by Fubhy.

Also I have added lines:


    $url = trim($url, '/');

To make sure user/[user:uid] is replaced to user, but not user/.

bitkid’s picture

Thanks for adding my patch.

You are absolutely right about the trailing slash.

If someone decides on a better text for the checkbox, please feel free to update it :-)

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

rohittiwari’s picture

Issue summary: View changes

Hi @dealancer,

Adding line

<?php
    $url = trim($url, '/');
?>

has resulted in truncating the trailing slash for link having menu token. So now,

https://[custom-tokens:example-domain]/arg1/arg2/ becomes https://[custom-tokens:example-domain]/arg1/arg2

I am looking into this. Requesting u to please if look into this.

Thanks,
Rohit Tiwari

rohittiwari’s picture

Hi I figured it out,

There are basically two things you have to do to override
1. Increase your module weight to be greater then Menu token module
2. And remove the above code

 <?php
    $url = trim($url, '/');
?>

to use the same.

For step 1 use in .install file

<?php
/**
* @file
* Provides install, upgrade and un-install functions for your module.
*/
/**
*  implements hook_update_N()
*
*/
function yourmodulename_update_7100($sandbox){
    // Get the weight of the menu_token module we want to compare against
    $weight = db_select('system', 's')
        ->fields('s', array('weight'))
        ->condition('name', 'menu_token', '=')
        ->execute()
        ->fetchField();

    // Set our module to a weight 1 heavier, so ours moves lower in execution order
    if(!empty($weight)){
        db_update('system')
          ->fields(array('weight' => $weight + 1))
          ->condition('name', '[your_module_name]', '=')
          ->execute();
    }
}

Then 2 step in your .module file

/**
 * Implements hook_translated_menu_link_alter().
 */
function yourmodulename_translated_menu_link_alter(&$item, $map) {
  global $menu_admin;
  if (empty($menu_admin) && isset($item['options']['menu_token_link_path'])) {
  $url = token_replace($item['options']['menu_token_link_path']);
  $url = drupal_get_normal_path($url);
  if (url_is_external($url)) {
      $item['href'] = $item['link_path'] = $url;
      return;
    }
  }
}

Thanks,
Rohit Tiwari

Drupal is awsome.