function menu_token_query_alter in Menu Token 7
Implements hook_query_alter().
No active trail is set when using paths with tokens, as the tokens are replaced, and the correct path is not found in the menu link table. To fix this, we replace the resolved path in the the preferred menu query's conditions with menutoken one. Also replace the path that will be returned with the resolved path, to allow it to be matched with the candidates in menu_link_get_preferred().
See also
File
- ./
menu_token.module, line 223 - Main module file for the Menu Token module.
Code
function menu_token_query_alter(QueryAlterableInterface $query) {
if ($query
->hasTag('preferred_menu_links')) {
// Get all the conditions as a reference.
$conditions =& $query
->conditions();
foreach ($conditions as &$condition) {
// If the current condition is the link_path one
if (is_array($condition) && isset($condition['field']) && $condition['field'] == 'ml.link_path') {
foreach ($condition['value'] as $path_key => $path_value) {
// If the path has a corresponding menutoken path
if (!empty($path_value) && ($menutoken_path = menu_token_find_link_path($path_value))) {
// Replace the condition value with the menutoken path.
$condition['value'] = array(
$path_key => $menutoken_path,
);
// Add the resolved link path as an expression to the query. This
// will replace the field link_path from the table menu_links with
// the resolved path, allowing menu_link_get_preferred() to match it
// based on data available in that function.
$query
->addExpression("'{$path_value}'", 'link_path');
}
}
}
}
}
}