0

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.

¯\_(ツ)_/¯

  • I don't want the site editor redirected once on form submission well then add a condition of when user role is not admin nor editor. – No Sssweat Oct 1 '18 at 2:12
  • I want to permanently add a redirect to the node's path, not fire off a one-time redirect event. – hotwebmatter Oct 1 '18 at 12:02
  • I edited the question to reflect this: "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." The problem is with the action fired, not the user role. – hotwebmatter Oct 1 '18 at 12:43
1

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.

  • This looks promising. If it works, I'll be sure to accept your answer. – hotwebmatter Oct 1 '18 at 20:04
  • 1
    I accepted your answer. Thanks for the suggestion. The Rabbit Hole module is great. I think I can get what I want by setting "Global event behavior (Page redirect)" and then manually overriding that on a per-node basis for the old nodes that don't have [node:field-external-url] set. (I might need to use Views Bulk Operations to set them all at once.) Going forward, the field will be set and the redirect will be automated. – hotwebmatter Oct 2 '18 at 19:43
  • 1
    It's even easier than that. I can set the global event behavior to "Display the page" and then override for individual entities, using the Rabbit Hole settings instead of the [node:field-external-url]. This gets the client everything they need with the least amount of effort. – hotwebmatter Oct 3 '18 at 15:16
0

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);

 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.