You are here

function civicrm_entity_contact_group_assign_field_field_widget_form in CiviCRM Entity 7.2

Implements hook_field_widget_form().

File

modules/civicrm_entity_contact_group_assign_field/civicrm_entity_contact_group_assign_field.module, line 235
Provide CiviCRM Entity Contact Group Assign Field Type. Provides a widget for adding/removing a contact to a selected list of groups.

Code

function civicrm_entity_contact_group_assign_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $orig_element = $element;
  $options = _civicrm_entity_contact_group_assign_field_get_field_settings_groups($field);
  $values = array();
  if (!empty($items[$delta]['groups'])) {
    foreach ($items[$delta]['groups'] as $group_id => $value) {
      $values[$group_id] = $value;
    }
  }
  switch ($instance['widget']['type']) {
    case 'civicrm_entity_contact_group_assign_field_checkboxes_widget':
      $widget = array();
      $widget = $orig_element + array(
        '#type' => 'checkboxes',
        '#default_value' => $values,
        '#options' => $options,
      );

      //just have field label?
      $widget['#title'] = $instance['label'];
      $widget['#weight'] = 0;
      $element['groups'] = $widget;
      break;
    case 'civicrm_entity_contact_group_assign_field_radios_widget':
      $default_value = 0;
      foreach ($values as $group_id => $value) {
        if ($value) {
          $default_value = $group_id;
          break;
        }
      }
      $widget = array();
      $widget = $orig_element + array(
        '#type' => 'radios',
        '#default_value' => $default_value,
        '#options' => $options,
      );

      //just have field label?
      $widget['#title'] = $instance['label'];
      $widget['#weight'] = 0;
      $element['groups'] = $widget;
      break;
  }
  return $element;
}