I waste all day fighting with drupal 7 hook_menu, days ago all was working when i create new modules, new menu entries, etc.
Im developing a cron, depends of 1 parameter, its generate a file to output, or read other file (input).
I trying to define a simple url to test the cron, and when i put ...lec_profile_cron in the brosers, it works, but if try ....companies_cron/1 or just companies_cron o other name y put at $items['route'], its doesnt works.
I tried to clean cache, memcached, use drush rr, all and dont understand what is happen.
I tried a lot of combinations and examples like the helloword_hello menu option in a new module i create called helloworld, and its returns 404 not found.
// CRON TEST
$items['companies_cron/%out'] = array(
'title' => t('Test cron'),
'page callback' => 'lec_profile_cron',
'page arguments' => array(1),
'access arguments' => array('administer lec profile configuration')
);
function lec_profile_cron($out)
{
// CRON OUT
if ($out == 1) {
//do stuff
} else {
//CRON IN
}
}
Maybe was a stupid thing, but i cant find...
Ty in advice.
According to the documentation I think this will work better. You dont need th %out for args in your $items and I also think you missing the trailing comma on 'access arguments' which I've added and also returning something.
$items['companies_cron'] = array(
'title' => t('Test cron'),
'page callback' => 'lec_profile_cron',
'page arguments' => array(1),
'access arguments' => array('administer lec profile configuration'),
return $items;
);
function lec_profile_cron($out = 0){
// If companies_cron/ is called, $out takes default value of 0
// If companies_cron/1 is called, $out value will be 1
// To pass multiple args, like companies_cron/1/2, you would require further params with defaults like $out = 0, $in = 0 ...
// CRON OUT
if ($out == 1) {
//do stuff
} else {
//CRON IN
}
};
Related
Drupal 7 Hook_menu access callback is not returning the correct boolean.
Before we begin. YES! Cache is cleared... a lot.
I implemented a simple function for testing:
$items['tutor_review_selection'] = array(
'title' => t('example'),
'page callback' => 'my_module_example_page',
'access callback' => my_module_access( array('administrator') ),
'type' => MENU_NORMAL_ITEM
);
function my_module_access( $roles ) {
global $user;
$check = array_intersect($roles, array_values($user->roles));
return empty( $check ) ? FALSE : TRUE;
}
This returns TRUE for logged in and logged out users.
Here is the important part:
I call 'my_module_access' function in 'my_module_example_page' function and it works correctly.
Can anyone shine some light onto why this would not work in access callback?
Maybe something to do with order of operation?
Cache is cleared.
If you check the Drupal 7 hook_menu documentation you will see the following code:
function mymodule_menu() {
$items['abc/def'] = array(
'page callback' => 'mymodule_abc_view',
'page arguments' => array(1, 'foo'),
);
return $items;
}
'page callback' accepts a string, which is the callback function name. The arguments to be sent to that function are provided in the 'page arguments' array.
edit Note that you should probably create a permission and assign your roles to that permission, then check the permission instead of checking for specific roles.
I am porting a module from Drupal 6 to Drupal 7 and I am trying to pass a variable from my custom module to a template. I have something like this:
function my_callback_function(){
... //some unrelated code
$page_params = array();
$page_params['items_per_page'] = 25;
$page_params['page'] = $_GET['page'] ? $_GET['page'] : 0;
$page_params['total_items'] = $data_provider->getNumItems();
$page_params['total_pages'] = $data_provider->getNumPages($page_params['items_per_page']);
return theme('my_theme', $page_params);
}
function my_module_theme($existing, $type, $theme, $path) {
return array(
'my_theme' => array(
'variables' => array('page_params' => NULL),
'template' => 'theme/my_template_file',
),
);
}
And inside *my_template_file.tpl.php* I try to use $page_params:
<?php print $page_params['total_items']; ?>
All that make my site to throw the following error:
Fatal error: Unsupported operand types in
C:...\includes\theme.inc on line 1075
Which corresponds with these lines of code in theme.inc:
// Merge in argument defaults.
if (!empty($info['variables'])) {
$variables += $info['variables']; // THIS IS THE VERY EXACT LINE
}
elseif (!empty($info['render element'])) {
$variables += array($info['render element'] => array());
}
If I leave the theme() call as it was in Drupal 6, the error doesn't appear but then my template doesn't recognize my $page_params variable:
return theme('my_theme', array('page_params' => $page_params));
I have read half the API trying to figure out what I am doing wrong but as far as I have read, it seems that this is the proper way to pass variables from a custom module to a template.
Thus, any kind of help will be more than welcome.
Thanks in advance.
Finally, I figured out what I was doing wrong. In fact, they were a couple of things:
My theme() call was ok:
return theme('my_theme', $page_params);
But my hook_theme implementation wasn't. If $page_params is my array of variables, i cannot use the whole array as a variable, I have to explicitly specify which are my variables inside the array. Something like this:
function my_module_theme($existing, $type, $theme, $path) {
return array(
'my_theme' => array(
'variables' => array(
'items_per_page' => NULL,
'page' => NULL,
'total_items' => NULL,
'total_pages' => NULL,
),
'template' => 'theme/my_template_file',
);
}
And finally, inside my_template_file.tpl.php I will have to use the variable names directly instead of using them as a component of $page_params:
<?php print $total_items; ?>
It may seem obvious for experienced users but it took me a while until I realized this. I hope it can be useful for other beginners like me.
You can use drupal variable_set() and variable_get() to store data in drupal session and get data from session.
Thanks
In drupal 7 I'm trying to give permissions of using a node if you are member of the same group as the node.
I want to use hook_menu and define my custom access check function. To this function I sent the nid as a parameter.
This is what I have got now, and I realy don't see why it is not working:
function modulename_pdf_menu() {
$items['pdf/node/%'] = array(
'page callback' => '_modulename_pdf',
'access callback' => '_modulename_pdf_access_check',
'access arguments' => array(2),
'type' => MENU_CALLBACK
);
return $items;
}
function _modulename_pdf_access_check($nid) {
echo $nid;
die();
}
I assume this should print my node id to the screen and stop. But it is still running the logic defined in _modulename_pdf. Any idea what I'm missing here?
Thanks in advance for your reply.
You are right, clear the cache and check it.
I'm trying to implement something like Mark Story's "Down for Maintenance" page using CakePHP 2.1.0. I'm pretty close to achieving this, but I'm running into two issues that I could use some help with. First of all, here is all of the relevant code (six files):
1) app/Config/bootstrap.php:
Configure::write('App.maintenance', true);
2) app/Config/core.php:
Configure::write('debug', 1);
...
Configure::write('Exception', array(
'handler' => 'ErrorHandler::handleException',
'renderer' => 'AppExceptionRenderer',
'log' => true
));
3) app/Controller/AppController.php:
if (Configure::read('App.maintenance') == true) {
App::uses('DownForMaintenanceException', 'Error/Exception');
throw new DownForMaintenanceException(null);
}
4) app/Lib/Error/Exception/DownForMaintenanceException.php:
<?php
class DownForMaintenanceException extends CakeException {}
5) app/Lib/Error/AppExceptionRenderer.php:
<?php
App::uses('ExceptionRenderer', 'Error');
class AppExceptionRenderer extends ExceptionRenderer {
function _outputMessage($template) {
// Call the "beforeFilter" method so that the "Page Not Found" page will
// know if the user is logged in or not and, therefore, show the links that
// it is supposed to show.
if (Configure::read('App.maintenance') == false)
{
$this->controller->beforeFilter();
}
parent::_outputMessage($template);
}
public function downForMaintenance() {
$url = $this->controller->request->here();
$code = 403;
$this->controller->response->statusCode($code);
$this->controller->set(array(
'code' => $code,
'url' => h($url),
'isMobile' => $this->controller->RequestHandler->isMobile(),
'logged_in' => false,
'title_for_layout' => 'Down for Maintenance'
));
$this->_outputMessage($this->template);
}
}
6) app/View/Errors/down_for_maintenance.ctp:
<p>Down for Maintenance</p>
Now, for the two issues I'm experiencing. First, this code only works when debug is set higher than 1. Is there anything I can do about that? Does that indicate that I'm going about this the wrong way? The second issue is that, although I'm setting the "isMobile" and "logged_in" view variables to boolean values in the "downForMaintenance" method, the "app/View/Layouts/default.ctp" file is seeing them as strings. What can I do about that?
Thanks!
here is a quick and dirty maintenance page for cakephp
in public index.php
define('MAINTENANCE', 0);
if(MAINTENANCE > 0 && $_SERVER['REMOTE_ADDR'] !='188.YOUR.IP.HERE')
{
require('maintenance.php'); die();
}
Then just change MAINTENANCE = 1 when you want to take your site down and it will still be viewable from your home/office.
BONUS: Works with all versions of cake!
A more elegant way would be to add a route overriding any other one at the very top of routes.php:
//Uncomment to set the site to "under construction"
Router::connect('/*', array('controller' => 'pages', 'action' => 'underConstruction'));
//any other route should be underneath
If you want to add any condition you can also do it here:
define('MAINTENANCE', 0);
if(MAINTENANCE > 0 && $_SERVER['REMOTE_ADDR'] !='188.YOUR.IP.HERE')
Router::connect('/*', array('controller' => 'pages', 'action' => 'underConstruction'));
}
We'll need to create a custom Dispatch Filter,CakePHP has you covered.
check below link
http://josediazgonzalez.com/2013/12/13/simple-application-maintenance-mode/
made a simple thankyou page (e.g. /product/3/thankyou) based on a menu callback in a custom module. the content shows up fine in the normal page layout but i want the regions and blocks to show up and they don't. suggestions?
// menu callback
function custom_menu() {
$items = array();
$items['product/%/thankyou'] = array(
'page callback' => 'custom_product_thankyou',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
// theme function
function custom_theme() {
return array(
'product_review_thankyou' => array(
'variables' => array('node' => NULL),
'template' => 'product_review_thankyou',
),
);
}
// page callback
function custom_product_thankyou() {
$node = node_load(arg(1));
$output = theme('product_review_thankyou', array('node' => $node));
return $output;
}
I just tried your code in a drupal installation and i have no issues with missing blocks. Is it possible that you configured your blocks to be displayed only on certain pages?
The one block that i still couldn't get to show up (no matter what the block visibility settings were) was a 'menu block'. Problem was there wasn't a link for the thank you page in this block. So, I ended up having to add links on the configuration page with paths like product/[node_id]/thankyou, and then I disabled the links so they wouldn't be visible. Refreshed the page, and the block appeared.
To me this is a little annoying, as I kind of wanted this to be dynamic and not have to write in the product node ids. But either way, problem solved.