You are here

function conditional_fields_dependency_edit_form in Conditional Fields 7.3

Dependency edit form.

See also

conditional_fields_dependency_edit_form_validate()

conditional_fields_dependency_edit_form_submit()

1 string reference to 'conditional_fields_dependency_edit_form'
conditional_fields_menu in ./conditional_fields.module
Implements hook_menu().

File

includes/conditional_fields.admin.inc, line 375
Administration of dependencies.

Code

function conditional_fields_dependency_edit_form($form, &$form_state, $dependency) {
  $form['#dependency'] = $dependency;
  $form['#attached']['css'][] = drupal_get_path('module', 'conditional_fields') . '/conditional_fields.css';

  // 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().
  $instances = field_read_instances(array(
    'id' => $dependency['dependee'],
  ));
  $dependee_instance = array_shift($instances);
  $dependee = field_info_field($dependee_instance['field_name']);

  // Build a dummy field widget to use as form field in single value selection
  // option.
  $dummy_form = array(
    '#parents' => array(),
  );
  if ($dependency['options']['values_set'] == CONDITIONAL_FIELDS_DEPENDENCY_VALUES_WIDGET) {
    $dependee_instance['default_value'] = $dependency['options']['value'];
  }
  $dependee_instance['default_value_function'] = '';
  $dependee_instance['required'] = FALSE;
  $dummy_field = field_default_form($dependee_instance['entity_type'], NULL, $dependee, $dependee_instance, LANGUAGE_NONE, array(), $dummy_form, $form_state);

  // Save dependee name in form.
  $form['dependee'] = array(
    '#type' => 'value',
    '#value' => $dependee_instance['field_name'],
  );
  $checkboxes = $dependee_instance['widget']['type'] == 'options_buttons' && $dependee['cardinality'] != 1 || $dependee_instance['widget']['type'] == 'options_onoff' ? TRUE : FALSE;
  $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' => conditional_fields_conditions($checkboxes),
    '#default_value' => $dependency['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' => $dependency['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", $dependency['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($dependency['options']['value']['RegExp']) ? $dependency['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' => $dependency['options']['grouping'],
    '#required' => TRUE,
  );
  $entity = entity_get_info($dependee_instance['entity_type']);
  $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' => $dependency['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'] : $dependency['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'] : $dependency['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 ($dependency['options']['effect'] == $effect_name) {
        $effect_option['#default_value'] = $dependency['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' => $dependency['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' => $dependency['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($dependency['options']['element_edit_roles'][$rid]) ? $dependency['options']['element_edit_roles'][$rid] : $dependency['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' => $dependency['options']['selector'],
  );
  $form['entity_view'] = array(
    '#type' => 'fieldset',
    '#title' => t('View context settings'),
    '#description' => t('These settings apply when the @entity is viewed.', array(
      '@entity' => drupal_strtolower($entity['label']),
    )),
    '#collapsible' => FALSE,
  );
  $form['entity_view']['element_view_per_role'] = array(
    '#type' => 'checkbox',
    '#title' => t('Activate per user role settings in view context'),
    '#description' => t('If the user has more than one role, the first matching role will be used.'),
    '#default_value' => $dependency['options']['element_view_per_role'],
  );
  $form['entity_view']['element_view'] = array(
    '#type' => 'checkboxes',
    '#title' => t('View context settings for all roles'),
    '#title_display' => 'invisible',
    '#description' => t('Note: Options that need to evaluate if the dependency is triggered only apply if the condition is "Value", "Empty", or "Filled".'),
    '#options' => $behaviors['view'],
    '#default_value' => $dependency['options']['element_view'],
    '#states' => array(
      'visible' => array(
        ':input[name="element_view_per_role"]' => array(
          'checked' => FALSE,
        ),
      ),
    ),
  );
  $element_view_roles = array(
    'element_view_roles' => array(
      '#tree' => TRUE,
    ),
  );
  foreach ($roles as $rid => $role) {
    $element_view_roles['element_view_roles'][$rid] = array(
      '#type' => 'checkboxes',
      '#title' => t('View context settings for %role', array(
        '%role' => $role,
      )),
      '#options' => $behaviors['view'],
      '#default_value' => isset($dependency['options']['element_view_roles'][$rid]) ? $dependency['options']['element_view_roles'][$rid] : $dependency['options']['element_view'],
      '#states' => array(
        'visible' => array(
          ':input[name="element_view_per_role"]' => array(
            'checked' => TRUE,
          ),
        ),
      ),
    );
  }
  array_push($form['entity_view'], $element_view_roles);
  $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;
}