1 path.module path_form_element(Entity $entity)

Return a portion of a form for setting an alias on an entity.

File

core/modules/path/path.module, line 779
Enables users to customize URLs and provide automatic URL alias patterns.

Code

function path_form_element(Entity $entity) {
  $langcode = isset($entity->langcode) ? $entity->langcode : LANGUAGE_NONE;
  $entity_type = $entity->entityType();
  $bundle = $entity->bundle();

  $path = array();
  if (!$entity->isNew()) {
    $uri = $entity->uri();
    $conditions = array(
      'source' => $uri['path'],
      'langcode' => $langcode,
    );
    $path = path_load($conditions);
    if ($path === FALSE) {
      $path = array(
        'source' => $uri['path'],
        'langcode' => $langcode,
      );
    }
  }
  $path += array(
    'pid' => NULL,
    'source' => NULL,
    'alias' => '',
    'langcode' => $langcode,
    'auto' => NULL,
  );

  $fieldset = array(
    '#type' => 'fieldset',
    '#title' => t('URL settings'),
    '#collapsible' => TRUE,
    '#collapsed' => empty($path['alias']),
    '#group' => 'additional_settings',
    '#attributes' => array(
      'class' => array('path-form'),
    ),
    '#attached' => array(
      'js' => array(backdrop_get_path('module', 'path') . '/js/path.js'),
    ),
    '#access' => user_access('create url aliases') || user_access('administer url aliases'),
    '#weight' => 30,
    '#tree' => TRUE,
    '#element_validate' => array('path_form_element_validate'),
  );

  $pattern = path_get_pattern_by_entity_type($entity_type, $bundle, $langcode);
  if ($pattern) {
    if (!isset($entity->path['auto'])) {
      $entity->path['auto'] = FALSE;
      if (!$entity->isNew()) {
        module_load_include('inc', 'path');
        $uri = $entity->uri();
        $automatic_alias = path_generate_entity_alias($entity, $uri['path'], $langcode);
        if ($automatic_alias !== FALSE) {
          $existing_alias = backdrop_get_path_alias($uri['path'], $langcode);
          $entity->path['auto'] = ($existing_alias != $uri['path'] && $existing_alias == $automatic_alias);
        }
      }
      else {
        $entity->path['auto'] = TRUE;
      }
    }

    $fieldset['auto'] = array(
      '#type' => 'checkbox',
      '#title' => t('Generate automatic URL alias'),
      '#default_value' => $entity->path['auto'],
      '#description' => t('Uncheck this to create a custom alias below.'),
      '#weight' => -1,
      '#attributes' => array('class' => array('automatic-alias')),
    );

    // Add a shortcut link to configure URL alias patterns.
    if (backdrop_valid_path('admin/config/urls/path/patterns')) {
      $fieldset['auto']['#description'] .= ' ' . l(t('Configure URL alias patterns.'), 'admin/config/urls/path/patterns');
    }
  }

  $fieldset['alias'] = array(
    '#type' => 'textfield',
    '#title' => t('URL alias'),
    '#default_value' => $path['alias'],
    '#maxlength' => 255,
  );

  if (isset($fieldset['auto'])) {
    $fieldset['alias']['#states'] = array(
      'disabled' => array(
        'input.automatic-alias' => array('checked' => TRUE),
      ),
    );
  }

  $fieldset['pid'] = array(
    '#type' => 'value',
    '#value' => $path['pid'],
  );
  $fieldset['source'] = array(
    '#type' => 'value',
    '#value' => $path['source'],
  );
  $fieldset['original'] = array(
    '#type' => 'value',
    '#value' => path_load($path['pid'])
  );

  $fieldset['langcode'] = array(
    '#type' => 'value',
    '#value' => $path['langcode'],
  );

  // Provide an element to remember the current alias if a new one is created.
  if (!empty($entity->path['alias'])) {
    $fieldset['old_alias'] = array(
      '#type' => 'value',
      '#value' => $entity->path['alias'],
    );
  }

  return $fieldset;
}