Drupal problem, how to create a fast content module? - drupal

Hi I need to create a module in drupal to display some data, not being a drupal developer and after reading a couple of tutorials, i cant seem to display anything.
I have the next code:
<?php
function helloworld_perm() {
return array('access helloworld content');
}
function helloworld_listado(){
return "yea";
}
function helloworld_menu(){
$items = array();
$items["listado"] = array(
'title' => t('Listado de empresas'),
'callback' => 'helloworld_listado',
'access' => array('access helloworld content'),
'type' => MENU_NORMAL_ITEM
);
return $items;
}
When i enter /listado i get a Access denied
You are not authorized to access this page.
Any idea what im doing wrong?
If i go to the admin->module->permissions, i have checked the permision for all roles to access hellowold content.
Ty!

From the way that your menu array is structured in helloworld_menu(), I'm assuming that this is Drupal 6. If so, you need to rename 'access' to 'access arguments'. See http://api.drupal.org/api/function/hook_menu/6.
The Drupal API documentation also includes a heavily-commented page_example.module that's doing basically what you're doing here, which you might want to check out: http://api.drupal.org/api/file/developer/examples/page_example.module/6/source
Hope that helps!
Oh. And don't forget to clear your cache afterwards from the "Clear cache" button on Administer >> Site configuration >> Performance.

