serial.module 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @file
  4. * The Serial module main file.
  5. */
  6. /**
  7. * Implements hook_field_info().
  8. */
  9. function serial_field_info() {
  10. return array(
  11. 'serial' => array(
  12. 'label' => t('Serial'),
  13. 'description' => t('Auto increment serial field type.'),
  14. // The "property_type" should be defined for accessing the
  15. // field by entity metadata wrapper.
  16. 'property_type' => 'serial',
  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 ('serial' == $field['type']) {
  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 > 0) {
  34. drupal_set_message(t('Serial values have been automatically set for %count existing nodes.', array(
  35. '%count' => $old_count,
  36. )));
  37. }
  38. }
  39. }
  40. /**
  41. * Implements hook_field_delete_instance().
  42. */
  43. function serial_field_delete_instance($instance) {
  44. $field = field_read_field($instance['field_name']);
  45. if ('serial' == $field['type']) {
  46. // Drop the assistant table.
  47. module_load_include('inc', 'serial');
  48. _serial_drop_table($field, $instance);
  49. }
  50. }
  51. /**
  52. * Implements hook_form_alter().
  53. */
  54. function serial_form_alter(&$form, $form_state, $form_id) {
  55. if ('field_ui_field_settings_form' == $form_id && 'serial' == $form['field']['type']['#value']) {
  56. drupal_set_message(t('Serial field %field has been created.', array(
  57. '%field' => $form['field']['field_name']['#value'],
  58. )));
  59. drupal_goto("admin/structure/types/manage/{$form['#bundle']}/fields");
  60. }
  61. }
  62. /**
  63. * Implements hook_field_presave().
  64. */
  65. function serial_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  66. if (empty($items)) {
  67. module_load_include('inc', 'serial');
  68. $items = array(array('value' => _serial_generate_value($instance['bundle'], $field['field_name'])));
  69. }
  70. }
  71. /**
  72. * Implements hook_field_is_empty().
  73. */
  74. function serial_field_is_empty($item, $field) {
  75. // Never should be treated as empty.
  76. return FALSE;
  77. }
  78. /**
  79. * Implements hook_node_presave().
  80. */
  81. function serial_node_presave($node) {
  82. if (module_exists('auto_nodetitle')) {
  83. if (auto_nodetitle_get_setting($node->type)) {
  84. auto_nodetitle_set_title($node);
  85. }
  86. }
  87. }
  88. /**
  89. * Implements hook_node_type_update().
  90. */
  91. function serial_node_type_update($info) {
  92. // Handle content type rename:
  93. if (isset($info->old_type) && ($info->old_type != $info->type)) {
  94. module_load_include('inc', 'serial');
  95. _serial_rename_tables($info->old_type, $info->type);
  96. }
  97. }
  98. /**
  99. * Implements hook_field_formatter_info().
  100. */
  101. function serial_field_formatter_info() {
  102. return array(
  103. 'serial_formatter_default' => array(
  104. 'label' => t('Default'),
  105. 'field types' => array('serial'),
  106. ),
  107. );
  108. }
  109. /**
  110. * Implements hook_field_formatter_view().
  111. */
  112. function serial_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  113. $element = array();
  114. // Define the field contents for the single default formatter.
  115. foreach ($items as $delta => $item) {
  116. $element[$delta] = array(
  117. '#markup' => theme('serial_formatter_default', array(
  118. 'serial_id' => $item['value'],
  119. )),
  120. );
  121. }
  122. return $element;
  123. }
  124. /**
  125. * Implements hook_theme().
  126. */
  127. function serial_theme() {
  128. // Register the theme for the default formatter.
  129. return array(
  130. 'serial_formatter_default' => array(
  131. 'variables' => array(
  132. 'serial_id' => NULL,
  133. ),
  134. ),
  135. );
  136. }
  137. /**
  138. * Theme function for the default formatter.
  139. */
  140. function theme_serial_formatter_default($variables) {
  141. return $variables['serial_id'];
  142. }
  143. /**
  144. * Implements hook_field_widget_info().
  145. */
  146. function serial_field_widget_info() {
  147. return array(
  148. 'serial' => array(
  149. 'label' => t('Hidden (Automatic)'),
  150. 'field types' => array('serial'),
  151. ),
  152. );
  153. }
  154. /**
  155. * Implements hook_field_widget().
  156. */
  157. function serial_field_widget(&$form, &$form_state, $field, $instance, $items, $delta = 0) {
  158. return array(
  159. 'value' => array(
  160. '#type' => 'hidden',
  161. '#default_value' => $items[$delta]['value'],
  162. ),
  163. );
  164. }