field_group.module 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783
  1. <?php
  2. /**
  3. * @file
  4. * Fieldgroup module.
  5. *
  6. * For an overview of all php and javascript hooks, see field_group.api.php.
  7. *
  8. */
  9. /**
  10. * Implements hook_menu().
  11. */
  12. function field_group_menu() {
  13. $items = array();
  14. // Ensure the following is not executed until field_bundles is working and
  15. // tables are updated. Needed to avoid errors on initial installation.
  16. if (defined('MAINTENANCE_MODE')) {
  17. return $items;
  18. }
  19. // Create tabs for all possible bundles.
  20. foreach (entity_get_info() as $entity_type => $entity_info) {
  21. if (isset($entity_info['fieldable']) && $entity_info['fieldable']) {
  22. foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
  23. if (isset($bundle_info['admin'])) {
  24. // Extract path information from the bundle.
  25. $path = $bundle_info['admin']['path'];
  26. // Different bundles can appear on the same path (e.g. %node_type and
  27. // %comment_node_type). To allow field_group_menu_load() to extract the
  28. // actual bundle object from the translated menu router path
  29. // arguments, we need to identify the argument position of the bundle
  30. // name string ('bundle argument') and pass that position to the menu
  31. // loader. The position needs to be casted into a string; otherwise it
  32. // would be replaced with the bundle name string.
  33. if (isset($bundle_info['admin']['bundle argument'])) {
  34. $bundle_arg = $bundle_info['admin']['bundle argument'];
  35. $bundle_pos = (string) $bundle_arg;
  36. }
  37. else {
  38. $bundle_arg = $bundle_name;
  39. $bundle_pos = '0';
  40. }
  41. // This is the position of the %field_group_menu placeholder in the
  42. // items below.
  43. $group_position = count(explode('/', $path)) + 1;
  44. // Extract access information, providing defaults.
  45. $access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array('access callback', 'access arguments')));
  46. $access += array(
  47. 'access callback' => 'user_access',
  48. 'access arguments' => array('administer site configuration'),
  49. );
  50. $items["$path/groups/%field_group_menu/delete"] = array(
  51. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  52. 'title' => 'Delete',
  53. 'page callback' => 'drupal_get_form',
  54. 'page arguments' => array('field_group_delete_form', $group_position),
  55. 'type' => MENU_CALLBACK,
  56. 'file' => 'field_group.field_ui.inc',
  57. ) + $access;
  58. }
  59. }
  60. }
  61. }
  62. return $items;
  63. }
  64. /**
  65. * Implements hook_permission().
  66. */
  67. function field_group_permission() {
  68. return array(
  69. 'administer fieldgroups' => array(
  70. 'title' => t('Administer fieldgroups'),
  71. 'description' => t('Display the administration for fieldgroups.'),
  72. ),
  73. );
  74. }
  75. /**
  76. * Menu Wildcard loader function to load group definitions.
  77. *
  78. * @param $group_name
  79. * The name of the group, as contained in the path.
  80. * @param $entity_type
  81. * The name of the entity.
  82. * @param $bundle_name
  83. * The name of the bundle, as contained in the path.
  84. * @param $bundle_pos
  85. * The position of $bundle_name in $map.
  86. * @param $map
  87. * The translated menu router path argument map.
  88. */
  89. function field_group_menu_load($group_name, $entity_type, $bundle_name, $bundle_pos, $map) {
  90. if ($bundle_pos > 0) {
  91. $bundle = $map[$bundle_pos];
  92. $bundle_name = field_extract_bundle($entity_type, $bundle);
  93. }
  94. $args = func_get_args();
  95. $args_pop = array_pop($args);
  96. $mode = array_pop($args_pop);
  97. return field_group_load_field_group($group_name, $entity_type, $bundle_name, $mode);
  98. }
  99. /**
  100. * Loads a group definition.
  101. *
  102. * @param $group_name
  103. * The name of the group.
  104. * @param $entity_type
  105. * The name of the entity.
  106. * @param $bundle_name
  107. * The name of the bundle.
  108. * @param $mode
  109. * The view mode to load.
  110. */
  111. function field_group_load_field_group($group_name, $entity_type, $bundle_name, $mode) {
  112. ctools_include('export');
  113. $objects = ctools_export_load_object('field_group', 'conditions', array(
  114. 'group_name' => $group_name,
  115. 'entity_type' => $entity_type,
  116. 'bundle' => $bundle_name,
  117. 'mode' => $mode,
  118. ));
  119. $object = array_shift($objects);
  120. if ($object && isset($object->data)) {
  121. return field_group_unpack($object);
  122. }
  123. return $object;
  124. }
  125. /**
  126. * Implements hook_ctools_plugin_api().
  127. */
  128. function field_group_ctools_plugin_api($owner, $api) {
  129. if ($owner == 'field_group' && $api == 'field_group') {
  130. return array('version' => 1);
  131. }
  132. }
  133. /**
  134. * Implements hook_theme().
  135. */
  136. function field_group_theme() {
  137. return array(
  138. 'multipage' => array(
  139. 'render element' => 'element',
  140. ),
  141. 'multipage_pane' => array(
  142. 'render element' => 'element',
  143. ),
  144. );
  145. }
  146. /**
  147. * Implements hook_theme_registry_alter().
  148. */
  149. function field_group_theme_registry_alter(&$theme_registry) {
  150. // Inject field_group_build_entity_groups in all entity theming functions.
  151. $entity_info = entity_get_info();
  152. $entities = array();
  153. foreach ($entity_info as $entity => $info) {
  154. if (isset($entity_info[$entity]['fieldable']) && $entity_info[$entity]['fieldable']) {
  155. // User uses user_profile for theming.
  156. if ($entity == 'user') $entity = 'user_profile';
  157. $entities[] = $entity;
  158. }
  159. }
  160. // Support for File Entity.
  161. if (isset($theme_registry['file_entity'])) {
  162. $entities[] = 'file_entity';
  163. }
  164. // Support for Entity API.
  165. if (isset($theme_registry['entity'])) {
  166. $entities[] = 'entity';
  167. }
  168. foreach ($entities as $entity) {
  169. if (isset($theme_registry[$entity])) {
  170. $theme_registry[$entity]['preprocess functions'][] = 'field_group_build_entity_groups';
  171. // DS support, make sure it comes after field_group.
  172. if ($key = array_search('ds_entity_variables', $theme_registry[$entity]['preprocess functions'])) {
  173. unset($theme_registry[$entity]['preprocess functions'][$key]);
  174. $theme_registry[$entity]['preprocess functions'][] = 'ds_entity_variables';
  175. }
  176. }
  177. }
  178. }
  179. /**
  180. * Implements hook_field_attach_delete_bundle().
  181. *
  182. * @param String $entity_type
  183. * @param String $bundle
  184. */
  185. function field_group_field_attach_delete_bundle($entity_type, $bundle) {
  186. ctools_include('export');
  187. $list = field_group_read_groups(array('bundle' => $bundle, 'entity_type' => $entity_type));
  188. // Delete the entity's entry from field_group of all entities.
  189. // We fetch the field groups first to assign the removal task to ctools.
  190. if (isset($list[$entity_type], $list[$entity_type][$bundle])) {
  191. foreach ($list[$entity_type][$bundle] as $group_mode => $groups) {
  192. foreach ($groups as $group) {
  193. ctools_export_crud_delete('field_group', $group);
  194. }
  195. }
  196. }
  197. }
  198. /**
  199. * Implements hook_field_attach_form().
  200. */
  201. function field_group_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  202. $form['#attached']['css'][] = drupal_get_path('module', 'field_group') . '/field_group.field_ui.css';
  203. field_group_attach_groups($form, 'form', $form_state);
  204. $form['#pre_render'][] = 'field_group_form_pre_render';
  205. }
  206. /**
  207. * Implements hook_form_FORM_ID_alter().
  208. * Using hook_form_field_ui_field_overview_form_alter.
  209. */
  210. function field_group_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
  211. form_load_include($form_state, 'inc', 'field_group', 'field_group.field_ui');
  212. field_group_field_ui_overview_form_alter($form, $form_state);
  213. }
  214. /**
  215. * Implements hook_form_FORM_ID_alter().
  216. * Using hook_form_field_ui_display_overview_form_alter.
  217. */
  218. function field_group_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
  219. form_load_include($form_state, 'inc', 'field_group', 'field_group.field_ui');
  220. field_group_field_ui_overview_form_alter($form, $form_state, TRUE);
  221. }
  222. /**
  223. * Implements hook_field_attach_view_alter().
  224. */
  225. function field_group_field_attach_view_alter(&$element, $context) {
  226. // Check whether the view mode uses custom display settings or the 'default' mode.
  227. $actual_mode = 'default';
  228. if (isset($element['#entity_type']) && isset($element['#bundle'])) {
  229. $view_mode_settings = field_view_mode_settings($element['#entity_type'], $element['#bundle']);
  230. $view_mode = $context['view_mode'];
  231. $actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
  232. field_group_attach_groups($element, $actual_mode);
  233. }
  234. }
  235. /**
  236. * Implements hook_field_group_formatter_info().
  237. */
  238. function field_group_field_group_formatter_info() {
  239. $info = array(
  240. 'form' => array(
  241. 'div' => array(
  242. 'label' => t('Div'),
  243. 'description' => t('This fieldgroup renders the inner content in a simple div with the title as legend.'),
  244. 'format_types' => array('open', 'collapsible', 'collapsed'),
  245. 'instance_settings' => array('description' => '', 'show_label' => 1, 'label_element' => 'h3', 'effect' => 'none', 'speed' => 'fast', 'classes' => '', 'required_fields' => 1, 'id' => ''),
  246. 'default_formatter' => 'open',
  247. ),
  248. 'html5' => array(
  249. 'label' => t('HTML5'),
  250. 'description' => t('This fieldgroup renders the inner content in a semantic HTML5 wrapper'),
  251. 'instance_settings' => array('wrapper' => '', 'classes' => ''),
  252. ),
  253. 'fieldset' => array(
  254. 'label' => t('Fieldset'),
  255. 'description' => t('This fieldgroup renders the inner content in a fieldset with the title as legend.'),
  256. 'format_types' => array('open', 'collapsible', 'collapsed'),
  257. 'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
  258. 'default_formatter' => 'collapsible',
  259. ),
  260. 'tabs' => array(
  261. 'label' => t('Vertical tabs group'),
  262. 'description' => t('This fieldgroup renders child groups in its own vertical tabs wrapper.'),
  263. 'instance_settings' => array('classes' => ''),
  264. ),
  265. 'tab' => array(
  266. 'label' => t('Vertical tab'),
  267. 'description' => t('This fieldgroup renders the content in a fieldset, part of vertical tabs group.'),
  268. 'format_types' => array('open', 'closed'),
  269. 'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
  270. 'default_formatter' => 'closed',
  271. ),
  272. 'multipage-group' => array(
  273. 'label' => ('Multipage group'),
  274. 'description' => t('This fieldgroup renders groups on separate pages.'),
  275. 'instance_settings' => array('classes' => '', 'page_header' => 3, 'move_additional' => 1, 'page_counter' => 1, 'move_button' => 0),
  276. ),
  277. 'multipage' => array(
  278. 'label' => t('Multipage'),
  279. 'format_types' => array('start', 'no-start'),
  280. 'description' => t('This fieldgroup renders the content in a page.'),
  281. 'default_formatter' => 'no-start',
  282. 'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
  283. ),
  284. 'accordion' => array(
  285. 'label' => t('Accordion group'),
  286. 'description' => t('This fieldgroup renders child groups as jQuery accordion.'),
  287. 'instance_settings' => array('effect' => 'none', 'classes' => ''),
  288. ),
  289. 'accordion-item' => array(
  290. 'label' => t('Accordion item'),
  291. 'format_types' => array('open', 'closed'),
  292. 'description' => t('This fieldgroup renders the content in a div, part of accordion group.'),
  293. 'default_formatter' => 'closed',
  294. 'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
  295. ),
  296. ),
  297. 'display' => array(
  298. 'div' => array(
  299. 'label' => t('Div'),
  300. 'description' => t('This fieldgroup renders the inner content in a simple div with the title as legend.'),
  301. 'format_types' => array('open', 'collapsible', 'collapsed'),
  302. 'instance_settings' => array('description' => '', 'show_label' => 1, 'label_element' => 'h3', 'effect' => 'none', 'speed' => 'fast', 'classes' => ''),
  303. 'default_formatter' => 'collapsible',
  304. ),
  305. 'html5' => array(
  306. 'label' => t('HTML5'),
  307. 'description' => t('This fieldgroup renders the inner content in a semantic HTML5 wrapper'),
  308. 'instance_settings' => array('wrapper' => '', 'classes' => ''),
  309. ),
  310. 'fieldset' => array(
  311. 'label' => t('Fieldset'),
  312. 'description' => t('This fieldgroup renders the inner content in a fieldset with the title as legend.'),
  313. 'format_types' => array('open', 'collapsible', 'collapsed'),
  314. 'instance_settings' => array('description' => '', 'classes' => ''),
  315. 'default_formatter' => 'collapsible',
  316. ),
  317. 'tabs' => array(
  318. 'label' => t('Vertical tabs group'),
  319. 'description' => t('This fieldgroup renders child groups in its own vertical tabs wrapper.'),
  320. 'instance_settings' => array('classes' => ''),
  321. ),
  322. 'tab' => array(
  323. 'label' => t('Vertical tab'),
  324. 'description' => t('This fieldgroup renders the content in a fieldset, part of vertical tabs group.'),
  325. 'format_types' => array('open', 'closed'),
  326. 'instance_settings' => array('description' => '', 'classes' => ''),
  327. 'default_formatter' => 'closed',
  328. ),
  329. 'accordion' => array(
  330. 'label' => t('Accordion group'),
  331. 'description' => t('This fieldgroup renders child groups as jQuery accordion.'),
  332. 'instance_settings' => array('description' => '', 'classes' => ''),
  333. ),
  334. 'accordion-item' => array(
  335. 'label' => t('Accordion item'),
  336. 'format_types' => array('open', 'closed'),
  337. 'description' => t('This fieldgroup renders the content in a div, part of accordion group.'),
  338. 'instance_settings' => array('classes' => ''),
  339. 'default_formatter' => 'closed',
  340. ),
  341. ),
  342. );
  343. // Extra format types derived from elements provided by elements module.
  344. if (module_exists('elements')) {
  345. $info['form']['htabs'] = array(
  346. 'label' => ('Horizontal tabs group'),
  347. 'description' => t('This fieldgroup renders child groups in its own horizontal tabs wrapper.'),
  348. 'instance_settings' => array('classes' => ''),
  349. );
  350. $info['form']['htab'] = array(
  351. 'label' => t('Horizontal tab item'),
  352. 'format_types' => array('open', 'closed'),
  353. 'description' => t('This fieldgroup renders the content in a fieldset, part of horizontal tabs group.'),
  354. 'default_formatter' => 'closed',
  355. 'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
  356. );
  357. $info['display']['htabs'] = array(
  358. 'label' => ('Horizontal tabs group'),
  359. 'description' => t('This fieldgroup renders child groups in its own horizontal tabs wrapper.'),
  360. 'instance_settings' => array('classes' => ''),
  361. );
  362. $info['display']['htab'] = array(
  363. 'label' => t('Horizontal tab item'),
  364. 'format_types' => array('open', 'closed'),
  365. 'description' => t('This fieldgroup renders the content in a fieldset, part of horizontal tabs group.'),
  366. 'instance_settings' => array('description' => '', 'classes' => ''),
  367. 'default_formatter' => 'closed',
  368. );
  369. }
  370. return $info;
  371. }
  372. /**
  373. * Implements hook_field_group_format_settings().
  374. * If the group has no format settings, default ones will be added.
  375. * @params Object $group The group object.
  376. * @return Array $form The form element for the format settings.
  377. */
  378. function field_group_field_group_format_settings($group) {
  379. // Add a wrapper for extra settings to use by others.
  380. $form = array(
  381. 'instance_settings' => array(
  382. '#tree' => TRUE,
  383. '#weight' => 2,
  384. ),
  385. );
  386. $field_group_types = field_group_formatter_info();
  387. $mode = $group->mode == 'form' ? 'form' : 'display';
  388. $formatter = $field_group_types[$mode][$group->format_type];
  389. // Add the required formatter type selector.
  390. if (isset($formatter['format_types'])) {
  391. $form['formatter'] = array(
  392. '#title' => t('Fieldgroup settings'),
  393. '#type' => 'select',
  394. '#options' => drupal_map_assoc($formatter['format_types']),
  395. '#default_value' => isset($group->format_settings['formatter']) ? $group->format_settings['formatter'] : $formatter['default_formatter'],
  396. '#weight' => 1,
  397. );
  398. }
  399. if (isset($formatter['instance_settings']['required_fields']) && $mode == 'form') {
  400. $form['instance_settings']['required_fields'] = array(
  401. '#type' => 'checkbox',
  402. '#title' => t('Mark group for required fields.'),
  403. '#default_value' => isset($group->format_settings['instance_settings']['required_fields']) ? $group->format_settings['instance_settings']['required_fields'] : (isset($formatter['instance_settings']['required_fields']) ? $formatter['instance_settings']['required_fields'] : ''),
  404. '#weight' => 2,
  405. );
  406. }
  407. if (isset($formatter['instance_settings']['id'])) {
  408. $form['instance_settings']['id'] = array(
  409. '#title' => t('ID'),
  410. '#type' => 'textfield',
  411. '#default_value' => isset($group->format_settings['instance_settings']['id']) ? $group->format_settings['instance_settings']['id'] : (isset($formatter['instance_settings']['id']) ? $formatter['instance_settings']['id'] : ''),
  412. '#weight' => 3,
  413. '#element_validate' => array('field_group_validate_id'),
  414. );
  415. }
  416. if (isset($formatter['instance_settings']['classes'])) {
  417. $form['instance_settings']['classes'] = array(
  418. '#title' => t('Extra CSS classes'),
  419. '#type' => 'textfield',
  420. '#default_value' => isset($group->format_settings['instance_settings']['classes']) ? $group->format_settings['instance_settings']['classes'] : (isset($formatter['instance_settings']['classes']) ? $formatter['instance_settings']['classes'] : ''),
  421. '#weight' => 4,
  422. '#element_validate' => array('field_group_validate_css_class'),
  423. );
  424. }
  425. if (isset($formatter['instance_settings']['description'])) {
  426. $form['instance_settings']['description'] = array(
  427. '#title' => t('Description'),
  428. '#type' => 'textarea',
  429. '#default_value' => isset($group->format_settings['instance_settings']['description']) ? $group->format_settings['instance_settings']['description'] : (isset($formatter['instance_settings']['description']) ? $formatter['instance_settings']['description'] : ''),
  430. '#weight' => 0,
  431. );
  432. }
  433. // Add optional instance_settings.
  434. switch ($group->format_type) {
  435. case 'div':
  436. $form['instance_settings']['show_label'] = array(
  437. '#title' => t('Show label'),
  438. '#type' => 'select',
  439. '#options' => array(0 => t('No'), 1 => t('Yes')),
  440. '#default_value' => isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : $formatter['instance_settings']['show_label'],
  441. '#weight' => 2,
  442. );
  443. $form['instance_settings']['label_element'] = array(
  444. '#title' => t('Label element'),
  445. '#type' => 'select',
  446. '#options' => array('h2' => t('Header 2'), 'h3' => t('Header 3')),
  447. '#default_value' => isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : $formatter['instance_settings']['label_element'],
  448. '#weight' => 2,
  449. );
  450. $form['instance_settings']['effect'] = array(
  451. '#title' => t('Effect'),
  452. '#type' => 'select',
  453. '#options' => array('none' => t('None'), 'blind' => t('Blind')),
  454. '#default_value' => isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : $formatter['instance_settings']['effect'],
  455. '#weight' => 2,
  456. );
  457. $form['instance_settings']['speed'] = array(
  458. '#title' => t('Speed'),
  459. '#type' => 'select',
  460. '#options' => array('none' => t('None'), 'slow' => t('Slow'), 'fast' => t('Fast')),
  461. '#default_value' => isset($group->format_settings['instance_settings']['speed']) ? $group->format_settings['instance_settings']['speed'] : $formatter['instance_settings']['speed'],
  462. '#weight' => 3,
  463. );
  464. break;
  465. case 'html5':
  466. $form['instance_settings']['wrapper'] = array(
  467. '#title' => t('HTML5 wrapper'),
  468. '#type' => 'select',
  469. '#options' => array('section' => t('Section'), 'article' => t('Article'), 'header' => t('Header'), 'footer' => t('footer'), 'aside' => t('Aside')),
  470. '#default_value' => isset($group->format_settings['instance_settings']['wrapper']) ? $group->format_settings['instance_settings']['wrapper'] : 'section',
  471. );
  472. break;
  473. case 'fieldset':
  474. break;
  475. case 'multipage-group':
  476. $form['instance_settings']['page_header'] = array(
  477. '#title' => t('Format page title'),
  478. '#type' => 'select',
  479. '#options' => array(0 => t('None'), 1 => t('Label only'), 2 => t('Step 1 of 10'), 3 => t('Step 1 of 10 [Label]'),),
  480. '#default_value' => isset($group->format_settings['instance_settings']['page_header']) ? $group->format_settings['instance_settings']['page_header'] : $formatter['instance_settings']['page_header'],
  481. '#weight' => 1,
  482. );
  483. $form['instance_settings']['page_counter'] = array(
  484. '#title' => t('Add a page counter at the bottom'),
  485. '#type' => 'select',
  486. '#options' => array(0 => t('No'), 1 => t('Format 1 / 10'), 2 => t('The count number only')),
  487. '#default_value' => isset($group->format_settings['instance_settings']['page_counter']) ? $group->format_settings['instance_settings']['page_counter'] : $formatter['instance_settings']['page_counter'],
  488. '#weight' => 2,
  489. );
  490. $form['instance_settings']['move_button'] = array(
  491. '#title' => t('Move submit button to last multipage'),
  492. '#type' => 'select',
  493. '#options' => array(0 => t('No'), 1 => t('Yes')),
  494. '#default_value' => isset($group->format_settings['instance_settings']['move_button']) ? $group->format_settings['instance_settings']['move_button'] : $formatter['instance_settings']['move_button'],
  495. '#weight' => 3,
  496. );
  497. $form['instance_settings']['move_additional'] = array(
  498. '#title' => t('Move additional settings to last multipage (if available)'),
  499. '#type' => 'select',
  500. '#options' => array(0 => t('No'), 1 => t('Yes')),
  501. '#default_value' => isset($group->format_settings['instance_settings']['move_additional']) ? $group->format_settings['instance_settings']['move_additional'] : $formatter['instance_settings']['move_additional'],
  502. '#weight' => 4,
  503. );
  504. case 'tabs':
  505. case 'htabs':
  506. break;
  507. case 'accordion':
  508. $form['instance_settings']['effect'] = array(
  509. '#title' => t('Effect'),
  510. '#type' => 'select',
  511. '#options' => array('none' => t('None'), 'bounceslide' => t('Bounce slide')),
  512. '#default_value' => isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : $formatter['instance_settings']['effect'],
  513. '#weight' => 2,
  514. );
  515. break;
  516. case 'multipage':
  517. break;
  518. case 'tab':
  519. case 'htab':
  520. case 'accordion-item':
  521. default:
  522. }
  523. return $form;
  524. }
  525. /**
  526. * Helper function to prepare basic variables needed for most formatters.
  527. *
  528. * Called in field_group_field_group_pre_render(), but can also be called in
  529. * other implementations of hook_field_group_pre_render().
  530. */
  531. function field_group_pre_render_prepare(&$group) {
  532. // Prepare extra classes.
  533. $group->classes = array('field-group-' . $group->format_type, str_replace('_', '-', $group->group_name));
  534. if (isset($group->format_settings['formatter'])) {
  535. $group->collapsible = in_array($group->format_settings['formatter'], array('collapsible', 'collapsed'));
  536. // Open or closed horizontal or vertical tabs will be collapsible by default.
  537. if ($group->format_type == 'tab' || $group->format_type == 'htab') {
  538. $group->collapsible = TRUE;
  539. }
  540. $group->collapsed = in_array($group->format_settings['formatter'], array('collapsed', 'closed'));
  541. if ($group->collapsible) {
  542. $group->classes[] = 'collapsible';
  543. if ($group->collapsed) {
  544. $group->classes[] = 'collapsed';
  545. }
  546. }
  547. }
  548. // Add a required-fields class to trigger the js.
  549. if (isset($group->format_settings['instance_settings']) && !empty($group->format_settings['instance_settings']['required_fields'])) {
  550. $group->classes[] = 'required-fields';
  551. }
  552. $group->classes = implode(' ', $group->classes);
  553. if (isset($group->format_settings['instance_settings'], $group->format_settings['instance_settings']['classes'])) {
  554. $group->classes .= ' ' . check_plain($group->format_settings['instance_settings']['classes']);
  555. }
  556. $group->description = isset($group->format_settings['instance_settings']['description']) ? filter_xss_admin($group->format_settings['instance_settings']['description']) : '';
  557. }
  558. /**
  559. * Implements hook_field_group_pre_render().
  560. *
  561. * @param Array $elements by address.
  562. * @param Object $group The Field group info.
  563. */
  564. function field_group_field_group_pre_render(& $element, &$group, & $form) {
  565. field_group_pre_render_prepare($group);
  566. $view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
  567. // Add all field_group format types to the js settings.
  568. $form['#attached']['js'][] = array(
  569. 'data' => array('field_group' => array($group->format_type => $view_mode)),
  570. 'type' => 'setting',
  571. );
  572. $form['#attached']['js'][] = 'misc/form.js';
  573. $form['#attached']['js'][] = 'misc/collapse.js';
  574. if (isset($group->format_settings['instance_settings']['id']) && !empty($group->format_settings['instance_settings']['id'])) {
  575. $element['#id'] = $group->format_settings['instance_settings']['id'];
  576. }
  577. else {
  578. $element['#id'] = $form['#entity_type'] . '_' . $form['#bundle'] . '_' . $view_mode . '_' . $group->group_name;
  579. }
  580. $element['#weight'] = $group->weight;
  581. // Call the pre render function for the format type.
  582. $function = "field_group_pre_render_" . str_replace("-", "_", $group->format_type);
  583. if (function_exists($function)) {
  584. $function($element, $group, $form);
  585. }
  586. }
  587. /**
  588. * Implements field_group_pre_render_<format-type>.
  589. * Format type: Fieldset.
  590. *
  591. * @param $element The field group form element.
  592. * @param $group The Field group object prepared for pre_render.
  593. * @param $form The root element or form.
  594. */
  595. function field_group_pre_render_fieldset(&$element, $group, &$form) {
  596. $element += array(
  597. '#type' => 'fieldset',
  598. '#title' => check_plain(t($group->label)),
  599. '#collapsible' => $group->collapsible,
  600. '#collapsed' => $group->collapsed,
  601. '#pre_render' => array(),
  602. '#attributes' => array('class' => explode(' ', $group->classes)),
  603. '#description' => $group->description,
  604. );
  605. $element['#attached']['js'][] = 'misc/form.js';
  606. $element['#attached']['js'][] = 'misc/collapse.js';
  607. }
  608. /**
  609. * Implements field_group_pre_render_<format-type>.
  610. * Format type: Div.
  611. *
  612. * @param $element The field group form element.
  613. * @param $group The Field group object prepared for pre_render.
  614. * @param $form The root element or form.
  615. */
  616. function field_group_pre_render_div(&$element, $group, &$form) {
  617. $show_label = isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : 0;
  618. $label_element = isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : 'h2';
  619. $effect = isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : 'none';
  620. $speed = isset($group->format_settings['instance_settings']['speed']) ? $group->format_settings['instance_settings']['speed'] : 'none';
  621. $element['#type'] = 'markup';
  622. $group->classes .= " speed-$speed effect-$effect";
  623. if ($group->format_settings['formatter'] != 'open') {
  624. $element['#prefix'] = '<div id="' . $element['#id'] . '" class="field-group-format ' . $group->classes . '">
  625. <' . $label_element . '><span class="field-group-format-toggler">' . check_plain(t($group->label)) . '</span></' . $label_element . '>
  626. <div class="field-group-format-wrapper" style="display: ' . ($group->collapsed ? 'none' : 'block') . ';">';
  627. $element['#suffix'] = '</div></div>';
  628. }
  629. else {
  630. $element['#prefix'] = '<div id="' . $element['#id'] . '" class="field-group-format ' . $group->group_name . ' ' . $group->classes . '">';
  631. if ($show_label) {
  632. $element['#prefix'] .= '<' . $label_element . '><span>' . check_plain(t($group->label)) . '</span></' . $label_element . '>';
  633. }
  634. $element['#suffix'] = '</div>';
  635. }
  636. if (!empty($group->description)) {
  637. $element['#prefix'] .= '<div class="description">' . $group->description . '</div>';
  638. }
  639. if ($effect == 'blind') {
  640. drupal_add_library('system', 'effects.blind');
  641. }
  642. }
  643. /**
  644. * Implements field_group_pre_render_<format-type>.
  645. * Format type: HTML5.
  646. *
  647. * @param $element The field group form element.
  648. * @param $group The Field group object prepared for pre_render.
  649. * @param $form The root element or form.
  650. */
  651. function field_group_pre_render_html5(&$element, $group, &$form) {
  652. $element += array(
  653. '#type' => 'markup',
  654. '#prefix' => '<' . $group->format_settings['instance_settings']['wrapper'] . ' id="' . $element['#id'] . '" class="field-group-format ' . $group->classes . '">',
  655. '#suffix' => '</' . $group->format_settings['instance_settings']['wrapper'] . '>',
  656. );
  657. }
  658. /**
  659. * Implements field_group_pre_render_<format-type>.
  660. * Format type: Accordion.
  661. *
  662. * @param $element The field group form element.
  663. * @param $group The Field group object prepared for pre_render.
  664. * @param $form The root element or form.
  665. */
  666. function field_group_pre_render_accordion(&$element, $group, &$form) {
  667. $effect = isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : 'none';
  668. $group->classes .= " effect-$effect";
  669. $element += array(
  670. '#type' => 'markup',
  671. '#prefix' => '<div class="field-group-' . $group->format_type . '-wrapper ' . $group->classes . '">',
  672. '#suffix' => '</div>',
  673. );
  674. }
  675. /**
  676. * Implements field_group_pre_render_<format-type>.
  677. * Format type: Accordion item.
  678. *
  679. * @param $element The field group form element.
  680. * @param $group The Field group object prepared for pre_render.
  681. * @param $form The root element or form.
  682. */
  683. function field_group_pre_render_accordion_item(&$element, $group, &$form) {
  684. $element += array(
  685. '#type' => 'markup',
  686. '#prefix' => '<h3 class="field-group-format-toggler ' . $group->format_type . ($group->collapsed ? '' : ' field-group-accordion-active') . '">' . check_plain(t($group->label)) . '</h3>
  687. <div class="field-group-format-wrapper ' . $group->classes . '">',
  688. '#suffix' => '</div>',
  689. //'#attributes' => array('class' => array($group->format_type)),
  690. );
  691. if (!empty($group->description)) {
  692. $element['#prefix'] .= '<div class="description">' . $group->description . '</div>';
  693. }
  694. drupal_add_library('system', 'ui.accordion');
  695. }
  696. /**
  697. * Implements field_group_pre_render_<format-type>.
  698. * Format type: Horizontal tabs group.
  699. *
  700. * @param $element The field group form element.
  701. * @param $group The Field group object prepared for pre_render.
  702. * @param $form The root element or form.
  703. */
  704. function field_group_pre_render_htabs(&$element, $group, &$form) {
  705. $element += array(
  706. '#type' => 'horizontal_tabs',
  707. '#title' => check_plain(t($group->label)),
  708. '#theme_wrappers' => array('horizontal_tabs'),
  709. '#prefix' => '<div class="field-group-' . $group->format_type . '-wrapper ' . $group->classes . '">',
  710. '#suffix' => '</div>',
  711. );
  712. }
  713. /**
  714. * Implements field_group_pre_render_<format-type>.
  715. * Format type: Horizontal tab.
  716. *
  717. * @param $element The field group form element.
  718. * @param $group The Field group object prepared for pre_render.
  719. * @param $form The root element or form.
  720. */
  721. function field_group_pre_render_htab(&$element, $group, &$form) {
  722. $element += array(
  723. '#type' => 'fieldset',
  724. '#title' => check_plain(t($group->label)),
  725. '#collapsible' => $group->collapsible,
  726. '#collapsed' => $group->collapsed,
  727. '#attributes' => array('class' => explode(" ", $group->classes)),
  728. '#group' => $group->parent_name,
  729. // very important. Cannot be added on the form!
  730. '#parents' => array($group->parent_name),
  731. '#description' => $group->description,
  732. );
  733. }
  734. /**
  735. * Implements field_group_pre_render_<format-type>.
  736. * Format type: Multipage group.
  737. *
  738. * @param $element The field group form element.
  739. * @param $group The Field group object prepared for pre_render.
  740. * @param $form The root element or form.
  741. */
  742. function field_group_pre_render_multipage_group(&$element, &$group, &$form) {
  743. $multipage_element = array(
  744. '#type' => 'multipage',
  745. '#theme_wrappers' => array('multipage'),
  746. '#prefix' => '<div class="field-group-' . $group->format_type . '-wrapper ' . $group->classes . '">',
  747. '#suffix' => '</div>',
  748. );
  749. $element += $multipage_element;
  750. $move_additional = isset($group->format_settings['instance_settings']['move_additional']) ? ($group->format_settings['instance_settings']['move_additional'] && isset($form['additional_settings'])) : isset($form['additional_settings']);
  751. $move_button = isset($group->format_settings['instance_settings']['move_button']) ? $group->format_settings['instance_settings']['move_button'] : 0;
  752. drupal_add_js(array(
  753. 'field_group' => array(
  754. 'multipage_move_submit' => (bool) $move_button,
  755. 'multipage_move_additional' => (bool) $move_additional
  756. )
  757. ), 'setting');
  758. }
  759. /**
  760. * Implements field_group_pre_render_<format-type>.
  761. * Format type: Multipage.
  762. *
  763. * @param $element The field group form element.
  764. * @param $group The Field group object prepared for pre_render.
  765. * @param $form The root element or form.
  766. */
  767. function field_group_pre_render_multipage(&$element, $group, &$form) {
  768. $group->classes .= $group->format_settings['formatter'] == 'start' ? ' multipage-open' : ' multipage-closed';
  769. $element += array(
  770. '#type' => 'multipage_pane',
  771. '#title' => check_plain(t($group->label)),
  772. '#collapsible' => $group->collapsible,
  773. '#collapsed' => $group->collapsed,
  774. '#attributes' => array('class' => explode(" ", $group->classes)),
  775. '#group' => $group->parent_name,
  776. '#group_object' => $group,
  777. '#parent_group_object' => $form['#groups'][$group->parent_name],
  778. // very important. Cannot be added on the form!
  779. '#parents' => array($group->parent_name),
  780. '#description' => $group->description,
  781. );
  782. }
  783. /**
  784. * Implements field_group_pre_render_<format-type>.
  785. * Format type: Vertical tabs wrapper.
  786. *
  787. * @param $element The field group form element.
  788. * @param $group The Field group object prepared for pre_render.
  789. * @param $form The root element or form.
  790. */
  791. function field_group_pre_render_tabs(&$element, $group, &$form) {
  792. $element += array(
  793. '#type' => 'vertical_tabs',
  794. '#theme_wrappers' => array('vertical_tabs'),
  795. '#prefix' => '<div class="field-group-' . $group->format_type . '-wrapper ' . $group->classes . '">',
  796. '#suffix' => '</div>',
  797. );
  798. $element[$group->group_name . '__active_tab'] = array(
  799. '#type' => 'hidden',
  800. '#default_value' => '',
  801. '#attributes' => array('class' => array('vertical-tabs-active-tab')),
  802. );
  803. $form['#attached']['library'][] = array('system', 'drupal.form');
  804. $form['#attached']['library'][] = array('system', 'drupal.collapse');
  805. }
  806. /**
  807. * Implements field_group_pre_render_<format-type>.
  808. * Format type: Vertical tab.
  809. *
  810. * @param $element The field group form element.
  811. * @param $group The Field group object prepared for pre_render.
  812. * @param $form The root element or form.
  813. */
  814. function field_group_pre_render_tab(&$element, $group, &$form) {
  815. $view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
  816. // Could be it never runs through htab.
  817. $form['#attached']['js'][] = array(
  818. 'data' => array('field_group' => array('tabs' => $view_mode)),
  819. 'type' => 'setting',
  820. );
  821. $add = array(
  822. '#type' => 'fieldset',
  823. '#id' => 'edit-' . $group->group_name,
  824. '#title' => check_plain(t($group->label)),
  825. '#collapsible' => $group->collapsible,
  826. '#collapsed' => $group->collapsed,
  827. '#attributes' => array('class' => explode(" ", $group->classes)),
  828. '#description' => $group->description,
  829. );
  830. // Front-end and back-end on configuration will lead
  831. // to vertical tabs nested in a separate vertical group.
  832. if ($view_mode != 'form') {
  833. $add['#group'] = empty($group->parent_name) ? 'additional_settings' : $group->parent_name;
  834. $add['#parents'] = array($add['#group']);
  835. $element += $add;
  836. }
  837. // Form fieldgroups which are nested into a vertical tab group
  838. // are handled a little different.
  839. elseif (!empty($group->parent_name)) {
  840. $add['#group'] = $group->parent_name;
  841. $element += $add;
  842. }
  843. // Forms "can" have additional settins. We'll try to locate it first, if not
  844. // exists, field_group will create a parallel additional settings entry.
  845. else {
  846. // Create the fieldgroup element.
  847. $add['#parents'] = array('additional_settings');
  848. $add['#group'] = 'additional_settings';
  849. $add['#weight'] = -30 + $group->weight; // hardcoded to bring our extra additional vtabs on top.
  850. // Check if the additional_settings exist for this type of form.
  851. if (isset($form['additional_settings']['group']['#groups']['additional_settings'])) {
  852. // Merge fieldgroups with the core additional settings.
  853. $form['additional_settings']['group']['#groups']['additional_settings'][$group->group_name] = $add;
  854. $form['additional_settings']['group']['#groups'][$group->group_name] = array('#group_exists' => TRUE);
  855. // Nest the fields inside the appropriate structure.
  856. foreach (element_children($element) as $fieldname) {
  857. $form['additional_settings']['group']['#groups']['additional_settings'][$group->group_name][$fieldname] = &$element[$fieldname];
  858. unset($element[$fieldname]);
  859. }
  860. }
  861. // Assumption the wrapper is in the root. This could be done by field_group itself
  862. // in previous loop of tabs in same wrapper or even some other contrib / custom module.
  863. else {
  864. if (!isset($form['additional_settings']['#type'])) {
  865. $form['additional_settings'] = array(
  866. '#type' => 'vertical_tabs',
  867. '#weight' => $group->weight,
  868. '#theme_wrappers' => array('vertical_tabs'),
  869. '#prefix' => '<div class="field-group-' . $group->format_type . '-wrapper">',
  870. '#suffix' => '</div>',
  871. );
  872. $form['#attached']['js'][] = 'misc/form.js';
  873. $form['#attached']['js'][] = 'misc/collapse.js';
  874. }
  875. $form['additional_settings'][$group->group_name] = $add;
  876. // Nest the fields inside the appropriate structure.
  877. foreach (element_children($element) as $fieldname) {
  878. $form['additional_settings'][$group->group_name][$fieldname] = &$element[$fieldname];
  879. unset($element[$fieldname]);
  880. }
  881. }
  882. }
  883. }
  884. /**
  885. * Implements hook_field_group_build_pre_render_alter().
  886. * @param Array $elements by address.
  887. */
  888. function field_group_field_group_build_pre_render_alter(& $element) {
  889. // Someone is doing a node view, in a node view. Reset content.
  890. // TODO Check if this breaks something else.
  891. if (isset($element['#node']->content) && count($element['#node']->content) > 0) {
  892. $element['#node']->content = array();
  893. }
  894. $display = isset($element['#view_mode']);
  895. $groups = array_keys($element['#groups']);
  896. // Dish the fieldgroups with no fields for non-forms.
  897. if ($display) {
  898. field_group_remove_empty_display_groups($element, $groups);
  899. }
  900. else {
  901. // Fix the problem on forms with additional settings.
  902. field_group_remove_empty_form_groups('form', $element, $groups, $element['#groups'], $element['#entity_type']);
  903. }
  904. // Add the default field_group javascript and stylesheet.
  905. $element['#attached']['js'][] = drupal_get_path('module', 'field_group') . '/field_group.js';
  906. $element['#attached']['css'][] = drupal_get_path('module', 'field_group') . '/field_group.css';
  907. // Move additional settings to the last multipage pane if configured that way.
  908. // Note that multipages MUST be in the root of the form.
  909. foreach (element_children($element) as $name) {
  910. if (isset($element[$name]['#type']) && $element[$name]['#type'] == 'multipage' && isset($element['additional_settings'])) {
  911. $parent_group = $element['#groups'][$name];
  912. $move_additional = isset($parent_group->format_settings['instance_settings']['move_additional']) ? $parent_group->format_settings['instance_settings']['move_additional'] : 1;
  913. $last_pane = NULL;
  914. foreach (element_children($element[$name], TRUE) as $pane) {
  915. $last_pane = $pane;
  916. }
  917. $element[$name][$last_pane]['additional_settings'] = $element['additional_settings'];
  918. unset($element['additional_settings']);
  919. }
  920. }
  921. }
  922. /**
  923. * Remove empty groups on forms.
  924. *
  925. * @param String $parent_name
  926. * The name of the element.
  927. * @param array $element
  928. * The element to check the empty state.
  929. * @param array $groups
  930. * Array of group objects.
  931. */
  932. function field_group_remove_empty_form_groups($name, & $element, $groups, &$form_groups, $entity) {
  933. $exceptions = array('user__account', 'comment__author');
  934. $children = element_children($element);
  935. $hasChildren = FALSE;
  936. if (count($children)) {
  937. foreach ($children as $childname) {
  938. if (in_array($childname, $groups)) {
  939. field_group_remove_empty_form_groups($childname, $element[$childname], $groups, $form_groups, $entity);
  940. }
  941. $exception = $entity . '__' . $childname;
  942. $hasChildren = $hasChildren ? TRUE : (isset($element[$childname]['#type']) || in_array($exception, $exceptions));
  943. }
  944. }
  945. if (!$hasChildren) {
  946. // Remove empty elements from the #groups.
  947. if (empty($element) && isset($form_groups[$name]) && !is_array($form_groups[$name])) {
  948. foreach ($form_groups as $group_name => $group) {
  949. if (isset($group->children)) {
  950. $group_children = array_flip($group->children);
  951. if (isset($group_children[$name])) {
  952. unset($form_groups[$group_name]->children[$group_children[$name]]);
  953. }
  954. }
  955. }
  956. }
  957. $element['#access'] = FALSE;
  958. }
  959. }
  960. /**
  961. * Remove empty groups on entity display.
  962. * @param array $element
  963. * The element to check the empty state.
  964. * @param array $groups
  965. * Array of group objects.
  966. */
  967. function field_group_remove_empty_display_groups(& $element, $groups) {
  968. $empty_child = TRUE;
  969. $empty_group = TRUE;
  970. // Loop through the children for current element.
  971. foreach (element_children($element) as $name) {
  972. // Descend if the child is a group.
  973. if (in_array($name, $groups)) {
  974. $empty_child = field_group_remove_empty_display_groups($element[$name], $groups);
  975. if (!$empty_child) {
  976. $empty_group = FALSE;
  977. }
  978. }
  979. // Child is a field, the element is not empty.
  980. elseif (!empty($element[$name])) {
  981. $empty_group = FALSE;
  982. }
  983. }
  984. // Reset an empty group.
  985. if ($empty_group) {
  986. $element = NULL;
  987. }
  988. return $empty_group;
  989. }
  990. /**
  991. * Implements hook_field_group_format_summary().
  992. */
  993. function field_group_field_group_format_summary($group) {
  994. $group_form = module_invoke_all('field_group_format_settings', $group);
  995. $output = '';
  996. if (isset($group->format_settings['formatter'])) {
  997. $output .= '<strong>' . $group->format_type . '</strong> ' . $group->format_settings['formatter'] . '';
  998. }
  999. if (isset($group->format_settings['instance_settings'])) {
  1000. $last = end($group->format_settings['instance_settings']);
  1001. $output .= '<br />';
  1002. foreach ($group->format_settings['instance_settings'] as $key => $value) {
  1003. if (empty($value)) {
  1004. continue;
  1005. }
  1006. $output .= '<strong>' . $key . '</strong> ';
  1007. if (isset($group_form['instance_settings'], $group_form['instance_settings'][$key]['#options'])) {
  1008. $value = $group_form['instance_settings'][$key]['#options'][$value];
  1009. }
  1010. // Shorten the string.
  1011. if (drupal_strlen($value) > 38) {
  1012. $value = truncate_utf8($value, 50, TRUE, TRUE);
  1013. }
  1014. // If still numeric, handle it as yes or no.
  1015. elseif (is_numeric($value)) {
  1016. $value = $value == '1' ? t('yes') : t('no');
  1017. }
  1018. $output .= check_plain($value);
  1019. $output .= $last == $value ? ' ' : '<br />';
  1020. }
  1021. }
  1022. return $output;
  1023. }
  1024. /**
  1025. * Implements hook_element_info().
  1026. */
  1027. function field_group_element_info() {
  1028. $types = array();
  1029. $types['multipage'] = array(
  1030. '#theme_wrappers' => array('multipage'),
  1031. '#default_tab' => '',
  1032. '#process' => array('form_process_multipage'),
  1033. );
  1034. $types['multipage_pane'] = array(
  1035. '#value' => NULL,
  1036. '#process' => array('form_process_fieldset', 'ajax_process_form'),
  1037. '#pre_render' => array('form_pre_render_fieldset'),
  1038. '#theme_wrappers' => array('multipage_pane'),
  1039. );
  1040. return $types;
  1041. }
  1042. /**
  1043. * Implements hook_library().
  1044. */
  1045. function field_group_library() {
  1046. $path = drupal_get_path('module', 'field_group');
  1047. // Multipage Tabs.
  1048. $libraries['multipage'] = array(
  1049. 'title' => 'Multipage',
  1050. 'website' => 'http://drupal.org/node/323112',
  1051. 'version' => '1.0',
  1052. 'js' => array(
  1053. $path . '/multipage/multipage.js' => array(),
  1054. ),
  1055. 'css' => array(
  1056. $path . '/multipage/multipage.css' => array(),
  1057. ),
  1058. );
  1059. return $libraries;
  1060. }
  1061. /**
  1062. * Implements hook_field_attach_rename_bundle().
  1063. * TODO: also update identifier
  1064. */
  1065. function field_group_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  1066. db_query('UPDATE {field_group} SET bundle = :bundle WHERE bundle = :old_bundle AND entity_type = :entity_type', array(
  1067. ':bundle' => $bundle_new,
  1068. ':old_bundle' => $bundle_old,
  1069. ':entity_type' => $entity_type,
  1070. ));
  1071. }
  1072. /**
  1073. * Creates a group formatted as multipage.
  1074. * This function will never be callable from within field_group rendering. Other
  1075. * modules using #type multipage will have the benefit of this processor.
  1076. *
  1077. * @param $element
  1078. * An associative array containing the properties and children of the
  1079. * fieldset.
  1080. * @param $form_state
  1081. * The $form_state array for the form this multipage tab widget belongs to.
  1082. * @return
  1083. * The processed element.
  1084. */
  1085. function form_process_multipage($element, &$form_state) {
  1086. // Inject a new fieldset as child, so that form_process_fieldset() processes
  1087. // this fieldset like any other fieldset.
  1088. $element['group'] = array(
  1089. '#type' => 'fieldset',
  1090. '#theme_wrappers' => array(),
  1091. '#parents' => $element['#parents'],
  1092. );
  1093. // The JavaScript stores the currently selected tab in this hidden
  1094. // field so that the active control can be restored the next time the
  1095. // form is rendered, e.g. on preview pages or when form validation
  1096. // fails.
  1097. $name = implode('__', $element['#parents']);
  1098. if (isset($form_state['values'][$name . '__active_control'])) {
  1099. $element['#default_tab'] = $form_state['values'][$name . '__active_control'];
  1100. }
  1101. $element[$name . '__active_control'] = array(
  1102. '#type' => 'hidden',
  1103. '#default_value' => $element['#default_control'],
  1104. '#attributes' => array('class' => array('multipage-active-control')),
  1105. );
  1106. return $element;
  1107. }
  1108. /**
  1109. * Returns HTML for an element's children fieldsets as multipage.
  1110. *
  1111. * @param $variables
  1112. * An associative array containing:
  1113. * - element: An associative array containing the properties and children of the
  1114. * fieldset. Properties used: #children.
  1115. *
  1116. * @ingroup themeable
  1117. */
  1118. function theme_multipage($variables) {
  1119. $element = $variables['element'];
  1120. // Add required JavaScript and Stylesheet.
  1121. drupal_add_library('field_group', 'multipage');
  1122. $output = '<h2 class="element-invisible">' . (!empty($element['#title']) ? $element['#title'] : t('Multipage')) . '</h2>';
  1123. $output .= '<div class="multipage-panes">';
  1124. $output .= $element['#children'];
  1125. $output .= '</div>';
  1126. return $output;
  1127. }
  1128. /**
  1129. * Returns HTML for multipage pane.
  1130. *
  1131. * @param $variables
  1132. * An associative array containing:
  1133. * - element: An associative array containing the properties and children of the
  1134. * fieldset. Properties used: #children.
  1135. *
  1136. * @ingroup themeable
  1137. */
  1138. function theme_multipage_pane($variables) {
  1139. $element = $variables['element'];
  1140. $group = $variables['element']['#group_object'];
  1141. $parent_group = $variables['element']['#parent_group_object'];
  1142. static $multipages;
  1143. if (!isset($multipages[$group->parent_name])) {
  1144. $multipages = array($group->parent_name => 0);
  1145. }
  1146. $multipages[$parent_group->group_name]++;
  1147. // Create a page title from the label.
  1148. $page_header = isset($parent_group->format_settings['instance_settings']['page_header']) ? $parent_group->format_settings['instance_settings']['page_header'] : 3;
  1149. switch ($page_header) {
  1150. case 1:
  1151. $title = $element['#title'];
  1152. break;
  1153. case 2:
  1154. $title = t('Step %count of %total', array('%count' => $multipages[$parent_group->group_name], '%total' => count($parent_group->children)));
  1155. break;
  1156. case 3:
  1157. $title = t('Step %count of %total !label', array('%count' => $multipages[$parent_group->group_name], '%total' => count($parent_group->children), '!label' => $element['#title']));
  1158. break;
  1159. case 0:
  1160. default:
  1161. $title = '';
  1162. break;
  1163. }
  1164. element_set_attributes($element, array('id'));
  1165. _form_set_class($element, array('form-wrapper'));
  1166. $output = '<div' . drupal_attributes($element['#attributes']) . '>';
  1167. if (!empty($element['#title'])) {
  1168. // Always wrap fieldset legends in a SPAN for CSS positioning.
  1169. $output .= '<h2 class="multipage-pane-title"><span>' . $title . '</span></h2>';
  1170. }
  1171. $output .= '<div class="fieldset-wrapper multipage-pane-wrapper">';
  1172. if (!empty($element['#description'])) {
  1173. $output .= '<div class="fieldset-description">' . $element['#description'] . '</div>';
  1174. }
  1175. $output .= $element['#children'];
  1176. if (isset($element['#value'])) {
  1177. $output .= $element['#value'];
  1178. }
  1179. // Add a page counter if needed.
  1180. // counter array(0 => t('No'), 1 => t('Format 1 / 10'), 2 => t('The count number only'));
  1181. $page_counter_format = isset($parent_group->format_settings['instance_settings']['page_counter']) ? $parent_group->format_settings['instance_settings']['page_counter'] : 1;
  1182. $multipage_element['#page_counter_rendered'] = '';
  1183. if ($page_counter_format == 1) {
  1184. $output .= t('<span class="multipage-counter">%count / %total</span>', array('%count' => $multipages[$parent_group->group_name], '%total' => count($parent_group->children)));
  1185. }
  1186. elseif ($page_counter_format == 2) {
  1187. $output .= t('<span class="multipage-counter">%count</span>', array('%count' => $multipages[$parent_group->group_name]));
  1188. }
  1189. $output .= '</div>';
  1190. $output .= "</div>\n";
  1191. return $output;
  1192. }
  1193. /**
  1194. * Get all groups.
  1195. *
  1196. * @param $entity_type
  1197. * The name of the entity.
  1198. * @param $bundle
  1199. * The name of the bundle.
  1200. * @param $view_mode
  1201. * The view mode.
  1202. * @param $reset.
  1203. * Whether to reset the cache or not.
  1204. */
  1205. function field_group_info_groups($entity_type = NULL, $bundle = NULL, $view_mode = NULL, $reset = FALSE) {
  1206. static $groups = FALSE;
  1207. if (!$groups || $reset) {
  1208. if (!$reset && $cached = cache_get('field_groups', 'cache_field')) {
  1209. $groups = $cached->data;
  1210. }
  1211. else {
  1212. drupal_static_reset('ctools_export_load_object');
  1213. drupal_static_reset('ctools_export_load_object_all');
  1214. $groups = field_group_read_groups();
  1215. cache_set('field_groups', $groups, 'cache_field');
  1216. }
  1217. }
  1218. if (!isset($entity_type)) {
  1219. return $groups;
  1220. }
  1221. else {
  1222. if (isset($groups[$entity_type][$bundle][$view_mode])) {
  1223. return $groups[$entity_type][$bundle][$view_mode];
  1224. }
  1225. else {
  1226. return array();
  1227. }
  1228. }
  1229. }
  1230. /**
  1231. * Read all groups.
  1232. *
  1233. * @param $params
  1234. * Parameters for the query
  1235. * $name The name of the entity.
  1236. * $bundle The name of the bundle.
  1237. * $view_mode The view mode.
  1238. */
  1239. function field_group_read_groups($params = array()) {
  1240. $groups = array();
  1241. ctools_include('export');
  1242. if (empty($params)) {
  1243. $records = ctools_export_load_object('field_group');
  1244. }
  1245. else {
  1246. $records = ctools_export_load_object('field_group', 'conditions', $params);
  1247. }
  1248. foreach ($records as $group) {
  1249. // Deleted groups.
  1250. // @TODO remove the code below and add something meaningfull for "disabled" state.
  1251. if (isset($group->disabled) && $group->disabled) {
  1252. continue;
  1253. }
  1254. $groups[$group->entity_type][$group->bundle][$group->mode][$group->group_name] = field_group_unpack($group);
  1255. }
  1256. return $groups;
  1257. }
  1258. /**
  1259. * Utility function to recreate identifiers.
  1260. */
  1261. function _field_group_recreate_identifiers() {
  1262. // Migrate the field groups so they have a unique identifier.
  1263. $result = db_select('field_group', 'fg')
  1264. ->fields('fg')
  1265. ->execute();
  1266. $rows = array();
  1267. foreach($result as $row) {
  1268. $row->identifier = $row->group_name . '|' . $row->entity_type . '|' . $row->bundle . '|' . $row->mode;
  1269. $row->data = unserialize($row->data);
  1270. $rows[] = $row;
  1271. }
  1272. foreach ($rows as $row) {
  1273. drupal_write_record('field_group', $row, array('id'));
  1274. }
  1275. }
  1276. /**
  1277. * Checks if a field_group exists in required context.
  1278. *
  1279. * @param String $group_name
  1280. * The name of the group.
  1281. * @param String $entity_type
  1282. * The name of the entity.
  1283. * @param String $bundle
  1284. * The bundle for the entity.
  1285. * @param String $mode
  1286. * The view mode context the group will be rendered.
  1287. */
  1288. function field_group_exists($group_name, $entity_type, $bundle, $mode) {
  1289. $groups = field_group_read_groups();
  1290. return !empty($groups[$entity_type][$bundle][$mode][$group_name]);
  1291. }
  1292. /**
  1293. * Unpacks a database row in a FieldGroup object.
  1294. * @param $group
  1295. * Database result object with stored group data.
  1296. * @return $group
  1297. * Field group object.
  1298. */
  1299. function field_group_unpack($group) {
  1300. // Extract unserialized data.
  1301. if (isset($group->data)) {
  1302. $data = $group->data;
  1303. unset($group->data);
  1304. $group->label = $data['label'];
  1305. $group->weight = $data['weight'];
  1306. $group->children = $data['children'];
  1307. $group->format_type = !empty($data['format_type']) ? $data['format_type'] : 'fieldset';
  1308. if (isset($data['format_settings'])) {
  1309. $group->format_settings = $data['format_settings'];
  1310. }
  1311. }
  1312. return $group;
  1313. }
  1314. /**
  1315. * Packs a FieldGroup object into a database row.
  1316. * @param $group
  1317. * FieldGroup object.
  1318. * @return $record
  1319. * Database row object, ready to be inserted/update
  1320. */
  1321. function field_group_pack($group) {
  1322. $record = clone $group;
  1323. $record->data = array(
  1324. 'label' => $record->label,
  1325. 'weight' => $record->weight,
  1326. 'children' => $record->children,
  1327. 'format_type' => !empty($record->format_type) ? $record->format_type : 'fieldset',
  1328. );
  1329. if (isset($record->format_settings)) {
  1330. $record->data['format_settings'] = $record->format_settings;
  1331. }
  1332. return $record;
  1333. }
  1334. /**
  1335. * Delete a field group.
  1336. * This function is also called by ctools export when calls are
  1337. * made through ctools_export_crud_delete().
  1338. *
  1339. * @param $group
  1340. * A group definition.
  1341. * @param $ctools_crud
  1342. * Is this function called by the ctools crud delete.
  1343. */
  1344. function field_group_group_export_delete($group, $ctools_crud = TRUE) {
  1345. $query = db_delete('field_group');
  1346. if (isset($group->identifier)) {
  1347. $query->condition('identifier', $group->identifier);
  1348. if (!$ctools_crud) {
  1349. ctools_export_crud_disable('field_group', $group->identifier);
  1350. }
  1351. }
  1352. elseif (isset($group->id)) {
  1353. $query->condition('id', $group->id);
  1354. }
  1355. if (!empty($group->mode)) {
  1356. $query->condition('mode', $group->mode);
  1357. }
  1358. $query->execute();
  1359. cache_clear_all('field_groups', 'cache_field');
  1360. module_invoke_all('field_group_delete_field_group', $group);
  1361. }
  1362. /**
  1363. * field_group_group_save().
  1364. *
  1365. * Saves a group definition.
  1366. * This function is called by ctools export when calls are made
  1367. * through ctools_export_crud_save().
  1368. *
  1369. * @param $group
  1370. * A group definition.
  1371. */
  1372. function field_group_group_save(& $group) {
  1373. // Prepare the record.
  1374. $object = field_group_pack($group);
  1375. if (isset($object->export_type) && $object->export_type & EXPORT_IN_DATABASE) {
  1376. // Existing record.
  1377. $update = array('id');
  1378. module_invoke_all('field_group_update_field_group', $object);
  1379. }
  1380. else {
  1381. // New record.
  1382. $update = array();
  1383. $object->export_type = EXPORT_IN_DATABASE;
  1384. module_invoke_all('field_group_create_field_group', $object);
  1385. }
  1386. return drupal_write_record('field_group', $object, $update);
  1387. }
  1388. /**
  1389. * Function to retrieve all format possibilities for the fieldgroups.
  1390. */
  1391. function field_group_formatter_info($display_overview = FALSE) {
  1392. $cache = &drupal_static(__FUNCTION__, array());
  1393. if (empty($cache)) {
  1394. if ($cached = cache_get('field_group_formatter_info', 'cache_field')) {
  1395. $formatters = $cached->data;
  1396. }
  1397. else {
  1398. $formatters = array();
  1399. $formatters += module_invoke_all('field_group_formatter_info');
  1400. $hidden_region = array(
  1401. 'label' => t('<Hidden>'),
  1402. 'description' => '',
  1403. 'format_types' => array(),
  1404. 'instance_settings' => array(),
  1405. 'default_formatter' => '',
  1406. );
  1407. //$formatters['form']['hidden'] = $hidden_region;
  1408. $formatters['display']['hidden'] = $hidden_region;
  1409. cache_set('field_group_formatter_info', $formatters, 'cache_field');
  1410. }
  1411. $cache = $formatters;
  1412. }
  1413. return $cache;
  1414. }
  1415. /**
  1416. * Attach groups to the (form) build.
  1417. *
  1418. * @param Array $element
  1419. * The part of the form.
  1420. * @param String $view_mode
  1421. * The mode for the build.
  1422. * @param Array $form_state
  1423. * The optional form state when in view_mode = form context.
  1424. */
  1425. function field_group_attach_groups(&$element, $view_mode, $form_state = array()) {
  1426. $entity_type = $element['#entity_type'];
  1427. $bundle = $element['#bundle'];
  1428. $element['#groups'] = field_group_info_groups($entity_type, $bundle, $view_mode);
  1429. $element['#fieldgroups'] = $element['#groups'];
  1430. // Create a lookup array.
  1431. $group_children = array();
  1432. foreach ($element['#groups'] as $group_name => $group) {
  1433. foreach ($group->children as $child) {
  1434. $group_children[$child] = $group_name;
  1435. }
  1436. }
  1437. $element['#group_children'] = $group_children;
  1438. }
  1439. /**
  1440. * Pre render callback for rendering groups.
  1441. * @see field_group_field_attach_form
  1442. * @param $element Form that is beïng rendered.
  1443. */
  1444. function field_group_form_pre_render(&$element) {
  1445. return field_group_build_entity_groups($element, 'form');
  1446. }
  1447. /**
  1448. * Preprocess/ Pre-render callback.
  1449. *
  1450. * @see field_group_form_pre_render()
  1451. * @see field_group_theme_registry_alter
  1452. * @see field_group_fields_nest()
  1453. * @param $vars preprocess vars or form element
  1454. * @param $type The type of object beïng rendered
  1455. * @return $element Array with re-arranged fields in forms.
  1456. */
  1457. function field_group_build_entity_groups(&$vars, $type) {
  1458. if ($type == 'form') {
  1459. $element = &$vars;
  1460. $nest_vars = NULL;
  1461. }
  1462. else {
  1463. $element = &$vars['elements'];
  1464. $nest_vars = &$vars;
  1465. }
  1466. // No groups on the entity.
  1467. if (empty($element['#groups'])) {
  1468. return $element;
  1469. }
  1470. // Nest the fields in the corresponding field groups.
  1471. field_group_fields_nest($element, $nest_vars);
  1472. // Allow others to alter the pre_rendered build.
  1473. drupal_alter('field_group_build_pre_render', $element);
  1474. // Return the element on forms.
  1475. if ($type == 'form') {
  1476. return $element;
  1477. }
  1478. // Put groups inside content if we are rendering an entity_view.
  1479. foreach ($element['#groups'] as $group) {
  1480. if (!empty($element[$group->group_name])) {
  1481. $vars['content'][$group->group_name] = $element[$group->group_name];
  1482. }
  1483. }
  1484. }
  1485. /**
  1486. * Recursive function to nest fields in the field groups.
  1487. *
  1488. * This function will take out all the elements in the form and
  1489. * place them in the correct container element, a fieldgroup.
  1490. * The current group element in the loop is passed recursively so we can
  1491. * stash fields and groups in it while we go deeper in the array.
  1492. * @param Array $element
  1493. * The current element to analyse for grouping.
  1494. * @param Array $vars
  1495. * Rendering vars from the entity beïng viewed.
  1496. */
  1497. function field_group_fields_nest(&$element, &$vars = NULL) {
  1498. // Create all groups and keep a flat list of references to these groups.
  1499. $group_references = array();
  1500. foreach ($element['#groups'] as $group_name => $group) {
  1501. $element[$group_name] = array();
  1502. $group_references[$group_name] = &$element[$group_name];
  1503. }
  1504. // Move all children to their parents. Use the flat list of references for
  1505. // direct access as we don't know where in the root_element hierarchy the
  1506. // parent currently is situated.
  1507. foreach ($element['#group_children'] as $child_name => $parent_name) {
  1508. // Entity beïng viewed
  1509. if ($vars) {
  1510. // If not a group, check vars['content'] for empty field.
  1511. if (!isset($element['#groups'][$child_name]) && isset($vars['content'][$child_name])) {
  1512. $group_references[$parent_name][$child_name] = $vars['content'][$child_name];
  1513. unset($vars['content'][$child_name]);
  1514. }
  1515. // If this is a group, we have to use a reference to keep the reference
  1516. // list intact (but if it is a field we don't mind).
  1517. else {
  1518. $group_references[$parent_name][$child_name] = &$element[$child_name];
  1519. unset($element[$child_name]);
  1520. }
  1521. }
  1522. // Form beïng viewed
  1523. else {
  1524. // Block denied fields (#access) before they are put in groups.
  1525. // Fields (not groups) that don't have children (like field_permissions) are removed
  1526. // in field_group_field_group_build_pre_render_alter.
  1527. if (isset($element[$child_name]) && (!isset($element[$child_name]['#access']) || $element[$child_name]['#access'])) {
  1528. // If this is a group, we have to use a reference to keep the reference
  1529. // list intact (but if it is a field we don't mind).
  1530. $group_references[$parent_name][$child_name] = &$element[$child_name];
  1531. }
  1532. // The child has been copied to its parent: remove it from the root element.
  1533. unset($element[$child_name]);
  1534. }
  1535. }
  1536. // Bring extra element wrappers to achieve a grouping of fields.
  1537. // This will mainly be prefix and suffix altering.
  1538. foreach ($element['#groups'] as $group_name => $group) {
  1539. field_group_pre_render($group_references[$group_name], $group, $element);
  1540. }
  1541. }
  1542. /**
  1543. * Function to pre render the field group element.
  1544. *
  1545. * @see field_group_fields_nest()
  1546. *
  1547. * @param $element Array of group element that needs to be created!
  1548. * @param $group Object with the group information.
  1549. * @param $form The form object itself.
  1550. */
  1551. function field_group_pre_render(& $element, $group, & $form) {
  1552. // Only run the pre_render function if the group has elements.
  1553. // $group->group_name
  1554. if ($element == array()) {
  1555. return;
  1556. }
  1557. // Let modules define their wrapping element.
  1558. // Note that the group element has no properties, only elements.
  1559. foreach (module_implements('field_group_pre_render') as $module) {
  1560. $function = $module . '_field_group_pre_render';
  1561. if (function_exists($function)) {
  1562. // The intention here is to have the opportunity to alter the
  1563. // elements, as defined in hook_field_group_formatter_info.
  1564. // Note, implement $element by reference!
  1565. $function($element, $group, $form);
  1566. }
  1567. }
  1568. // Allow others to alter the pre_render.
  1569. drupal_alter('field_group_pre_render', $element, $group, $form);
  1570. }