You are here

function inline_conditions_field_widget_form in Inline Conditions 7

Implements hook_field_widget_form().

File

./inline_conditions.field.inc, line 150
Field module functionality for the Inline Conditions module.

Code

function inline_conditions_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // Ensure this .inline_conditions files are loaded when the form is rebuilt
  // from the cache.
  foreach (inline_conditions_get_info_by_module() as $module => $condition) {
    $form_state['build_info']['files']["form_ic_{$module}"] = drupal_get_path('module', $module) . "/{$module}.inline_conditions.inc";
  }

  // Prepare a list of all possible conditions.
  $condition_info = inline_conditions_get_info_by_type(empty($instance['settings']['entity_type']) ? 'commerce_order' : $instance['settings']['entity_type'], $instance['entity_type']);

  // Build main wrapper id.
  $main_wrapper_id = sprintf('inline-conditions-%s', $field['field_name']);
  $element = array(
    '#type' => 'container',
    '#id' => $main_wrapper_id,
    '#caption' => $element['#title'],
    '#theme' => 'inline_conditions_table',
    '#element_validate' => array(
      'inline_conditions_field_widget_form_validate',
    ),
  ) + $element;

  // Attach module style sheet to widget form.
  $form['#attached']['css'][] = drupal_get_path('module', 'inline_conditions') . '/css/inline_conditions.css';

  // If "Add a new condition" button has been pressed.
  if (isset($form_state['triggering_element']['#condition_action'])) {
    switch ($form_state['triggering_element']['#condition_action']) {
      case 'and':
        $items[] = array(
          'condition_name' => '',
          'condition_settings' => array(),
          'condition_logic_operator' => INLINE_CONDITIONS_AND,
        );
        break;
      case 'or':
        $items[] = array(
          'condition_name' => '',
          'condition_settings' => array(),
          'condition_logic_operator' => INLINE_CONDITIONS_OR,
        );
        break;
      case 'remove':
        unset($items[$form_state['triggering_element']['#element_delta']]);

        // Rebuild array keys.
        $items = array_values($items);
        break;
    }
  }

  // If no items found, create an empty item for display purposes.
  if (empty($items)) {
    $items[] = array(
      'condition_name' => '',
      'condition_settings' => array(),
    );
  }
  foreach ($items as $delta => $item) {
    $settings_wrapper_id = sprintf('inline-conditions-settings-wrapper-%s-%s', $instance['id'], $delta);

    // Condition selector.
    $element[$delta]['condition_name'] = array(
      '#type' => 'select',
      '#options' => inline_conditions_options_list($condition_info),
      '#default_value' => $item['condition_name'],
      '#ajax' => array(
        'callback' => 'inline_conditions_form_ajax_callback',
        'wrapper' => $settings_wrapper_id,
      ),
      '#condition_action' => 'select',
      // Identifies the condition operated upon.
      '#element_delta' => $delta,
      '#limit_validation_errors' => array(),
    );

    // Condition settings.
    $element[$delta]['condition_settings'] = array(
      '#type' => 'value',
      '#value' => array(),
    );

    // A condition has been selected, and a "configure" callback exists. Fills
    // up the condition_settings key with form elements returned by condition
    // configure callback.
    if (!empty($item['condition_name']) && !empty($condition_info[$item['condition_name']]['callbacks']['configure'])) {
      $callback = $condition_info[$item['condition_name']]['callbacks']['configure'];
      $element[$delta]['condition_settings'] = $callback($item['condition_settings'], $instance, $delta);
      $element[$delta]['#attributes'] = array(
        'class' => array(
          'container-inline',
        ),
      );
    }

    // Merge additional properties for the ajax wrapping.
    $element[$delta]['condition_settings'] += array(
      '#prefix' => sprintf('<div id="%s" class="%s">', $settings_wrapper_id, 'inline-conditions-settings-wrapper'),
      '#suffix' => '</div>',
    );
    $element[$delta]['condition_negate'] = array(
      '#type' => 'checkbox',
      '#title' => t('Negate'),
      '#default_value' => !empty($item['condition_negate']) ? $item['condition_negate'] : FALSE,
    );

    // Remove condition button.
    $element[$delta]['remove_condition'] = array(
      '#type' => 'button',
      '#name' => $field['field_name'] . '-' . $instance['id'] . '-' . $delta . '-remove_condition',
      '#value' => t('Remove'),
      '#ajax' => array(
        'callback' => 'inline_conditions_form_ajax_callback',
        'wrapper' => 'inline-conditions-' . $field['field_name'],
        'effect' => 'fade',
      ),
      '#element_delta' => $delta,
      '#condition_action' => 'remove',
      '#executes_submit_callback' => FALSE,
      '#limit_validation_errors' => array(),
    );

    // Add an option list to select logical disjunctions for each inline
    // condition.
    if ($delta > 0) {

      // Add an option list to select the logic operator for the current
      // condition.
      $element[$delta]['condition_logic_operator'] = array(
        '#type' => 'select',
        '#options' => array(
          INLINE_CONDITIONS_OR => t('OR'),
          INLINE_CONDITIONS_AND => t('AND'),
        ),
        '#default_value' => isset($item['condition_logic_operator']) ? $item['condition_logic_operator'] : INLINE_CONDITIONS_AND,
        '#attributes' => array(
          'class' => array(
            'inline-conditions-logic-operator',
          ),
        ),
        '#weight' => -50,
      );
    }
  }

  // Prepare the action buttons.
  $element['and_condition'] = array(
    '#type' => 'button',
    '#name' => sprintf('%s-%s-and-condition', $field['field_name'], $instance['id']),
    '#value' => t('Attach AND condition'),
    '#ajax' => array(
      'callback' => 'inline_conditions_form_ajax_callback',
      'wrapper' => $main_wrapper_id,
      'effect' => 'fade',
    ),
    '#attributes' => array(
      'class' => array(
        'and-condition',
      ),
    ),
    '#element_delta' => $element['#delta'],
    '#condition_action' => 'and',
    '#limit_validation_errors' => array(),
  );
  $element['or_condition'] = array(
    '#type' => 'button',
    '#name' => sprintf('%s-%s-or-condition', $field['field_name'], $instance['id']),
    '#value' => t('Attach OR condition'),
    '#ajax' => array(
      'callback' => 'inline_conditions_form_ajax_callback',
      'wrapper' => $main_wrapper_id,
      'effect' => 'fade',
    ),
    '#attributes' => array(
      'class' => array(
        'or-condition',
      ),
    ),
    '#element_delta' => $element['#delta'],
    '#condition_action' => 'or',
    '#limit_validation_errors' => array(),
  );
  return $element;
}