You are here

function cer_settings_form in Corresponding Entity References 7

Same name and namespace in other branches
  1. 7.2 cer.admin.inc \cer_settings_form()

The settings form.

1 string reference to 'cer_settings_form'
cer_menu in ./cer.module
Implements hook_menu().

File

./cer.admin.inc, line 10
Admin functionality, separated for performance purposes.

Code

function cer_settings_form() {
  $form['intro'] = array(
    '#markup' => t('Check which entity references should listen to each other. When checking a check box a reference on entity type A to entity B will automatically update the entity reference field on entity B adding an entry which points to entity A.'),
  );
  $result = db_query("SELECT field_name, data FROM {field_config} WHERE type = :type AND deleted = 0", array(
    ':type' => 'entityreference',
  ));
  foreach ($result as $row) {
    $data = unserialize($row->data);
    if (empty($data['settings']['handler_settings']['target_bundles'])) {
      $references[$row->field_name][] = $data['settings']['target_type'];
    }
    if (isset($data['settings']['handler_settings']['target_bundles'])) {
      foreach ($data['settings']['handler_settings']['target_bundles'] as $reference) {
        if ($reference != '0') {
          $references[$row->field_name][] = $reference;
        }
      }
    }
  }
  $result = db_query("SELECT fci.field_name, fci.entity_type, fci.bundle FROM {field_config_instance} fci INNER JOIN {field_config} fc ON fc.field_name = fci.field_name WHERE fc.type = :type", array(
    ':type' => 'entityreference',
  ));
  foreach ($result as $row) {
    if (!empty($references[$row->field_name])) {
      foreach ($references[$row->field_name] as $reference) {
        $fields_to_compare[] = array(
          'field_type' => $row->field_name,
          'bundle' => $row->bundle,
          'reference' => $reference,
          'entity' => $row->entity_type,
        );
      }
    }
  }
  if (!empty($fields_to_compare)) {
    $corr_entityrefs = array();
    foreach ($fields_to_compare as $field) {
      foreach ($fields_to_compare as $second_field) {
        if ($field['bundle'] == $second_field['reference'] && $second_field['bundle'] == $field['reference']) {
          if (!array_key_exists($field['entity'] . ' ' . $field['bundle'] . ' ' . $field['field_type'] . ' ' . $second_field['entity'] . ' ' . $second_field['bundle'] . ' ' . $second_field['field_type'], $corr_entityrefs) && !array_key_exists($second_field['entity'] . ' ' . $second_field['bundle'] . ' ' . $second_field['field_type'] . ' ' . $field['entity'] . ' ' . $field['bundle'] . ' ' . $field['field_type'], $corr_entityrefs)) {
            $corr_entityrefs[$field['entity'] . ' ' . $field['bundle'] . ' ' . $field['field_type'] . ' ' . $second_field['entity'] . ' ' . $second_field['bundle'] . ' ' . $second_field['field_type']] = array(
              'entity_1' => $field['entity'],
              'bundle_1' => $field['bundle'],
              'field_1' => $field['field_type'],
              'entity_2' => $second_field['entity'],
              'bundle_2' => $second_field['bundle'],
              'field_2' => $second_field['field_type'],
            );
          }
        }
      }
    }
    if (!empty($corr_entityrefs)) {
      $count = 0;
      foreach ($corr_entityrefs as $key => $corr_entityref) {
        $formatted_key = str_replace(' ', '*', $key);
        $mapping[] = $formatted_key;
        $form['values'][$count] = array(
          '#type' => 'fieldset',
        );
        $form['values'][$count]['enabled_' . $count] = array(
          '#type' => 'checkbox',
          '#default_value' => cer_preset_enabled($formatted_key),
          '#title' => theme('cer_label', array(
            'key' => $key,
          )),
        );
        $count++;
      }

      //We are using a hidden field to submit the configuration because on some systems the checkbox name length is limited

      //, using the key would cause the length to be too long see issue #558612
      $form['mapping'] = array(
        '#type' => 'hidden',
        '#value' => serialize($mapping),
      );
      $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Save'),
      );
    }
  }
  else {
    $form['text'] = array(
      '#markup' => '<div>' . t('There are no entity types which have a corresponding entity reference') . '</div>',
    );
  }
  return $form;
}