You are here

barcodefield.module in Barcode 6.2

File

barcodefield.module
View source
<?php

/**
 * Implementation of hook_theme().
 */
function barcodefield_theme() {
  return array(
    'barcode_formatter_plain' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'barcode_formatter_default' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
    'barcode_textfield' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
}

/**
 * Theme function for 'plain' barcode field formatter.
 */
function theme_barcode_formatter_plain($element) {
  if (!empty($element['#item']['title'])) {
    return $element['#item']['title'] . ': ' . $element['#item']['barcode'];
  }
  else {
    return $element['#item']['barcode'];
  }
}

/**
 * Theme function for 'default' barcode field formatter.
 */
function theme_barcode_formatter_default($element) {
  if (!empty($element['#item']['barcode'])) {
    $barcode_image = theme('barcode_image', $element['#item']['barcode']);
    if (!empty($element['#item']['title'])) {
      return $element['#item']['title'] . ':' . $barcode_image;
    }
    else {
      return $barcode_image;
    }
  }
  else {
    return '';
  }
}

/**
 * FAPI theme for an individual text elements.
 */
function theme_barcode_textfield($element) {
  drupal_add_css(drupal_get_path('module', 'barcode') . '/barcode.css');

  // Prefix single value barcode fields with the name of the field.
  if (empty($element['#field']['multiple'])) {
    if (isset($element['barcode']) && isset($element['title'])) {
      $element['barcode']['#title'] = $element['#title'] . ' ' . $element['barcode']['#title'];
      $element['title']['#title'] = $element['#title'] . ' ' . $element['title']['#title'];
    }
    elseif ($element['barcode']) {
      $element['barcode']['#title'] = $element['#title'];
    }
  }
  $output = '';
  $output .= '<div class="barcode-field-subrow clear-block">';
  if (isset($element['title'])) {
    $output .= '<div class="barcode-field-title barcode-field-column">' . theme('textfield', $element['title']) . '</div>';
  }
  $output .= '<div class="barcode-field-url' . (isset($element['title']) ? ' barcode-field-column' : '') . '">' . theme('textfield', $element['barcode']) . '</div>';
  $output .= '</div>';
  return $output;
}

/**
 * Implementation of hook_field_info().
 */
function barcode_field_info() {
  return array(
    'barcode' => array(
      'label' => 'Barcode',
      'callbacks' => array(
        'tables' => CONTENT_CALLBACK_DEFAULT,
        'arguments' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
  );
}

/**
 * Implementation of hook_field_settings().
 */
function barcode_field_settings($op, $field) {
  switch ($op) {
    case 'form':
      $form = array(
        '#theme' => 'barcode_field_settings',
      );
      $title_options = array(
        'optional' => t('Optional title'),
        'required' => t('Required title'),
        'none' => t('No title'),
      );
      $form['title'] = array(
        '#type' => 'radios',
        '#title' => t('Bar Code Title'),
        '#default_value' => isset($field['title']) ? $field['title'] : 'optional',
        '#options' => $title_options,
        '#description' => t('If the barcode title is optional or required, a field will be displayed to the end user.'),
      );
      return $form;
    case 'save':
      return array(
        'title',
        'title_value',
      );
    case 'database columns':
      return array(
        'barcode' => array(
          'type' => 'varchar',
          'length' => 20,
          'not null' => FALSE,
          'sortable' => TRUE,
        ),
        'title' => array(
          'type' => 'varchar',
          'length' => 20,
          'not null' => FALSE,
          'sortable' => TRUE,
        ),
      );
  }
}

/**
 * Implementation of FAPI hook_elements().
 */
function barcodefield_elements() {
  $type['barcode_textfield'] = array(
    '#input' => TRUE,
    '#process' => array(
      'barcodefield_process',
    ),
  );
  return $type;
}

/**
 * Process an individual element.
 */
function barcodefield_process($element, $edit, $form_state, $form) {
  $field = $form['#field_info'][$element['#field_name']];
  $field_key = $element['#columns'][0];

  // here $field_key is barcode
  $delta = $element['#delta'];
  if ($field['title'] != 'none' && $field['title'] != 'value') {
    $element['title'] = array(
      '#type' => 'textfield',
      '#maxlength' => '20',
      '#size' => '20',
      '#title' => t('Title'),
      '#required' => $field['title'] == 'required' && !empty($element['#value'][$field_key]) ? TRUE : FALSE,
      '#default_value' => isset($element['#value']['title']) ? $element['#value']['title'] : NULL,
    );
  }
  module_load_include('inc', 'barcode', 'includes/barcode.plugins');
  $maxlength = barcode_plugin_max_length(variable_get('barcode_encoding', 'EAN-13'));
  $element[$field_key] = array(
    '#type' => 'textfield',
    '#maxlength' => $maxlength,
    '#size' => '20',
    '#title' => t($field['widget']['label']),
    '#description' => t($field['widget']['description']),
    '#required' => $element['#required'],
    '#default_value' => isset($element['#value'][$field_key]) ? $element['#value'][$field_key] : NULL,
  );
  return $element;
}

/**
 * Implementation of hook_field().
 */
function barcode_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'validate':

      // validate the entered data according to the encoding method.
      $encoding = variable_get('barcode_encoding', 'EAN-13');
      foreach ($items as $delta => $item) {
        if ($encoding == 'UPC-A') {

          //UPC-A length validation
          $len = strlen(trim(check_plain($item['barcode'])));
          if ($len != 0 && strlen(trim(check_plain($item['title'] != 0)))) {

            // If a bacode is entered.
            if ($len != 12) {
              form_set_error($field['field_name'] . '][' . $delta . '][barcode', t('UPC-A code must have 12 digits! Check your ' . ($delta + 1) . ' entry. Length: ' . $len));
            }
          }
        }
      }
      break;
    case 'sanitize':
      foreach ($items as $delta => $item) {
        $items[$delta]['barcode'] = trim(check_plain($item['barcode']));
        $items[$delta]['title'] = trim(check_plain($item['title']));
      }
      break;
  }
}

/**
 * Implementation of hook_content_is_empty().
 */
function barcode_content_is_empty($item, $field) {
  if (empty($item['barcode'])) {
    return TRUE;
  }
  return FALSE;
}

/**
 * Implementation of hook_field_formatter_info().
 *
 */
function barcode_field_formatter_info() {
  $formats = array(
    'default' => array(
      'label' => t('Barcode Image'),
      'field types' => array(
        'barcode',
        'title',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
    'plain' => array(
      'label' => t('Barcode Text'),
      'field types' => array(
        'barcode',
        'title',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
  );
  return $formats;
}

/**
 * Implementation of hook_widget_info().
 */
function barcode_widget_info() {
  return array(
    'barcode_textfield' => array(
      'label' => t('Text field'),
      'field types' => array(
        'barcode',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_DEFAULT,
      ),
    ),
  );
}

/**
 * Implementation of hook_widget().
 */
function barcode_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  $element = array(
    '#type' => $field['widget']['type'],
    '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
  );
  return $element;
}

Functions

Namesort descending Description
barcodefield_elements Implementation of FAPI hook_elements().
barcodefield_process Process an individual element.
barcodefield_theme Implementation of hook_theme().
barcode_content_is_empty Implementation of hook_content_is_empty().
barcode_field Implementation of hook_field().
barcode_field_formatter_info Implementation of hook_field_formatter_info().
barcode_field_info Implementation of hook_field_info().
barcode_field_settings Implementation of hook_field_settings().
barcode_widget Implementation of hook_widget().
barcode_widget_info Implementation of hook_widget_info().
theme_barcode_formatter_default Theme function for 'default' barcode field formatter.
theme_barcode_formatter_plain Theme function for 'plain' barcode field formatter.
theme_barcode_textfield FAPI theme for an individual text elements.