Adding a tab to the node edit form in Drupal - drupal

I have added a tab to the node edit form in Drupal 7 like this (code snippet from hook_menu implementation, irrelevant lines removed):
'node/%/products' => array(
'title' => t('Products'),
'page callback' => 'some_function',
'page arguments' => array(
1
),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK
)
The tab shows up and works, however, the page is shown in the site's default theme, not in the admin theme. Also the other tabs are missing from the page that is displayed.
I tried fixing this by including this inside an implementation of hook_admin_paths, but it didn't make a difference:
return array(
'node/%/products' => TRUE,
);
How can I enforce my page to show in the admin theme and display the other tabs for the node edit form (such as "Edit", "Revisions" etc.) ?
EDIT: The box Use the administration theme when editing or creating content at admin/appearance is ticked, and system-defined pages such as node/%/edit display in the admin theme, but my new page does not.

I found out what I was doing wrong. In hook_menu % is used to denote an argument; in hook_admin_paths, these have to be replaced by asterisks. The following change to my implementation of hook_admin_paths solved it:
return array(
'node/*/products' => TRUE,
);

On /admin/appearance page, at the bottom of the page where you set the administration menu, check the value of the tickbox "Use the administration theme when editing or creating content".

I had the same problem and I solved with the following hook_menu :
<?php
function <mymodulename>_menu() {
$items = array();
$output['node/%node/mypath'] = array(
'title' => t('Title'),
'type' => MENU_LOCAL_TASK,
'page arguments' => array('node', 1),
'page callback' => 'callback_function',
'theme callback' => 'variable_get',
'theme arguments' => array('admin_theme'),
)
}
function callback_function() {
return 'My New Page.';
}
I think you need to have %node inside the path because that's the right way to "auto load" the node (Drupal take care of this) and pass it as an argument to callback_function where you can use the loaded node.

Related

Custom Drupal 6 Module with multiple pages

