Still on Drupal 7? Security support for Drupal 7 ended on 5 January 2025. Please visit our Drupal 7 End of Life resources page to review all of your options.Problem/Motivation
When workbench moderation is enabled, new Documents with media attachments immediately redirect to the attachment when clicking Publish (not draft) instead of the node view page (if they are moderated).
The problem comes from the additional drupal_goto in workbench_moderation. The default behavior of Documents with attachments is that we should be redirected to the node view page when publishing new content. Redirecting to the attached media file url is only expected on subsequent visits to the node/[nid] (and there's 1 attachment and the "Show page instead of file?" checkbox remains unchecked.
We can see this being set up in the oa_wiki redirect submit handler and hook_init():
function oa_wiki_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'oa_wiki_page_node_form' && module_exists('oa_files')) {
$submit_handlers = array(
'node_form_submit',
'save_draft_submit',
);
foreach (element_children($form['actions']) as $key) {
if (!empty($form['actions'][$key]['#submit']) && array_intersect($submit_handlers, $form['actions'][$key]['#submit'])) {
$form['actions'][$key]['#submit'][] = 'oa_wiki_redirect_to_view_submit';
}
}
}
}
function oa_wiki_redirect_to_view_submit($form, &$form_state) {
if (!empty($form_state['nid']) && !empty($form_state['redirect']) && 'node/' . $form_state['nid'] == $form_state['redirect']) {
$form_state['redirect'] .= '/view';
}
}
function oa_wiki_init() {
if (module_exists('oa_files') && arg(0) == 'node' && is_numeric(arg(1)) && !arg(2) && ($node = menu_get_object()) && $node->type == 'oa_wiki_page') {
if (($path = oa_wiki_node_uri($node)) && $path['path'] != $_GET['q']) {
drupal_goto($path['path']);
}
}
}
However, in workbench_moderation.module, a redirect is defined for new nodes:
function workbench_moderation_form_node_form_alter(&$form, $form_state) {
......
$form['actions']['submit']['#submit'][] = 'workbench_moderation_node_form_submit';
}
function workbench_moderation_node_form_submit($form, &$form_state) {
$form_state['redirect'] = array('node/' . $form_state['node']->nid . '/current-revision');
}
And workbench_moderation_node_current_view() does another redirect!
function workbench_moderation_node_current_view($node) {
if (_workbench_moderation_access_current_draft($node)) {
drupal_goto('node/' . $node->nid . '/draft');
}
drupal_goto('node/' . $node->nid);
}
oa_wiki_init() gets called a couple of times because we are going thru subsequent Drupal bootstraps. We first get redirected to node/[nid]/current-revision after form submission. node/[nid]/revision gets a passthru in oa_wiki_init(). However, because workbench_moderation_node_current_view() redirects to node/[nid], upon the second run the conditions of oa_wiki_init() are met and we are redirected to the attached media url.
Proposed resolution
The multiple redirects loses the original redirect. As discussed with @mpotter, one way to handle this track the original redirect in $_SESSION. That way the solution isn't tied to workbench_moderation and can still support any other modules that have redirects that would impact the document node creation workflow. I also think that a check for the node status is necessary as well.
function oa_wiki_redirect_to_view_submit($form, &$form_state) {
if (!empty($form_state['nid']) && !empty($form_state['redirect'])) {
if ('node/' . $form_state['nid'] == $form_state['redirect']) {
$form_state['redirect'] .= '/view';
}
// Track the original redirect just in case we loose it on multiple calls to oa_wiki_init().
$_SESSION['oa_wiki']['redirects'][$form_state['nid']] = $form_state['redirect'];
}
}
/**
* Implements hook_init().
*/
function oa_wiki_init() {
if (module_exists('oa_files') && arg(0) == 'node' && is_numeric(arg(1)) && !arg(2) && ($node = menu_get_object()) && $node->type == 'oa_wiki_page') {
if (!empty($_SESSION['oa_wiki']['redirects'][arg(1)])) {
unset($_SESSION['oa_wiki']['redirects'][arg(1)]);
}
elseif ($node->status == 1) {
if (($path = oa_wiki_node_uri($node)) && $path['path'] != $_GET['q']) {
drupal_goto($path['path']);
}
}
}
}
| Comment | File | Size | Author |
|---|---|---|---|
| #2 | oa_wiki-fix-redirect-issue-workbench-moderation-2447787-2.patch | 1.53 KB | mirie |
| #1 | oa_wiki-fix-redirect-issue-workbench-moderation-2447787-1.patch | 1.29 KB | mirie |
Comments
Comment #1
mirie commentedComment #2
mirie commentedPrevious patch did not match code snippet and incorrectly wrapped the session variable in the entire original if block. Attaching new patch.
Comment #3
Argus commentedWhy didn't you set this issue to "needs review"?
Comment #4
mpotter commentedThanks for bumping this so we didn't lose it. I committed a minor variation of this to 3309891. All I did was merge it with the latest code and also add a check for anonymous users and sessions before setting a session variable.