You are here

function domain_form_alter in Domain Access 7.3

Same name and namespace in other branches
  1. 5 domain.module \domain_form_alter()
  2. 6.2 domain.module \domain_form_alter()
  3. 7.2 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 2451
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('domain_ignore');
  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_user_set($user);
  $_domain = domain_get_domain();

  // Get the default assigned domains.
  $defaults = domain_get_node_defaults($form['#node']->type);

  // 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.
  $default_domain_site = (int) $defaults['domain_site'];

  // 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 = $defaults['domain_id'];
  }
  $options = array();

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

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

      // Checkboxes must be filtered, select lists should not.
      $options[$data['domain_id']] = 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' => variable_get('domain_collapse_options', 0),
    );

    // Display in vertical tab group, if required
    if (variable_get('domain_vertical_tab', 0)) {
      $form['domain']['#group'] = 'additional_settings';
      $form['domain']['#attributes'] = array(
        'class' => array(
          'domain-access-options-form',
        ),
      );
      $form['domain']['#attached'] = array(
        'js' => array(
          drupal_get_path('module', 'domain') . '/domain.node.js',
          array(
            'data' => array(
              'domain' => array(
                'fieldType' => $format,
              ),
            ),
            'type' => 'setting',
          ),
        ),
      );
    }
    $form['domain']['domain_site'] = array(
      '#type' => 'checkbox',
      '#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 : $default_domain_site,
    );
    $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 : $defaults['domain_id'],
    );
    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)) {
            drupal_access_denied();
            drupal_exit();
          }
          $form['domain'] = array(
            '#type' => 'fieldset',
            '#title' => t('Affiliate publishing options'),
            '#collapsible' => TRUE,
            '#collapsed' => variable_get('domain_collapse_options', 0),
          );

          // Display in vertical tab group, if required
          if (variable_get('domain_vertical_tab', 0)) {
            $form['domain']['#group'] = 'additional_settings';
            $form['domain']['#attributes'] = array(
              'class' => array(
                'domain-access-options-form',
              ),
            );
            $format = domain_select_format();
            $form['domain']['#attached'] = array(
              'js' => array(
                drupal_get_path('module', 'domain') . '/domain.node.js',
                array(
                  'data' => array(
                    'domain' => array(
                      'fieldType' => $format,
                    ),
                  ),
                  'type' => 'setting',
                ),
              ),
            );
          }

          // 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) {
              $raw_domains = domain_lookup($did);
              $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 : $default_domain_site,
    );

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