serial.module 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * @file
  4. * The Serial module main file.
  5. */
  6. //==================//
  7. // Field Definition //
  8. //==================//
  9. /**
  10. * Implements hook_field_info().
  11. */
  12. function serial_field_info() {
  13. return array(
  14. 'serial' => array(
  15. 'label' => t('Serial'),
  16. 'description' => t('Auto increment serial field type.'),
  17. 'default_widget' => 'serial',
  18. 'default_formatter' => 'serial_formatter_default',
  19. ),
  20. );
  21. }
  22. /**
  23. * Implements hook_field_create_instance().
  24. */
  25. function serial_field_create_instance($instance) {
  26. $field = field_read_field($instance['field_name']);
  27. if ($field['type'] == 'serial') {
  28. // Create the assistant table:
  29. module_load_include('inc', 'serial');
  30. _serial_create_table($field, $instance);
  31. // Set serial values for old objects
  32. $old_count = _serial_init_old_nodes($instance['bundle'], $field['field_name']);
  33. if ($old_count) {
  34. drupal_set_message(
  35. t('Serial values have been automatically set for %count existing nodes.',
  36. array('%count' => $old_count))
  37. );
  38. }
  39. }
  40. }
  41. /**
  42. * Implements hook_field_delete_instance().
  43. */
  44. function serial_field_delete_instance($instance) {
  45. $field = field_read_field($instance['field_name']);
  46. if ($field['type'] == 'serial') {
  47. // Drop the assistant table:
  48. module_load_include('inc', 'serial');
  49. _serial_drop_table($field, $instance);
  50. }
  51. }
  52. /**
  53. * Implements hook_form_alter().
  54. */
  55. function serial_form_alter(&$form, $form_state, $form_id) {
  56. if ($form_id == 'field_ui_field_settings_form' && $form['field']['type']['#value'] == 'serial') {
  57. // Show messages:
  58. $field_name = $form['field']['field_name']['#value'];
  59. drupal_set_message(
  60. t('Serial field %field has been created.',
  61. array('%field' => $field_name))
  62. );
  63. // Go back to Managed Fields:
  64. $type = $form['#bundle'];
  65. drupal_goto("admin/structure/types/manage/$type/fields");
  66. }
  67. }
  68. /**
  69. * Implements hook_field_presave().
  70. */
  71. function serial_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  72. module_load_include('inc', 'serial');
  73. if (empty($items)) {
  74. $sid = _serial_generate_value($instance['bundle'], $field['field_name']);
  75. $items = array(array('value' => $sid));
  76. }
  77. }
  78. /**
  79. * Implements hook_field_is_empty().
  80. */
  81. function serial_field_is_empty($item, $field) {
  82. return FALSE; // never should be treated as empty
  83. }
  84. /**
  85. * Implements hook_node_presave().
  86. */
  87. function serial_node_presave($node) {
  88. if (module_exists('auto_nodetitle')) {
  89. if (auto_nodetitle_get_setting($node->type)) {
  90. auto_nodetitle_set_title($node);
  91. }
  92. }
  93. }
  94. /**
  95. * Implements hook_node_type_update().
  96. */
  97. function serial_node_type_update($info) {
  98. // Handle content type rename:
  99. if (isset($info->old_type) && ($info->old_type != $info->type)) {
  100. module_load_include('inc', 'serial');
  101. _serial_rename_tables($info->old_type, $info->type);
  102. }
  103. }
  104. // Tokens for fields are currently not supported - http://drupal.org/node/691078.
  105. ///**
  106. // * Implements hook_token_info().
  107. // */
  108. //function serial_token_info() {
  109. // $type = array(
  110. // 'name' => t('Nodes'),
  111. // 'description' => t('Tokens related to individual nodes.'),
  112. // 'needs-data' => 'node',
  113. // );
  114. // $node['serial'] = array(
  115. // 'name' => t("Serial Field"),
  116. // 'description' => t('Serial field value (unique per node type)'),
  117. // 'needs-data' => 'node',
  118. // );
  119. // return array(
  120. // 'types' => array('node' => $type),
  121. // 'tokens' => array('node' => $node),
  122. // );
  123. //}
  124. //
  125. ///**
  126. // * Implements hook_tokens().
  127. // */
  128. //function serial_tokens($type, $tokens, $data, $options) {
  129. // // TODO
  130. //}
  131. //=================//
  132. // Field Formatter //
  133. //=================//
  134. /**
  135. * Implements hook_field_formatter_info().
  136. */
  137. function serial_field_formatter_info() {
  138. return array(
  139. 'serial_formatter_default' => array(
  140. 'label' => t('Default'),
  141. 'field types' => array('serial'),
  142. )
  143. );
  144. }
  145. /**
  146. * Implements hook_field_formatter_view().
  147. */
  148. function serial_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  149. $element = array();
  150. // Define the field contents for the single default formatter.
  151. foreach ($items as $delta => $item) {
  152. $element[$delta] = array(
  153. '#markup' => theme('serial_formatter_default', array(
  154. 'serial_id' => $item['value'],
  155. ))
  156. );
  157. }
  158. return $element;
  159. }
  160. /**
  161. * Theme Functions
  162. */
  163. /**
  164. * Implements hook_theme().
  165. */
  166. function serial_theme() {
  167. // Register the theme for the default formatter.
  168. return array(
  169. 'serial_formatter_default' => array(
  170. 'variables' => array(
  171. 'serial_id' => NULL,
  172. ),
  173. ),
  174. );
  175. }
  176. /**
  177. * Theme function for the default formatter.
  178. */
  179. function theme_serial_formatter_default($variables) {
  180. return $variables['serial_id'];
  181. }
  182. //==============//
  183. // Field Widget //
  184. //==============//
  185. /**
  186. * Implements hook_field_widget_info().
  187. */
  188. function serial_field_widget_info() {
  189. return array(
  190. 'serial' => array(
  191. 'label' => t('Hidden (Automatic)'),
  192. 'field types' => array('serial'),
  193. ),
  194. );
  195. }
  196. /**
  197. * Implements hook_field_widget().
  198. */
  199. function serial_field_widget(&$form, &$form_state, $field, $instance, $items, $delta = 0) {
  200. return array(
  201. 'value' => array(
  202. '#type' => 'hidden',
  203. '#default_value' => $items[$delta]['value'],
  204. )
  205. );
  206. }