You are here

function domain_entity_source_field_widget_form_alter in Domain Access Entity 7

Implements hook_field_widget_form().

File

domain_entity_source/domain_entity_source.module, line 6

Code

function domain_entity_source_field_widget_form_alter(&$element, $form_state, $context) {
  if ($context['field']['type'] == 'domain_entity' && !empty($element['domain_id'])) {
    $items = isset($context['items']) ? $context['items'] : array();

    // Set the domain default value if no value has been set :
    $default_value = 0;
    if (isset($items[0]) && !empty($items[0]) && isset($items[0]['domain_id'])) {
      foreach ($items as $delta => $item) {
        if (isset($item['domain_source']) && $item['domain_source']) {
          $default_value = $item['domain_id'];
        }
      }
    }

    // Get the accessible domains option.
    $options = $element['domain_id']['#options'];
    foreach ($element['domain_id'] as $key => $option) {
      if (strpos($key, '#') === 0) {
        continue;
      }
      if ($key != 0 && !empty($option['#disabled'])) {
        unset($options[$key]);
      }
    }
    $element['domain_source'] = array(
      '#title' => t('Domain source'),
      '#description' => t('Select the canonical domain'),
      '#type' => 'select',
      '#options' => $options,
      '#default_value' => $default_value,
    );
    $element['domain_source']['#options'][0] = t('No canonical domain.');
  }
}