You are here

function pathauto_field_attach_form in Pathauto 7

Implements hook_field_attach_form().

Add the automatic alias form elements to an existing path form fieldset.

2 calls to pathauto_field_attach_form()
pathauto_form_node_form_alter in ./pathauto.module
Implements hook_form_BASE_FORM_ID_alter().
pathauto_form_taxonomy_form_term_alter in ./pathauto.module
Implements hook_form_FORM_ID_alter().

File

./pathauto.module, line 249
Main file for the Pathauto module, which automatically generates aliases for content.

Code

function pathauto_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  list($id, , $bundle) = entity_extract_ids($entity_type, $entity);
  if (!isset($form['path'])) {

    // This entity must be supported by core's path.module first.
    // @todo Investigate removing this and supporting all fieldable entities.
    return;
  }
  else {

    // Taxonomy terms do not have an actual fieldset for path settings.
    // Merge in the defaults.
    $form['path'] += array(
      '#type' => 'fieldset',
      '#title' => t('URL path settings'),
      '#collapsible' => TRUE,
      '#collapsed' => empty($form['path']['alias']),
      '#group' => 'additional_settings',
      '#attributes' => array(
        'class' => array(
          'path-form',
        ),
      ),
      '#access' => user_access('create url aliases') || user_access('administer url aliases'),
      '#weight' => 30,
      '#tree' => TRUE,
      '#element_validate' => array(
        'path_form_element_validate',
      ),
    );
  }
  $pattern = pathauto_pattern_load_by_entity($entity_type, $bundle, $langcode);
  if (empty($pattern)) {
    return;
  }
  if (!isset($entity->path['pathauto'])) {
    if (!empty($id)) {
      module_load_include('inc', 'pathauto');
      $uri = entity_uri($entity_type, $entity);
      $pathauto_alias = pathauto_create_alias($entity_type, 'return', $uri['path'], array(
        $entity_type => $entity,
      ), $bundle, $langcode);
      if ($pathauto_alias === FALSE) {

        // If Pathauto is not going to be able to generate an alias, then we
        // should not bother to show the checkbox since it wouldn't do anything.
        // Note that if a pattern does apply, but all the tokens currently
        // evaluate to empty strings, then $pathauto_alias would equal null and
        // not false.
        return;
      }
      else {
        $path = drupal_get_path_alias($uri['path'], $langcode);
        $entity->path['pathauto'] = $path != $uri['path'] && $path == $pathauto_alias;
      }
    }
    else {
      $entity->path['pathauto'] = TRUE;
    }
  }

  // Add JavaScript that will disable the path textfield when the automatic
  // alias checkbox is checked.
  $form['path']['alias']['#states']['!enabled']['input[name="path[pathauto]"]'] = array(
    'checked' => TRUE,
  );

  // Override path.module's vertical tabs summary.
  $form['path']['#attached']['js'] = array(
    'vertical-tabs' => drupal_get_path('module', 'pathauto') . '/pathauto.js',
  );
  $form['path']['pathauto'] = array(
    '#type' => 'checkbox',
    '#title' => t('Generate automatic URL alias'),
    '#default_value' => $entity->path['pathauto'],
    '#description' => t('Uncheck this to create a custom alias below.'),
    '#weight' => -1,
  );

  // Add a shortcut link to configure URL alias patterns.
  if (drupal_valid_path('admin/config/search/path/patterns')) {
    $form['path']['pathauto']['#description'] .= ' ' . l(t('Configure URL alias patterns.'), 'admin/config/search/path/patterns');
  }
  if ($entity->path['pathauto'] && !empty($entity->old_alias) && empty($entity->path['alias'])) {
    $form['path']['alias']['#default_value'] = $entity->old_alias;
    $entity->path['alias'] = $entity->old_alias;
  }

  // For Pathauto to remember the old alias and prevent the Path module from
  // deleting it when Pathauto wants to preserve it.
  if (!empty($entity->path['alias'])) {
    $form['path']['old_alias'] = array(
      '#type' => 'value',
      '#value' => $entity->path['alias'],
    );
  }
}