You are here

function context_field_field_widget_form in Context Field 7

Implements hook_field_widget_form().

Set the context param as a value so that it gets passed through.

File

./context_field.module, line 309
Context Field

Code

function context_field_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {
  switch ($instance['widget']['type']) {

    // Find all Context that are in our category and make a select list to pick from
    case 'context_field_autocomplete':
      $cat = $instance['widget']['settings']['form_element']['category'];
      $default_value = '';
      if (!empty($items[$delta]['context']) && ($context = context_load($items[$delta]['context']))) {
        $default_value = "{$context->description} [{$context->name}]";
      }
      $field_form['context'] = array(
        '#title' => check_plain($instance['label']),
        '#type' => 'textfield',
        '#autocomplete_path' => "context_field/autocomplete/context/{$cat}",
        '#default_value' => $default_value,
        '#description' => check_plain($instance['description']),
      );
      return $field_form;
      break;
    case 'context_field_select':
      $contexts = context_load();
      $context_options[''] = t("No Context Selected");
      $cat = $instance['widget']['settings']['form_element']['category'];
      foreach ($contexts as $key => $context) {
        if (isset($context->conditions['context_field']['values'][1]) && isset($context->conditions['context_field']['options']['context_field_category']) && $context->conditions['context_field']['options']['context_field_category'] == $cat) {
          $context_options[$key] = $context->description ? $context->description : $key;
        }
      }
      $field_form['context'] = array(
        '#title' => check_plain($instance['label']),
        '#type' => 'select',
        '#options' => $context_options,
        '#default_value' => isset($items[$delta]['context']) ? $items[$delta]['context'] : $instance['settings']['default_context'],
        '#description' => check_plain($instance['description']),
      );
      return $field_form;
      break;
    case 'context_field':
    case 'context_field_default':
      if ($instance['widget']['settings']['form_element']['user_toggle']) {
        $default_value = isset($items[$delta]['context']) ? (bool) $items[$delta]['context'] : 0;
        $field_form['context'] = array(
          '#type' => 'checkbox',
          '#title' => check_plain($instance['label']),
          '#description' => check_plain($instance['description']),
          '#default_value' => $default_value,
        );
      }
      else {
        $field_form['context'] = array(
          '#type' => 'hidden',
          '#value' => isset($items[$delta]['context']) ? $items[$delta]['context'] : '',
        );
      }
      return $field_form;
      break;
    default:
      $field_form['context'] = array(
        '#type' => 'hidden',
        '#value' => isset($items[$delta]['context']) ? $items[$delta]['context'] : '',
      );
      return $field_form;
      break;
  }
}