Change tab names in Drupal module depending on the user Logged In. - php

Working on Intranet built on Drupal where I got a module with couple of tabs.
There are two types of users, admin and non-admins. I like to change the name of the tab depending on who is viewing it.
Currently this is what I got:
function lessons_learned_menu(){
...
$items['lessons-learned/projects/viewAll'] = array(
'title' => 'View All Projects - LL',
'description' => 'Review page that lists projects that are currently open.',
'page callback' => 'pmo_lessons_learned_projects_viewAll',
'access arguments' => array('Access pmo_lessons_learned'),
'type' => MENU_LOCAL_TASK
);
...
The access argument 'Access pmo_lessons_learned', everyone has (including Admins) and 'Administrator pmo_lessons_learned' only Admins have.
Something that didn't work:
if(!user_access("Administer pmo_lessons_learned")){
$items['lessons-learned/projects/viewAll'] = array(
'title' => 'View All Projects - LL',
'description' => 'Review page that lists projects that are currently open.',
'page callback' => 'pmo_lessons_learned_projects_viewAll',
'access arguments' => array('Access pmo_lessons_learned'),
'type' => MENU_LOCAL_TASK
);
}
else
{
$items['lessons-learned/projects/viewAll'] = array(
'title' => 'Review Projects',
...
}
When I tried to debug it, I always got the user as Admin (when it was non-Admin), therefore went to the else condition every time.
So I like to change the name of the tab if the user is not Admin and vice versa.

The hook_menu is invoked once to build and cache the menu items and is not supposed to run again until the cache is cleared. This hook is invoked by Drupal user 1 (admin), this explains why user_access("Administer pmo_lessons_learned") returns true.
However you can use the 'title callback' config in the menu items definition to generate the title with a function.

Related

Drupal permission for anonymous user

I have create module for uploading files into database, and only administrator can upload that files. So I have hook_permission for administer to upload files:
function upload_permission() {
return array(
'administer uploader' => array(
'title' => t('Administer Uploader'),
'description' => t('Allow the following roles to upload files files to the server.'),
),
);
}
Also I create several custom nodes with path files/node/% and now I need permission for anonymous users to see page with custom nodes. Below I add this permission:
'access files/node/%' => array(
'title' => t('Access Files'),
'description' => t('Access Files.'),
),
and still don't work. Is there any other solution how anonymous user can view the page with custom nodes ?
As far I know, just check the permission "view published content" in the CMS permission page that should be checked for the anonymous user role. For viewing a Drupal node no specific permission needed until you are using any individual node permission settings. Also, for your custom node path please use the below settings array in your hook_menu to make all path works with the URL 'files/node/%'.
/**
* Implements hook_menu().
*/
function yourmodule_menu() {
$items = array();
$items['files/node/%'] = array(
'title' => 'Files node',
'page callback' => '_yourmodule_page_callback',
'page arguments' => array(2),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
Just notice the below line of code, this say that anyone with the permission 'access content' (View published content) can see these node.
'access arguments' => array('access content'),
Hope this will help you!

Assist with Drupal Admin Blank page for custom functions or code

I am trying to learn Drupal. What I want to do is create a backend page (that is on main menu) where I can run my own functions and code. I have been doing research and found out that to do this I need to create a module. And if I run the "hook_menu" function - i can get that backend page to be on the menu. I found code for a drupal module that does this, and it loads a form for a "config settings" page. Here is the code:
function add_game_menu() {
$items = array();
$items['admin/add_game'] = array(
'title' => 'Add Gm Pg',
'description' => 'Description of your add game page',
'page callback' => 'drupal_get_form',
'page arguments' => array('add_game_admin'),
'access arguments' => array('administer add_game settings'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function add_game_admin() {
$form = array();
$form['add_game_maxdisp'] = array(
'#type' => 'textfield',
'#title' => t('Maximum number of links'),
'#default_value' => variable_get('add_game_maxdisp', 3),
'#size' => 2,
'#maxlength' => 2,
'#description' => t("The maximum number of links to display in the block."),
'#required' => TRUE,
);
return system_settings_form($form);
}
I modified it a little but it does work. What I want to do is do this but run my own functions and code on here and not the "drupal_get_form" function.
I tried to do this and just created a function to echo text and then put the function name in the "page callback" field of the array. This did work, it did execute my function on the page instead of the drupal form function, but the page was a blank white page with none of the "drupal backend styling or menus or anything"; it was literally just completely blank white webpage with just my text printed on it.
So I am thinking the "drupal_get_form" function not only puts a form on the page, but it also makes it so it is drupal backend page with proper header, footer, menus etc.
So I am thinking that i need a function like "drupal_get_form" but it has a "blank slate" where I can run whatever code or functions that I want.
Would anybody know anything about this or how to approach doing this?
Thanks so much...
Everything you need is to create a template, register it and use theme function in your code. You can take a look at this Quick Introduction to Drupal's hook_menu() and hook_theme()
So your code might look like:
function add_game_menu() {
$items = array();
$items['admin/add_game'] = array(
'title' => 'Add Gm Pg',
'description' => 'Description of your add game page',
'page callback' => 'my_function',
'access arguments' => array('administer add_game settings'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function my_function(){
// Call theme() function, so that Drupal includes the custom-page.tpl.php template
return theme('my_custom_template');
}
/*
* Implementation of hook_theme().
*/
function add_game_theme(){
return array(
'my_custom_template' => array(
// template file name will be custom-page.tpl.php
'template' => 'custom-page',
),
);
}

Adding new custom Permissions in Drupal tutorial

I created module now i want to set permission on that module so some of the users can see that field. I searched on google and stackoverflow but I didn't get proper answer as i needed.
My code as below
function downloaded_menu() {
$items['user/%user/downloaded_poems'] = array(
'title' => 'Downloaded Poems',
'page callback' => 'downloaded_content_page',
'access arguments' => array('poet downloaded work'),
'type' => MENU_LOCAL_TASK,
'weight' => 11,
);
return $items;
}
Now I want to give permission to particular user. who can only see.
You will have to use hook_permission to do so.
Code example:
function downloaded_permission()
{
return array(
'poet downloaded work' => array(
'title' => t('poet downloaded work'), // the title to be shown in the permissions page
'description' => t('poet downloaded work'), // the description to be shown in the permissions page
'restrict access' => FALSE,
),
);
}
Then go to the permissions page and give the permissions to the required roles.
Hope this helps... Muhammad.

Drupal get parameters from url in edit form and populate the form

i have an employee table with fields like id, name, age and salary.
Iam showing the list of employee names in my custom module and when i click on an employee name, i have to show the edit form of that employee.
The employee names are given a link, as like:
<a href='".$base_url."/my_module/edit/".$employee->id."'>
and the corresponding menu path is configured as:
$items['my_module/edit/%'] = array(
'title' => t('My form'),
'description' => t('Edit employee'),
'page callback' => 'my_module_edit',
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
And when i click on the 5th employee name, the url would be: ..../my_module/edit/5
So now my questions are:
How can i get the id (5) of the employee from the url in my my_module_edit function ?
How can i show the edit form of the employee, populating the fields (by querying with id) ?
For question 2: You have got it mixed up a bit, you don't create a custom 'page callback' menu item for forms, you should use 'drupal_get_form' as the 'page callback' with the name of your form() function (my_module_employee_form) as the 'page arguments'.
function my_module_menu() {
$items['my_module/employee/edit/%'] = array(
'title' => t('Edit employee'),
'page callback' => 'drupal_get_form',
'page arguments' => array('my_module_employee_form', 3),
'access arguments' => array('administer my_module'),
'type' => MENU_CALLBACK,
);
}
function my_module_employee_form() {
... read database and generate form..
}
function my_module_employee_form_submit($form_id, &$form) {
... read form and update database ...
}
Then in the function my_module_employee_form() you generate the form and populate it, and in my_module_employee_form_submit() you read the values back and store them in the database.
$request_parts = explode('/', $_SERVER['REQUEST_URI');
$employee_id = intval(array_pop($request_parts));
That's for your question #1. Not sure I understand #2
For your first question, you can get the arguments passed to a callback by using arg(0), arg(1), arg(2) etc...
<a href='".$base_url."/my_module/edit/".$employee->id."'>
For this href arg(0) == 'my_module', arg(1) == 'edit', arg(2) == '5', take a look at arg()'s documentation.
You can also parse the query string your self by accessing the $_GET['q'] variable.
For your second question, if your employee table is a Drupal table you can just make the link be the edit link to the Drupal node, like href="node/5/edit". If it is a custom table you will have to create your own form by implementing hook_form()

Drupal Module Development hook_menu() For Semi Static Pages

I have a page that is definitely not a form but I need to use some callback functions to load data from an external source and display (e.g. a list of buildings on campus and their accessibility information).
What I have a need for is a landing listing page (lists all the buildings) and a 'view individual building' page. Also, I have a page where you punch in your student ID and view information on testing procedures. And finally I have a page that is basically a form (which I have done before successfully in the past).
Now, I HAD the building list working, however I made a small change and it stopped working!
Currently my hook_menu() function looks as below:
<?php
/**
* Implementation of hook_menu()
*/
function disability_menu()
{
$items = array();
// Ignore me, shell
$items['quickreg'] = array(
'title' => 'Quick Registration',
'description' => t(''),
'page callback' => 'drupal_get_form',
'page arguments' => array(),
'file' => 'disability.quickreg.view.inc',
'access arguments' => array('access quick registration system'),
'type' => MENU_SUGGESTED_ITEM,
);
$items['tests/status'] = array(
'title' => 'Test Status Results',
'description' => t('Check on the status of your tests'),
'page callback' => 'disability_view_testing_status',
'page arguments' => array(),
'file' => 'disability.tests.view.inc',
'access arguments' => array('access test check information'),
'type' => MENU_CALLBACK,
);
$items['tests'] = array(
'title' => 'Testing Services',
'description' => t('Check on the status of your tests'),
'page callback' => 'disability_view_testing',
'page arguments' => array(),
'file' => 'disability.tests.view.inc',
'access arguments' => array('access test check information'),
'type' => MENU_SUGGESTED_ITEM,
);
$items['access/%building'] = array(
'title' => 'Campus Accessibility Guide',
'description' => t('A summary list of detailed accessibliity information about each building on the A&M campus'),
'page callback' => 'disability_view_access',
'page arguments' => array(1),
'file' => 'disability.access.view.inc',
'access arguments' => array('access building access information'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
Before some change I must have made the menu item for "Campus Accessibility Guide" would show up properly (after being enabled of course). The /access url would work correctly displaying a list of all building and the /access/12345 would correctly display the single record of ID# 12345.
Now the access/%building menu entry is not even showing up and even sending the url /access into a redirect loop (making me think it's passing in something for the ID which sends it into the view specific function that redirects to /access when the ID doesn't exist).
Can anyone tell me what I'm doing wrong or what I need to do to support 2 themed pages: a /access and /access/%building url pattern?
You should only use %name instead of % in urls when you have a function that you want to act on the url. Drupal does this all over the place with user and node, and this is very smart, as you only one place need to have the code to load an user or a node, but it get used in a lot of places. In this case I bet it's a bit overkill to make a function to load the building. On the other hand the advantage is that doing it that way, you get the 404 handling, if no object can be found. The best solution really comes down to how you want to handle buildings that does not exist. You could even make your 'Campus Accessibility Guide' function handle the 404 which would make the two options more or less equal. I would go for whatever is easiest for you to make.

Categories