field_group.install 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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['type_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'] . '-' . $field['type_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. // Force drupal's schema to be rebuilt
  199. drupal_get_schema('field_group', TRUE);
  200. module_load_include('module', 'field_group');
  201. _field_group_recreate_identifiers();
  202. }
  203. db_update('system')
  204. ->fields(array('weight' => 1))
  205. ->condition('name', 'field_group')
  206. ->execute();
  207. }
  208. /**
  209. * Update hook to clear cache for new changes to take effect.
  210. */
  211. function field_group_update_7002() {
  212. module_load_include('module', 'field_group');
  213. // This hook is called to satify people with older version of field_group.
  214. // This will recreate all identifiers for the field_groups known in database.
  215. // At the moment, we only trigger field_groups that are stored in the database, where
  216. // we should maybe get all field_groups as ctools has registered them.
  217. // See http://drupal.org/node/1169146.
  218. // See http://drupal.org/node/1018550.
  219. _field_group_recreate_identifiers();
  220. }
  221. /**
  222. * Update hook to recreate identifiers.
  223. * @see function field_group_update_7002.
  224. */
  225. function field_group_update_7003() {
  226. module_load_include('module', 'field_group');
  227. _field_group_recreate_identifiers();
  228. }
  229. /**
  230. * Update hook to make sure identifier is set as unique key.
  231. */
  232. function field_group_update_7004() {
  233. db_drop_unique_key('field_group', 'identifier');
  234. db_add_unique_key('field_group', 'identifier', array('identifier'));
  235. }
  236. /**
  237. * Checks all existing groups and removes optional HTML classes
  238. * while adding them as extra classes.
  239. */
  240. function field_group_update_7005() {
  241. // Migrate the field groups so they have a unique identifier.
  242. $result = db_select('field_group', 'fg')
  243. ->fields('fg')
  244. ->execute();
  245. $rows = array();
  246. foreach($result as $row) {
  247. //$row->identifier = $row->group_name . '|' . $row->entity_type . '|' . $row->bundle . '|' . $row->mode;
  248. $row->data = unserialize($row->data);
  249. $classes = explode(" ", $row->data['format_settings']['instance_settings']['classes']);
  250. $optional_classes = array(str_replace("_", "-", $row->group_name), 'field-group-' . $row->data['format_type']);
  251. foreach ($optional_classes as $optional_class) {
  252. if (!in_array($optional_class, $classes)) {
  253. $classes[] = $optional_class;
  254. }
  255. }
  256. $row->data['format_settings']['instance_settings']['classes'] = implode(" ", $classes);
  257. $rows[] = $row;
  258. }
  259. foreach ($rows as $row) {
  260. drupal_write_record('field_group', $row, array('id'));
  261. }
  262. }
  263. /**
  264. * Save all optional HTML classes for fieldgroups located in features.
  265. * If you need the optional classes, recreate feature after this update.
  266. * If not, you can revert it.
  267. */
  268. function field_group_update_7006() {
  269. ctools_include("export");
  270. // Migrate the field groups so they have a unique identifier.
  271. $field_groups = ctools_export_load_object("field_group");
  272. foreach ($field_groups as $row) {
  273. // Only update feature field_groups this time.
  274. // Don't touch the fieldgroups in db.
  275. if ($row->export_type == EXPORT_IN_CODE) {
  276. $classes = explode(" ", $row->data['format_settings']['instance_settings']['classes']);
  277. $optional_classes = array(str_replace("_", "-", $row->group_name), 'field-group-' . $row->data['format_type']);
  278. foreach ($optional_classes as $optional_class) {
  279. if (!in_array($optional_class, $classes)) {
  280. $classes[] = $optional_class;
  281. }
  282. }
  283. $row->data['format_settings']['instance_settings']['classes'] = implode(" ", $classes);
  284. unset($row->id);
  285. drupal_write_record('field_group', $row);
  286. }
  287. }
  288. }