You are here

function conditional_fields_content_admin_field in Conditional Fields 6.2

Same name and namespace in other branches
  1. 5 conditional_fields.module \conditional_fields_content_admin_field()
  2. 6 conditional_fields.module \conditional_fields_content_admin_field()

Alteration of the field editing form

1 call to conditional_fields_content_admin_field()
conditional_fields_form_alter in ./conditional_fields.module
Implementation of hook_form_alter().

File

./conditional_fields.module, line 327
Content fields and groups visibility based on the values of user defined 'trigger' fields.

Code

function conditional_fields_content_admin_field(&$form) {
  if (!user_access('administer conditional fields')) {
    return;
  }
  $type = (array) content_types($form['type_name']['#value']);

  // Load conditional fields data.
  $data = conditional_fields_load_data($type['type']);

  // Get all fields controlled by this one.
  $controlled_fields = array();
  foreach ($data as $row) {
    if ($row['control_field_name'] == $form['field_name']['#value']) {
      $controlled_fields[$row['field_name']] = $row['trigger_values'];
    }
  }

  // Build controlling field form.
  if (!empty($controlled_fields)) {

    // Needed to handle nested fields.
    $form['#controlled_fields'] = $controlled_fields;

    // Add validation function
    $form['#validate'] = array_merge(array(
      'conditional_fields_content_admin_field_validate',
    ), $form['#validate']);
    $form['controlled_fields'] = array(
      '#type' => 'value',
      '#value' => $controlled_fields,
    );
    $controlled_fieldset = conditional_fields_content_admin_field_controlling($type, $controlled_fields);
  }

  // Check if field is in a group
  if (isset($form['widget']['group'])) {
    $group_of_controlled_field = $form['widget']['group']['#value'];
  }

  // Get candidate controlling fields
  foreach ($type['fields'] as $field) {

    // - Exclude this one :)
    if ($field['field_name'] == $form['field_name']['#value']) {
      continue;
    }

    // - Exclude fields already controlled by this one
    if (isset($controlled_fields[$field['field_name']])) {
      continue;
    }

    // - Exclude fields without Allowed values
    if (!($allowed_values[$field['field_name']] = conditional_fields_allowed_values($field))) {
      continue;
    }

    // If a field is inside a controlled group, it can control or be controlled only by fields in the same group.
    // This is needed to avoid uncontrollable loops.
    // First we gather data about groups
    $group_of_candidate_field = conditional_fields_get_group($field['type_name'], $field['field_name']);
    if ($group_of_controlled_field || $group_of_candidate_field) {
      $group_of_controlled_field_is_controlled = FALSE;
      $group_of_candidate_field_is_controlled = FALSE;
      foreach ($data as $row) {
        if ($row['field_name'] == $group_of_controlled_field) {
          $group_of_controlled_field_is_controlled = TRUE;
        }
        elseif ($row['field_name'] == $group_of_candidate_field) {
          $group_of_candidate_field_is_controlled = TRUE;
        }
      }
    }

    // If the controlled field is in a group...
    if ($group_of_controlled_field) {

      // - Exclude candidates outside the controlled field's group if the group is controlled
      if (!$group_of_candidate_field && $group_of_controlled_field_is_controlled) {
        continue;
      }
      elseif ($group_of_candidate_field && $group_of_candidate_field != $group_of_controlled_field && ($group_of_candidate_field_is_controlled || $group_of_controlled_field_is_controlled)) {
        continue;
      }
    }
    else {

      // - Exclude fields in controlled groups
      if ($group_of_candidate_field && $group_of_candidate_field_is_controlled) {
        continue;
      }
    }
    $available_fields[$field['field_name']] = $field;
  }
  if (isset($available_fields)) {
    $default_values = conditional_fields_available_fields_default_values($form['field_name']['#value'], $available_fields);
    $available_fieldset = conditional_fields_content_admin_field_controllable($type, $default_values, $available_fields, $allowed_values, 'field');

    // Add validation function
    $form['#validate'] = array_merge(array(
      'conditional_fields_content_admin_field_validate',
    ), $form['#validate']);

    // Add submission function
    $form['#submit'] = array_merge(array(
      'conditional_fields_forms_submit',
    ), $form['#submit']);
  }

  // Add the fieldsets to the form
  if (isset($controlled_fieldset) || isset($available_fieldset)) {
    $form['widget']['conditional_fields'] = array(
      '#type' => 'fieldset',
      '#title' => t('Conditional fields settings'),
      '#tree' => TRUE,
      '#collapsible' => TRUE,
      '#collapsed' => empty($default_values),
      '#weight' => 8,
      '#attributes' => array(
        'id' => 'conditional-fields-settings',
      ),
    );
    if (isset($controlled_fieldset)) {
      $form['widget']['conditional_fields']['controlling_fields'] = $controlled_fieldset;
    }
    if (isset($available_fieldset)) {
      if (isset($controlled_fieldset)) {
        $available_fieldset['#description'] .= '<p><strong>' . t('Note: this field is already controlling other fields. If you make it controlled, its own controlled fields will be assigned the same settings.') . '</strong></p>';
      }
      $form['widget']['conditional_fields']['available_fields'] = $available_fieldset;
    }
  }
  return;
}