You are here

function civicrm_entity_profile_field_widget_form in CiviCRM Entity 7.2

Implements hook_field_widget_form().

File

modules/civicrm_entity_profile/civicrm_entity_profile.module, line 431

Code

function civicrm_entity_profile_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  $orig_element = $element;
  $options = _civicrm_entity_profile_get_profiles_for_options_list();
  switch ($instance['widget']['type']) {
    case 'civicrm_entity_profile_field_default_widget':
      $uf_group_id = NULL;
      $target_id = NULL;
      if (!empty($items[$delta]['target_id'])) {
        $target_id = $items[$delta]['target_id'];
        $uf_join = entity_load_single('civicrm_uf_join', $target_id);
        if (!empty($uf_join->uf_group_id)) {
          $uf_group_id = $uf_join->uf_group_id;
        }
      }
      $widget = array();
      $widget = $orig_element + array(
        '#type' => 'hidden',
        '#value' => $target_id,
      );

      //just have field label?
      $widget['#weight'] = 0;
      $element['target_id'] = $widget;
      $widget = array();
      $widget = $orig_element + array(
        '#type' => 'select',
        '#default_value' => $uf_group_id,
        '#options' => $options,
      );

      //just have field label?
      $widget['#weight'] = 0;
      $element['uf_group_id'] = $widget;
      break;
  }

  // add remove button for fields with unlimited field values
  // code example taken from https://www.drupal.org/project/multiple_fields_remove_button module
  $field_parents = isset($element['#field_parents']) ? $element['#field_parents'] : array();
  $field_name = isset($element['#field_name']) ? $element['#field_name'] : NULL;
  $parents = array_merge($field_parents, array(
    $field_name,
    $langcode,
    $delta,
  ));
  if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
    $element['remove_button'] = array(
      '#delta' => $delta,
      '#name' => implode('_', $parents) . '_remove_button',
      '#type' => 'submit',
      '#value' => t('Remove'),
      '#validate' => array(),
      '#attributes' => array(
        'class' => array(
          'multiple-fields-remove-button',
        ),
      ),
      '#submit' => array(
        '_civicrm_entity_profile_remove_button_submit_handler',
      ),
      '#limit_validation_errors' => array(),
      '#ajax' => array(
        'path' => 'civicrm_entity_profile_multiple_fields_remove_button/ajax',
        'effect' => 'fade',
      ),
      '#weight' => 1000,
    );
  }
  return $element;
}