field_group.install 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. 'load callback' => 'field_group_group_load_by_identifier',
  18. 'save callback' => 'field_group_group_save',
  19. 'delete callback' => 'field_group_group_export_delete',
  20. 'can disable' => TRUE,
  21. 'api' => array(
  22. 'owner' => 'field_group',
  23. 'api' => 'field_group',
  24. 'minimum_version' => 1,
  25. 'current_version' => 1,
  26. ),
  27. ),
  28. 'fields' => array(
  29. 'id' => array(
  30. 'type' => 'serial',
  31. 'not null' => TRUE,
  32. 'description' => 'The primary identifier for a group',
  33. 'no export' => TRUE,
  34. ),
  35. 'identifier' => array(
  36. 'type' => 'varchar',
  37. 'length' => 255,
  38. 'not null' => TRUE,
  39. 'default' => '',
  40. 'description' => 'The unique string identifier for a group.',
  41. ),
  42. 'group_name' => array(
  43. 'type' => 'varchar',
  44. 'length' => 32,
  45. 'not null' => TRUE,
  46. 'default' => '',
  47. 'description' => 'The name of this group.',
  48. ),
  49. 'entity_type' => array(
  50. 'type' => 'varchar',
  51. 'length' => 32,
  52. 'not null' => TRUE,
  53. 'default' => '',
  54. ),
  55. 'bundle' => array(
  56. 'type' => 'varchar',
  57. 'length' => 128,
  58. 'not null' => TRUE,
  59. 'default' => ''
  60. ),
  61. 'mode' => array(
  62. 'type' => 'varchar',
  63. 'length' => 128,
  64. 'not null' => TRUE,
  65. 'default' => ''
  66. ),
  67. // @todo 'parent_name' is redundant with the data in the 'children'
  68. // entry, brings a risk of inconsistent data. This should be removed from
  69. // the schema and pre-computed it if needed in field_group_get_groups().
  70. 'parent_name' => array(
  71. 'type' => 'varchar',
  72. 'length' => 32,
  73. 'not null' => TRUE,
  74. 'default' => '',
  75. 'description' => 'The parent name for a group',
  76. ),
  77. 'data' => array(
  78. 'type' => 'blob',
  79. 'size' => 'big',
  80. 'not null' => TRUE,
  81. 'serialize' => TRUE,
  82. 'description' => 'Serialized data containing the group properties that do not warrant a dedicated column.',
  83. ),
  84. ),
  85. 'primary key' => array('id'),
  86. 'indexes' => array(
  87. 'group_name' => array('group_name'),
  88. ),
  89. 'unique keys' => array(
  90. 'identifier' => array('identifier'),
  91. ),
  92. );
  93. return $schema;
  94. }
  95. /**
  96. * Utility function: fetch all the field_group definitions from the database.
  97. */
  98. function _field_group_install_read_groups() {
  99. $groups = array();
  100. if (db_table_exists('content_group')) {
  101. $query = db_select('content_group', 'cg', array('fetch' => PDO::FETCH_ASSOC))
  102. ->fields('cg')
  103. // We only want non-multigroups.
  104. ->condition('group_type', 'standard');
  105. foreach ($query->execute() as $record) {
  106. $record['settings'] = unserialize($record['settings']);
  107. $groups[$record['group_name'] . '-' . $record['type_name']] = $record;
  108. }
  109. foreach ($groups as $key => $group) {
  110. $query2 = db_select('content_group_fields', 'cgf', array('fetch' => PDO::FETCH_ASSOC))
  111. ->fields('cgf')
  112. ->condition('group_name', $group['group_name']);
  113. foreach ($query2->execute() as $field) {
  114. $groups[$field['group_name'] . '-' . $field['type_name']]['children'][] = $field;
  115. }
  116. }
  117. }
  118. return $groups;
  119. }
  120. /**
  121. * Implements of hook_install().
  122. *
  123. * Because this is a new module in D7, hook_update_N() doesn't help D6
  124. * users who upgrade to run the migration path. So, we try that here as
  125. * the module is being installed.
  126. */
  127. function field_group_install() {
  128. $groups = _field_group_install_read_groups();
  129. module_load_include('module', 'field_group');
  130. if (!empty($groups)) {
  131. module_load_include('module', 'ctools');
  132. ctools_include('export');
  133. foreach ($groups as $group) {
  134. $group = (object) $group;
  135. $new = new stdClass();
  136. $new->group_name = $group->group_name;
  137. $new->entity_type = 'node';
  138. $new->bundle = $group->type_name;
  139. $new->label = $group->label;
  140. $new->parent_name = '';
  141. $new->children = array();
  142. foreach ($group->children as $child) {
  143. $new->children[] = $child['field_name'];
  144. }
  145. // The form.
  146. $new->id = NULL;
  147. $new->weight = $group->weight;
  148. $new->mode = 'form';
  149. $new->format_type = 'fieldset';
  150. $new->format_settings = array(
  151. 'formatter' => preg_match("/fieldset/", $group->settings['form']['style']) ? 'collapsible' : 'collapsed',
  152. 'instance_settings' => array(),
  153. );
  154. $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
  155. ctools_export_crud_save('field_group', $new);
  156. // The full node.
  157. $new->id = NULL;
  158. $new->weight = $group->weight;
  159. $new->mode = 'default';
  160. $new->format_type = $group->settings['display']['full']['format'];
  161. $new->format_settings = array(
  162. 'formatter' => 'collapsible',
  163. 'instance_settings' => array(),
  164. );
  165. $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
  166. ctools_export_crud_save('field_group', $new);
  167. // The teaser node.
  168. $new->id = NULL;
  169. $new->weight = $group->weight;
  170. $new->mode = 'teaser';
  171. $new->format_type = $group->settings['display']['teaser']['format'];
  172. $new->format_settings = array(
  173. 'formatter' => 'collapsible',
  174. 'instance_settings' => array(),
  175. );
  176. $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
  177. ctools_export_crud_save('field_group', $new);
  178. }
  179. }
  180. // Set weight to 1.
  181. db_update('system')
  182. ->fields(array('weight' => 1))
  183. ->condition('name', 'field_group')
  184. ->execute();
  185. }
  186. /**
  187. * Update hook on the field_group table to add an unique identifier.
  188. */
  189. function field_group_update_7001() {
  190. if (!db_field_exists('field_group', 'identifier')) {
  191. // Add the new string identifier field for ctools.
  192. db_add_field('field_group', 'identifier', array(
  193. 'type' => 'varchar',
  194. 'length' => 255,
  195. 'not null' => TRUE,
  196. 'default' => '',
  197. 'description' => 'The unique string identifier for a group.',
  198. ));
  199. // Force drupal's schema to be rebuilt
  200. drupal_get_schema('field_group', TRUE);
  201. module_load_include('module', 'field_group');
  202. _field_group_recreate_identifiers();
  203. }
  204. db_update('system')
  205. ->fields(array('weight' => 1))
  206. ->condition('name', 'field_group')
  207. ->execute();
  208. // Clear drupal and static cache.
  209. field_group_info_groups(NULL, NULL, NULL, TRUE);
  210. }
  211. /**
  212. * Update hook to clear cache for new changes to take effect.
  213. */
  214. function field_group_update_7002() {
  215. module_load_include('module', 'field_group');
  216. // This hook is called to satify people with older version of field_group.
  217. // This will recreate all identifiers for the field_groups known in database.
  218. // At the moment, we only trigger field_groups that are stored in the database, where
  219. // we should maybe get all field_groups as ctools has registered them.
  220. // See http://drupal.org/node/1169146.
  221. // See http://drupal.org/node/1018550.
  222. _field_group_recreate_identifiers();
  223. // Clear drupal and static cache.
  224. field_group_info_groups(NULL, NULL, NULL, TRUE);
  225. }
  226. /**
  227. * Update hook to recreate identifiers.
  228. * @see function field_group_update_7002.
  229. */
  230. function field_group_update_7003() {
  231. module_load_include('module', 'field_group');
  232. _field_group_recreate_identifiers();
  233. // Clear drupal and static cache.
  234. field_group_info_groups(NULL, NULL, NULL, TRUE);
  235. }
  236. /**
  237. * Update hook to make sure identifier is set as unique key.
  238. */
  239. function field_group_update_7004() {
  240. db_drop_unique_key('field_group', 'identifier');
  241. db_add_unique_key('field_group', 'identifier', array('identifier'));
  242. }
  243. /**
  244. * Checks all existing groups and removes optional HTML classes
  245. * while adding them as extra classes.
  246. */
  247. function field_group_update_7005() {
  248. // Migrate the field groups so they have a unique identifier.
  249. $result = db_select('field_group', 'fg')
  250. ->fields('fg')
  251. ->execute();
  252. $rows = array();
  253. foreach($result as $row) {
  254. //$row->identifier = $row->group_name . '|' . $row->entity_type . '|' . $row->bundle . '|' . $row->mode;
  255. $row->data = unserialize($row->data);
  256. $classes = explode(" ", $row->data['format_settings']['instance_settings']['classes']);
  257. $optional_classes = array(str_replace("_", "-", $row->group_name), 'field-group-' . $row->data['format_type']);
  258. foreach ($optional_classes as $optional_class) {
  259. if (!in_array($optional_class, $classes)) {
  260. $classes[] = $optional_class;
  261. }
  262. }
  263. $row->data['format_settings']['instance_settings']['classes'] = implode(" ", $classes);
  264. $rows[] = $row;
  265. }
  266. foreach ($rows as $row) {
  267. drupal_write_record('field_group', $row, array('id'));
  268. }
  269. // Clear drupal and static cache.
  270. field_group_info_groups(NULL, NULL, NULL, TRUE);
  271. }
  272. /**
  273. * Save all optional HTML classes for fieldgroups located in features.
  274. * If you need the optional classes, recreate feature after this update.
  275. * If not, you can revert it.
  276. */
  277. function field_group_update_7006() {
  278. ctools_include("export");
  279. // Migrate the field groups so they have a unique identifier.
  280. $field_groups = ctools_export_load_object("field_group");
  281. foreach ($field_groups as $row) {
  282. // Only update feature field_groups this time.
  283. // Don't touch the fieldgroups in db.
  284. if ($row->export_type == EXPORT_IN_CODE) {
  285. $classes = array();
  286. if (isset($row->data['format_settings'], $row->data['format_settings']['instance_settings'], $row->data['format_settings']['instance_settings']['classes'])) {
  287. $classes = explode(" ", $row->data['format_settings']['instance_settings']['classes']);
  288. }
  289. $optional_classes = array(str_replace("_", "-", $row->group_name), 'field-group-' . $row->data['format_type']);
  290. foreach ($optional_classes as $optional_class) {
  291. if (!in_array($optional_class, $classes)) {
  292. $classes[] = $optional_class;
  293. }
  294. }
  295. $row->data['format_settings']['instance_settings']['classes'] = implode(" ", $classes);
  296. unset($row->id);
  297. drupal_write_record('field_group', $row);
  298. }
  299. }
  300. // Clear drupal and static cache.
  301. field_group_info_groups(NULL, NULL, NULL, TRUE);
  302. }
  303. /**
  304. * Id attributes are now a setting. This update will insert the old id in the setting so old
  305. * markup doesn't get broken. If you don't want an attribute, you can delete
  306. * them on the fieldgroup settings.
  307. */
  308. function field_group_update_7007() {
  309. ctools_include("export");
  310. // Migrate the field groups so they have a unique identifier.
  311. $field_groups = ctools_export_load_object("field_group");
  312. foreach ($field_groups as $row) {
  313. if ($row->data['format_type'] == 'div' || $row->data['format_type'] == 'html5' || $row->data['format_type'] == 'html-element') {
  314. // If mode is default, we don't know what view mode it was. Take full then.
  315. $view_mode = $row->mode == 'default' ? 'full' : $row->mode;
  316. $id = $row->entity_type . '_' . $row->bundle . '_' . $view_mode . '_' . $row->group_name;
  317. $row->data['format_settings']['instance_settings']['id'] = $id;
  318. }
  319. if ($row->export_type == EXPORT_IN_CODE) {
  320. unset($row->id);
  321. drupal_write_record('field_group', $row);
  322. }
  323. else {
  324. drupal_write_record('field_group', $row, array('id'));
  325. }
  326. }
  327. // Clear drupal and static cache.
  328. field_group_info_groups(NULL, NULL, NULL, TRUE);
  329. }
  330. /**
  331. * Clear cache to notice the CTools load callback.
  332. */
  333. function field_group_update_7008() {
  334. drupal_flush_all_caches();
  335. }