serial.module 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * @file
  4. * The Serial module main file.
  5. */
  6. define('SERIAL_FIELD_TYPE', 'serial');
  7. /**
  8. * Implements hook_field_info().
  9. */
  10. function serial_field_info() {
  11. return array(
  12. SERIAL_FIELD_TYPE => array(
  13. 'label' => t('Serial'),
  14. 'description' => t('Auto increment serial field type.'),
  15. // The "property_type" should be defined for accessing the
  16. // field by entity metadata wrapper.
  17. 'property_type' => 'integer',
  18. 'default_widget' => 'serial_widget_default',
  19. 'default_formatter' => 'serial_formatter_default',
  20. ),
  21. );
  22. }
  23. /**
  24. * Implements hook_field_widget_info().
  25. */
  26. function serial_field_widget_info() {
  27. return array(
  28. 'serial_widget_default' => array(
  29. 'label' => t('Hidden (Automatic)'),
  30. 'field types' => array(SERIAL_FIELD_TYPE),
  31. ),
  32. );
  33. }
  34. /**
  35. * Implements hook_field_formatter_info().
  36. */
  37. function serial_field_formatter_info() {
  38. return array(
  39. 'serial_formatter_default' => array(
  40. 'label' => t('Default'),
  41. 'field types' => array(SERIAL_FIELD_TYPE),
  42. ),
  43. );
  44. }
  45. /**
  46. * Implements hook_field_create_instance().
  47. */
  48. function serial_field_create_instance(array $instance) {
  49. $field = field_read_field($instance['field_name']);
  50. if (SERIAL_FIELD_TYPE === $field['type']) {
  51. // Create the assistant table:
  52. module_load_include('inc', 'serial');
  53. _serial_create_table($field, $instance);
  54. // Set serial values for old objects.
  55. $old_count = _serial_init_old_entities($instance['entity_type'], $instance['bundle'], $field['field_name']);
  56. if ($old_count > 0) {
  57. drupal_set_message(t('Serial values have been automatically set for %count existing entities.', array(
  58. '%count' => $old_count,
  59. )));
  60. }
  61. }
  62. }
  63. /**
  64. * Implements hook_field_delete_instance().
  65. */
  66. function serial_field_delete_instance(array $instance) {
  67. $field = field_read_field($instance['field_name']);
  68. if (SERIAL_FIELD_TYPE == $field['type']) {
  69. // Drop the assistant table.
  70. module_load_include('inc', 'serial');
  71. _serial_drop_table($field, $instance);
  72. }
  73. }
  74. /**
  75. * Implements hook_field_presave().
  76. */
  77. function serial_field_presave($entity_type, $entity, array $field, array $instance, $langcode, array &$items) {
  78. if (empty($items)) {
  79. module_load_include('inc', 'serial');
  80. $items[] = array(
  81. 'value' => _serial_generate_value($entity_type, $instance['bundle'], $field['field_name']),
  82. );
  83. }
  84. }
  85. /**
  86. * Implements hook_field_is_empty().
  87. */
  88. function serial_field_is_empty($item, $field) {
  89. // Never should be treated as empty.
  90. return empty($item['value']);
  91. }
  92. /**
  93. * Implements hook_clone_node_alter().
  94. */
  95. function serial_clone_node_alter(stdClass $node) {
  96. // Reset values of all serial fields when cloning a node.
  97. foreach (field_info_instances('node', $node->type) as $field_name => $instance) {
  98. $info = field_info_field($field_name);
  99. if ('serial' === $info['type']) {
  100. $node->{$field_name} = array();
  101. }
  102. }
  103. }
  104. /**
  105. * Implements hook_field_attach_rename_bundle().
  106. */
  107. function serial_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  108. if ($bundle_old !== $bundle_new) {
  109. module_load_include('inc', 'serial');
  110. _serial_rename_tables($entity_type, $bundle_old, $bundle_new);
  111. }
  112. }
  113. /**
  114. * Implements hook_field_formatter_view().
  115. */
  116. function serial_field_formatter_view($entity_type, $entity, array $field, array $instance, $langcode, array $items, array $display) {
  117. $element = array();
  118. // Define the field contents for the single default formatter.
  119. foreach ($items as $delta => $item) {
  120. $element[$delta] = array(
  121. '#markup' => theme('serial_formatter_default', array(
  122. 'serial_id' => $item['value'],
  123. )),
  124. );
  125. }
  126. return $element;
  127. }
  128. /**
  129. * Implements hook_theme().
  130. */
  131. function serial_theme() {
  132. // Register the theme for the default formatter.
  133. return array(
  134. 'serial_formatter_default' => array(
  135. 'variables' => array(
  136. 'serial_id' => NULL,
  137. ),
  138. ),
  139. );
  140. }
  141. /**
  142. * Theme function for the default formatter.
  143. */
  144. function theme_serial_formatter_default(array $variables) {
  145. return $variables['serial_id'];
  146. }
  147. /**
  148. * Implements hook_field_widget_form().
  149. */
  150. function serial_field_widget_form(
  151. array &$form,
  152. array &$form_state,
  153. array $field,
  154. array $instance,
  155. $langcode,
  156. array $items,
  157. $delta,
  158. array $element
  159. ) {
  160. $element['#type'] = 'hidden';
  161. if (isset($items[$delta]['value'])) {
  162. $element['#default_value'] = $items[$delta]['value'];
  163. }
  164. return array('value' => $element);
  165. }