You are here

function domain_entity_field_widget_form in Domain Access Entity 7

Implements hook_field_widget_form().

File

./domain_entity.module, line 525
Defines field (e.g. domain_entity) for entities, and access query alter.

Code

function domain_entity_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $widget = $instance['widget'];
  $current_domain = domain_get_domain();
  $form_state['domain_entity_field_name'] = $field['field_name'];
  $allowed_entity_types = domain_entity_allowed_entity_types();
  $site_domains = domain_domains();
  $checkboxes = array(
    DOMAIN_ENTITY_SEND_TO_ALL => t('Send to all affiliates'),
  );
  foreach ($site_domains as $site_domain) {
    $checkboxes[$site_domain['domain_id']] = $site_domain['sitename'];
  }

  // Set the domain default value if no value has been set :
  $default_values = array();
  if (!isset($items[0]) || empty($items[0]) || !isset($items[0]['domain_id'])) {

    // Get default bundle value,
    // e.g (all, current_domain, list(domain_id)).
    $default_bundle_values = reset($allowed_entity_types[$instance['entity_type']][$instance['bundle']]);
    if (in_array(DOMAIN_ALL, $default_bundle_values)) {
      $default_values[DOMAIN_ENTITY_SEND_TO_ALL] = DOMAIN_ENTITY_SEND_TO_ALL;
    }
    elseif (in_array(DOMAIN_ACTIVE, $default_bundle_values)) {
      $default_values[$current_domain['domain_id']] = $current_domain['domain_id'];
    }
    else {
      foreach ($default_bundle_values as $did) {
        $default_values[$did] = $did;
      }
    }
  }
  else {
    foreach ($items as $delta => $item) {
      $default_values[$item['domain_id']] = $item['domain_id'];
    }
  }

  // Add a checkboxes for domain id's:
  $element['domain_id'] = array(
    '#type' => 'checkboxes',
    '#options' => $checkboxes,
    '#title' => t('Domain'),
    '#default_value' => $default_values,
    '#required' => TRUE,
    '#element_validate' => array(
      'domain_entity_widget_multiple_values_form_validate',
    ),
    '#entity_type' => $instance['entity_type'],
  );
  if (!user_access('set domain access status for all entities')) {

    // Send to all affiliates should not be accessible.
    $element['domain_id'][DOMAIN_ENTITY_SEND_TO_ALL]['#disabled'] = TRUE;

    // Disable the domain checkbox that the user cannot edit.
    global $user;
    $user_granted_domain_ids = domain_get_user_domains($user);
    $is_editable = FALSE;
    foreach ($site_domains as $domain) {

      // We let the super user override this restriction.
      if (!in_array($domain['domain_id'], $user_granted_domain_ids) && $domain['domain_id'] != 0 && $user->uid != 1) {
        $element['domain_id'][$domain['domain_id']]['#disabled'] = TRUE;
      }
      else {
        $is_editable = TRUE;
      }
    }
    if (!$is_editable) {
      $element['domain_id']['#access'] = FALSE;
    }
  }
  if ($widget['type'] == DOMAIN_ENTITY_BEHAVIOR_AUTO) {
    $element['domain_id']['#access'] = FALSE;
  }
  return $element;
}