You are here

function domain_source_form_alter in Domain Access 6.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. 7.3 domain_source/domain_source.module \domain_source_form_alter()
  4. 7.2 domain_source/domain_source.module \domain_source_form_alter()

Implement 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, but make sure we are not on the CCK field configuration form.
  // See http://drupal.org/node/186624.
  if ($form['#id'] == 'node-form' && !isset($form['#node']->cck_dummy_node_form)) {
    global $_domain, $user;
    if (!empty($form['#node']->nid)) {
      $default_source = db_result(db_query("SELECT domain_id FROM {domain_source} WHERE nid = %d", $form['#node']->nid));
    }
    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')) {
      $options[DOMAIN_SOURCE_USE_ACTIVE] = t('Use active domain');
      $show = TRUE;
      foreach ($domains as $domain) {
        if ($domain['valid'] || user_access('access inactive domains')) {
          $options[$domain['domain_id']] = $domain['sitename'];
        }
      }
    }
    else {
      if (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(
              '#value' => '<label><b>' . t('Source domain:') . '</b></label><div class="description">' . t('This content is assigned to %domain and cannot be reassigned.', array(
                '%domain' => $name,
              )) . '</div>',
            );
          }
        }
      }
    }

    // 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('The canonical domain for this content.'),
      );
    }
    else {
      $form['domain']['domain_source'] = array(
        '#type' => 'value',
        '#value' => $default_source,
      );
    }
  }
}