You are here

serial.module in Serial Field 6

Same filename and directory in other branches
  1. 8 serial.module
  2. 7 serial.module

The Serial module main file.

File

serial.module
View source
<?php

/**
 * @file
 * The Serial module main file.
 */

//==================//

// Field Definition //

//==================//

/**
 * Implementation of hook_field_info().
 */
function serial_field_info() {
  return array(
    'serial' => array(
      'label' => t('Serial'),
      'description' => t('Auto increment serial field type.'),
    ),
  );
}

/**
 * Implementation of hook_fieldapi().
 */
function serial_content_fieldapi($op, $field) {
  if ($field['type'] == 'serial') {
    switch ($op) {
      case 'create instance':
        module_load_include('inc', 'serial');
        _serial_create_table($field);
        break;
      case 'delete instance':
        module_load_include('inc', 'serial');
        _serial_drop_table($field);
        break;
    }
  }
}

/**
 *
 * Implementation of hook_field_settings().
 */
function serial_field_settings($op, $field) {
  switch ($op) {
    case 'database columns':
      $columns['value'] = array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'sortable' => TRUE,
        'views' => TRUE,
        'index' => TRUE,
      );
      return $columns;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function serial_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'content_field_edit_form' && $form['#field']['type'] == 'serial') {

    // Hide irrelevant settings:
    unset($form['widget']);
    unset($form['field']);

    // After creation of a new field:
    if (preg_match('#^.*destination.*\\=(.*)$#', $form['#action'], $matches)) {

      // Set serial values for old objects
      module_load_include('inc', 'serial');
      $field = $form['#field'];
      $old_count = _serial_init_old_nodes($field);

      // (field is not ready yet at serial_content_fieldapi's 'create instance')
      // Show messages:
      drupal_set_message(t('Serial field %field has been created.', array(
        '%field' => $field['field_name'],
      )));
      if ($old_count) {
        drupal_set_message(t('Serial values have been automatically set for %count existing nodes.', array(
          '%count' => $old_count,
        )));
      }

      // Go back to Managed Fields:
      drupal_goto(urldecode($matches[1]));
    }
  }
}

/**
 * Implementation of hook_field().
 */
function serial_field($op, &$node, $field, &$items, $teaser, $page) {
  switch ($op) {
    case 'insert':
      module_load_include('inc', 'serial');
      $sid = _serial_generate_value($node->nid, $field);
      $items = array(
        array(
          'value' => $sid,
        ),
      );
      $node->{$field}['field_name'] = $items;
  }
}

/**
 * Implementation of hook_content_is_empty().
 */
function serial_content_is_empty($item, $field) {
  return FALSE;

  // never should be treated as empty
}

/**
 * Implementation of hook_node_type()
 */
function serial_node_type($op, $info) {
  switch ($op) {
    case 'update':

      // Handle content type rename:
      if (isset($info->old_type) && $info->old_type != $info->type) {
        module_load_include('inc', 'serial');
        _serial_rename_tables($info->old_type, $info->type);
      }
      break;
  }
}

/**
 * Implementation of hook_token_list().
 */
function serial_token_list($type = 'all') {
  if ($type == 'field' || $type == 'all') {
    $tokens['serial']['id'] = t('Serial field value (unique per node type)');
    return $tokens;
  }
}

/**
 * Implementation of hook_token_values().
 */
function serial_token_values($type, $object = NULL) {
  if ($type == 'field') {
    $tokens['id'] = $object[0]['value'];
    return $tokens;
  }
}

//=================//

// Field Formatter //

//=================//

/**
 * Implementation of hook_field_formatter_info().
 */
function serial_field_formatter_info() {
  return array(
    'default' => array(
      'label' => '9999',
      'field types' => array(
        'serial',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
    ),
  );
}

/**
 * Implementation of hook_theme().
 */
function serial_theme() {
  return array(
    'serial_formatter_default' => array(
      'arguments' => array(
        'element' => NULL,
      ),
    ),
  );
}

/**
 * Theme function for 'default' serial field formatter.
 */
function theme_serial_formatter_default($element) {
  return $element['#item']['value'];
}

//==============//

// Field Widget //

//==============//

/**
 * Implementation of hook_widget_info().
 */
function serial_widget_info() {
  return array(
    'serial_widget' => array(
      'label' => t('Hidden (Automatic)'),
      'field types' => array(
        'serial',
      ),
      'multiple values' => CONTENT_HANDLE_CORE,
      'callbacks' => array(
        'default value' => CONTENT_CALLBACK_NONE,
      ),
    ),
  );
}

/**
 * Implementation of hook_widget().
 */
function serial_widget(&$form, &$form_state, $field, $items, $delta = 0) {
  return array(
    'value' => array(
      '#type' => 'hidden',
      '#default_value' => $items[$delta]['value'],
    ),
  );
}

Functions

Namesort descending Description
serial_content_fieldapi Implementation of hook_fieldapi().
serial_content_is_empty Implementation of hook_content_is_empty().
serial_field Implementation of hook_field().
serial_field_formatter_info Implementation of hook_field_formatter_info().
serial_field_info Implementation of hook_field_info().
serial_field_settings Implementation of hook_field_settings().
serial_form_alter Implementation of hook_form_alter().
serial_node_type Implementation of hook_node_type()
serial_theme Implementation of hook_theme().
serial_token_list Implementation of hook_token_list().
serial_token_values Implementation of hook_token_values().
serial_widget Implementation of hook_widget().
serial_widget_info Implementation of hook_widget_info().
theme_serial_formatter_default Theme function for 'default' serial field formatter.