You are here

function redirect_field_attach_form in Redirect 7.2

Same name and namespace in other branches
  1. 7 redirect.module \redirect_field_attach_form()

Implements hook_field_attach_form().

@todo Investigate using hook_entity_load() to load all entity redirects. @todo Figure out how to support entity URIs that contain query strings.

File

./redirect.module, line 1638

Code

function redirect_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  list($id) = entity_extract_ids($entity_type, $entity);
  if (!empty($form['redirect']) || empty($id)) {
    return;
  }

  // Check if this entity type supports redirects.
  if (!redirect_entity_type_supports_redirects($entity_type)) {
    return;
  }
  $uri = entity_uri($entity_type, $entity);
  if (empty($uri['path'])) {

    // If the entity has no source path, then we cannot lookup the existing
    // redirects.
    return;
  }
  $info = entity_get_info($entity_type);
  $form['redirect'] = array(
    '#type' => 'fieldset',
    '#title' => t('URL redirects'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#access' => redirect_access('list', 'redirect'),
    '#weight' => 30,
    '#attributes' => array(
      'class' => array(
        'redirect-list',
      ),
    ),
  );

  // Only support vertical tabs if there is a vertical tab element.
  foreach (element_children($form) as $key) {
    if (isset($form[$key]['#type']) && $form[$key]['#type'] == 'vertical_tabs') {
      $form['redirect']['#group'] = $key;
      $form['redirect']['#attached']['js']['vertical-tabs'] = drupal_get_path('module', 'redirect') . '/redirect.js';
    }
  }
  $redirect = array(
    'redirect' => $uri['path'],
    'redirect_options' => array_diff_key($uri['options'], array(
      'entity_type' => '',
      'entity' => '',
    )),
    'language' => $langcode,
  );
  $form['redirect']['actions'] = array(
    '#theme' => 'links',
    '#links' => array(),
    '#attributes' => array(
      'class' => array(
        'action-links',
      ),
    ),
  );
  if (redirect_access('create', 'redirect')) {
    $form['redirect']['actions']['#links']['add'] = array(
      'title' => t('Add URL redirect'),
      'href' => 'admin/config/search/redirect/add',
      'query' => array_filter($redirect) + drupal_get_destination(),
    );
  }

  // We don't have to put our include in $form_state['build_info']['files']
  // since the build array will already be cached.
  module_load_include('inc', 'redirect', 'redirect.admin');
  $redirects = redirect_load_multiple(FALSE, array(
    'redirect' => $uri['path'],
  ));
  $header = array(
    'source',
    'status',
    'status_code',
    'language',
    'count',
    'access',
    'operations',
  );
  $form['redirect'] += redirect_list_table($redirects, $header);
}