0

I've created a view with a contextual filter for 'field_object_id' -> this is a field that the content type has which is unique but it's not the Node ID, these field_object_id's are brought in from a JSON feed.

So I can go to my view page like this:

drupal-site.com/my-view

Which would display a page not found. Then going to:

drupal-site.com/my-view/230

Would load up the content node that has '230' set for 'field_object_id' -> however that content node ID is not 230, I have no idea what it is. It could be 8509.

What I need to do, is set my view to automatically redirect the page to the node that it finds (I would set it to redirect to the first result it finds).

I have Panels installed too, would this be easier to do with Panels or should I look to creating a module?

  • I don't think redirecting was ever built into Views. But it seems to me that you do not want real redirect (http 302 found), just to load and display one node. Could you clarify? – Mołot Jul 29 '13 at 8:48
  • Yeah, I'm not too concerned about the real redirect (302), just to load and display one node but to "redirect" to that nodes' URL. Someone in IRC told me to use Location(node/[nid]) in the View -> Footer -> Text area (PHP), which seems to work well. – dan2k3k4 Jul 29 '13 at 9:10
1

One way I think of is to to this in a custom menu callback. So you could have an URL /mycallback/[OBJECT_ID] which would redirect to the first node that has the given [OBJECT_ID].

Something like this:

/**
 * Implements hook_menu().
 */
function MYMODULE_menu() {
  $items['mycallback/%'] = array(
    'page callback' => 'fieldvalue_redirect_callback',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Custom callback given a object id.
 */
function fieldvalue_redirect_callback($id) {
  $query = new EntityFieldQuery();
  $query->entityCondition('entity_type', 'node')
  ->fieldCondition('field_object_id', 'value', $id, '=')
  ->addMetaData('account', user_load(1));                            
  $result = $query->execute();

  if (isset($result['node'])) {
    drupal_goto(url('node/' . key($result['node'])));
  } else {
    drupal_set_message(t('Page not found'), 'error');
    drupal_goto('<front>');
  }
}

First comes the definition of the menu callback, MYMODULE_menu().

The argument after mycallback/ is being passed on the the page callback function by defining it here 'page arguments' => array(1),.

fieldvalue_redirect_callback() will get the id entered by the user after /mycallback as argument. In the function we use EntityFieldQuery to fetch the node that matches the given ID.

And last we redirect if we get a result, otherwise we show an error message and the front page.

Hope this can help you or someone else.

|improve this answer|||||
0

So I was able to get this working (although maybe not the best way) by doing:

Setting show fields instead of content.

Selecting node ID in the fields (and set it to be excluded from display, although not necessary)

Then in the Views' FOOTER, add Text Area and set the filter to PHP.

Then I put:

<?php
  global $base_url;
  header("Location: $base_url/node/[nid]");
?>

And this seems to do what I want. Now going to the view page with an ID, it automatically redirects to the Node which redirects to the nodes' URL Alias.

|improve this answer|||||

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.