Posted by Imaaxa-Cory on December 12, 2012 at 3:10am
I have a page that is created by views using contextual filters to filter down the page results.
The contextual filter is the nid of a content type.
I created a view to create a menu on the page that just links back to the page with the context argument added to the end. Example: resource/44
Client added the requirement later for drop-down menus. So I am using Nice Menus for this.
To try to make it easy for the client to update the drop-down menu, the menu cache will be flushed upon the creation of the content type mentioned above.
Here is the code I am using:
/**
* Implementation of hook_menu()
*/
function mymodule_menu(){
// get the content type data from the DB
$result = cm_get_category_nodes();
// loop through the $result
// and create a menu item with the data
foreach ($result as $key => $value) {
$items['resource/' . $key] = array(
'title' => $value,
'description' => 'Resources',
'page callback' => 'views_page',
'page arguments' => array('resources', 'page_1',),
'access_callback' => 'views_access',
'access arguments' => array('administer filters'),
'weight' => 0,
'menu_name' => 'main-menu',
'tab_root' => 'resource',
);
}
return $items;
}The problem is now the views page is not filtering with the nid from the end of the url. Can anyone see what I am missing?
Comments
Why is Views not filtering Update
I just noticed that if I add the content type id again as another argument, the view filters on the argument.
Example: resource/44 <-does not work. resource/44/44 <-does work.
Is there something I am not adding to the menu $items array causing views to not filter on the first argument instance?
New discovery
During testing, I disabled the module, flushed the caches. The links that the module created remained in the database, and views is now filtering the contextual argument properly.
I made a copy of the menu-cache with it working correctly. Enabled the module again, flushed the caches, tested that views once again is not filtering the contextual arguments. Made a copy of the menu-cache again.
I compared the two cache entries and only found some extra closing curly brackets }.
Does anyone know if there is a way to add the menu links to the menu_links table without using hook_menu()?
I am also open to other ideas.
Solution found
Made change to above function:
Changed the 'page callback' => 'views_page'; to 'page callback' => 'mymodule_page',
/**
* Implementation of hook_menu()
*/
function mymodule_menu(){
// get the content type data from the DB
$result = cm_get_category_nodes();
// loop through the $result
// and create a menu item with the data
foreach ($result as $key => $value) {
$items['resource/' . $key] = array(
'title' => $value,
'description' => 'Resources',
'page callback' => 'mymodule_page',
'page arguments' => array('resources', 'page_1',),
'access_callback' => 'views_access',
'access arguments' => array('administer filters'),
'weight' => 0,
'menu_name' => 'main-menu',
'tab_root' => 'resource',
);
}
return $items;
}
Then copied the views_page function from views.module, renamed it with mymodule name and added some php:
function mymodule_page($name, $display_id) {
// test to see if this is the right views page processor
if($name == 'resources'){
$args = func_get_args();
// Remove $name and $display_id from the arguments.
array_shift($args);
array_shift($args);
// if the $args are empty add the missing argument
if(!$args){
$marg = explode('/', current_path());
array_push($args, $marg[1]);
}
// Load the view and render it.
if ($view = views_get_view($name)) {
return $view->execute_display($display_id, $args);
}
// Fallback; if we get here no view was found or handler was not valid.
return MENU_NOT_FOUND;
}
// send to views if sent here by mistake
views_page($name, $display_id);
}