You are here

function domain_form_alter in Domain Access 5

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

Implement 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 1039
Core module functions for the Domain Access suite.

Code

function domain_form_alter($form_id, &$form) {

  // 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'])) {
    global $_domain;
    $action = parse_url($form['#action']);
    if ($action['query']) {
      $action['path'] .= '?';
    }

    // 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'] = check_url($_domain['scheme'] . '://' . $_domain['subdomain'] . $action['path'] . $action['query']);
    }
  }

  // Save our settings for the default domain.
  if ($form_id == 'system_site_information_settings' && arg(2) != 'domain') {
    $form['#submit']['domain_form_sitename_submit'] = array();
  }

  // Apply to all node editing forms, but make sure we are not on the CCK field configuration form.
  if ($form['#id'] == 'node-form' && !$form['#node']->cck_dummy_node_form) {
    global $_domain, $user;

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

    // How is core content handled for this site?
    $behavior = variable_get('domain_behavior', DOMAIN_INSTALL_RULE);
    if ($_domain['domain_id'] == 0) {
      $default[] = -1;
    }

    // Some options will be passed as hidden values, we need to run some checks on those.
    if ($form['#node']->nid) {
      $raw = $form['#node']->domains;
    }
    else {
      $raw = $default;
    }
    $options = array();
    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('administer domains')) {
        $options[$key] = check_plain($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.'),
        '#default_value' => isset($form['#node']->nid) ? $form['#node']->domain_site : variable_get('domain_node_' . $form['#node']->type, $behavior),
      );
      $form['domain']['domains'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Publish to'),
        '#options' => $options,
        '#required' => TRUE,
        '#description' => t('Select which affiliates can access this content.'),
        '#default_value' => isset($form['#node']->nid) ? $form['#node']->domains : $default,
      );
    }
    else {
      if (user_access('view domain publishing')) {
        $action = variable_get('domain_options', 0);
        $user_domains = array();
        $default_options = array();
        if (!isset($user->domain_user)) {
          $user->domain_user = array();
        }
        foreach ($user->domain_user as $key => $value) {
          if (abs($value) > 0) {
            $user_domains[] = $value;
          }
        }
        $first_domain = current($user_domains);
        $user_options = array();
        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 || $domain['valid'] == 0) {
              domain_goto(domain_default());
            }
            else {
              if ($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,
            );

            // We must preserve publishing options that the user cannot access, but only for
            // existing nodes.
            if ($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' => 'checkboxes',
              '#title' => t('Publish to'),
              '#options' => $user_options,
              '#required' => $required,
              '#description' => t('Select which affiliates can access this content.'),
              '#default_value' => isset($form['#node']->nid) ? $form['#node']->domains : $default_options,
            );

            // Show the options that cannot be changed.
            $list = array();
            if ($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(
                '#value' => '<label><b>' . t('Publishing status:') . '</b></label>' . theme('item_list', $list) . '<div class="description">' . t('This content has also been published to these affiliates.') . '</div>',
              );
            }
            break;
        }
      }

      // These form elements are hidden from non-privileged users, by design.
      $form['domain_site'] = array(
        '#type' => 'value',
        '#value' => isset($form['#node']->nid) ? $form['#node']->domain_site : variable_get('domain_node_' . $form['#node']->type, $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 (variable_get('domain_editors', DOMAIN_EDITOR_RULE) == 1 && user_access('edit domain nodes')) {
      $access = variable_get('domain_form_elements', array(
        'options',
        'delete',
        'comment_settings',
        'path',
      ));
      foreach ($access as $item) {
        $form[$item]['#access'] = TRUE;
      }
    }
  }
}