I am trying to write my first Drupal custom module (using drupal 6). I got the basic module working but I would like to add another page to the project. For example, right now my module's path looks like this: www.mysite.com/my_custom_module. I would like to add another page like this: www.mysite.com/my_custom_module/my_sub_page. I've tried adding a new .module and .info file but this does not work. I've also tried adding new items to the menu hook, like this:
my_custom_module.module:
function my_custom_module_menu(){
$items = array();
$items['my_custom_module'] = array(
'title' => "My Custom Module",
'page callback' => "my_custom_module_info",
'access callback' => true,
'type' => MENU_NORMAL_ITEM,
);
$items['my_custom_module/my_sub_page'] = array(
'title' => "My Sub Page",
'page callback' => "my_custom_module_sub_page_info",
'access callback' => true,
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function my_custom_module_info(){
$result = 'My Page URL was hit';
return $result;
}
function my_custom_module_sub_page_info(){
$result = 'My Sub Page URL was hit';
return $result;
}
In this example, if I go to .../my_custom_module it works fine. But, if I go to .../my_custom_module/my_sub_page, it still load, and displays my_custom_module. When I debug with a break point in each function, only my_custom_module_info is hit. Not the sub page. What am I doing wrong? I this even the correct way to create multi pages in a module? Just an FYI, each of these pages will have different audiences. The first page is to allow a user to submit some form data. The second is to allow an elevated user view the data.
Thanks
jason

Drupal create form for admins to change specific content

I'm not really sure what direction to go in.
I have a box on multiple pages that displays the status of various items. I want it easily update-able and would prefer it to be updated via a module. I'm not sure what direction to go in and just need a gentle push.
I have created a table in the drupal sql db but not sure how I would go about creating an admin tool in the drupal control panel to make changes to it.
Does anyone have any ideas of how I should go about this?
p.s. I'm using drupal 7
Custom admin pages can be defined as follows:
function mymodule_menu() {
$items['admin/def'] = array(
'page callback' => 'mymodule_abc_view',
'page arguments' => array(1, 'foo'),
'access arguments' => array('administer nodes'),
'title' => 'Foo settings',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
Where mymodule is the name of your module and mymodule_abc_view is the function that returns the markup for your admin page

How to hook_load a CCK custom content type in a MENU_CALLBACK drupal

This is for the DRUPAL ninjas:
I am creating a menu_hook for my custom module called testmodule;
I want to call a MENU_CALLBACK and return a loaded CCK object of a custom content type I created called "VideoNodes"
example:
$items['save_video_data/%my_custom_cck_type/%'] = array(
'type' => MENU_CALLBACK,
'page callback' => 'save_data_to_db',
'access arguments' => array('Save Data to my_custom_cck_type'),
'page arguments' => array(2),
'type' => MENU_CALLBACK,
'title' => 'Save a Data!'
);
Now, with the above code, my understanding is that I am telling drupal to load my_custom_cck_type and use the array(2) position (my argument) as the id field of the node to find it in the database.
I also understand that, I am going to need to create a my_custom_cck_type_load() function.
QUESTION:
What shoudl be in my_custom_cck_type_load()?? How shall I grab all of the custom fields associated with the CCK type into the returned object? Or does drupal do this for me?
I'll use node_load as an example for this. Out of the box Drupal's menu system will load nodes at paths like.
node/1/view
node/2/view
node/3/view
(note: the "/view" part of the path is often hidden because it's the default tab)
The menu item setup to do this looks something like:
$items['node/%node/view'] = array(
'type' => MENU_DEFAULT_LOCAL_TASK,
'title' => 'blabla',
'page callback' => 'node_view',
'page arguments' => array(1)
/* more stuff */
);
In the real path the %node is replaced by a number, the NID of the node. This will cause menu to fire node_load using that number as the argument. We're also using %node as the page argument for the node_view callback. The argument that callback receives will be the fully loaded node object.

Drupal 7: How to Create a Menu/Route Item That Doesn't Appear in the Site Navigation

How can I create a new route/menu in Drupal that doesn't automatically render a navigation link?
I'm trying to create a simple page callback in Drupal that doesn't show up in the Navigation menu.
I have a module named helloworld.
The .module file contains the following
function _helloword_page_callback()
{
return array('#markup'=>'The quick brown fox says hello, and the lazy dogs thanks you
for following conventions.');
}
function helloworld_menu()
{
$items['helloworld'] = array(
'title' => 'Hello World',
'page callback' => '_helloword_page_callback',
'access arguments' => array('content'),
'type' => MENU_CALLBACK
);
return $items;
}
This successfully exposes a URL on the site of
http://example.drupal.com/helloworld
However, I'm still getting a link in the left hand (Bartik) navigation menu, despite the use of
'type' => MENU_CALLBACK
So, why isn't this working? Am I configuring the Menu item correctly? A more likely question: How am I misinterpreting the use of the menu type constants/system? Are there additional caches to clear that
drush cc all
wouldn't take care of? What other steps can I take to debug this?
There must be something else wrong (perhaps you forgot to clear the caches ?) because even with Bartik, it works as expected. In that example, only "Hello 2" is shown in the navigation:
function helloworld_menu(){
return array(
'hello1' => array(
'title' => 'Hello 1',
'page callback' => 'helloworld_page_callback',
'access arguments' => array('content'),
'type' => MENU_CALLBACK
),
'hello2' => array(
'title' => 'Hello 2',
'page callback' => 'helloworld_page_callback',
'access arguments' => array('content')
)
);
}
function helloworld_page_callback(){
return array('#markup'=>'The quick brown fox says hello, and the lazy dogs thanks you for following conventions.');
}
By the way, there is a typo in your snipplet (helloroute_menu should be named helloworld_menu), but I assume this is due to code simplification before posting on StackOverflow.
Check out that menu link in the menu administration. If you customized it there (weight change for example), it's possible that it remains even though you set to type to callback.
If that's the case, you can just delete there.

Drupal 6: how to display node with its local tasks tabs in menu item

In my case each user has a gallery. Gallery is a node. I'd like to hide default "Create content" menu and add custom menu link that links to user gallery.
function custom_menu() {
$items = array();
$items['galleries/editgallery'] = array(
'title' => 'Edit gallery',
'description' => 'edit gallery',
'page callback' => 'custom_edit_gallery',
'access callback' => 'custom_access_editgallery',
);
return $items;
}
function custom_edit_gallery (){
global $user;
$node = node_load ($user->gallerynid);
return node_page_view ($node);
}
But it doesn't show local tasks tabs(like "Edit" tab).
You would need to add them yourself.
With normal theming, you could create a custom template file or overwrite a theme function etc to add the tabs you want.
You could also do this within hook_menu, by using MENU_LOCAL_TASK and MENU_DEFAULT_LOCAL_TASK, see the api.

Resources