function path_form_node_form_alter

Implements hook_form_BASE_FORM_ID_alter() for node_form().

See also

path_form_element_validate()

File

drupal/core/modules/path/path.module, line 99
Enables users to rename URLs.

Code

function path_form_node_form_alter(&$form, $form_state) {
  $node = $form_state['controller']
    ->getEntity();
  $path = array();
  if (!empty($node->nid)) {
    $conditions = array(
      'source' => 'node/' . $node->nid,
    );
    if ($node->langcode != Language::LANGCODE_NOT_SPECIFIED) {
      $conditions['langcode'] = $node->langcode;
    }
    $path = drupal_container()
      ->get('path.crud')
      ->load($conditions);
    if ($path === FALSE) {
      $path = array();
    }
  }
  $path += array(
    'pid' => NULL,
    'source' => isset($node->nid) ? 'node/' . $node->nid : NULL,
    'alias' => '',
    'langcode' => isset($node->langcode) ? $node->langcode : Language::LANGCODE_NOT_SPECIFIED,
  );
  $form['path'] = array(
    '#type' => 'details',
    '#title' => t('URL path settings'),
    '#collapsed' => empty($path['alias']),
    '#group' => 'advanced',
    '#attributes' => array(
      'class' => array(
        'path-form',
      ),
    ),
    '#attached' => array(
      'library' => array(
        array(
          'path',
          'drupal.path',
        ),
      ),
    ),
    '#access' => user_access('create url aliases') || user_access('administer url aliases'),
    '#weight' => 30,
    '#tree' => TRUE,
    '#element_validate' => array(
      'path_form_element_validate',
    ),
  );
  $form['path']['alias'] = array(
    '#type' => 'textfield',
    '#title' => t('URL alias'),
    '#default_value' => $path['alias'],
    '#maxlength' => 255,
    '#description' => t('The alternative URL for this content. Use a relative path without a trailing slash. For example, enter "about" for the about page.'),
  );
  $form['path']['pid'] = array(
    '#type' => 'value',
    '#value' => $path['pid'],
  );
  $form['path']['source'] = array(
    '#type' => 'value',
    '#value' => $path['source'],
  );
  $form['path']['langcode'] = array(
    '#type' => 'value',
    '#value' => $path['langcode'],
  );
}