field_test.install 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the field_test module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function field_test_install() {
  10. // hook_entity_info_alter() needs to be executed as last.
  11. db_update('system')
  12. ->fields(array('weight' => 1))
  13. ->condition('name', 'field_test')
  14. ->execute();
  15. }
  16. /**
  17. * Implements hook_schema().
  18. */
  19. function field_test_schema() {
  20. $schema['test_entity'] = array(
  21. 'description' => 'The base table for test_entities.',
  22. 'fields' => array(
  23. 'ftid' => array(
  24. 'description' => 'The primary identifier for a test_entity.',
  25. 'type' => 'serial',
  26. 'unsigned' => TRUE,
  27. 'not null' => TRUE,
  28. ),
  29. 'ftvid' => array(
  30. 'description' => 'The current {test_entity_revision}.ftvid version identifier.',
  31. 'type' => 'int',
  32. 'unsigned' => TRUE,
  33. 'not null' => TRUE,
  34. 'default' => 0,
  35. ),
  36. 'fttype' => array(
  37. 'description' => 'The type of this test_entity.',
  38. 'type' => 'varchar',
  39. 'length' => 32,
  40. 'not null' => TRUE,
  41. 'default' => '',
  42. ),
  43. 'ftlabel' => array(
  44. 'description' => 'The label of this test_entity.',
  45. 'type' => 'varchar',
  46. 'length' => 255,
  47. 'not null' => TRUE,
  48. 'default' => '',
  49. ),
  50. ),
  51. 'unique keys' => array(
  52. 'ftvid' => array('ftvid'),
  53. ),
  54. 'primary key' => array('ftid'),
  55. );
  56. $schema['test_entity_bundle_key'] = array(
  57. 'description' => 'The base table for test entities with a bundle key.',
  58. 'fields' => array(
  59. 'ftid' => array(
  60. 'description' => 'The primary identifier for a test_entity_bundle_key.',
  61. 'type' => 'int',
  62. 'unsigned' => TRUE,
  63. 'not null' => TRUE,
  64. 'default' => 0,
  65. ),
  66. 'fttype' => array(
  67. 'description' => 'The type of this test_entity.',
  68. 'type' => 'varchar',
  69. 'length' => 32,
  70. 'not null' => FALSE,
  71. 'default' => '',
  72. ),
  73. ),
  74. );
  75. $schema['test_entity_bundle'] = array(
  76. 'description' => 'The base table for test entities with a bundle.',
  77. 'fields' => array(
  78. 'ftid' => array(
  79. 'description' => 'The primary identifier for a test_entity_bundle.',
  80. 'type' => 'int',
  81. 'unsigned' => TRUE,
  82. 'not null' => TRUE,
  83. 'default' => 0,
  84. ),
  85. ),
  86. );
  87. $schema['test_entity_revision'] = array(
  88. 'description' => 'Stores information about each saved version of a {test_entity}.',
  89. 'fields' => array(
  90. 'ftid' => array(
  91. 'description' => 'The {test_entity} this version belongs to.',
  92. 'type' => 'int',
  93. 'unsigned' => TRUE,
  94. 'not null' => TRUE,
  95. 'default' => 0,
  96. ),
  97. 'ftvid' => array(
  98. 'description' => 'The primary identifier for this version.',
  99. 'type' => 'serial',
  100. 'unsigned' => TRUE,
  101. 'not null' => TRUE,
  102. ),
  103. ),
  104. 'indexes' => array(
  105. 'nid' => array('ftid'),
  106. ),
  107. 'primary key' => array('ftvid'),
  108. );
  109. return $schema;
  110. }
  111. /**
  112. * Implements hook_field_schema().
  113. */
  114. function field_test_field_schema($field) {
  115. if ($field['type'] == 'test_field') {
  116. return array(
  117. 'columns' => array(
  118. 'value' => array(
  119. 'type' => 'int',
  120. 'size' => 'medium',
  121. 'not null' => FALSE,
  122. ),
  123. ),
  124. 'indexes' => array(
  125. 'value' => array('value'),
  126. ),
  127. );
  128. }
  129. else {
  130. $foreign_keys = array();
  131. // The 'foreign keys' key is not always used in tests.
  132. if (!empty($field['settings']['foreign_key_name'])) {
  133. $foreign_keys['foreign keys'] = array(
  134. // This is a dummy foreign key definition, references a table that
  135. // doesn't exist, but that's not a problem.
  136. $field['settings']['foreign_key_name'] => array(
  137. 'table' => $field['settings']['foreign_key_name'],
  138. 'columns' => array($field['settings']['foreign_key_name'] => 'id'),
  139. ),
  140. );
  141. }
  142. return array(
  143. 'columns' => array(
  144. 'shape' => array(
  145. 'type' => 'varchar',
  146. 'length' => 32,
  147. 'not null' => FALSE,
  148. ),
  149. 'color' => array(
  150. 'type' => 'varchar',
  151. 'length' => 32,
  152. 'not null' => FALSE,
  153. ),
  154. ),
  155. ) + $foreign_keys;
  156. }
  157. }