=> t('Listado de empresas'),
'page callback' => 'helloworld_listado',
'access arguments' => array('access helloworld content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
Note that , MENU_NORMAL_ITEM being the default value for type, you do not need to specify it.
Also, as our esteemed webchick just said

It seems you are using a mix of Drupal 5 (array contents) and Drupal 6 (no $may_cache, $items indexed by path) syntaxes for hook_menu.
If you are using Drupal 6, this should look like:
<?php
function helloworld_perm() {
return array('access helloworld content');
}
function helloworld_listado(){
return "yea";
}
function helloworld_menu(){
$items = array();
$items["listado"] = array(
'title' => t('Listado de empresas'),
'page callback' => 'helloworld_listado',
'access arguments' => array('access helloworld content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
?>
Note that, MENU_NORMAL_ITEM being the default value for 'type', you do not need to specify it.
Also, as our esteemed webchick just said, you can find a detailed explanation on the page she points to.

just FYI , above link moved to
http://api.drupal.org/api/examples/page_example--page_example.module/6

Related

Drupal - using views_embed_view with an external Drupal site

I have two Drupal sites hosted on different servers. In the main they need to operate separately however site1 has one specific content type which I'd like to show in a list on site2.
I can't simply use feeds to import as the original has to remain and if edited, the changes be instantly reflected on both sites. Site2 has no requirement to edit the content - only show it.
The content is already being presented in a list on site1. The list was created using views.
My intention was to call the view on site2 using the following code in a custom module.
function site2_menu() {
$items = array();
$items['content-from-site1'] = array(
'title' => 'Content from Site1',
'page callback' => 'site_two_list',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function site2_list() {
db_set_active('site1');
$content = views_embed_view('articles', 'default');
db_set_active('default');
return $content;
}
site1 DB is defined in the settings.php file.
However this isn't returning any data. Using the same approach on Site1 (without switching DBs) works fine.
Was I being over-optimistic in hoping this approach would work or am I missing something obvious?
If this isn't likely to work, what would the alternative be? I can do my own SQL query, but I'd prefer to use views for built in arguments, pagination, templates, etc.
Thanks.
I've not been able to use views_embed_view, however I have been able to use views_get_view to retrieve everything I need and iterate over the results myself. It's a very close second.
function site2_menu() {
$items = array();
$items['content-from-site1'] = array(
'title' => 'Content from Site1',
'page callback' => 'theme_site2_list',
'access arguments' => array('access content'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function site2_theme() {
return array (
'site2_list_page' => array(
'arguments' => array('content' => NULL),
'template' => 'templates/site2-list-page'
)
);
}
function theme_site2_list() {
db_set_active('site1');
$view = views_get_view('articles');
$view->base_database = "site1";
$view->init_display();
$view->pre_execute();
$view->execute();
db_set_active('default');
foreach ($view->result as $key => $data) {
$content[$key]['nid'] => $data->nid;
$content[$key]['title'] => $data->node_title;
$content[$key]['body'] => $data->field_body;
$content[$key]['image'] => $data->field_field_image;
}
return theme('site2_list_page', array('content' => $content));
}
Then in site2-list-page.tpl.php I can use the $content array to do what I need. It's not quite as clean as a simple views_embed_view, but it's a close second and allows the content from one site to be pulled to another fairly easily.

how to display any message or data in drupal 7 .module file

how to display any message or data in mymodule.module file in drupal 7
i have used following line but it didn't display any thing
drupal_set_message(t('test message'));
aslo i want to display any variable data like for example $data = "hello"
then how to display this variable data in drupal 7
i am new to drupal , so if any one knows please let me know .
i have search a lot , but didn't get anything.
thanks in advance.
I have used folllowing code by creating module in drupal 7
<?php
function form_example_menu() {
$items = array();
$items['form_example/form'] = array(
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form',
'page arguments' => array('form_example_form'),
'access arguments' => array('access content'), //put the name of the form here
'access callback' => TRUE
);
return $items;
}
function form_example_form($form, &$form_state) {
$form['price'] = array(
'#type' => 'textfield',
'#title' => 'What is Your Price?',
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE, //make this field required
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
);
$form['form_example_form']['#submit'][] = 'form_example_form_submit';
return $form;
}
function form_example_form_validate(&$form, &$form_state) {
if (!($form_state['values']['price'] > 0)){
form_set_error('price', t('Price must be a positive number.'));
}
}
function form_example_form_submit($form, &$form_state) {
$result = db_insert('test')->fields(array('price' => $form_state['values']['price'],))->execute();
drupal_set_message(t('Your Price was saved'));
}
In above code data is inserted in database , but message didn't displaying .
If you know , what is problem please let me know , i have search a lot for this problem
. Thanks in advance.
Here's the proper way to display some data in the message:
drupal_set_message(t('test message: !data', array('!data' => $data)));
As for the message not displaying, if other messages do display on your site, it sounds like your function isn't executing. I'd need more info on what you're attempting to do (including the code involved) to debug it.
The function watchdog is also available in Drupal 7
Here is an exemple of how you can use it:
watchdog('MyModule', '<pre>'. print_r($variable, TRUE) .'</pre>', array(), WATCHDOG_INFO, NULL);
You can watch the log in Reports -> Recent log messages (admin/reports/dblog) if the core module "Database logging" is activated.

Drupal - Toggle Visibility of Menu Item Via Custom Permissions

I am currently using Drupal 7 and I am writing a custom code such that users with a certain permission("use business dashboard") should see a menu item in their main menu. The problem is that only I(admin) can see this menu item. I have been able to create a custom permission on the permissions page and have set it to give access to "admin" and my user-specific role and have implemented the following code(nevermind the "xxxxxx" that is inplace of the module name, I would rather keep it anonymous for now, but just know that they are all in place of the machine-readable module name):
function xxxxxx_menu(){
$items = array();
$items['xxxxxxx'] = array(
'title' => 'Business Owner Dashboard',
'page callback' => '_xxxxxx_page',
'access arguments' => array('use business dashboard'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function xxxxxx_permission(){
return array(
'use business dashboard' => array(
'title' => t('Have access to business dashboard'),
'description' => t('Allow user to send out SMS messages via database query forms'),
),
);
}
When I log in as my test user which has the role-specific permission of "use business dashboard" I cannot see the menu item. I am sure this is incredibly simple, but I have been Googling and prodding at the code for hours. Any help would be greatly appreciated!
Can't figure this out either. Can you try to break down the access callback, if it didn't work, at least it'll give you a tip about what's going on.
Your code can go like this:
function xxxxxx_menu(){
$items = array();
$items['xxxxxxx'] = array(
'title' => 'Business Owner Dashboard',
'page callback' => '_xxxxxx_page',
'access callback' => 'my_custom_access_callback',
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function my_custom_access_callback()
{
if(user_access('use business dashboard'))
return TRUE;
return FALSE;
}
Till me if this works... Muhammad.

Drupal 7 example module, page not found, why?

I wrote a simple test module example, 2 files, test.module, test.info, and enabled them in drupal 7 modules.
I cleared all the cache, and still when i'm trying to go to localhost/drupal/hello , i get drupal 404 page not found, why is that?
Here is my code:
<?php
function test_world_help($section) {
switch ($section) {
case 'admin/help#hello_world':
$output = '<p>Hello world help...</p>';
return $output;
case 'admin/modules#description':
return 'Hello world module description...';
}
}
function test_world_menu($may_cache) {
$items = array();
if ($may_cache) {
}
else {
$items['hello'] = array(
'title' => 'Hello world page...',
'callback' => 'test_world_page',
'access' => TRUE,
'type' => MENU_CALLBACK
);
}
return $items;
}
function test_world_page() {
return '<p>Hello world!</p>';
}
You have posted almost the same question once and twice before. Why don't you update the original one instead of posting new ones?
The hook_menu() does not have the $may_cache argument in Drupal 7. You should remove it. However, it should not solve your problem as it is unset and false. Thus, the $items should still be populated.
It is correct, as jprofitt says, that you should change 'callback' to 'page callback'.
There is no such thing as 'access', but there is 'access callback' and 'access arguments'. You are most likely looking for 'access callback'. However, you can't just set it to 'true'. It expects a function name which returns either 'true' or 'false'. It defaults to 'user_access', so you should just leave it that way. However, you might want to set 'access arguments' to something like 'access content'.
Does the following piece of code work better?
function test_world_menu() {
$items = array();
$items['hello'] = array(
'title' => 'Hello World!',
'page callback' => 'test_world_page',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
It seems that you haven't really had a look at the documentation. I might be wrong. However, the documentation at api.drupal.org is always a good start to look when you want to learn the basics of how something work.
You should probably change 'callback' to 'page callback', as I don't believe hook_menu() has just a plain "callback" option. And since you're working with Drupal 7, its hook_menu() actually doesn't have parameters.
Uninstall and reinstall your custom module. I hope this will help you. Because it's necessary for drupal core to know the newly created path created using hook_menu.

drupal hook_menu_alter() for adding tabs

I want to add some tabs in the "node/%/edit" page from my module called "cssswitch".
When I click "Rebuild Menus", the two new tabs are displayed, but they are displayed for ALL nodes when editing them, not just for the node "cssswitch". I want these new tabs to be displayed only when editing node of type "cssswitch".
The other problem is when I clear all cache, the tabs completely dissapear from all edit pages. Below is the code I wrote.
function cssswitch_menu_alter(&$items) {
$node = menu_get_object();
//print_r($node);
//echo $node->type; //exit();
if ($node->type == 'cssswitch') {
$items['node/%/edit/schedulenew'] = array(
'title' => 'Schedule1',
'access callback'=>'user_access',
'access arguments'=>array('view cssswitch'),
'page callback' => 'cssswitch_schedule',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight'=>4,
);
$items['node/%/edit/schedulenew2'] = array(
'title' => 'Schedule2',
'access callback'=>'user_access',
'access arguments'=>array('view cssswitch'),
'page callback' => 'cssswitch_test2',
'page arguments' => array(1),
'type' => MENU_LOCAL_TASK,
'weight'=>3,
);
}
}
function cssswitch_test(){
return 'test';
}
function cssswitch_test2(){
return 'test2';
}
Thanks for any help.
hook_menu_alter() is only called during the menu building process, so you can't do dynamic node type checks within that function.
However, to achieve what you want, you can do this with a custom access callback as follows:
// Note, I replaced the '%' in your original code with '%node'. See hook_menu() for details on this.
$items['node/%node/edit/schedulenew2'] = array(
...
'access callback'=>'cssswitch_schedulenew_access',
// This passes in the $node object as the argument.
'access arguments'=>array(1),
...
);
Then, in your new custom access callback:
function cssswitch_schedulenew_access($node) {
// Check that node is the proper type, and that the user has the proper permission.
return $node->type == 'cssswitch' && user_access('view cssswitch');
}
For other node types, this function will return false, thus denying access, and thus removing the tab.

Resources