I need to do something and I just can't figure out what's the best option to do that.
In drupal, I need to add a page that displays information with a simple php script, and it should display it only to administrators.
I thought about adding a menu item like this:
$items['admin/visits_log'] = array(
'page callback' => 'visitst_log',
'access callback' => true,
'access arguments' => TRUE,
'type' => MENU_CALLBACK,
);
but it's not showing with a page, just text...
I know it's a simple question, but I just need a little direction..
You must return $output as HTML code (instead of print & exit), to be rendered by drupal theme system. That callback can be coded like this:
function visitst_log() {
$output = "<p>Hello world!</p>";
return $output;
}
Note that if you want to make the page only visible for admin users, you need to have access setup properly instead of just "true". For example:
$items['admin/visits_log'] = array(
'page callback' => 'visitst_log',
'access arguments' => array('administer nodes'),
'type' => MENU_CALLBACK,
);
(if you don't specify access callback it will use user_access() by default
Related
So I want my module to make a page first. But the link to the simple page should be user entered.
I`m doing this on Drupal 7.
I made a settings form in the _menu function.
$items['admin/config/content/settings'] = array(
'title' => 'Setings',
'description' => 'A form to change settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('settings_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
Then I added this:
function settings_form($form, &$form_state){
$form['locationUrl'] = array(
'#type' => 'textfield',
'#title' => t("URL kādu vēlaties?"),
'#description' => t("Example. about"),
'#required' => true
);
return system_settings_form($form);
}
And then i added to the _menu function:
$items[variable_get('admin/config/content/settings','locationUrl')] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => '', //page title
'page callback' => 'DGMap', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'access arguments' => array('access simple page'),
);
And i add this:
function page(){
return array('#markup' => '<h1>Hello?</h1>');
}
I save it all up. Clear cache. And go to settings and save the locationUrl as for example 'about'. And then try /drupal/about it gives me page not found exception.
Could anyone please help me with this? I hope that it is understandable what im trying to make.
Thanks for help.
The end thing i want to make is so that my module could create a page with custom JavaScript in it. If anyone could link me to a tutorial that would be great for this, i would really appreciate that. If there is a way for my module to place custom created JavaScript in the page that would be fine too.
Not sure if it's a good aproach for dynamic routing but you're getting the variable wrong for your second link in hook_menu(). In fact you make a route to YourDrupal/locationUrl instead of your example YourDrupal/about.
Use this for the second menu link (with an extra check on whether or not the setting is being saved):
if (!empty($setting = variable_get('locationUrl', ''))) {
$items[$setting] = array(
'title' => '', //page title
'page callback' => 'page', // use the same name as your pagecallback
// 'access arguments' => array('access simple page'),
'access arguments' => array('access content'), // temporarily use this
);
}
You also might be in need of a check on existing routes. Not sure what happens if the user has saved your form with a locationUrl like for example "admin" giving a link to YourDrupal/admin which already exists.
Drupal 7
function custom_menu() {
$items['node_update/%nid/%pic/%name'] = array (
'title' => 'Redirecting to your profile page',
'page callback' => custom_node_update,
'page arguments' => array(3,4,5),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
When i use http://myserver.com/node_update/51/sites%2fdefault%2fimages/picture.jpg/admin
so when im using %2f hook_menu somewhat still consider it as forward slash '/' therefore it somewhat recognised as this instead http://myserver.com/node_update/51/sites/default/images/picture.jpg/admin which is of course beyond the arguments i'm listening on hook_menu.
Anyone can help on this please. how to use url_encoded string as argument.
Your page arguments are not set correctly. Try to change it to be:
'page arguments' => array(1, 2, 3),
Then flash the website cache and try again.
I'm wondering which is the best way to display a form on a page (for example : a register form for vip users ... and not in a block but as the main content).
The user.module way in user_menu (*hook_menu*) :
$items['vip/register'] = array(
'title' => 'Create new vip account',
'page callback' => 'drupal_get_form',
'page arguments' => array('vip_register_form'),
'access callback' => 'user_register_access',
'type' => MENU_LOCAL_TASK,
);
Or by creating a theme via use_theme (*hook_theme*) (fictive) :
$items['vip/register'] = array(
'title' => 'Create new vip account',
'page callback' => 'theme',
'page arguments' => array('vip_register'),
'access callback' => 'user_register_access',
'type' => MENU_LOCAL_TASK,
);
function user_theme() {
return array(
'vip_register' => array(
)
);
}
function theme_vip_register(){
return drupal_get_form('vip_register_form');
}
I'm wondering about this for theming purpose, because a designer will do the graphic integration afterwards.
Thank you for advices.
This is not an actual answer but I'm not quite sure what is your question at first place.
Drupal customs #1: Never hack core!
As its name, theme functions are just to theme something. So you need the form built first.
$items['vip/register'] = array(
'title' => 'Create new vip account',
'page callback' => 'drupal_get_form',
'page arguments' => array('vip_register_form'),
'access callback' => 'user_register_access',
'type' => MENU_LOCAL_TASK,
);
When a user accesses example.com/vip/register page, drupal_get_form function will be called with argument vip_register_form.
So now you need to define a function to return this (vip user registration) form.
function vip_register_form($form, &$form_state){
..your FAPI stuff here.
return $form;
}
Now user who open the vip register page will see this form instead of the regular form. even password and username fields will not be available unless you add them. If you want to alter the existing form, just copy the menu hook to a new path:
$user_menu_routers = user_menu();
$items['vip/register'] = $user_menu_routers['user/register'];
Now you can alter your the form at vip/register page (which is same as normal user register page) using a form_alter hook. You can theme the form manually without affecting existing one as well.
I'm creating a module that let's your users add feeds.
So i want my code to provide tabs that can be overwritten at the theme layer.
I thought this could be done with the hook_menu:
$items['tab_add_feed'] = array(
'title' => 'Add Feed',
'page callback' => 'xml_parser_add_feed',
'access callback' => 'user_access',
'access arguments' => array('manage own feeds'),
'type' => MENU_LOCAL_TASK,
);
something like the above.
But I'm using it on the front side of the site.
How can i add tabs or links on top of my page the drupal way?
//edit
There are none tabs present at the moment, maybe i have to make them visible?
fixed it by adding add feed to the main page callback function
But this is ugly, hard coded and not theme-able. waiting for a better solution.
//edit
This is the code i am using now
function xml_parser_menu() {
$items = array();
$items['xml_parser'] = array(
'path' => 'xml_parser',
'title' => t('Feed'),
'description' => t('Add/edit feeds'),
'page callback' => 'xml_parser_manage_overview',
'access callback' => 'user_access', // get user access
'access arguments' => array('manage own feeds'), // check user if premission is set
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'primary-links', // add to primary menu
);
$items['xml_parser/add_feed'] = array(
'path' => 'xml_parser/add_feed',
'title' => 'Add Feed',
'page callback' => 'xml_parser_add_feed',
'access callback' => 'user_access',
'access arguments' => array('manage own feeds'),
//'access' => user_access('maintain own subscriptions'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
I think the name of the item would be something like 'user/%/add_feed', with the % argument being the user id. Also, the access callback is spelled incorrectly, should be user_access. This should create a tab for a user on the user profile page. You could also do node/%/add_feed to add a tab to a node.
While developing this module, you may find it useful to also use this:
function mymodule_init() {
cache_clear_all();
menu_router_build();
}
Until you get the menu straight.
If you want to just add an arbitrary tab to a page to add a feed, it would probably be an autonomous menu or a themed link. I would need to know more about the context of the feeds you are trying to provide and how users are subscribing.
You should be able to add a tab to the front page. Remember that the front page of your website is not really "/", it is "/node" by default (unless you changed it).
I'm just guessing because I haven't tried this, but your code should work if you change the key for your tab in $items to node/tab_add_feed.
If you have something else as your front page (ie. a view or panel), this would not apply.
I need to add a menu item with the following link: user/16/addresses
It is the link to a tab of user profiles. Of course 16 is the user ID and it should change according to the user.
Can I use tokens directly into menu items ? such as [uid] ?
thanks
hook_menu() does most of the work for you with this.
function example_menu() {
return array(
'user/%/addressess' => array(
'title' => 'User Addresses',
'page callback' => 'example_callback',
'page arguments' => array(1),
'weight' => 2,
'type' => MENU_LOCAL_TASK,
),
);
}
This will add the tab when you are on a user page and put the UID in the URL.
As I understand it the MENU_LOCAL_TASK works off the current URL, so you couldn't substitute another USER id into the menu with this.