You are here

function acquia_lift_form_field_ui_field_edit_form_alter in Acquia Lift Connector 7

Same name and namespace in other branches
  1. 7.2 acquia_lift.module \acquia_lift_form_field_ui_field_edit_form_alter()

Implements hook_form_FORM_ID_alter().

File

./acquia_lift.module, line 2624
acquia_lift.module Provides Acquia Lift-specific personalization functionality.

Code

function acquia_lift_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  if (!empty($form['field']['settings']['personalize']['enabled'])) {
    $field = $form['#field'];

    // Due to the way the Acquia Lift JS widget works for personalized fields,
    // we actually have to enforce (for now) that when Acquia Lift is enabled,
    // only fields with unlimited cardinality may be personalized.
    $form['field']['settings']['personalize']['enabled']['#states'] = array(
      'enabled' => array(
        ':input[name="field[cardinality]"]' => array(
          'value' => -1,
        ),
      ),
      'valid' => array(
        ':input[name="field[cardinality]"]' => array(
          'value' => -1,
        ),
      ),
    );
    $form['field']['settings']['personalize']['enabled']['#description'] = t('This setting is only valid for fields with unlimited cardinality.');

    // Add a state to the form elements to make sure they only appear when the
    // personalizable checkbox is checked.
    $state = array(
      ':input[name="field[settings][personalize][enabled]"]' => array(
        'checked' => TRUE,
      ),
    );

    // Checkbox for auto creation of a goal should only be visible if the selected
    // campaign type is one of the Lift campaign types.
    $visible_states = array();
    $acquia_lift_agent_types = acquia_lift_get_agent_types();
    foreach (array_keys($acquia_lift_agent_types) as $type) {
      $visible_states[] = array(
        'value' => $type,
      );
    }
    $form['field']['settings']['personalize']['create_goal'] = array(
      '#type' => 'checkbox',
      '#title' => t('Auto-create goal'),
      '#default_value' => isset($field['settings']['personalize']['create_goal']) ? $field['settings']['personalize']['create_goal'] : 1,
      '#description' => t('Should a goal of "clicks the field" automatically get created for this campaign?'),
      '#states' => array(
        'visible' => array(
          ':input[name="field[settings][personalize][agent_type]"]' => $visible_states,
        ),
      ),
    );
    personalize_form_element_add_states($state, $form['field']['settings']['personalize']['create_goal']);

    // Textarea to limit the pages the goal fires on.
    $form['field']['settings']['personalize']['goal_pages'] = array(
      '#type' => 'textarea',
      '#title' => t('Pages for goal'),
      '#default_value' => isset($field['settings']['personalize']['goal_pages']) ? $field['settings']['personalize']['goal_pages'] : '',
      '#description' => t('Specify pages to limit the goal to using their paths (one path per line.) Leave blank to apply to all pages.'),
      '#states' => array(
        'visible' => array(
          ':input[name="field[settings][personalize][agent_type]"]' => $visible_states,
          ':input[name="field[settings][personalize][create_goal]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    personalize_form_element_add_states($state, $form['field']['settings']['personalize']['goal_pages']);

    // Checkbox for auto-starting the campaign should only be visible if the chosen agent
    // type is a Lift agent type and the auto-create goal box is checked.
    $form['field']['settings']['personalize']['auto_start'] = array(
      '#type' => 'checkbox',
      '#title' => t('Auto-start the campaign'),
      '#default_value' => isset($field['settings']['personalize']['auto_start']) ? $field['settings']['personalize']['auto_start'] : 1,
      '#description' => t('Should the campaign be set to running automatically after creation?'),
      '#states' => array(
        'visible' => array(
          ':input[name="field[settings][personalize][agent_type]"]' => $visible_states,
          ':input[name="field[settings][personalize][create_goal]"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
    personalize_form_element_add_states($state, $form['field']['settings']['personalize']['auto_start']);

    // Add a checkbox for automatically stopping the campaign once a winner has been found.
    $form['field']['settings']['personalize']['auto_stop'] = array(
      '#type' => 'checkbox',
      '#title' => t('End when campaign thresholds are reached'),
      '#default_value' => isset($field['settings']['personalize']['auto_stop']) ? $field['settings']['personalize']['auto_stop'] : 0,
      '#description' => t('Should the campaign\'s status be changed to complete when both the campaign !duration and !decisions thresholds are reached? In this case, the variation with the highest number of completed goals at that time is set as the winning variation for display.', array(
        '!duration' => l(t('duration'), 'admin/config/content/personalize/acquia_lift'),
        '!decisions' => l(t('decisions'), 'admin/config/content/personalize/acquia_lift'),
      )),
      '#states' => array(
        'visible' => array(
          ':input[name="field[settings][personalize][agent_type]"]' => $visible_states,
        ),
      ),
    );
    personalize_form_element_add_states($state, $form['field']['settings']['personalize']['auto_stop']);
  }
}