field_group.install 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. <?php
  2. /**
  3. * @file
  4. * Fieldgroup module install file.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. */
  9. function field_group_schema() {
  10. $schema['field_group'] = array(
  11. 'description' => t('Table that contains field group entries and settings.'),
  12. // CTools export definitions.
  13. 'export' => array(
  14. 'key' => 'identifier',
  15. 'identifier' => 'field_group',
  16. 'default hook' => 'field_group_info',
  17. 'save callback' => 'field_group_group_save',
  18. 'delete callback' => 'field_group_group_export_delete',
  19. 'can disable' => TRUE,
  20. 'api' => array(
  21. 'owner' => 'field_group',
  22. 'api' => 'field_group',
  23. 'minimum_version' => 1,
  24. 'current_version' => 1,
  25. ),
  26. ),
  27. 'fields' => array(
  28. 'id' => array(
  29. 'type' => 'serial',
  30. 'not null' => TRUE,
  31. 'description' => 'The primary identifier for a group',
  32. 'no export' => TRUE,
  33. ),
  34. 'identifier' => array(
  35. 'type' => 'varchar',
  36. 'length' => 255,
  37. 'not null' => TRUE,
  38. 'default' => '',
  39. 'description' => 'The unique string identifier for a group.',
  40. ),
  41. 'group_name' => array(
  42. 'type' => 'varchar',
  43. 'length' => 32,
  44. 'not null' => TRUE,
  45. 'default' => '',
  46. 'description' => 'The name of this group.',
  47. ),
  48. 'entity_type' => array(
  49. 'type' => 'varchar',
  50. 'length' => 32,
  51. 'not null' => TRUE,
  52. 'default' => '',
  53. ),
  54. 'bundle' => array(
  55. 'type' => 'varchar',
  56. 'length' => 128,
  57. 'not null' => TRUE,
  58. 'default' => ''
  59. ),
  60. 'mode' => array(
  61. 'type' => 'varchar',
  62. 'length' => 128,
  63. 'not null' => TRUE,
  64. 'default' => ''
  65. ),
  66. // @todo 'parent_name' is redundant with the data in the 'children'
  67. // entry, brings a risk of inconsistent data. This should be removed from
  68. // the schema and pre-computed it if needed in field_group_get_groups().
  69. 'parent_name' => array(
  70. 'type' => 'varchar',
  71. 'length' => 32,
  72. 'not null' => TRUE,
  73. 'default' => '',
  74. 'description' => 'The parent name for a group',
  75. ),
  76. 'data' => array(
  77. 'type' => 'blob',
  78. 'size' => 'big',
  79. 'not null' => TRUE,
  80. 'serialize' => TRUE,
  81. 'description' => 'Serialized data containing the group properties that do not warrant a dedicated column.',
  82. ),
  83. ),
  84. 'primary key' => array('id'),
  85. 'indexes' => array(
  86. 'group_name' => array('group_name'),
  87. ),
  88. 'unique keys' => array(
  89. 'identifier' => array('identifier'),
  90. ),
  91. );
  92. return $schema;
  93. }
  94. /**
  95. * Utility function: fetch all the field_group definitions from the database.
  96. */
  97. function _field_group_install_read_groups() {
  98. $groups = array();
  99. if (db_table_exists('content_group')) {
  100. $query = db_select('content_group', 'cg', array('fetch' => PDO::FETCH_ASSOC))
  101. ->fields('cg')
  102. // We only want non-multigroups.
  103. ->condition('group_type', 'standard');
  104. foreach ($query->execute() as $record) {
  105. $record['settings'] = unserialize($record['settings']);
  106. $groups[$record['group_name']] = $record;
  107. }
  108. foreach ($groups as $key => $group) {
  109. $query2 = db_select('content_group_fields', 'cgf', array('fetch' => PDO::FETCH_ASSOC))
  110. ->fields('cgf')
  111. ->condition('group_name', $group['group_name']);
  112. foreach ($query2->execute() as $field) {
  113. $groups[$field['group_name']]['children'][] = $field;
  114. }
  115. }
  116. }
  117. return $groups;
  118. }
  119. /**
  120. * Implements of hook_install().
  121. *
  122. * Because this is a new module in D7, hook_update_N() doesn't help D6
  123. * users who upgrade to run the migration path. So, we try that here as
  124. * the module is being installed.
  125. */
  126. function field_group_install() {
  127. $groups = _field_group_install_read_groups();
  128. module_load_include('module', 'field_group');
  129. if (!empty($groups)) {
  130. module_load_include('module', 'ctools');
  131. ctools_include('export');
  132. foreach ($groups as $group) {
  133. $group = (object) $group;
  134. $new = new stdClass();
  135. $new->group_name = $group->group_name;
  136. $new->entity_type = 'node';
  137. $new->bundle = $group->type_name;
  138. $new->label = $group->label;
  139. $new->parent_name = '';
  140. $new->children = array();
  141. foreach ($group->children as $child) {
  142. $new->children[] = $child['field_name'];
  143. }
  144. // The form.
  145. $new->id = NULL;
  146. $new->weight = $group->weight;
  147. $new->mode = 'form';
  148. $new->format_type = 'fieldset';
  149. $new->format_settings = array(
  150. 'formatter' => preg_match("/fieldset/", $group->settings['form']['style']) ? 'collapsible' : 'collapsed',
  151. 'instance_settings' => array(),
  152. );
  153. $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
  154. ctools_export_crud_save('field_group', $new);
  155. // The full node.
  156. $new->id = NULL;
  157. $new->weight = $group->weight;
  158. $new->mode = 'default';
  159. $new->format_type = $group->settings['display']['full']['format'];
  160. $new->format_settings = array(
  161. 'formatter' => 'collapsible',
  162. 'instance_settings' => array(),
  163. );
  164. $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
  165. ctools_export_crud_save('field_group', $new);
  166. // The teaser node.
  167. $new->id = NULL;
  168. $new->weight = $group->weight;
  169. $new->mode = 'teaser';
  170. $new->format_type = $group->settings['display']['teaser']['format'];
  171. $new->format_settings = array(
  172. 'formatter' => 'collapsible',
  173. 'instance_settings' => array(),
  174. );
  175. $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
  176. ctools_export_crud_save('field_group', $new);
  177. }
  178. }
  179. // Set weight to 1.
  180. db_update('system')
  181. ->fields(array('weight' => 1))
  182. ->condition('name', 'field_group')
  183. ->execute();
  184. }
  185. /**
  186. * Update hook on the field_group table to add an unique identifier.
  187. */
  188. function field_group_update_7001() {
  189. if (!db_field_exists('field_group', 'identifier')) {
  190. // Add the new string identifier field for ctools.
  191. db_add_field('field_group', 'identifier', array(
  192. 'type' => 'varchar',
  193. 'length' => 255,
  194. 'not null' => TRUE,
  195. 'default' => '',
  196. 'description' => 'The unique string identifier for a group.',
  197. ));
  198. module_load_include('module', 'field_group');
  199. _field_group_recreate_identifiers();
  200. }
  201. db_update('system')
  202. ->fields(array('weight' => 1))
  203. ->condition('name', 'field_group')
  204. ->execute();
  205. }
  206. /**
  207. * Update hook to clear cache for new changes to take effect.
  208. */
  209. function field_group_update_7002() {
  210. module_load_include('module', 'field_group');
  211. // This hook is called to satify people with older version of field_group.
  212. // This will recreate all identifiers for the field_groups known in database.
  213. // At the moment, we only trigger field_groups that are stored in the database, where
  214. // we should maybe get all field_groups as ctools has registered them.
  215. // See http://drupal.org/node/1169146.
  216. // See http://drupal.org/node/1018550.
  217. _field_group_recreate_identifiers();
  218. }
  219. /**
  220. * Update hook to recreate identifiers.
  221. * @see function field_group_update_7002.
  222. */
  223. function field_group_update_7003() {
  224. module_load_include('module', 'field_group');
  225. _field_group_recreate_identifiers();
  226. }