You are here

function field_redirection_goto in Field Redirection 7

Redirect to the destination value from a given field.

Parameters

$instance: The field settings.

$item: The field data to work on.

$response_code: The HTTP Response Code to use, defaults to "301".

1 call to field_redirection_goto()
field_redirection_field_formatter_view in ./field_redirection.module
Implements hook_field_formatter_view().

File

./field_redirection.module, line 99
Provides a field formatter to redirect to another path.

Code

function field_redirection_goto($instance, $item, $response_code = 301) {
  $element = array();

  // Work out the destination path to redirect to.
  $path = '';
  if (!empty($instance['widget']['type'])) {
    switch ($instance['widget']['type']) {

      // Link field.
      case 'link_field':
        if (!empty($item['url'])) {
          $path = $item['url'];
        }
        break;

      // Node reference field.
      case 'node_reference':
      case 'node_reference_autocomplete':
        if (!empty($item['nid'])) {

          // Wrap the internal system path with its alias.
          $path = drupal_get_path_alias('node/' . $item['nid']);
        }
        break;

      // User reference field.
      case 'user_reference':
      case 'user_reference_autocomplete':
        if (!empty($item['uid'])) {

          // Wrap the internal system path with its alias.
          $path = drupal_get_path_alias('user/' . $item['uid']);
        }
        break;
    }
  }

  // Only proceed if a path was identified.
  if (!empty($path)) {

    // If the user has permission to bypass the page redirection, return a
    // message explaining where they would have been redirected to.
    if (user_access('bypass redirection')) {
      drupal_set_message(t('This page is set to redirect to <a href="!path">another URL</a>, but you have permission to see the page and not be automatically redirected.', array(
        '!path' => base_path() . $path,
      )), 'warning');
    }
    else {
      drupal_goto($path, array(), $response_code);
    }
  }
  return $element;
}