View source
<?php
define('SERIAL_FIELD_TYPE', 'serial');
function serial_field_info() {
return array(
SERIAL_FIELD_TYPE => array(
'label' => t('Serial'),
'description' => t('Auto increment serial field type.'),
'property_type' => 'integer',
'default_widget' => 'serial_widget_default',
'default_formatter' => 'serial_formatter_default',
),
);
}
function serial_field_widget_info() {
return array(
'serial_widget_default' => array(
'label' => t('Hidden (Automatic)'),
'field types' => array(
SERIAL_FIELD_TYPE,
),
),
);
}
function serial_field_formatter_info() {
return array(
'serial_formatter_default' => array(
'label' => t('Default'),
'field types' => array(
SERIAL_FIELD_TYPE,
),
),
);
}
function serial_field_create_instance(array $instance) {
$field = field_read_field($instance['field_name']);
if (SERIAL_FIELD_TYPE === $field['type']) {
module_load_include('inc', 'serial');
_serial_create_table($field, $instance);
$old_count = _serial_init_old_entities($instance['entity_type'], $instance['bundle'], $field['field_name']);
if ($old_count > 0) {
drupal_set_message(t('Serial values have been automatically set for %count existing entities.', array(
'%count' => $old_count,
)));
}
}
}
function serial_field_delete_instance(array $instance) {
$field = field_read_field($instance['field_name']);
if (SERIAL_FIELD_TYPE == $field['type']) {
module_load_include('inc', 'serial');
_serial_drop_table($field, $instance);
}
}
function serial_field_presave($entity_type, $entity, array $field, array $instance, $langcode, array &$items) {
if (empty($items)) {
module_load_include('inc', 'serial');
$items[] = array(
'value' => _serial_generate_value($entity_type, $instance['bundle'], $field['field_name']),
);
}
}
function serial_field_is_empty($item, $field) {
return empty($item['value']);
}
function serial_clone_node_alter(stdClass $node) {
foreach (field_info_instances('node', $node->type) as $field_name => $instance) {
$info = field_info_field($field_name);
if ('serial' === $info['type']) {
$node->{$field_name} = array();
}
}
}
function serial_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
if ($bundle_old !== $bundle_new) {
module_load_include('inc', 'serial');
_serial_rename_tables($entity_type, $bundle_old, $bundle_new);
}
}
function serial_field_formatter_view($entity_type, $entity, array $field, array $instance, $langcode, array $items, array $display) {
$element = array();
foreach ($items as $delta => $item) {
$element[$delta] = array(
'#markup' => theme('serial_formatter_default', array(
'serial_id' => $item['value'],
)),
);
}
return $element;
}
function serial_theme() {
return array(
'serial_formatter_default' => array(
'variables' => array(
'serial_id' => NULL,
),
),
);
}
function theme_serial_formatter_default(array $variables) {
return $variables['serial_id'];
}
function serial_field_widget_form(array &$form, array &$form_state, array $field, array $instance, $langcode, array $items, $delta, array $element) {
$element['#type'] = 'hidden';
if (isset($items[$delta]['value'])) {
$element['#default_value'] = $items[$delta]['value'];
}
return array(
'value' => $element,
);
}