You are here

function flexiform_conditional_fields_configure_form in Flexiform 7

Form to edit conditional fields.

1 string reference to 'flexiform_conditional_fields_configure_form'
flexiform_conditional_fields_operation in flexiform_conditional_fields/flexiform_conditional_fields.admin.inc
Page Callback for handling conditional fields operations.

File

flexiform_conditional_fields/flexiform_conditional_fields.admin.inc, line 239
Admin UI for flexiform_conditional_fields.

Code

function flexiform_conditional_fields_configure_form($form, &$form_state, $flexiform, $element_namespace, $dependency_key) {
  list($dependee) = explode('|', $dependency_key);
  $form['#flexiform'] = $flexiform;
  $form['#flexiform_element'] = FlexiformElement::getElement($flexiform, $element_namespace);
  $form['#flexiform_dependee'] = FlexiformElement::getElement($flexiform, $dependee);
  $form['#dependency_key'] = $dependency_key;
  $settings = $form['#flexiform_element']
    ->getSettings();
  $options = $settings['conditional_fields'][$dependency_key];
  $form['#attached']['css'][] = drupal_get_path('module', 'conditional_fields') . '/conditional_fields.css';
  if ($form['#flexiform_dependee'] instanceof FlexiformElementFieldAPIInterface) {

    // Retrieve needed information from the dependee instance.
    // Since we only have the instance id here (id column of the
    // field_config_instance table), not the entity id (id column of field_config)
    // we can't call field_info_field_by_id. This is needed because we don't
    // want dependencies to be shared between bundles.
    // So we first load instance information to obtain the entity id, then we load
    // the entity using field_info_field().
    $dependee_instance = $form['#flexiform_dependee']
      ->getInstance();
    $dependee_field = $form['#flexiform_dependee']
      ->getField();

    // Build a dummy field widget to use as form field in single value selection
    // option.
    $dummy_form = array(
      '#parents' => array(),
    );
    $dependee_instance['default_value'] = $options['value'];
    $dependee_instance['default_value_function'] = '';
    $dependee_instance['required'] = FALSE;
    $dummy_field = field_default_form($dependee_instance['entity_type'], NULL, $dependee_field, $dependee_instance, LANGUAGE_NONE, array(), $dummy_form, $form_state);
  }
  else {
    $dummy_field = array(
      '#markup' => t('Value comparisons are only supported for field API fields.'),
    );
  }
  $checkboxes = FALSE;
  if ($dependee_instance) {
    $checkboxes = $dependee_instance['widget']['type'] == 'options_buttons' && $dependee_field['cardinality'] != 1 || $dependee_instance['widget']['type'] == 'options_onoff' ? TRUE : FALSE;
  }
  $condition_options = conditional_fields_conditions($checkboxes);

  // Currently only support value if this is an actual field API field.
  if (!$dependee_instance) {
    unset($condition_options['value']);
  }
  $form['condition'] = array(
    '#type' => 'select',
    '#title' => t('Condition'),
    '#description' => t('The condition that should be met by the dependee %field to trigger the dependency.', array(
      '%field' => $dependee_instance['label'],
    )),
    '#options' => $condition_options,
    '#default_value' => $options['condition'],
    '#required' => TRUE,
  );
  $form['values_set'] = array(
    '#type' => 'select',
    '#title' => t('Values input mode'),
    '#description' => t('The input mode of the values that trigger the dependency.'),
    '#options' => array(
      CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET => t('Insert value from widget...'),
      CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX => t('Regular expression...'),
      t('Set of values') => array(
        CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND => t('All these values (AND)...'),
        CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR => t('Any of these values (OR)...'),
        CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR => t('Only one of these values (XOR)...'),
        CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT => t('None of these values (NOT)...'),
      ),
    ),
    '#default_value' => $options['values_set'],
    '#required' => TRUE,
    '#states' => array(
      'visible' => array(
        ':input[name="condition"]' => array(
          'value' => 'value',
        ),
      ),
    ),
  );
  $form['value'] = array(
    '#type' => 'fieldset',
    '#title' => t('Insert value from widget'),
    '#description' => t('The dependency is triggered when the field has exactly the same value(s) inserted in the widget below.'),
    '#states' => array(
      'visible' => array(
        ':input[name="values_set"]' => array(
          'value' => (string) CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET,
        ),
        ':input[name="condition"]' => array(
          'value' => 'value',
        ),
      ),
    ),
    '#tree' => TRUE,
    'field' => $dummy_field,
  );
  $form['values'] = array(
    '#type' => 'textarea',
    '#title' => t('Set of values'),
    '#description' => t('The values of the dependee %field that trigger the dependency.', array(
      '%field' => $dependee_instance['label'],
    )) . '<br>' . t('Enter one value per line. Note: if the dependee has allowed values, these are actually the keys, not the labels, of those values.'),
    '#default_value' => implode("\n", $options['values']),
    '#states' => array(
      'visible' => array(
        ':input[name="values_set"]' => array(
          array(
            'value' => (string) CONDITIONAL_FIELDS_DEPENDENCY_VALUES_AND,
          ),
          array(
            'value' => (string) CONDITIONAL_FIELDS_DEPENDENCY_VALUES_OR,
          ),
          array(
            'value' => (string) CONDITIONAL_FIELDS_DEPENDENCY_VALUES_XOR,
          ),
          array(
            'value' => (string) CONDITIONAL_FIELDS_DEPENDENCY_VALUES_NOT,
          ),
        ),
        ':input[name="condition"]' => array(
          'value' => 'value',
        ),
      ),
      'required' => array(
        ':input[name="condition"]' => array(
          'value' => 'value',
        ),
      ),
    ),
  );
  $form['regex'] = array(
    '#type' => 'textfield',
    '#title' => t('Regular expression'),
    '#description' => t('The dependency is triggered when all the values of the dependee %field match the regular expression. The expression should be valid both in PHP and in Javascript. Do not include delimiters.', array(
      '%field' => $dependee_instance['label'],
    )) . '<br>' . t('Note: If the dependee has allowed values, these are actually the keys, not the labels, of those values.'),
    '#maxlength' => 2048,
    '#size' => 120,
    '#default_value' => isset($options['value']['RegExp']) ? $options['value']['RegExp'] : '',
    '#states' => array(
      'visible' => array(
        ':input[name="values_set"]' => array(
          'value' => (string) CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
        ),
        ':input[name="condition"]' => array(
          'value' => 'value',
        ),
      ),
      'required' => array(
        ':input[name="values_set"]' => array(
          'value' => (string) CONDITIONAL_FIELDS_DEPENDENCY_VALUES_REGEX,
        ),
        ':input[name="condition"]' => array(
          'value' => 'value',
        ),
      ),
    ),
  );
  $form['grouping'] = array(
    '#type' => 'radios',
    '#title' => t('Interaction with other dependencies'),
    '#description' => t('When this dependent has more than one dependee, how should this condition be evaluated against the others?') . '<br />' . t('Note that sets will be grouped this way: (ANDs) AND (ORs) AND (XORs).'),
    '#options' => array(
      'AND' => 'AND',
      'OR' => 'OR',
      'XOR' => 'XOR',
    ),
    '#default_value' => $options['grouping'],
    '#required' => TRUE,
  );
  $entity = entity_get_info($form['#flexiform_dependee']
    ->getEntityType());
  $form['entity_edit'] = array(
    '#type' => 'fieldset',
    '#title' => t('Edit context settings'),
    '#description' => t('These settings apply when the @entity is being added or edited in a form.', array(
      '@entity' => drupal_strtolower($entity['label']),
    )),
    '#collapsible' => FALSE,
  );
  $form['entity_edit']['state'] = array(
    '#type' => 'select',
    '#title' => t('Form state'),
    '#description' => t('The Javascript form state that is applied to the dependent field when the condition is met. Note: this has no effect on server-side logic and validation.'),
    '#options' => conditional_fields_states(),
    '#default_value' => $options['state'],
    '#required' => TRUE,
    '#ajax' => array(
      'callback' => 'conditional_fields_ajax_admin_state_callback',
      'wrapper' => 'effects-wrapper',
    ),
  );
  $effects = $effects_options = array();
  $selected_state = isset($form_state['values']['state']) ? $form_state['values']['state'] : $options['state'];
  foreach (conditional_fields_effects() as $effect_name => $effect) {
    if (in_array($selected_state, $effect['states'])) {
      $effects[$effect_name] = $effect['label'];
      if (isset($effect['options'])) {
        $effects_options[$effect_name] = $effect['options'];
      }
    }
  }
  $form['entity_edit']['effects_wrapper'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'id' => 'effects-wrapper',
    ),
  );
  $effect = isset($form_state['values']['effect']) ? $form_state['values']['effect'] : $options['effect'];
  if (count($effects) == 1) {
    $effects_keys = array_keys($effects);
    $form['entity_edit']['effects_wrapper']['effect'] = array(
      '#type' => 'hidden',
      '#value' => array_shift($effects_keys),
      '#default_value' => array_shift($effects_keys),
    );
  }
  elseif (count($effects) > 1) {
    $form['entity_edit']['effects_wrapper']['effect'] = array(
      '#type' => 'select',
      '#title' => t('Effect'),
      '#description' => t('The effect that is applied to the dependent when its state is changed.'),
      '#options' => $effects,
      '#default_value' => $effect,
      '#states' => array(
        'visible' => array(
          ':input[name="state"]' => array(
            array(
              'value' => 'visible',
            ),
            array(
              'value' => '!visible',
            ),
          ),
        ),
      ),
    );
  }
  $form['entity_edit']['effects_wrapper']['effect_options'] = array(
    '#tree' => TRUE,
  );
  foreach ($effects_options as $effect_name => $effect_options) {
    foreach ($effect_options as $effect_option_name => $effect_option) {
      $effect_option += array(
        '#title' => t('@effect effect option: @effect_option', array(
          '@effect' => $effects[$effect_name],
          '@effect_option' => $effect_option_name,
        )),
        '#states' => array(
          'visible' => array(
            ':input[name="effect"]' => array(
              array(
                'value' => $effect_name,
              ),
            ),
          ),
        ),
      );
      if (isset($form_state['values']['effect_options'][$effect_name][$effect_option_name])) {
        $effect_option['#default_value'] = $form_state['values']['effect_options'][$effect_name][$effect_option_name];
      }
      elseif ($options['effect'] == $effect_name) {
        $effect_option['#default_value'] = $options['effect_options'][$effect_option_name];
      }
      $form['entity_edit']['effects_wrapper']['effect_options'][$effect_name][$effect_option_name] = $effect_option;
    }
  }
  $form['entity_edit']['element_edit_per_role'] = array(
    '#type' => 'checkbox',
    '#title' => t('Activate per user role settings in edit context'),
    '#description' => t('If the user has more than one role, the first matching role will be used.'),
    '#default_value' => $options['element_edit_per_role'],
  );
  $behaviors = conditional_fields_behaviors();
  $form['entity_edit']['element_edit'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Edit context settings for all roles'),
    '#title_display' => 'invisible',
    '#options' => $behaviors['edit'],
    '#default_value' => $options['element_edit'],
    '#states' => array(
      'visible' => array(
        ':input[name="element_edit_per_role"]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
  );
  $roles = user_roles();
  $element_edit_roles = array(
    'element_edit_roles' => array(
      '#tree' => TRUE,
    ),
  );
  foreach ($roles as $rid => $role) {
    $element_edit_roles['element_edit_roles'][$rid] = array(
      '#type' => 'checkboxes',
      '#title' => t('Edit context settings for %role', array(
        '%role' => $role,
      )),
      '#options' => $behaviors['edit'],
      '#default_value' => isset($options['element_edit_roles'][$rid]) ? $options['element_edit_roles'][$rid] : $options['element_edit'],
      '#states' => array(
        'visible' => array(
          ':input[name="element_edit_per_role"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  array_push($form['entity_edit'], $element_edit_roles);
  $form['entity_edit']['dependency_advanced'] = array(
    '#type' => 'fieldset',
    '#title' => t('Advanced edit context settings', array(
      '@entity' => drupal_strtolower($entity['label']),
    )),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $selector_description = t('Only use if you know what you are doing, otherwise leave the field empty to let the dependency use an automatically generated selector.');
  $selector_description .= '<br />' . t('You can use the following placeholders:');
  $selector_description .= "<ul>\n";
  $selector_description .= '<li>' . t('%lang: current language of the field.') . "</li>\n";
  $selector_description .= '<li>' . t('%key: part identifier for fields composed of multiple form elements, like checkboxes.') . "</li>\n";
  $selector_description .= '</ul>';
  $form['entity_edit']['dependency_advanced']['selector'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom jQuery selector for dependee'),
    '#description' => $selector_description,
    '#default_value' => $options['selector'],
  );
  $form['actions'] = array(
    '#type' => 'actions',
    'save' => array(
      '#type' => 'submit',
      '#value' => t('Save settings'),
    ),
  );

  // Redirect to bundle dependencies form if destination is set.
  $destination = drupal_get_destination();
  if ($destination['destination'] != 'admin/structure/dependencies') {
    $form_state['redirect'] = $destination['destination'];
  }
  return $form;
}