You are here

function domain_form_alter in Domain Access 7.2

Same name and namespace in other branches
  1. 5 domain.module \domain_form_alter()
  2. 6.2 domain.module \domain_form_alter()
  3. 7.3 domain.module \domain_form_alter()

Implements hook_form_alter()

This function is crucial, as it appends our node access elements to the node edit form. For users without the "set domain access" permission, this happens silently.

File

./domain.module, line 1921
Core module functions for the Domain Access suite.

Code

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

  // There are forms that we never want to alter, and they are passed here.
  $forms = module_invoke_all('domainignore');
  if (in_array($form_id, $forms)) {
    return;
  }

  // Set a message if we are on an admin page.
  domain_warning_check($form_id);

  // If SEO is turned on, then form actions need to be absolute paths
  // to the currently active domain.  See http://drupal.org/node/196217.
  $seo = variable_get('domain_seo', 0);
  if ($seo && isset($form['#action'])) {

    // We cannot use the global domain here, since it can be modified.
    $domain = domain_initial_domain();
    $action = parse_url($form['#action']);
    if (isset($action['query'])) {
      $action['path'] .= '?';
    }
    else {
      $action['query'] = '';
    }

    // We cannot reset this if it has already been set by another module.
    // See http://drupal.org/node/306551
    if (empty($action['host'])) {
      $form['#action'] = $domain['scheme'] . '://' . $domain['subdomain'] . $action['path'] . $action['query'];
    }
  }

  // Apply to all node editing forms only.
  if (empty($form['#node_edit_form'])) {
    return;
  }

  // Grab the globals we need.
  global $user;
  $_domain = domain_get_domain();

  // By default, the requesting domain is assigned.
  $default = array(
    $_domain['domain_id'] == 0 ? -1 : $_domain['domain_id'],
  );

  // How is core content handled for this site?
  // In D7, type handling is strict, so make this value 0 or 1.
  // @TODO: clean up DOMAIN_INSTALL handling.
  $all_sites = (int) variable_get('domain_behavior', DOMAIN_INSTALL_RULE);
  if ($all_sites == 1) {
    $behavior = 1;
  }
  else {
    $behavior = variable_get('domain_node_' . $form['#node']->type, 0);
  }
  if ($_domain['domain_id'] == 0) {
    $default[] = -1;
  }

  // Some options will be passed as hidden values, we need to run some checks on those.
  if (isset($form['#node']->nid) && !empty($form['#node']->domains)) {
    $raw = $form['#node']->domains;
  }
  else {
    $raw = $default;
  }
  $options = array();

  // Get the display format of the form element.
  $format = domain_select_format();
  foreach (domain_domains() as $data) {

    // Cannot pass zero in checkboxes.
    $data['domain_id'] == 0 ? $key = -1 : ($key = $data['domain_id']);

    // The domain must be valid.
    if ($data['valid'] || user_access('access inactive domains')) {

      // Checkboxes must be filtered, select lists should not.
      $options[$key] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
    }
  }

  // If the user is a site admin, show the form, otherwise pass it silently.
  if (user_access('set domain access')) {
    $form['domain'] = array(
      '#type' => 'fieldset',
      '#title' => t('Domain access options'),
      '#collapsible' => TRUE,
      '#collapsed' => FALSE,
    );
    $form['domain']['domain_site'] = array(
      '#type' => 'checkbox',
      '#prefix' => t('<p><b>Publishing options:</b>'),
      '#suffix' => '</p>',
      '#title' => t('Send to all affiliates'),
      '#required' => FALSE,
      '#description' => t('Select if this content can be shown to all affiliates. This setting will override the options below, but you must still select a domain that "owns" this content.'),
      '#default_value' => isset($form['#node']->domain_site) ? $form['#node']->domain_site : $behavior,
    );
    $form['domain']['domains'] = array(
      '#type' => empty($format) ? 'checkboxes' : 'select',
      '#title' => t('Publish to'),
      '#options' => $options,
      '#required' => TRUE,
      '#description' => t('Select which affiliates can access this content.'),
      '#default_value' => isset($form['#node']->domains) ? $form['#node']->domains : $default,
    );
    if ($format) {
      $form['domain']['domains']['#multiple'] = TRUE;
      $form['domain']['domains']['#size'] = count($options) > 10 ? 10 : count($options);
    }
  }
  else {
    $action = domain_form_permission_check();
    if (!empty($action)) {

      // hook_user() has not run, so get the domain data for this user.
      $user->domain_user = domain_get_user_domains($user);
      $user_domains = array();
      $default_options = array();
      $user_options = array();
      $raw_options = array();
      if (!empty($user->domain_user)) {
        foreach ($user->domain_user as $key => $value) {
          if (abs($value) > 0) {
            $user_domains[] = $value;
          }
        }

        // Set the best match for the user's primary domain.
        // If they are a member of the current domain, use that one.
        if (in_array($_domain['domain_id'], $user_domains)) {
          $first_domain = $_domain['domain_id'];
        }
        else {
          $first_domain = current($user_domains);
        }
        foreach ($options as $key => $value) {
          if (in_array($key, $user_domains)) {
            $user_options[$key] = $value;
          }
        }
      }

      // Raw data checks for published nodes.
      foreach ($raw as $key => $value) {
        if (in_array($value, $user_domains)) {
          $default_options[] = $value;
        }
        else {
          $raw_options[] = $value;
        }
      }

      // Act on the behavior desired by the site admin.
      switch ($action) {

        // 1 == go to the default domain.
        case 1:
          $root = domain_default();
          if ($root['domain_id'] != $_domain['domain_id']) {
            domain_goto($root);
          }
          break;

        // 2 == go to the user's assigned domain.
        case 2:
          $domain = domain_lookup($first_domain);

          // If the domain is invalid, go to the primary domain.
          if ($domain == -1 || empty($domain['valid']) && !user_access('access inactive domains')) {
            domain_goto(domain_default());
          }
          elseif ($domain['domain_id'] != $_domain['domain_id']) {
            domain_goto($domain);
          }
          break;

        // 3 == show checkboxes of available domains.
        case 3:

          // If the user has no available domains, then they cannot post.
          if (empty($user_options)) {
            $form = array();
            return drupal_access_denied();
          }
          $form['domain'] = array(
            '#type' => 'fieldset',
            '#title' => t('Affiliate publishing options'),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
            '#group' => variable_get('domain_vertical_tab', 0) ? 'additional_settings' : '',
          );

          // We must preserve publishing options that the user cannot access, but only for
          // existing nodes.
          if (!empty($form['#node']->nid)) {
            $raw = $raw_options;
          }
          else {
            $raw = array();
          }

          // If the raw options are being passed, then no input is technically required.
          empty($raw) ? $required = TRUE : ($required = FALSE);
          $form['domain']['domains'] = array(
            '#type' => empty($format) ? 'checkboxes' : 'select',
            '#title' => t('Publish to'),
            '#options' => $user_options,
            '#required' => $required,
            '#description' => t('Select which affiliates can access this content.'),
            '#default_value' => isset($form['#node']->domains) ? $form['#node']->domains : $default_options,
          );
          if ($format) {
            $form['domain']['domains']['#multiple'] = TRUE;
            $form['domain']['domains']['#size'] = count($user_options) > 10 ? 10 : count($user_options);
          }

          // Show the options that cannot be changed.
          $list = array();
          if (!empty($form['#node']->domain_site)) {
            $list[]['data'] = t('All affiliates');
          }
          if (!empty($raw)) {
            foreach ($raw as $did) {
              $did == -1 ? $id = 0 : ($id = $did);
              $raw_domains = domain_lookup($id);
              $list[]['data'] = check_plain($raw_domains['sitename']);
            }
          }
          if (!empty($list)) {
            $form['domain']['domains_notes'] = array(
              '#type' => 'item',
              '#title' => t('Publishing status'),
              '#markup' => theme('item_list', array(
                'items' => $list,
              )),
              '#description' => t('This content has also been published to these affiliates.'),
            );
          }
          break;
      }
    }

    // These form elements are hidden from non-privileged users, by design.
    $form['domain_site'] = array(
      '#type' => 'value',
      '#value' => isset($form['#node']->domain_site) ? $form['#node']->domain_site : $behavior,
    );

    // Domains that have been assigned and cannot be changed.
    $form['domains_raw'] = array(
      '#type' => 'value',
      '#value' => $raw,
    );
  }

  // THIS SECTION BREAKS CCK if we don't check for cck_dummy_node_form!  See http://drupal.org/node/186624
  // and note the !$form['#node']->cck_dummy_node_form in the IF check at the top of the function.
  // Some editors cannot administer nodes, so we have to add these form elements.
  if (user_access('edit domain content')) {
    $access = variable_get('domain_form_elements', array(
      'options',
      'delete',
      'comment_settings',
      'path',
    ));
    foreach ($access as $item) {
      $form[$item]['#access'] = TRUE;
    }
  }
}