Question

Background

Client has Event webforms hosted externally by a marketing automation service, leading to an unusual situation: a node of Event content type should have its summary displayed in certain Views on the client site, but navigating directly to the node address should result in a 301 redirect to the offsite form.

Client can achieve this manually by using the Redirect module:

/admin/config/search/redirect/add?redirect=node/1234

... and then entering the node path in the From field and the offsite destination in the To field. (Yes, this is exactly backwards, but it kind of works.) However, they would like to automate this process.

I tried to do this with Rules:

Events:

  • After saving new content of type Event
  • After updating existing content of type Event

Conditions:

  • NOT Data value is empty (Parameter: Data to check: [node:field-external-url])

Actions:

  • Page redirect (Parameter: URL: [node:field-external-url])

Unfortunately, this does not modify the Page Redirect settings on the node by adding an entry to the redirect table in the database.

Instead, it fires a one-time system event and redirects the site administrator to the desired URL at the moment that the Event content node is created or updated. Subsequent visits to the node URL do not redirect site visitors; instead, they see the node at that URL.

So, I'm trying a different approach, with a small custom_event_redirect module:

/**
 * Implements hook_form_alter().
 */
function custom_event_redirect_form_alter(&$form, &$form_state, $form_id) {
  // add submit handler only to Event content forms
  if ($form_id === 'event_node_form') {
    array_unshift($form['#submit'], 'custom_event_redirect_search_form_submit');
  }
}

/**
 * Drupal FAPI submit handler implementation
 */
function custom_event_redirect_search_form_submit(array $form, array &$form_state) {
  if (!empty($form['field_external_url']['und'][0]['value']['#default_value'])) {
    //create redirect programmatically

  }
}

Is it possible to use the Redirect API to fire off a redirect_edit_form_submit() when the event_node_form is submitted?

This is such an edge case. Google results are skewed heavily toward drupal_goto() redirects after form submission -- but if that was what I wanted, I could have used Rules.

I don't want to fire off a one-time redirect on form submission; I want to add an entry to the redirect table on form submission.

¯\_(ツ)_/¯

Was it helpful?

Solution

Rabbit Hole should get you close, if not entirely there. After enabling the rh_node submodule, admin/structure/types/manage/event will show "Rabbit Hole settings" where you can set the behavior to "Page redirect" and the "Redirect path" field has "You may enter tokens in this field" in the help text, so [node:field-external-url] should work there. You probably need custom code for your "Conditions: NOT Data value is empty" but you might be able to have a custom module for that, so long as you set the weight of your custom module such that it runs earlier than rh_node (alternatively, Rabbit Hole offers a PHP filter, but that has an implied dependency on Bad Judgement).

Out of the box, Rabbit Hole does offer a "Bypass Rabbit Hole action for nodes" permission that you can grant to administrators.

OTHER TIPS

You can use below code snippets to create programmatically URL redirect. [Drupal 7]

// Create an object with the redirect parameters
$redirect = new stdClass();
redirect_object_prepare($redirect);
$redirect->source = 'old-url';
$redirect->source_options = array();
$redirect->redirect = 'node/5'; // New system path
$redirect->redirect_options = array();
$redirect->type = 'redirect';
$redirect->language = LANGUAGE_NONE;
redirect_save($redirect);

 

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top