You are here

biblio_fields.module in Bibliography Module 7.2

File

modules/biblio_fields/biblio_fields.module
View source
<?php

/**
 * Implements hook_field_info().
 */
function biblio_fields_field_info() {
  return array(
    'biblio_text' => array(
      'label' => t('Biblio Text'),
      'description' => t('Standard Text Field with the addition of the option to select a vertical tab for the add/edit form.'),
      'default_widget' => 'biblio_text_widget',
      'default_formatter' => 'biblio_text_formatter',
      'instance_settings' => array(
        'vtab' => t('None'),
      ),
      // property_type and property_callbacks keys are neccessary to use the
      // entity API to get/set custom fields in entities.
      'property_type' => 'text',
      'property_callbacks' => array(
        'entity_metadata_field_text_property_callback',
      ),
    ),
  );
}

/**
 * Implements hook_field_is_empty().
 *
 * Pseudo-hook.
 */
function biblio_fields_field_is_empty($item, $field) {
  if ($field['type'] == 'biblio_text') {
    if (empty($item['value']) && empty($item['vtab'])) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Implements hook_field_instance_settings_form().
 *
 * Pseudo-hook.
 */
function biblio_fields_field_instance_settings_form($field, $instance) {
  if ($field['type'] == 'biblio_text') {
    $form['vtab'] = array(
      '#type' => 'select',
      '#title' => t('Add/Edit form Vertical Tab'),
      '#options' => array(
        t('None'),
        t('Authors'),
        t('Abstract'),
        t('Full Text'),
        t('Publication'),
        t('Publisher'),
        t('Identifiers'),
        t('Locators'),
        t('Keywords'),
        t('Notes'),
        t('Alternate Titles'),
        t('Other'),
      ),
      '#default_value' => isset($instance['settings']['vtab']) ? $instance['settings']['vtab'] : t('None'),
      '#required' => TRUE,
      '#description' => t('The vertical tab location of the field in the Biblio add/edit form. Select "None" to leave the field out of the vertical tabs.'),
    );
    return $form;
  }
}

/**
 * Implements hook_field_widget_info().
 */
function biblio_fields_field_widget_info() {
  return array(
    'biblio_text_widget' => array(
      'label' => t('Text field'),
      'description' => t('Shows as a standard text field'),
      'field types' => array(
        'biblio_text',
      ),
      'behaviors' => array(
        'multiple values' => FIELD_BEHAVIOR_DEFAULT,
        'default value' => FIELD_BEHAVIOR_DEFAULT,
      ),
    ),
  );
}

/**
 * Implements hook_field_widget_form().
 *
 * Pseudo-hook.
 */
function biblio_fields_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $element) {

  // @todo: form validation
  $base = $element;

  // Assemble our default value for the form element out of the values available
  // in the field
  if ($instance['widget']['type'] == 'biblio_text_widget') {
    $element['value'] = array(
      '#type' => 'textfield',
      '#default_value' => isset($items[$delta]['value']) ? $items[$delta]['value'] : NULL,
      '#vtab' => $instance['settings']['vtab'],
    ) + $base;
  }
  return $element;
}

/**
 * Implements hook_field_formatter_info().
 */
function biblio_fields_field_formatter_info() {
  return array(
    'biblio_text_formatter' => array(
      'label' => t('Default'),
      'field types' => array(
        'biblio_text',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_view().
 */
function biblio_fields_field_formatter_view($obj_type, $object, $field, $instance, $langcode, $items, $display) {
  $element = array();
  foreach ($items as $delta => $item) {
    $element[$delta] = biblio_format_field($item);
  }
  return $element;
}

/**
 * Helper function for creating field formatters
 * @param type $item 
 */
function biblio_format_field($item) {
  $element = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'field-item',
      ),
    ),
  );
  $element['value'] = array(
    'item' => array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array(
          'field-item',
        ),
      ),
      'text' => array(
        '#markup' => $item['value'],
      ),
    ),
  );
  return $element;
}