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.
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.
I have created a custom Drupal7 form module , but I want to show the form to the authenticated users only . I want to create the module likewise that it could have check-box options in the people->permission section from where I could set the permission for this module for all type of users .Here is the menu_hook
function form_example_menu() {
$items = array();
$items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_example_form'), //put the name of the form here
'access arguments' => array('access administration pages'),
'access callback' => TRUE
);
return $items;
}
I am new in drupal so any help regarding this would be appreciable.If someone could write down the hook_permission rather than giving examples then it would be a great help.
Here is the implementation of hook_permission
/**
* Implements hook_permission().
*/
function form_example_permission() {
return array(
'administer your module' => array(
'title' => t('Administer permission for your module'),
'description' => t('Some description that would appear on the permission page..'),
),
);
}
And you have to give the key of the returned array (administer your module) to access arguments in the implementation of hook_menu
So, your hook_menu implementation would become:
function form_example_menu() {
$items = array();
$items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_example_form'), //put the name of the form here
'access arguments' => array('administer your module'),
);
return $items;
}
Note that you'll have to flush the cache after you change anything in hook_menu. You can do it from admin/config/development/performace/
Try this. After adding this clear your cache and go to the people->permission and from there you could set permission for users .
function form_example_permission() {
return array(
'administer my module' => array(
'title' => t('Administer my module'),
'description' => t('Perform administration tasks for my module.'),
),
);
}
I'm new for Drupal , so I want to create a simple module that custom user profile, specically I want insert some tabs for user profile (the traditional user profile have 2 tabs "View tab" and "Edit Tab") so now I want add more 3 tabs.
Show History
Share profile
Shortcuts
but it only apply for user authenticated not admin profile (when admin show his profile ,the profile is traditional profile).
So it my code (my module's name is :tung_tab) and here is code of tung_tab.module:
function tung_tab_menu()
{
$items = array();
global $user;
//if (!user_access('administer'))
if($user->uid!=1)
{
$items['user/%/rvhistory'] = array(
'title' => 'Review History ',
'description' => 'Review History',
//'page callback' => 'drupal_get_form',
//'page arguments' => array('tung_tab_confirm_reviewh', 1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
$items['user/%/sharehistory'] = array(
'title' => 'Share History',
'description' => 'Share History',
//'page callback' => 'drupal_get_form',
//'page arguments' => array('tung_tab_confirm_shareh', 1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
$items['user/%/sc'] = array(
'title' => 'Shortcut',
'description' => 'Shortcut',
//'page callback' => 'getDescription',
//'page arguments' => array('tung_tab_confirm_shareh', 1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
It work properlly for admin tab, but when I log out and then login with other user
but not thing is changed, so if I comment the line //if($user->uid), everthing is ok
so when I add more code in this line
'title' => 'Review History '.$user->uid,
When I'm admin It show that Review History 1
When I loged in with another account , everything is the same , Review History 1
I can't understand why? I think my code get some mistake
I put my module in "mysite/sites/default/modules/"
You shouldn't define the menu items inside the if condition. You should control the access to the menu callbacks using access callback and access arguments instead.
WRONG:
function tung_tab_menu()
{
$items = array();
global $user;
if($user->uid!=1)
{
// defined menu items
}
return $items;
}
CORRECT:
function tung_tab_menu()
{
$items = array();
global $user;
// define the menu items here...
return $items;
}
Try to fix that then see if you still get the same results.
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
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.