1 redirect.module redirect_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode)

Implements hook_field_attach_form().

File

core/modules/redirect/redirect.module, line 1314

Code

function redirect_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  if (!empty($form['redirect']) || !empty($entity->is_new)) {
    return;
  }

  // Check if this entity type supports redirects.
  if (!redirect_entity_type_supports_redirects($entity_type)) {
    return;
  }

  $uri = $entity->uri();
  if (empty($uri['path']) || $entity->isNew()) {
    // If the entity has no source path, or the entity is a new one, 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' => user_access('administer redirects'),
    '#weight' => 31,
    '#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'] = backdrop_get_path('module', 'redirect') . '/js/redirect.js';
    }
  }

  $redirect = array(
    'redirect' => $uri['path'],
    'redirect_options' => array_diff_key($uri['options'], array('entity_type' => '', 'entity' => '')),
    'langcode' => $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 to this @type', array('@type' => backdrop_strtolower($info['label']))),
      'href' => 'admin/config/urls/redirect/add',
      'query' => array_filter($redirect) + backdrop_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(array(), array('redirect' => $uri['path']));
  $header = array('source', 'status_code', 'language', 'count', 'access', 'operations');
  $form['redirect'] += redirect_list_table($redirects, $header);
}