You are here

function redhen_fields_field_widget_form in RedHen CRM 7

Implements hook_field_widget_form().

File

modules/redhen_fields/redhen_fields.module, line 496
Defines email, phone and address field types for RedHen CRM.

Code

function redhen_fields_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  switch ($instance['widget']['type']) {
    case 'redhen_email_widget':
      $field_class = drupal_html_class($field['type']);
      if ($field['cardinality'] == 1) {
        $element_class = $field_class . '-single-value';
        $element['#prefix'] = '<div class="' . $element_class . '">';
        $element['#suffix'] = '</div>';
      }
      $element['value'] = array(
        '#title' => t("@label", array(
          '@label' => $instance['label'],
        )),
        '#type' => 'textfield',
        '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
        '#description' => isset($instance['description']) ? $instance['description'] : NULL,
        '#size' => 35,
        '#weight' => -1,
      );
      $fieldset_info = element_info('fieldset');
      $process = array_merge($fieldset_info['#process'], array(
        'redhen_fields_flatten_email_options',
      ));
      $element['options'] = array(
        '#type' => 'container',
        '#attributes' => array(
          'class' => array(
            $field_class . '-options',
          ),
        ),
        '#weight' => 1,
        '#process' => $process,
      );
      if (!empty($field['settings']['labels'])) {
        $label_options[0] = '- None -';
        $label_options = $label_options + $field['settings']['labels'];
        $element['options']['label_id'] = array(
          '#title' => t('Label'),
          '#type' => 'select',
          '#multiple' => FALSE,
          '#options' => $label_options,
          '#default_value' => isset($items[$delta]['label_id']) ? $items[$delta]['label_id'] : 0,
          '#weight' => 1,
        );
      }
      $element['options']['hold'] = array(
        '#title' => t('Hold'),
        '#type' => 'checkbox',
        '#default_value' => isset($items[$delta]['hold']) ? $items[$delta]['hold'] : 0,
        '#weight' => 2,
      );
      $element['options']['bulk'] = array(
        '#title' => t('Bulk'),
        '#type' => 'checkbox',
        '#default_value' => isset($items[$delta]['bulk']) ? $items[$delta]['bulk'] : 0,
        '#weight' => 2,
      );
      $element['options']['default'] = array(
        '#title' => t('Primary'),
        '#type' => 'checkbox',
        '#default_value' => isset($items[$delta]['default']) ? $items[$delta]['default'] : $delta == 0,
        '#weight' => 2,
      );
      break;
    case "redhen_email_simple_widget":
      $field_class = drupal_html_class($field['type']);
      if ($field['cardinality'] == 1) {
        $element_class = $field_class . '-single-value';
        $element['#prefix'] = '<div class="' . $element_class . '">';
        $element['#suffix'] = '</div>';
      }
      $element['value'] = array(
        '#title' => t("@label", array(
          '@label' => $instance['label'],
        )),
        '#type' => 'textfield',
        '#description' => isset($instance['description']) ? $instance['description'] : NULL,
        '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
        '#size' => 35,
      );
      $element['default'] = array(
        '#title' => t('Primary'),
        '#type' => 'checkbox',
        '#default_value' => TRUE,
        '#access' => FALSE,
      );
      if (module_exists('redhen_contact')) {
        $element['value']['#required'] = variable_get(REDHEN_CONTACT_REQUIRE_EMAIL, FALSE);
      }
      break;
  }
  $path = drupal_get_path('module', 'redhen_fields');
  $element['#attached']['js'] = array(
    $path . '/redhen_fields.js',
  );
  $element['#attached']['css'] = array(
    $path . '/redhen_fields.css',
  );
  return $element;
}