You are here

function domain_source_form_alter in Domain Access 7.2

Same name and namespace in other branches
  1. 8 domain_source/domain_source.module \domain_source_form_alter()
  2. 5 domain_source/domain_source.module \domain_source_form_alter()
  3. 6.2 domain_source/domain_source.module \domain_source_form_alter()
  4. 7.3 domain_source/domain_source.module \domain_source_form_alter()

Implements hook_form_alter()

File

domain_source/domain_source.module, line 22
Creates a source domain for linking to content from other domains.

Code

function domain_source_form_alter(&$form, &$form_state, $form_id) {

  // Apply to all node editing forms only.
  if (empty($form['#node_edit_form'])) {
    return;
  }
  global $user;
  $_domain = domain_get_domain();
  if (!empty($form['#node']->nid)) {
    $default_source = db_query("SELECT domain_id FROM {domain_source} WHERE nid = :nid", array(
      ':nid' => $form['#node']->nid,
    ))
      ->fetchField();
  }
  if (!isset($default_source)) {
    $default_source = $_domain['domain_id'];
  }

  // Prevent invalid domains from being used.
  $lookup = domain_lookup($default_source);
  if ($default_source != DOMAIN_SOURCE_USE_ACTIVE && empty($lookup['valid'])) {
    $default_source = NULL;
  }
  $account = $user;
  domain_get_user_domains($account);

  // Only uses with 'set domain access' can assign content to all affilaites, so they get a new option.
  // This option allows domain source to be ignored on a per-node basis.
  $options = array();
  $domains = domain_domains();
  $show = FALSE;
  if (user_access('set domain access')) {
    $show = TRUE;
    $options[DOMAIN_SOURCE_USE_ACTIVE] = t('Use active domain');
    foreach ($domains as $domain) {
      if ($domain['valid'] || user_access('access inactive domains')) {
        $options[$domain['domain_id']] = $domain['sitename'];
      }
    }
  }
  elseif (user_access('publish to any assigned domain') && !empty($account->domain_user)) {
    $show = FALSE;
    $options[DOMAIN_SOURCE_USE_ACTIVE] = t('Use active domain');

    // Get the user's allowed domains.
    foreach ($domains as $domain) {
      $key = $domain['domain_id'];
      if ($domain['domain_id'] == 0) {
        $key = -1;
      }
      if (!empty($user->domain_user[$key]) && ($domain['valid'] || user_access('access inactive domains'))) {
        $options[$key] = $domain['sitename'];
      }
    }

    // Is this node assigned to a source that the user can control?
    if (isset($form['#node']->domain_source)) {

      // Transform the 0 to -1 for lookups.
      $source = $form['#node']->domain_source == 0 ? -1 : $form['#node']->domain_source;
    }
    else {
      $source = NULL;
      $show = TRUE;
    }
    if (!is_null($source) && isset($account->domain_user[$source])) {
      if ($account->domain_user[$source] == $source) {
        $show = TRUE;
      }
      else {
        $name = $source != -5 ? $domains[$source]['sitename'] : t('the active domain');
        $form['domain']['domain_source_notes'] = array(
          '#type' => 'item',
          '#title' => t('Source domain'),
          '#markup' => t('This content is assigned to %domain and cannot be reassigned.', array(
            '%domain' => $name,
          )),
        );
      }
    }
  }

  // Determine how to show the form element.
  if ($show) {
    $form['domain']['domain_source'] = array(
      '#type' => 'select',
      '#title' => t('Source domain'),
      '#options' => $options,
      '#default_value' => $default_source,
      '#description' => t('This affiliate will be used to write links to this content from other affiliates, as needed.'),
    );
  }
  else {
    $form['domain']['domain_source'] = array(
      '#type' => 'value',
      '#value' => $default_source,
    );
  }
}