You are here

function biblio_ui_create_fields in Bibliography Module 7.3

Manage the fields of the biblio bundle.

1 string reference to 'biblio_ui_create_fields'
biblio_ui_menu in modules/biblio_ui/biblio_ui.module
Implements hook_menu().

File

modules/biblio_ui/biblio_ui.module, line 851
Main functionality file for the biblio UI module.

Code

function biblio_ui_create_fields($form, &$form_state, $bundle) {

  // Save the bundle for later.
  $form_state['bundle'] = $bundle;

  // Get the existing fields and the fields which not yet added.
  $fields = biblio_fields_info();
  $existing_fields = field_info_instances('biblio', $bundle);
  $options = array();
  foreach ($fields as $name => $field) {
    if (in_array($name, array_keys($existing_fields))) {
      continue;
    }
    $options[$name] = $field['instance']['label'];
  }
  if (!$options) {
    drupal_set_message(t("There aren't any fields to add."), 'warning');
  }

  // Sorting the fields name in alphabetical order.
  asort($options);

  // Creating a select. When changing the description of the field will be shown
  // via AJAX.
  $field_info = '';
  $selected_field_name = !empty($form_state['values']['field_name']) ? $form_state['values']['field_name'] : '';
  if (isset($fields[$selected_field_name]['instance']['description'])) {
    $field_info = t('Description: <b>' . $fields[$selected_field_name]['instance']['description'] . '</b>');
  }
  $form['field_info_wrapper'] = array(
    '#prefix' => '<div id="field-info-wrapper">',
    '#suffix' => '</div>',
    '#parents' => array(
      'field_info_wrapper',
    ),
  );
  $form['field_info_wrapper']['field_name'] = array(
    '#type' => 'select',
    '#title' => t('Select field to add'),
    '#description' => t('Select the field you would like to add.'),
    '#options' => array(
      '' => t('Select a field'),
    ) + $options,
    '#ajax' => array(
      'callback' => 'biblio_ui_fields_ajax_callback',
      'wrapper' => 'field-info-wrapper',
    ),
  );
  $form['field_info_wrapper']['description'] = array(
    '#markup' => $field_info,
  );
  $form['actions'] = array(
    '#type' => 'actions',
    'submit' => array(
      '#type' => 'submit',
      '#value' => t('Add field'),
    ),
  );
  return $form;
}