Drupal 7 Messages only show after class registry clear - php

Need to show some messages from hook_block_view.
so in the function setting a message like:
drupal_set_message('Block should have loaded');
Does not work.
If I clear class registry, it works once, then every other time it does not show.
It looks like drupal is redirecting or something before the page is rendered. Using drupal_exit(); by the end of this function does show everything is correct so far, but does not make it to the final output.
Edit:
It works if I throw in drupal_flush_all_caches But then goes super slow obviously. Seems to be some sort of caching problem.

Turn off full caching for the block under hook_block_info() by setting 'cache' to: DRUPAL_NO_CACHE
function hook_block_info() {
$blocks = array();
$blocks['abc'] = array(
'info' => t('test block'),
'visibility' => 0,
'status' => TRUE,
'region' => 'none',
'weight' => 99,
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
Then try to write
drupal_set_message('Block should have loaded');
within hook_block_view and check now...
it may be help for you.

Related

How to output html from content array property?

I'm new to theming and I just inherited this code. I need to control where and how to print #output but it's always showing up at the top of the page, above the HTML tags. I looked at the renderable arrays API but I couldn't find anything specific to my problem.
In mytheme.module:
function mytheme_output_block() {
$content = array(
'#theme' => 'my_theme',
'#output' => function_that_returns_JSON();
...
function mytheme_hook_theme() {
return array(
'my_theme' => array(
'vars' => array(
'output' => '',
...
And in my_theme.tpl.php I tried:
<?php print $output; ?>
But it gets ignored. How do I control #output so I can style it?
not super sure but you might need to call the array element directly. Something like:
$theme = mytheme_hook_theme();
echo $theme['output'];
Hope it helps
Which Drupal you use? I assume D7.
Take a look at the documentation of hook_theme: https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_theme/7.x
It's not vars - it's variables
You have missed the template key in your implementation
template: If specified, this theme implementation is a template, and this is the template file without an extension. Do not put .tpl.php on this file; that extension will be added automatically by the default rendering engine (which is PHPTemplate). If 'path', above, is specified, the template should also be in this path.
So what your hook should look like:
function mytheme_hook_theme() {
return array(
'my_theme' => array(
'variables' => array(
'output' => '',
),
'template' => 'my-theme',
...
Remember to clear the caches afterwards.

CodeIgniter 3 / Basic Hook Loading Problems

Hey I'm sure I miss something obvious, but can't figure out what it is :
I can't get a simple hook working with my quasi blank installation of CI 3 ...
I've put the simplest of the code possible with a Hook called MyTest and it works fine :
application/config/hooks.php
$hook['post_controller_constructor'][] = array(
'class' => 'MyTest',
'function' => 'TestMe',
'filename' => 'MyTest.php',
'filepath' => 'hooks',
'params' => ''
);
application/hooks/MyTest.php
class MyTest{
function TestMe()
{
die("die by the hook TEST");
}
}
->this outputs "die by the hook TEST", which is what is expected.
But almost the same with a Hook called URI_Actions doesn't work at all :
application/config/hooks.php
$hook['post_controller_constructor'][] = array(
'class' => 'URI_Actions',
'function' => 'index',
'filename' => 'URI_Actions.php',
'filepath' => 'hooks',
'params' => ''
);
application/hooks/URI_Actions.php
class URI_Actions{
function index()
{
die("die by the hook URI Actions");
}
}
-> this doesn't output anything ... I'm sure I'm missing something enormous, any idea ?
Epilogue
Of course the problem was elsewhere : binary FTP transfert was the issue, once the files uploaded normally the same codes worked perfectly. Thanks to trajchevska which made me realize the code was not the problem !
Does it not display anything or it displays "die by the hook TEST" only? The thing is that you have them both defined one after the other and they're both doing dump and die. So when the Test hook displays the result, the program dies and doesn't get to the URI_Actions hook.
Try using a simple var_dump instead of die, you should get the expected result. I tested both locally and they work fine.

404 router in Lithium PHP

I know how to make response routes in the actual /config/routes.php file, but I can't find where to change the default 'fetal dispatcher' error. I'd like to be able to have it route to a nice 404 page I've made when there's a missing page/action controller. Is that possible?
Yes, you can take advantage of lithium\core\ErrorHandler for this. See the code in the default config/bootstrap/errors.php:
ErrorHandler::apply('lithium\action\Dispatcher::run', array(), function($info, $params) {
$response = new Response(array(
'request' => $params['request'],
'status' => $info['exception']->getCode()
));
Media::render($response, compact('info', 'params'), array(
'library' => true,
'controller' => '_errors',
'template' => 'development',
'layout' => 'error',
'request' => $params['request']
));
return $response;
});
This is saying, if any exception occurs during Dispatcher::run(), display the development.html.php template from the views/_errors folder with the layouts/error.html.php layout.
So you can change that -- maybe you check the Environment to see if this is a dev or production environment and display a different template for production.
Maybe if $info['exception']->getCode() === 404, you can switch to a template specifically for 404 errors.

Drupal : Create variable from custom .module to custom .tpl.php

I create a custom module and I want use the complete_url of my future website in the template who used/created by my module.
I tried several ways and searched Google but I don't find a solution.
However this issue seems very simply, that became me crazy x)
So, I must create a variable in my .module and add her when I return the array in the main_socialtag_theme function. Where/How can I do that ?
My .module:
function main_socialtag_block_info(){
$block['main_socialtag']=array(
'info' => t('Main socialtag'),
'cache' => DRUPAL_NO_CACHE,
);
return $block;
}
function main_socialtag_theme(){
return array(
'main_socialtag_block' => array(
'template' => 'theme/main_socialtag_block',
'variables' => array(),
),
);
}
function main_socialtag_block_view($delta=''){
if ($delta == 'main_socialtag'){
return array(
'subject' => '',
'content' => array(
'#theme' => 'main_socialtag_block',
)
);
}
}
Thanks for help, and sorry for my bad english writing !
If you are looking for a way to add, modify and call variables. Check variable_get and variable_set.
To add or modify a variable value:
variable_set("my_variable_name", "value");
To retrieve the value:
$myVal = variable_get("my_variable_name", "");
For hook_theme Implementation, kindly check this question.

Drupal 7 Form API - Custom Select Theme

I have been searching all day but unable to find any answers - I am sure I am doing it right as worked fine in Drupal 6 and should work fine in Drupal 7.
I want to give a custom theme function to my select element in my form
$form['field_name'] = array(
'#type' => 'select',
'#title' => t('Title Here'),
'#theme' => 'custom_select',
'#options' => $values,
);
I have the theme hook right to declare the new custom theme function but my problem is when using that custom theme function as above I get an empty $variables array which just reads
Array([element] => null)
can anyone see what I may be doing wrong? cleared cache, done everything I can think of - any ideas why Drupal is not passing the element data to the theme function? thanks
well finally figured this one out incase any one else has the problem - make sure you set render element in hook_theme and not variables!
before
function hook_theme(){
return array(
'select_currency' => array(
'variables' => array('element' => null),
'file' => 'module_name.theme.inc',
));
}
after
function hook_theme(){
return array(
'select_currency' => array(
'render element' => 'element',
'file' => 'module_name.theme.inc',
));
}
I was pulling my hair out until I remembered the render element!

Categories