You are here

function domain_path_form_node_form_alter in Domain Path 7

Implements hook_form_NODE_FORM_alter().

File

./domain_path.module, line 249
Path alias handling for multiple domains.

Code

function domain_path_form_node_form_alter(&$form, $form_state) {

  // Custom setting for vertical tabs for domains.
  // This is in domain 7.x.3 but not 7.x.2.
  if (isset($form['domain'])) {
    $form['domain']['#group'] = 'additional_settings';
  }
  $form['domain_path'] = array(
    '#tree' => TRUE,
    '#title' => t('Domain-specific paths'),
    '#type' => 'fieldset',
    '#group' => 'additional_settings',
    '#access' => user_access('edit domain paths'),
    '#attached' => array(
      'js' => array(
        drupal_get_path('module', 'domain_path') . '/domain_path.js',
      ),
    ),
  );

  // If the user doesn't have access to the domain form element then we will
  // create a domain id setting for the js so that it will know which path
  // field to display.
  if (!user_access('publish to any assigned domain') && !user_access('set domain access')) {
    $domain_id = FALSE;

    // Only set the default domain id as the js domain id when the user can
    // publish from default domain but not when the user can publish from
    // assigned domains.
    if (user_access('publish from default domain') && !user_access('publish from assigned domain')) {
      $default_domain = domain_default();
      $domain_id = $default_domain['domain_id'];
    }
    else {
      global $_domain;
      $domain_id = $_domain['domain_id'];
    }
    $form['domain_path']['#attached']['js'][] = array(
      'data' => array(
        'domainPath' => array(
          'domainId' => $domain_id,
        ),
      ),
      'type' => 'setting',
    );
  }
  $format = domain_select_format();
  $form['domain_path']['#attached']['js'][] = array(
    'data' => array(
      'domainPath' => array(
        'fieldType' => $format,
      ),
    ),
    'type' => 'setting',
  );
  $paths = array();
  $domains = domain_domains();
  $current = t('<none>');
  $path = $nid = FALSE;
  if (!empty($form['#node']->nid)) {
    $current = drupal_get_path_alias('node/' . $form['#node']->nid);
    $nid = $form['#node']->nid;
  }
  $form['domain_path']['current'] = array(
    '#type' => 'item',
    '#title' => t('Current alias'),
    '#markup' => check_plain($current),
    '#weight' => -5,
  );
  $show_delete = FALSE;
  foreach ($domains as $domain_id => $domain) {
    $path = FALSE;
    if ($nid) {
      $path = domain_path_lookup_path('alias', 'node/' . $nid, $domain_id);
    }
    $default = '';
    if ($path) {
      $show_delete = TRUE;
    }

    // TODO: Only exposed checked domains?
    $form['domain_path'][$domain_id] = array(
      '#type' => 'textfield',
      '#title' => check_plain($domain['path']),
      '#default_value' => $path ? $path : $default,
      '#access' => user_access('edit domain paths'),
      '#attributes' => array(
        'data-domain_id' => $domain_id,
      ),
    );
  }
  $form['domain_path']['domain_path_delete'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete domain-specific aliases'),
    '#default_value' => FALSE,
    '#access' => $show_delete,
    '#weight' => -1,
  );
}