I want to welcome the user with a friendly message and a little music after login (and only after login).
Believe me, this is for didactical reasons only and will never be done on a production site.
Have been trying this for days.
Added and enabled audio.module to upload an audio file.
Enabled Trigger to create an action to be triggered at login.
In my custom welcome.module, I created an action which is successfully triggered at login. The message is shown as expected.
See code for this action:
/**
* Implementation of hook_action_info().
*/
function welcome_action_info() {
return array(
'welcome_with_message_and_music' => array(
'type' => 'user',
'description' => t('Welcome a user with a friendly message and a little music after login.'),
'configurable' => FALSE,
'hooks' => array(
'user' => array('login')
)
)
);
}
function welcome_with_message_and_music(&$object,$context){
drupal_set_message(t("Welcome $object->name!"));
$node = node_load(6);
$content = node_view($node, FALSE, TRUE, TRUE);
return $content;
}
But the audio node is not shown on the page after login. I'm sure node_load works correctly because when I put the node in the drupal messages list, the node is shown, like this...
function welcome_with_message_and_music(&$object,$context){
drupal_set_message(t("Welcome $object->name!"));
$node = node_load(6);
$content = node_view($node, FALSE, TRUE, TRUE);
drupal_set_message($content);
return $content;
}
Of course, I don't want the node shown as a message. (Actually, I'm planning to hide the media player altogether using css z-index, but that is not my question here. I know how to do that.)
It seems that my statement $content = node_view($node, FALSE, TRUE, TRUE); is ignored. (I'v put prints in function node_view() to verify this.)
I am under the impression I'm misunderstanding some basic Drupal thing here.
I would appreciate your help.
Thanks.
Comments
The rest of it looks good,
The rest of it looks good, but I think your problem may be with the expectation of what actions do.
They do atomic actions. Usually on other objects. often as a side-effect of other things happening or a bulk operation. And optionally tell you what they've done.
I do not know what is done with the return value of an action (I'd expect it to be a boolean success message?) but it generally would not be printed to the screen, which you seem to be assuming.
If you look, I think you'll find that all actions are silent, log, or set messages. They do not print pages. Or return anything (?)
You have a login event trigger.
It triggers the welcome action - which happens.
The normal login result page is generated and displayed.
At no point was the return value from the action side effect expected to influence the page generation process.
Soo ... I think this might not be the right way of going about what you want. Although you could (I guess) set a semaphore flag in the action that triggers something to be rendered elsewhere. *speculation*
All that said, I've never written an action myself
.dan.
if you are asking a question you think should be documented, please provide a link to the handbook where you think the answer should be found.
| http://www.coders.co.nz/ |
.dan. is the New Zealand Drupal Developer working on Government Web Standards
How and where to code insertion of existing node on a page?
Thanks for your response dman. I appreciate it.
I think I understand your point concerning actions being atomic, but don't know enough Drupal to understand your semaphore flag thing.
I did try this in another way which seemed alright at the time, but which did not work either. I tried it using the hook_user() and basically trying the same thing for operation login.
I think here too, statement
seems not to be executed. (I have a print statement in node_view() and this shows the function does get executed for the other audio nodes I have on my front page.)
Try to debug your code
In order to be sure if node_view gets executed, try to debug the code.
If you have Eclipse PDT, then you can debug using either XDebug or the Zend Debugger. It takes a little while to set up (I believe there are YouTube videos that show you how); so you might as an alternative try the watchdog function (http://api.drupal.org/api/function/watchdog) instead to write to the Drupal log before and after the call to node_view.
Hope this helps,
Gerald
When to programmatically add an existing node on a page?
I did some debugging. Watchdog, debug_backtrace(), about a zillion drupal_set_message() calls, you name it.
The node_view does seem to be executed for my node with nid 6, but what bothers me is that it seems to be too late, because when I put a drupal_set_message() in function theme() (of theme.inc), I notice that the node_view for my node 6 is called after the page has been gone through the theme() function. So it seems I'm calling node_view() too late.
Actually, the node_view() for my particular node 6 is executed about right after the page has gone through theme().
And it has probably nothing to do with module weight. I renamed my welcome.module to awelcome.module. No difference.
Am I talking sense here?
Try drupal_set_content
http://api.drupal.org/api/function/drupal_set_content - this might help :)
---
Threshold Interactive :: www.thresholdinteractive.com
---
http://phpdiva.wordpress.com
How to insert existing node on a page, untidy solution
I've found a solution, but I'm not sure Drupal gurus will appreciate it.
The simple welcome message with an action, triggered at login, already worked.
Now, how to insert the already existing audio node.
I abuse the global $user , save the node as data belonging to the user, and remove it at two places (to be sure):
My welcome.module uses the hook_user() to load the node and save the output as user data.
The world is a strange place. So it seems that the page preprocessing happens before the user login operation. Anyway, I remove the audio node in template.php function template_preprocess_page() to prevent it from being rendered more than once.
If you're actually reading this, you've noticed I did the same thing at logout in my custom module.
I insert my hiddenplayer in page.tpl.php, just before the content:
print $user->hiddenplayer;I also modified the node.tpl.php just a little to give my particular node an extra class:
Of course, I give this class some css attributes to hide it. I don't want this particular flashplayer shown. I don't use display:none because in that case, it just doesn't play.
And I also modified the node somewhat in my custom module to have the player autostart and prevent it from looping.
I realize there's some ugly logic in this, especially keeping the audio node as user data.
I promise that one day, I'look into this drupal_set_content, one day.
cheers.
Joachim
I wonder if using a view to
I wonder if using a view to display a node and then embedding that view is a simpler approach...