How do I create a popup which can use the node variables after saving a new node?
For example, I want to show a popup with the social media share button. Should I use node_save()
?
2 Answers
When a node is saved, the user is normally redirected to the page showing the node, which is not a form. In that case, hook_form_alter()
doesn't help. I said the user is normally redirected to the page showing the node, as that is what Drupal does, but there could be a module that redirects the user to a different page, such as the one showing the node edit form to add a new node.
To show the popup when the node is viewed, you can use hook_node_view(), which can be used to add HTML tags to the node view page.
The example shown in the documentations shows how to add markup to the node view page.
function mymodule_node_view($node, $view_mode, $langcode) {
$additional_markup = 'Markup to add to the page';
$node->content['my_additional_markup'] = array(
'#markup' => $additional_markup,
'#weight' => 10,
);
}
As for showing the popup right after the node has been saved, you could implement hook_form_alter()
to add a form submission handler to the "Save" button; that submission handler would add a session variable that is the checked from your implementation of hook_node_view()
. Its code should then be similar to the following one.
function mymodule_node_view($node, $view_mode, $langcode) {
if (isset($_SESSION['nid']) && isset($_SESSION['timestamp']) && $_SESSION['nid'] == $node->nid && mymodule_check_timestamp($_SESSION['timestamp'])) {
$additional_markup = 'Markup to add to the page';
$node->content['my_additional_markup'] = array(
'#markup' => $additional_markup,
'#weight' => 10,
);
}
}
The values in $_SESSION
are the ones set by the form submission handler. mymodule_check_timestamp()
should return TRUE
when $_SESSION['timestamp']
doesn't contain a value too past in time; this would help in the case there is a module that redirect the user to a different page, and then the user visit the node page to review the node s/he saved. For example, it could return TRUE
when $_SESSION['timestamp']
is a timestamp 5 seconds earlier. It is up to you to decide how much delay should be acceptable between when the node is saved, and when the node is viewed from the same user who edited it.
You can change the weight used in hook_node_view()
; that changes where in the node view page your markup is added.
-
-
Hi kiamlaluno, function bartik_node_view($node, $view_mode, $langcode) { if (isset($_SESSION['nid']) && isset($_SESSION['timestamp']) && $_SESSION['nid'] == $node->nid && bartik_check_timestamp($_SESSION['timestamp'])) { $additional_markup = 'Markup to add to the page'; $node->content['my_additional_markup'] = array( '#markup' => $additional_markup, '#weight' => 10, ); } } this does not work for me Aug 7, 2012 at 13:06
-
What I write in my answer is just part of the code you need. You still need to write code for
mymodule_check_timestamp()
, and add a form submission handler to the node edit form to set those variables in the session.– apaderno ♦Aug 7, 2012 at 13:13
drupal_add_js inside a hook_form_alter, like so:
function yourmodule_form_alter(&$form, &$form_state, $form_id)
{
if ([your logic to pick out the node form])
{
drupal_add_js([your javascript]);
}
}
Or you could probably do it with Rules calling some custom code.
-
Hi Beth, I've used hook_form_alter, how do I get the node variables? Aug 6, 2012 at 13:49
-
First I'd look in $form to see if what you want is in there. If not,
if (arg(0) == 'node') { $nid = arg(1); }
should give you the current node NID and then you can node_load() or whatever you like. (Credits to this question)– bethAug 6, 2012 at 13:57 -
-
-
Now the problem is not how to create a popup, I can use a dialog or lighbox or whatever, the problem is that the js works with the html and as long as i cannot print html through node_form_alter I cannot call anything... unless i do a drupal_set_message(<div class="popup">contents</div>) ... it could be a solution... Any other suggestion? Aug 6, 2012 at 15:05