You are here

function field_redirection_field_formatter_view in Field Redirection 7

Same name and namespace in other branches
  1. 7.2 field_redirection.module \field_redirection_field_formatter_view()

Implements hook_field_formatter_view().

If we have a node reference and we can redirect to it lets do it!

File

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

Code

function field_redirection_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];

  // Only proceed if there's data.
  if (!empty($items[0])) {

    // All of these are handled the same way, just with a different HTTP
    // response code.  See http://en.wikipedia.org/wiki/URL_redirection for
    // full details.
    $responses = array(
      'redirect_300' => 300,
      'redirect_301' => 301,
      'redirect_302' => 302,
      'redirect_303' => 303,
      'redirect_304' => 304,
      'redirect_305' => 305,
      'redirect_307' => 307,
    );

    // The default response code is 301.
    $response_code = 301;
    if (isset($responses[$display['type']])) {
      $response_code = $responses[$display['type']];
    }
    $element = field_redirection_goto($instance, $items[0], $response_code);
  }
  return $element;
}