file_entity.install 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the file_entity module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function file_entity_schema() {
  10. $schema['file_display'] = array(
  11. 'description' => 'Stores configuration options for file displays.',
  12. 'fields' => array(
  13. // @todo Can be refactored as a compond primary key after
  14. // http://drupal.org/node/924236 is implemented.
  15. 'name' => array(
  16. 'description' => 'A combined string (FILE_TYPE__VIEW_MODE__FILE_FORMATTER) identifying a file display configuration. For integration with CTools Exportables, stored as a single string rather than as a compound primary key.',
  17. 'type' => 'varchar',
  18. 'length' => '255',
  19. 'not null' => TRUE,
  20. ),
  21. 'weight' => array(
  22. 'type' => 'int',
  23. 'not null' => TRUE,
  24. 'default' => 0,
  25. 'description' => 'Weight of formatter within the display chain for the associated file type and view mode. A file is rendered using the lowest weighted enabled display configuration that matches the file type and view mode and that is capable of displaying the file.',
  26. ),
  27. 'status' => array(
  28. 'type' => 'int',
  29. 'unsigned' => TRUE,
  30. 'not null' => TRUE,
  31. 'default' => 0,
  32. 'size' => 'tiny',
  33. 'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
  34. ),
  35. 'settings' => array(
  36. 'type' => 'blob',
  37. 'not null' => FALSE,
  38. 'size' => 'big',
  39. 'serialize' => TRUE,
  40. 'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
  41. ),
  42. ),
  43. 'primary key' => array('name'),
  44. // Exportable support via CTools.
  45. 'export' => array(
  46. 'key' => 'name',
  47. 'key name' => 'Name',
  48. 'primary key' => 'name',
  49. // The {file_display}.status field is used to control whether the display
  50. // is active in the display chain. CTools-level disabling is something
  51. // different, and it's not yet clear how to interpret it for file displays.
  52. // Until that's figured out, prevent CTools-level disabling.
  53. 'can disable' => FALSE,
  54. 'default hook' => 'file_default_displays',
  55. 'identifier' => 'file_display',
  56. 'api' => array(
  57. 'owner' => 'file_entity',
  58. 'api' => 'file_default_displays',
  59. 'minimum_version' => 1,
  60. 'current_version' => 1,
  61. ),
  62. ),
  63. );
  64. return $schema;
  65. }
  66. /**
  67. * Implements hook_schema_alter().
  68. */
  69. function file_entity_schema_alter(&$schema) {
  70. $schema['file_managed']['fields']['type'] = array(
  71. 'description' => 'The type of this file.',
  72. 'type' => 'varchar',
  73. 'length' => 50,
  74. 'not null' => TRUE,
  75. // If the FILE_TYPE_NONE constant ever changes, then change the value here
  76. // too, and add an update function to deal with existing records. The
  77. // constant isn't used here, because there may be cases where this function
  78. // runs without the module file loaded.
  79. 'default' => 'undefined',
  80. );
  81. $schema['file_managed']['indexes']['file_type'] = array('type');
  82. }
  83. /**
  84. * Implements hook_install().
  85. */
  86. function file_entity_install() {
  87. $schema = array();
  88. file_entity_schema_alter($schema);
  89. $spec = $schema['file_managed']['fields']['type'];
  90. $indexes_new = array('indexes' => $schema['file_managed']['indexes']);
  91. // If another module (e.g., Media) had added a {file_managed}.type field,
  92. // then change it to the expected specification. Otherwise, add the field.
  93. if (db_field_exists('file_managed', 'type')) {
  94. // db_change_field() will fail if any records have type=NULL, so update
  95. // them to the new default value.
  96. db_update('file_managed')->fields(array('type' => $spec['default']))->isNull('type')->execute();
  97. // Indexes using a field being changed must be dropped prior to calling
  98. // db_change_field(). However, the database API doesn't provide a way to do
  99. // this without knowing what the old indexes are. Therefore, it is the
  100. // responsibility of the module that added them to drop them prior to
  101. // allowing this module to be installed.
  102. db_change_field('file_managed', 'type', 'type', $spec, $indexes_new);
  103. }
  104. else {
  105. db_add_field('file_managed', 'type', $spec, $indexes_new);
  106. }
  107. }
  108. /**
  109. * Implements hook_uninstall().
  110. */
  111. function file_entity_uninstall() {
  112. db_drop_field('file_managed', 'type');
  113. }
  114. /**
  115. * Create the {file_display} database table.
  116. */
  117. function file_entity_update_7000() {
  118. $schema['file_display'] = array(
  119. 'description' => 'Stores configuration options for file displays.',
  120. 'fields' => array(
  121. 'name' => array(
  122. 'description' => 'A combined string (FILE_TYPE__VIEW_MODE__FILE_FORMATTER) identifying a file display configuration. For integration with CTools Exportables, stored as a single string rather than as a compound primary key.',
  123. 'type' => 'varchar',
  124. 'length' => '255',
  125. 'not null' => TRUE,
  126. ),
  127. 'weight' => array(
  128. 'type' => 'int',
  129. 'not null' => TRUE,
  130. 'default' => 0,
  131. 'description' => 'Weight of formatter within the display chain for the associated file type and view mode. A file is rendered using the lowest weighted enabled display configuration that matches the file type and view mode and that is capable of displaying the file.',
  132. ),
  133. 'status' => array(
  134. 'type' => 'int',
  135. 'unsigned' => TRUE,
  136. 'not null' => TRUE,
  137. 'default' => 0,
  138. 'size' => 'tiny',
  139. 'description' => 'The status of the display. (1 = enabled, 0 = disabled)',
  140. ),
  141. 'settings' => array(
  142. 'type' => 'blob',
  143. 'not null' => FALSE,
  144. 'size' => 'big',
  145. 'serialize' => TRUE,
  146. 'description' => 'A serialized array of name value pairs that store the formatter settings for the display.',
  147. ),
  148. ),
  149. 'primary key' => array('name'),
  150. );
  151. db_create_table('file_display', $schema['file_display']);
  152. }
  153. /**
  154. * Move file display configurations from the 'file_displays' variable to the
  155. * {file_display} database table.
  156. */
  157. function file_entity_update_7001() {
  158. $file_displays = variable_get('file_displays');
  159. if (!empty($file_displays)) {
  160. foreach ($file_displays as $file_type => $file_type_displays) {
  161. if (!empty($file_type_displays)) {
  162. foreach ($file_type_displays as $view_mode => $view_mode_displays) {
  163. if (!empty($view_mode_displays)) {
  164. foreach ($view_mode_displays as $formatter_name => $display) {
  165. if (!empty($display)) {
  166. db_merge('file_display')
  167. ->key(array(
  168. 'name' => implode('__', array($file_type, $view_mode, $formatter_name)),
  169. ))
  170. ->fields(array(
  171. 'status' => isset($display['status']) ? $display['status'] : 0,
  172. 'weight' => isset($display['weight']) ? $display['weight'] : 0,
  173. 'settings' => isset($display['settings']) ? serialize($display['settings']) : NULL,
  174. ))
  175. ->execute();
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. variable_del('file_displays');
  184. }
  185. /**
  186. * Drupal 7.8 disallows empty string as the value for a bundle key, so update
  187. * empty {file_managed}.type records to 'undefined' instead.
  188. */
  189. function file_entity_update_7002() {
  190. db_update('file_managed')
  191. // Using 'undefined' instead of FILE_TYPE_NONE, because update functions can
  192. // run for disabled modules.
  193. ->fields(array('type' => 'undefined'))
  194. ->condition('type', '')
  195. ->execute();
  196. }