field_group.module 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318
  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. $items["$path/groups/%field_group_menu/enable"] = array(
  59. 'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
  60. 'title' => 'Enable',
  61. 'page callback' => 'drupal_get_form',
  62. 'page arguments' => array('field_group_enable_form', $group_position),
  63. 'type' => MENU_CALLBACK,
  64. 'file' => 'field_group.field_ui.inc',
  65. ) + $access;
  66. }
  67. }
  68. }
  69. }
  70. return $items;
  71. }
  72. /**
  73. * Implements hook_permission().
  74. */
  75. function field_group_permission() {
  76. return array(
  77. 'administer fieldgroups' => array(
  78. 'title' => t('Administer fieldgroups'),
  79. 'description' => t('Display the administration for fieldgroups.'),
  80. ),
  81. );
  82. }
  83. /**
  84. * Menu Wildcard loader function to load group definitions.
  85. *
  86. * @param $group_name
  87. * The name of the group, as contained in the path.
  88. * @param $entity_type
  89. * The name of the entity.
  90. * @param $bundle_name
  91. * The name of the bundle, as contained in the path.
  92. * @param $bundle_pos
  93. * The position of $bundle_name in $map.
  94. * @param $map
  95. * The translated menu router path argument map.
  96. */
  97. function field_group_menu_load($group_name, $entity_type, $bundle_name, $bundle_pos, $map) {
  98. if ($bundle_pos > 0) {
  99. $bundle = $map[$bundle_pos];
  100. $bundle_name = field_extract_bundle($entity_type, $bundle);
  101. }
  102. $args = func_get_args();
  103. $args_pop = array_pop($args);
  104. $mode = array_pop($args_pop);
  105. $group = field_group_load_field_group($group_name, $entity_type, $bundle_name, $mode);
  106. return empty($group) ? FALSE : $group;
  107. }
  108. /**
  109. * Ctools load callback to load fieldgroup by identifier.
  110. */
  111. function field_group_load_field_group_by_identifier($identifier) {
  112. $parts = explode('|', $identifier);
  113. if (count($parts) != 4) {
  114. return;
  115. }
  116. return field_group_load_field_group($parts[0], $parts[1], $parts[2], $parts[3]);
  117. }
  118. /**
  119. * Loads a group definition.
  120. *
  121. * @param $group_name
  122. * The name of the group.
  123. * @param $entity_type
  124. * The name of the entity.
  125. * @param $bundle_name
  126. * The name of the bundle.
  127. * @param $mode
  128. * The view mode to load.
  129. */
  130. function field_group_load_field_group($group_name, $entity_type, $bundle_name, $mode) {
  131. ctools_include('export');
  132. $objects = ctools_export_load_object('field_group', 'conditions', array(
  133. 'group_name' => $group_name,
  134. 'entity_type' => $entity_type,
  135. 'bundle' => $bundle_name,
  136. 'mode' => $mode,
  137. ));
  138. $object = array_shift($objects);
  139. if ($object && isset($object->data)) {
  140. return field_group_unpack($object);
  141. }
  142. return $object;
  143. }
  144. /**
  145. * Implements hook_ctools_plugin_api().
  146. */
  147. function field_group_ctools_plugin_api($owner, $api) {
  148. if ($owner == 'field_group' && $api == 'field_group') {
  149. return array('version' => 1);
  150. }
  151. }
  152. /**
  153. * Implements hook_theme().
  154. */
  155. function field_group_theme() {
  156. return array(
  157. 'horizontal_tabs' => array(
  158. 'render element' => 'element',
  159. ),
  160. 'multipage' => array(
  161. 'render element' => 'element',
  162. ),
  163. 'multipage_pane' => array(
  164. 'render element' => 'element',
  165. ),
  166. );
  167. }
  168. /**
  169. * Implements hook_theme_registry_alter().
  170. */
  171. function field_group_theme_registry_alter(&$theme_registry) {
  172. // Inject field_group_build_entity_groups in all entity theming functions.
  173. $entity_info = entity_get_info();
  174. $entities = array();
  175. foreach ($entity_info as $entity => $info) {
  176. if (isset($entity_info[$entity]['fieldable']) && $entity_info[$entity]['fieldable']) {
  177. // User uses user_profile for theming.
  178. if ($entity == 'user') $entity = 'user_profile';
  179. $entities[] = $entity;
  180. }
  181. }
  182. // Support for File Entity.
  183. if (isset($theme_registry['file_entity'])) {
  184. $entities[] = 'file_entity';
  185. }
  186. // Support for Entity API.
  187. if (isset($theme_registry['entity'])) {
  188. $entities[] = 'entity';
  189. }
  190. foreach ($entities as $entity) {
  191. if (isset($theme_registry[$entity])) {
  192. $theme_registry[$entity]['preprocess functions'][] = 'field_group_build_entity_groups';
  193. // DS support, make sure it comes after field_group.
  194. if ($key = array_search('ds_entity_variables', $theme_registry[$entity]['preprocess functions'])) {
  195. unset($theme_registry[$entity]['preprocess functions'][$key]);
  196. $theme_registry[$entity]['preprocess functions'][] = 'ds_entity_variables';
  197. }
  198. }
  199. }
  200. }
  201. /**
  202. * Implements hook_field_attach_delete_bundle().
  203. *
  204. * @param String $entity_type
  205. * @param String $bundle
  206. */
  207. function field_group_field_attach_delete_bundle($entity_type, $bundle) {
  208. ctools_include('export');
  209. $list = field_group_read_groups(array('bundle' => $bundle, 'entity_type' => $entity_type));
  210. // Delete the entity's entry from field_group of all entities.
  211. // We fetch the field groups first to assign the removal task to ctools.
  212. if (isset($list[$entity_type], $list[$entity_type][$bundle])) {
  213. foreach ($list[$entity_type][$bundle] as $group_mode => $groups) {
  214. foreach ($groups as $group) {
  215. ctools_export_crud_delete('field_group', $group);
  216. }
  217. }
  218. }
  219. }
  220. /**
  221. * Implements hook_field_attach_form().
  222. */
  223. function field_group_field_attach_form($entity_type, $entity, &$form, &$form_state, $langcode) {
  224. field_group_attach_groups($form, 'form', $form_state);
  225. $form['#pre_render'][] = 'field_group_form_pre_render';
  226. }
  227. /**
  228. * Implements hook_form_FORM_ID_alter().
  229. * Using hook_form_field_ui_field_overview_form_alter.
  230. */
  231. function field_group_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
  232. form_load_include($form_state, 'inc', 'field_group', 'field_group.field_ui');
  233. field_group_field_ui_overview_form_alter($form, $form_state);
  234. }
  235. /**
  236. * Implements hook_form_FORM_ID_alter().
  237. * Using hook_form_field_ui_display_overview_form_alter.
  238. */
  239. function field_group_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
  240. form_load_include($form_state, 'inc', 'field_group', 'field_group.field_ui');
  241. field_group_field_ui_overview_form_alter($form, $form_state, TRUE);
  242. }
  243. /**
  244. * Implements hook_field_attach_view_alter().
  245. */
  246. function field_group_field_attach_view_alter(&$element, $context) {
  247. // Check whether the view mode uses custom display settings or the 'default' mode.
  248. $actual_mode = 'default';
  249. if (isset($element['#entity_type']) && isset($element['#bundle'])) {
  250. $view_mode_settings = field_view_mode_settings($element['#entity_type'], $element['#bundle']);
  251. $view_mode = $context['view_mode'];
  252. $actual_mode = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? $view_mode : 'default');
  253. field_group_attach_groups($element, $actual_mode);
  254. }
  255. }
  256. /**
  257. * Implements hook_field_group_formatter_info().
  258. */
  259. function field_group_field_group_formatter_info() {
  260. return array(
  261. 'form' => array(
  262. 'html-element' => array(
  263. 'label' => t('HTML element'),
  264. 'description' => t('This fieldgroup renders the inner content in a HTML element with classes and attributes.'),
  265. 'instance_settings' => array('element' => 'div', 'show_label' => 0, 'label_element' => 'div', 'classes' => '', 'attributes' => '', 'required_fields' => 1, 'id' => ''),
  266. ),
  267. 'div' => array(
  268. 'label' => t('Div'),
  269. 'description' => t('This fieldgroup renders the inner content in a simple div with the title as legend.'),
  270. 'format_types' => array('open', 'collapsible', 'collapsed'),
  271. 'instance_settings' => array('description' => '', 'show_label' => 1, 'label_element' => 'h3', 'effect' => 'none', 'speed' => 'fast', 'classes' => '', 'required_fields' => 1, 'id' => ''),
  272. 'default_formatter' => 'open',
  273. ),
  274. 'html5' => array(
  275. 'label' => t('HTML5'),
  276. 'description' => t('This fieldgroup renders the inner content in a semantic HTML5 wrapper'),
  277. 'instance_settings' => array('wrapper' => '', 'classes' => '', 'id' => ''),
  278. ),
  279. 'fieldset' => array(
  280. 'label' => t('Fieldset'),
  281. 'description' => t('This fieldgroup renders the inner content in a fieldset with the title as legend.'),
  282. 'format_types' => array('open', 'collapsible', 'collapsed'),
  283. 'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1, 'id' => ''),
  284. 'default_formatter' => 'collapsible',
  285. ),
  286. 'tabs' => array(
  287. 'label' => t('Vertical tabs group'),
  288. 'description' => t('This fieldgroup renders child groups in its own vertical tabs wrapper.'),
  289. 'instance_settings' => array('classes' => '', 'id' => ''),
  290. ),
  291. 'tab' => array(
  292. 'label' => t('Vertical tab'),
  293. 'description' => t('This fieldgroup renders the content in a fieldset, part of vertical tabs group.'),
  294. 'format_types' => array('open', 'closed'),
  295. 'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
  296. 'default_formatter' => 'closed',
  297. ),
  298. 'htabs' => array(
  299. 'label' => t('Horizontal tabs group'),
  300. 'description' => t('This fieldgroup renders child groups in its own horizontal tabs wrapper.'),
  301. 'instance_settings' => array('classes' => '', 'id' => ''),
  302. ),
  303. 'htab' => array(
  304. 'label' => t('Horizontal tab'),
  305. 'format_types' => array('open', 'closed'),
  306. 'description' => t('This fieldgroup renders the content in a fieldset, part of horizontal tabs group.'),
  307. 'default_formatter' => 'closed',
  308. 'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
  309. ),
  310. 'multipage-group' => array(
  311. 'label' => t('Multipage group'),
  312. 'description' => t('This fieldgroup renders groups on separate pages.'),
  313. 'instance_settings' => array('classes' => '', 'page_header' => 3, 'move_additional' => 1, 'page_counter' => 1, 'move_button' => 0),
  314. ),
  315. 'multipage' => array(
  316. 'label' => t('Multipage'),
  317. 'format_types' => array('start', 'no-start'),
  318. 'description' => t('This fieldgroup renders the content in a page.'),
  319. 'default_formatter' => 'no-start',
  320. 'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
  321. ),
  322. 'accordion' => array(
  323. 'label' => t('Accordion group'),
  324. 'description' => t('This fieldgroup renders child groups as jQuery accordion.'),
  325. 'instance_settings' => array('effect' => 'none', 'classes' => '', 'id' => ''),
  326. ),
  327. 'accordion-item' => array(
  328. 'label' => t('Accordion item'),
  329. 'format_types' => array('open', 'closed'),
  330. 'description' => t('This fieldgroup renders the content in a div, part of accordion group.'),
  331. 'default_formatter' => 'closed',
  332. 'instance_settings' => array('description' => '', 'classes' => '', 'required_fields' => 1),
  333. ),
  334. ),
  335. 'display' => array(
  336. 'html-element' => array(
  337. 'label' => t('HTML element'),
  338. 'description' => t('This fieldgroup renders the inner content in a HTML element with classes and attributes.'),
  339. 'instance_settings' => array('element' => 'div', 'show_label' => 0, 'label_element' => 'div', 'classes' => '', 'attributes' => '', 'required_fields' => 1, 'id' => ''),
  340. ),
  341. 'div' => array(
  342. 'label' => t('Div'),
  343. 'description' => t('This fieldgroup renders the inner content in a simple div with the title as legend.'),
  344. 'format_types' => array('open', 'collapsible', 'collapsed'),
  345. 'instance_settings' => array('description' => '', 'show_label' => 1, 'label_element' => 'h3', 'effect' => 'none', 'speed' => 'fast', 'classes' => '', 'id' => ''),
  346. 'default_formatter' => 'collapsible',
  347. ),
  348. 'html5' => array(
  349. 'label' => t('HTML5'),
  350. 'description' => t('This fieldgroup renders the inner content in a semantic HTML5 wrapper'),
  351. 'instance_settings' => array('wrapper' => '', 'classes' => '', 'id' => ''),
  352. ),
  353. 'fieldset' => array(
  354. 'label' => t('Fieldset'),
  355. 'description' => t('This fieldgroup renders the inner content in a fieldset with the title as legend.'),
  356. 'format_types' => array('open', 'collapsible', 'collapsed'),
  357. 'instance_settings' => array('description' => '', 'classes' => '', 'id' => ''),
  358. 'default_formatter' => 'collapsible',
  359. ),
  360. 'tabs' => array(
  361. 'label' => t('Vertical tabs group'),
  362. 'description' => t('This fieldgroup renders child groups in its own vertical tabs wrapper.'),
  363. 'instance_settings' => array('classes' => '', 'id' => ''),
  364. ),
  365. 'tab' => array(
  366. 'label' => t('Vertical tab'),
  367. 'description' => t('This fieldgroup renders the content in a fieldset, part of vertical tabs group.'),
  368. 'format_types' => array('open', 'closed'),
  369. 'instance_settings' => array('description' => '', 'classes' => ''),
  370. 'default_formatter' => 'closed',
  371. ),
  372. 'htabs' => array(
  373. 'label' => t('Horizontal tabs group'),
  374. 'description' => t('This fieldgroup renders child groups in its own horizontal tabs wrapper.'),
  375. 'instance_settings' => array('classes' => '', 'id' => ''),
  376. ),
  377. 'htab' => array(
  378. 'label' => t('Horizontal tab item'),
  379. 'format_types' => array('open', 'closed'),
  380. 'description' => t('This fieldgroup renders the content in a fieldset, part of horizontal tabs group.'),
  381. 'instance_settings' => array('description' => '', 'classes' => '', 'id' => ''),
  382. 'default_formatter' => 'closed',
  383. ),
  384. 'accordion' => array(
  385. 'label' => t('Accordion group'),
  386. 'description' => t('This fieldgroup renders child groups as jQuery accordion.'),
  387. 'instance_settings' => array('description' => '', 'classes' => '', 'effect' => 'bounceslide', 'id' => ''),
  388. ),
  389. 'accordion-item' => array(
  390. 'label' => t('Accordion item'),
  391. 'format_types' => array('open', 'closed'),
  392. 'description' => t('This fieldgroup renders the content in a div, part of accordion group.'),
  393. 'instance_settings' => array('classes' => ''),
  394. 'default_formatter' => 'closed',
  395. ),
  396. ),
  397. );
  398. }
  399. /**
  400. * Implements hook_field_group_format_settings().
  401. * If the group has no format settings, default ones will be added.
  402. * @params Object $group The group object.
  403. * @return Array $form The form element for the format settings.
  404. */
  405. function field_group_field_group_format_settings($group) {
  406. // Add a wrapper for extra settings to use by others.
  407. $form = array(
  408. 'instance_settings' => array(
  409. '#tree' => TRUE,
  410. '#weight' => 2,
  411. ),
  412. );
  413. $field_group_types = field_group_formatter_info();
  414. $mode = $group->mode == 'form' ? 'form' : 'display';
  415. $formatter = $field_group_types[$mode][$group->format_type];
  416. // Add the required formatter type selector.
  417. if (isset($formatter['format_types'])) {
  418. $form['formatter'] = array(
  419. '#title' => t('Fieldgroup settings'),
  420. '#type' => 'select',
  421. '#options' => drupal_map_assoc($formatter['format_types']),
  422. '#default_value' => isset($group->format_settings['formatter']) ? $group->format_settings['formatter'] : $formatter['default_formatter'],
  423. '#weight' => -4,
  424. );
  425. }
  426. if (isset($formatter['instance_settings']['required_fields']) && $mode == 'form') {
  427. $form['instance_settings']['required_fields'] = array(
  428. '#type' => 'checkbox',
  429. '#title' => t('Mark group as required if it contains required fields.'),
  430. '#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'] : ''),
  431. '#weight' => 2,
  432. );
  433. }
  434. if (isset($formatter['instance_settings']['id'])) {
  435. $form['instance_settings']['id'] = array(
  436. '#title' => t('ID'),
  437. '#type' => 'textfield',
  438. '#default_value' => isset($group->format_settings['instance_settings']['id']) ? $group->format_settings['instance_settings']['id'] : (isset($formatter['instance_settings']['id']) ? $formatter['instance_settings']['id'] : ''),
  439. '#weight' => 10,
  440. '#element_validate' => array('field_group_validate_id'),
  441. );
  442. }
  443. if (isset($formatter['instance_settings']['classes'])) {
  444. $form['instance_settings']['classes'] = array(
  445. '#title' => t('Extra CSS classes'),
  446. '#type' => 'textfield',
  447. '#default_value' => isset($group->format_settings['instance_settings']['classes']) ? $group->format_settings['instance_settings']['classes'] : (isset($formatter['instance_settings']['classes']) ? $formatter['instance_settings']['classes'] : ''),
  448. '#weight' => 11,
  449. '#element_validate' => array('field_group_validate_css_class'),
  450. );
  451. }
  452. if (isset($formatter['instance_settings']['description'])) {
  453. $form['instance_settings']['description'] = array(
  454. '#title' => t('Description'),
  455. '#type' => 'textarea',
  456. '#default_value' => isset($group->format_settings['instance_settings']['description']) ? $group->format_settings['instance_settings']['description'] : (isset($formatter['instance_settings']['description']) ? $formatter['instance_settings']['description'] : ''),
  457. '#weight' => 0,
  458. );
  459. }
  460. // Add optional instance_settings.
  461. switch ($group->format_type) {
  462. case 'html-element':
  463. $form['instance_settings']['element'] = array(
  464. '#title' => t('Element'),
  465. '#type' => 'textfield',
  466. '#default_value' => isset($group->format_settings['instance_settings']['element']) ? $group->format_settings['instance_settings']['element'] : $formatter['instance_settings']['element'],
  467. '#description' => t('E.g. div, section, aside etc.'),
  468. '#weight' => 1,
  469. );
  470. $form['instance_settings']['show_label'] = array(
  471. '#title' => t('Show label'),
  472. '#type' => 'select',
  473. '#options' => array(0 => t('No'), 1 => t('Yes')),
  474. '#default_value' => isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : $formatter['instance_settings']['show_label'],
  475. '#weight' => 2,
  476. );
  477. $form['instance_settings']['label_element'] = array(
  478. '#title' => t('Label element'),
  479. '#type' => 'textfield',
  480. '#default_value' => isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : $formatter['instance_settings']['label_element'],
  481. '#weight' => 3,
  482. );
  483. $form['instance_settings']['attributes'] = array(
  484. '#title' => t('Attributes'),
  485. '#type' => 'textfield',
  486. '#default_value' => isset($group->format_settings['instance_settings']['attributes']) ? $group->format_settings['instance_settings']['attributes'] : $formatter['instance_settings']['attributes'],
  487. '#description' => t('E.g. name="anchor"'),
  488. '#weight' => 4,
  489. );
  490. break;
  491. case 'div':
  492. $form['label']['#description'] = t('Please enter a label for collapsible elements');
  493. $form['instance_settings']['show_label'] = array(
  494. '#title' => t('Show label'),
  495. '#type' => 'select',
  496. '#options' => array(0 => t('No'), 1 => t('Yes')),
  497. '#default_value' => isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : $formatter['instance_settings']['show_label'],
  498. '#weight' => 2,
  499. );
  500. $form['instance_settings']['label_element'] = array(
  501. '#title' => t('Label element'),
  502. '#type' => 'select',
  503. '#options' => array('h2' => t('Header 2'), 'h3' => t('Header 3')),
  504. '#default_value' => isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : $formatter['instance_settings']['label_element'],
  505. '#weight' => 2,
  506. );
  507. $form['instance_settings']['effect'] = array(
  508. '#title' => t('Effect'),
  509. '#type' => 'select',
  510. '#options' => array('none' => t('None'), 'blind' => t('Blind')),
  511. '#default_value' => isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : $formatter['instance_settings']['effect'],
  512. '#weight' => 3,
  513. );
  514. $form['instance_settings']['speed'] = array(
  515. '#title' => t('Speed'),
  516. '#type' => 'select',
  517. '#options' => array('none' => t('None'), 'slow' => t('Slow'), 'fast' => t('Fast')),
  518. '#default_value' => isset($group->format_settings['instance_settings']['speed']) ? $group->format_settings['instance_settings']['speed'] : $formatter['instance_settings']['speed'],
  519. '#weight' => 3,
  520. );
  521. break;
  522. case 'html5':
  523. $form['instance_settings']['wrapper'] = array(
  524. '#title' => t('HTML5 wrapper'),
  525. '#type' => 'select',
  526. '#options' => array('section' => t('Section'), 'article' => t('Article'), 'header' => t('Header'), 'footer' => t('Footer'), 'aside' => t('Aside')),
  527. '#default_value' => isset($group->format_settings['instance_settings']['wrapper']) ? $group->format_settings['instance_settings']['wrapper'] : 'section',
  528. );
  529. break;
  530. case 'fieldset':
  531. $form['label']['#description'] = t('Please enter a label for collapsible elements');
  532. break;
  533. case 'multipage-group':
  534. $form['instance_settings']['page_header'] = array(
  535. '#title' => t('Format page title'),
  536. '#type' => 'select',
  537. '#options' => array(0 => t('None'), 1 => t('Label only'), 2 => t('Step 1 of 10'), 3 => t('Step 1 of 10 [Label]'),),
  538. '#default_value' => isset($group->format_settings['instance_settings']['page_header']) ? $group->format_settings['instance_settings']['page_header'] : $formatter['instance_settings']['page_header'],
  539. '#weight' => 1,
  540. );
  541. $form['instance_settings']['page_counter'] = array(
  542. '#title' => t('Add a page counter at the bottom'),
  543. '#type' => 'select',
  544. '#options' => array(0 => t('No'), 1 => t('Format 1 / 10'), 2 => t('The count number only')),
  545. '#default_value' => isset($group->format_settings['instance_settings']['page_counter']) ? $group->format_settings['instance_settings']['page_counter'] : $formatter['instance_settings']['page_counter'],
  546. '#weight' => 2,
  547. );
  548. $form['instance_settings']['move_button'] = array(
  549. '#title' => t('Move submit button to last multipage'),
  550. '#type' => 'select',
  551. '#options' => array(0 => t('No'), 1 => t('Yes')),
  552. '#default_value' => isset($group->format_settings['instance_settings']['move_button']) ? $group->format_settings['instance_settings']['move_button'] : $formatter['instance_settings']['move_button'],
  553. '#weight' => 3,
  554. );
  555. $form['instance_settings']['move_additional'] = array(
  556. '#title' => t('Move additional settings to last multipage (if available)'),
  557. '#type' => 'select',
  558. '#options' => array(0 => t('No'), 1 => t('Yes')),
  559. '#default_value' => isset($group->format_settings['instance_settings']['move_additional']) ? $group->format_settings['instance_settings']['move_additional'] : $formatter['instance_settings']['move_additional'],
  560. '#weight' => 4,
  561. );
  562. case 'tabs':
  563. case 'htabs':
  564. break;
  565. case 'accordion':
  566. $form['instance_settings']['effect'] = array(
  567. '#title' => t('Effect'),
  568. '#type' => 'select',
  569. '#options' => array('none' => t('None'), 'bounceslide' => t('Bounce slide')),
  570. '#default_value' => isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : $formatter['instance_settings']['effect'],
  571. '#weight' => 2,
  572. );
  573. break;
  574. case 'multipage':
  575. break;
  576. case 'tab':
  577. case 'htab':
  578. case 'accordion-item':
  579. default:
  580. }
  581. return $form;
  582. }
  583. /**
  584. * Helper function to prepare basic variables needed for most formatters.
  585. *
  586. * Called in field_group_field_group_pre_render(), but can also be called in
  587. * other implementations of hook_field_group_pre_render().
  588. */
  589. function field_group_pre_render_prepare(&$group) {
  590. $classes = _field_group_get_html_classes($group);
  591. $group->classes = implode(' ', $classes->required);
  592. $group->description = !empty($group->format_settings['instance_settings']['description']) ? filter_xss_admin(t($group->format_settings['instance_settings']['description'])) : '';
  593. }
  594. /**
  595. * Implements hook_field_group_pre_render().
  596. *
  597. * @param Array $elements by address.
  598. * @param Object $group The Field group info.
  599. */
  600. function field_group_field_group_pre_render(&$element, &$group, & $form) {
  601. field_group_pre_render_prepare($group);
  602. $view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
  603. // Add all field_group format types to the js settings.
  604. $form['#attached']['js'][] = array(
  605. 'data' => array('field_group' => array($group->format_type => $view_mode)),
  606. 'type' => 'setting',
  607. );
  608. if (isset($group->format_settings['instance_settings']['id']) && !empty($group->format_settings['instance_settings']['id'])) {
  609. $element['#id'] = drupal_html_id($group->format_settings['instance_settings']['id']);
  610. }
  611. $element['#weight'] = $group->weight;
  612. // Call the pre render function for the format type.
  613. $function = "field_group_pre_render_" . str_replace("-", "_", $group->format_type);
  614. if (function_exists($function)) {
  615. $function($element, $group, $form);
  616. }
  617. }
  618. /**
  619. * Implements field_group_pre_render_<format-type>.
  620. * Format type: Fieldset.
  621. *
  622. * @param $element The field group form element.
  623. * @param $group The Field group object prepared for pre_render.
  624. * @param $form The root element or form.
  625. */
  626. function field_group_pre_render_fieldset(&$element, $group, &$form) {
  627. $element += array(
  628. '#type' => 'fieldset',
  629. '#title' => check_plain(t($group->label)),
  630. '#collapsible' => $group->collapsible,
  631. '#collapsed' => $group->collapsed,
  632. '#pre_render' => array(),
  633. '#attributes' => array('class' => explode(' ', $group->classes)),
  634. '#description' => $group->description,
  635. );
  636. if ($group->collapsible || $group->collapsed) {
  637. $element['#attached']['library'][] = array('system', 'drupal.collapse');
  638. }
  639. }
  640. /**
  641. * Implements field_group_pre_render_<format-type>.
  642. * Format type: HTML element.
  643. *
  644. * @param $element The field group form element.
  645. * @param $group The Field group object prepared for pre_render.
  646. * @param $form The root element or form.
  647. */
  648. function field_group_pre_render_html_element(&$element, $group, &$form) {
  649. $html_element = isset($group->format_settings['instance_settings']['element']) ? $group->format_settings['instance_settings']['element'] : 'div';
  650. $show_label = isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : 0;
  651. $label_element = isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : 'div';
  652. $configured_attributes = isset($group->format_settings['instance_settings']['attributes']) ? ' ' . $group->format_settings['instance_settings']['attributes'] : '';
  653. $group->classes = trim($group->classes);
  654. // This regex split the attributes string so that we can pass that
  655. // later to drupal_attributes().
  656. preg_match_all('/([^\s=]+)="([^"]+)"/', $configured_attributes, $matches);
  657. $element_attributes = array();
  658. // Put the attribute and the value together.
  659. foreach ($matches[1] as $key => $attribute) {
  660. $element_attributes[$attribute] = $matches[2][$key];
  661. }
  662. // Add the classes to the attributes array.
  663. if (!isset($element_attributes['class']) && $group->classes) {
  664. $element_attributes['class'] = $group->classes;
  665. }
  666. elseif (isset($element_attributes['class']) && $group->classes) {
  667. $element_attributes['class'] .= ' ' . $group->classes;
  668. }
  669. if (isset($element['#id'])) {
  670. $element_attributes['id'] = $element['#id'];
  671. }
  672. // Sanitize the attributes.
  673. $element_attributes = _filter_xss_attributes(drupal_attributes($element_attributes));
  674. $attributes = $element_attributes ? ' ' . implode(' ', $element_attributes) : '';
  675. $element['#prefix'] = '<' . $html_element . $attributes . '>';
  676. if ($show_label) {
  677. $element['#prefix'] .= '<' . $label_element . '><span>' . check_plain(t($group->label)) . '</span></' . $label_element . '>';
  678. }
  679. $element['#suffix'] = '</' . $html_element . '>';
  680. }
  681. /**
  682. * Implements field_group_pre_render_<format-type>.
  683. * Format type: Div.
  684. *
  685. * @param $element The field group form element.
  686. * @param $group The Field group object prepared for pre_render.
  687. * @param $form The root element or form.
  688. */
  689. function field_group_pre_render_div(&$element, $group, &$form) {
  690. $show_label = isset($group->format_settings['instance_settings']['show_label']) ? $group->format_settings['instance_settings']['show_label'] : 0;
  691. $label_element = isset($group->format_settings['instance_settings']['label_element']) ? $group->format_settings['instance_settings']['label_element'] : 'h2';
  692. $effect = isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : 'none';
  693. $element['#type'] = 'markup';
  694. $id = isset($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
  695. if ($group->format_settings['formatter'] != 'open') {
  696. $element['#prefix'] = '<div' . $id . ' class="' . $group->classes . '">
  697. <' . $label_element . '><span class="field-group-format-toggler">' . check_plain(t($group->label)) . '</span></' . $label_element . '>
  698. <div class="field-group-format-wrapper" style="display: ' . (!empty($group->collapsed) ? 'none' : 'block') . ';">';
  699. $element['#suffix'] = '</div></div>';
  700. }
  701. else {
  702. $class_attribute = !empty($group->classes) ? ' class="' . $group->classes . '"' : '';
  703. $element['#prefix'] = '<div' . $id . $class_attribute . '>';
  704. if ($show_label) {
  705. $element['#prefix'] .= '<' . $label_element . '><span>' . check_plain(t($group->label)) . '</span></' . $label_element . '>';
  706. }
  707. $element['#suffix'] = '</div>';
  708. }
  709. if (!empty($group->description)) {
  710. $element['#prefix'] .= '<div class="description">' . $group->description . '</div>';
  711. }
  712. if ($effect == 'blind') {
  713. $element['#attached']['library'][] = array('system', 'effects.blind');
  714. }
  715. }
  716. /**
  717. * Implements field_group_pre_render_<format-type>.
  718. * Format type: HTML5.
  719. *
  720. * @param $element The field group form element.
  721. * @param $group The Field group object prepared for pre_render.
  722. * @param $form The root element or form.
  723. */
  724. function field_group_pre_render_html5(&$element, $group, &$form) {
  725. $id = !empty($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
  726. $class = !empty($group->classes) ? ' class="' . $group->classes . '"' : '';
  727. $element += array(
  728. '#type' => 'markup',
  729. '#prefix' => '<' . $group->format_settings['instance_settings']['wrapper'] . $id . $class . '>',
  730. '#suffix' => '</' . $group->format_settings['instance_settings']['wrapper'] . '>',
  731. );
  732. }
  733. /**
  734. * Implements field_group_pre_render_<format-type>.
  735. * Format type: Accordion.
  736. *
  737. * @param $element The field group form element.
  738. * @param $group The Field group object prepared for pre_render.
  739. * @param $form The root element or form.
  740. */
  741. function field_group_pre_render_accordion(&$element, $group, &$form) {
  742. // Add the jQuery UI accordion.
  743. $element['#attached']['library'][] = array('system', 'ui.accordion');
  744. $id = !empty($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
  745. $element += array(
  746. '#type' => 'markup',
  747. '#prefix' => '<div class="' . $group->classes . '"' . $id .'>',
  748. '#suffix' => '</div>',
  749. );
  750. }
  751. /**
  752. * Implements field_group_pre_render_<format-type>.
  753. * Format type: Accordion item.
  754. *
  755. * @param $element The field group form element.
  756. * @param $group The Field group object prepared for pre_render.
  757. * @param $form The root element or form.
  758. */
  759. function field_group_pre_render_accordion_item(&$element, $group, &$form) {
  760. $element += array(
  761. '#type' => 'markup',
  762. '#prefix' => '<h3 class="field-group-format-toggler ' . $group->format_type . ($group->collapsed ? '' : ' field-group-accordion-active') . '"><a href="#">' . check_plain(t($group->label)) . '</a></h3>
  763. <div class="field-group-format-wrapper ' . $group->classes . '">',
  764. '#suffix' => '</div>',
  765. //'#attributes' => array('class' => array($group->format_type)),
  766. );
  767. if (!empty($group->description)) {
  768. $element['#prefix'] .= '<div class="description">' . $group->description . '</div>';
  769. }
  770. }
  771. /**
  772. * Implements field_group_pre_render_<format-type>.
  773. * Format type: Horizontal tabs group.
  774. *
  775. * @param $element The field group form element.
  776. * @param $group The Field group object prepared for pre_render.
  777. * @param $form The root element or form.
  778. */
  779. function field_group_pre_render_htabs(&$element, $group, &$form) {
  780. $classes = 'field-group-' . $group->format_type . '-wrapper';
  781. if (!empty($group->classes)) {
  782. $classes .= ' ' . $group->classes;
  783. }
  784. $id = !empty($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
  785. $element += array(
  786. '#type' => 'horizontal_tabs',
  787. '#title' => check_plain(t($group->label)),
  788. '#theme_wrappers' => array('horizontal_tabs'),
  789. '#prefix' => '<div class="' . $classes . '"' . $id . '>',
  790. '#suffix' => '</div>',
  791. );
  792. // By default vertical_tabs don't have titles but you can override it in the theme.
  793. if (!empty($group->label)) {
  794. $element['#title'] = check_plain($group->label);
  795. }
  796. // Only add form.js on forms.
  797. if (!empty($form['#type']) && $form['#type'] == 'form') {
  798. $element['#attached']['js'][] = 'misc/form.js';
  799. }
  800. $element['#attached']['library'][] = array('field_group', 'horizontal-tabs');
  801. }
  802. /**
  803. * Implements field_group_pre_render_<format-type>.
  804. * Format type: Horizontal tab.
  805. *
  806. * @param $element The field group form element.
  807. * @param $group The Field group object prepared for pre_render.
  808. * @param $form The root element or form.
  809. */
  810. function field_group_pre_render_htab(&$element, $group, &$form) {
  811. $element += array(
  812. '#type' => 'fieldset',
  813. '#title' => check_plain(t($group->label)),
  814. '#collapsible' => $group->collapsible,
  815. '#collapsed' => $group->collapsed,
  816. '#attributes' => array('class' => explode(" ", $group->classes)),
  817. '#group' => $group->parent_name,
  818. // very important. Cannot be added on the form!
  819. '#parents' => array($group->parent_name),
  820. '#description' => $group->description,
  821. );
  822. }
  823. /**
  824. * Implements field_group_pre_render_<format-type>.
  825. * Format type: Multipage group.
  826. *
  827. * @param $element The field group form element.
  828. * @param $group The Field group object prepared for pre_render.
  829. * @param $form The root element or form.
  830. */
  831. function field_group_pre_render_multipage_group(&$element, &$group, &$form) {
  832. $multipage_element = array(
  833. '#type' => 'multipage',
  834. '#theme_wrappers' => array('multipage'),
  835. '#prefix' => '<div class="field-group-' . $group->format_type . '-wrapper ' . $group->classes . '">',
  836. '#suffix' => '</div>',
  837. );
  838. $element += $multipage_element;
  839. $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']);
  840. $move_button = isset($group->format_settings['instance_settings']['move_button']) ? $group->format_settings['instance_settings']['move_button'] : 0;
  841. drupal_add_js(array(
  842. 'field_group' => array(
  843. 'multipage_move_submit' => (bool) $move_button,
  844. 'multipage_move_additional' => (bool) $move_additional
  845. )
  846. ), 'setting');
  847. }
  848. /**
  849. * Implements field_group_pre_render_<format-type>.
  850. * Format type: Multipage.
  851. *
  852. * @param $element The field group form element.
  853. * @param $group The Field group object prepared for pre_render.
  854. * @param $form The root element or form.
  855. */
  856. function field_group_pre_render_multipage(&$element, $group, &$form) {
  857. $group->classes .= $group->format_settings['formatter'] == 'start' ? ' multipage-open' : ' multipage-closed';
  858. $element += array(
  859. '#type' => 'multipage_pane',
  860. '#title' => check_plain(t($group->label)),
  861. '#collapsible' => $group->collapsible,
  862. '#collapsed' => $group->collapsed,
  863. '#attributes' => array('class' => explode(" ", $group->classes)),
  864. '#group' => $group->parent_name,
  865. '#group_object' => $group,
  866. '#parent_group_object' => $form['#groups'][$group->parent_name],
  867. // very important. Cannot be added on the form!
  868. '#parents' => array($group->parent_name),
  869. '#description' => $group->description,
  870. );
  871. $element['#attached']['library'][] = array('field_group', 'multipage');
  872. }
  873. /**
  874. * Implements field_group_pre_render_<format-type>.
  875. * Format type: Vertical tabs wrapper.
  876. *
  877. * @param $element The field group form element.
  878. * @param $group The Field group object prepared for pre_render.
  879. * @param $form The root element or form.
  880. */
  881. function field_group_pre_render_tabs(&$element, $group, &$form) {
  882. $classes = 'field-group-' . $group->format_type . '-wrapper';
  883. if (!empty($group->classes)) {
  884. $classes .= ' ' . $group->classes;
  885. }
  886. $id = !empty($element['#id']) ? ' id="' . $element['#id'] . '"' : '';
  887. $element += array(
  888. '#type' => 'vertical_tabs',
  889. '#theme_wrappers' => array('vertical_tabs'),
  890. '#prefix' => '<div class="' . $classes . '"' . $id . '>',
  891. '#suffix' => '</div>',
  892. );
  893. // By default vertical_tabs don't have titles but you can override it in the theme.
  894. if (!empty($group->label)) {
  895. $element['#title'] = check_plain($group->label);
  896. }
  897. $element[$group->group_name . '__active_tab'] = array(
  898. '#type' => 'hidden',
  899. '#default_value' => '',
  900. '#attributes' => array('class' => array('vertical-tabs-active-tab')),
  901. );
  902. $element['#attached']['library'][] = array('system', 'drupal.collapse');
  903. }
  904. /**
  905. * Implements field_group_pre_render_<format-type>.
  906. * Format type: Vertical tab.
  907. *
  908. * @param $element The field group form element.
  909. * @param $group The Field group object prepared for pre_render.
  910. * @param $form The root element or form.
  911. */
  912. function field_group_pre_render_tab(&$element, $group, &$form) {
  913. $view_mode = isset($form['#view_mode']) ? $form['#view_mode'] : 'form';
  914. // Could be it never runs through htab.
  915. $form['#attached']['js'][] = array(
  916. 'data' => array('field_group' => array('tabs' => $view_mode)),
  917. 'type' => 'setting',
  918. );
  919. $add = array(
  920. '#type' => 'fieldset',
  921. '#id' => 'edit-' . $group->group_name,
  922. '#title' => check_plain(t($group->label)),
  923. '#collapsible' => $group->collapsible,
  924. '#collapsed' => $group->collapsed,
  925. '#attributes' => array('class' => explode(" ", $group->classes)),
  926. '#description' => $group->description,
  927. );
  928. // Front-end and back-end on configuration will lead
  929. // to vertical tabs nested in a separate vertical group.
  930. if ($view_mode != 'form') {
  931. $add['#group'] = empty($group->parent_name) ? 'additional_settings' : $group->parent_name;
  932. $add['#parents'] = array($add['#group']);
  933. $element += $add;
  934. }
  935. // Form fieldgroups which are nested into a vertical tab group
  936. // are handled a little different.
  937. elseif (!empty($group->parent_name)) {
  938. $add['#group'] = $group->parent_name;
  939. $element += $add;
  940. }
  941. // Forms "can" have additional settins. We'll try to locate it first, if not
  942. // exists, field_group will create a parallel additional settings entry.
  943. else {
  944. // Create the fieldgroup element.
  945. $add['#parents'] = array('additional_settings');
  946. $add['#group'] = 'additional_settings';
  947. $add['#weight'] = -30 + $group->weight; // hardcoded to bring our extra additional vtabs on top.
  948. // Check if the additional_settings exist for this type of form.
  949. if (isset($form['additional_settings']['group']['#groups']['additional_settings'])) {
  950. // Merge fieldgroups with the core additional settings.
  951. $form['additional_settings']['group']['#groups']['additional_settings'][$group->group_name] = $add;
  952. $form['additional_settings']['group']['#groups'][$group->group_name] = array('#group_exists' => TRUE);
  953. // Nest the fields inside the appropriate structure.
  954. foreach (element_children($element) as $fieldname) {
  955. $form['additional_settings']['group']['#groups']['additional_settings'][$group->group_name][$fieldname] = &$element[$fieldname];
  956. unset($element[$fieldname]);
  957. }
  958. }
  959. // Assumption the wrapper is in the root. This could be done by field_group itself
  960. // in previous loop of tabs in same wrapper or even some other contrib / custom module.
  961. else {
  962. if (!isset($form['additional_settings']['#type'])) {
  963. $form['additional_settings'] = array(
  964. '#type' => 'vertical_tabs',
  965. '#weight' => $group->weight,
  966. '#theme_wrappers' => array('vertical_tabs'),
  967. '#prefix' => '<div class="field-group-' . $group->format_type . '-wrapper">',
  968. '#suffix' => '</div>',
  969. );
  970. $form['#attached']['library'][] = array('system', 'drupal.collapse');
  971. }
  972. $form['additional_settings'][$group->group_name] = $add;
  973. // Nest the fields inside the appropriate structure.
  974. foreach (element_children($element) as $fieldname) {
  975. $form['additional_settings'][$group->group_name][$fieldname] = &$element[$fieldname];
  976. unset($element[$fieldname]);
  977. }
  978. }
  979. }
  980. }
  981. /**
  982. * Implements hook_field_group_build_pre_render_alter().
  983. * @param Array $elements by address.
  984. */
  985. function field_group_field_group_build_pre_render_alter(&$element) {
  986. // Someone is doing a node view, in a node view. Reset content.
  987. // TODO Check if this breaks something else.
  988. if (isset($element['#node']->content) && count($element['#node']->content) > 0) {
  989. $element['#node']->content = array();
  990. }
  991. $display = isset($element['#view_mode']);
  992. $groups = array_keys($element['#groups']);
  993. // Dish the fieldgroups with no fields for non-forms.
  994. if ($display) {
  995. field_group_remove_empty_display_groups($element, $groups);
  996. }
  997. else {
  998. // Fix the problem on forms with additional settings.
  999. field_group_remove_empty_form_groups('form', $element, $groups, $element['#groups'], $element['#entity_type']);
  1000. }
  1001. // Add the default field_group javascript and stylesheet.
  1002. $element['#attached']['js'][] = drupal_get_path('module', 'field_group') . '/field_group.js';
  1003. // Move additional settings to the last multipage pane if configured that way.
  1004. // Note that multipages MUST be in the root of the form.
  1005. foreach (element_children($element) as $name) {
  1006. if (isset($element[$name]['#type']) && $element[$name]['#type'] == 'multipage' && isset($element['additional_settings'])) {
  1007. $parent_group = $element['#groups'][$name];
  1008. $move_additional = isset($parent_group->format_settings['instance_settings']['move_additional']) ? $parent_group->format_settings['instance_settings']['move_additional'] : 1;
  1009. $last_pane = NULL;
  1010. foreach (element_children($element[$name], TRUE) as $pane) {
  1011. $last_pane = $pane;
  1012. }
  1013. $element[$name][$last_pane]['additional_settings'] = $element['additional_settings'];
  1014. unset($element['additional_settings']);
  1015. }
  1016. }
  1017. }
  1018. /**
  1019. * Remove empty groups on forms.
  1020. *
  1021. * @param String $parent_name
  1022. * The name of the element.
  1023. * @param array $element
  1024. * The element to check the empty state.
  1025. * @param array $groups
  1026. * Array of group objects.
  1027. */
  1028. function field_group_remove_empty_form_groups($name, & $element, $groups, &$form_groups, $entity) {
  1029. $children = element_children($element);
  1030. $hasChildren = FALSE;
  1031. if (count($children)) {
  1032. foreach ($children as $childname) {
  1033. if (in_array($childname, $groups, TRUE)) {
  1034. field_group_remove_empty_form_groups($childname, $element[$childname], $groups, $form_groups, $entity);
  1035. }
  1036. $hasChildren = $hasChildren ? TRUE : _field_group_is_empty_element($element, $entity, $childname, $groups);
  1037. }
  1038. }
  1039. if (!$hasChildren) {
  1040. // Remove empty elements from the #groups.
  1041. if (empty($element) && isset($form_groups[$name]) && !is_array($form_groups[$name])) {
  1042. foreach ($form_groups as $group_name => $group) {
  1043. if (isset($group->children)) {
  1044. $group_children = array_flip($group->children);
  1045. if (isset($group_children[$name])) {
  1046. unset($form_groups[$group_name]->children[$group_children[$name]]);
  1047. }
  1048. }
  1049. }
  1050. }
  1051. $element['#access'] = FALSE;
  1052. }
  1053. }
  1054. /**
  1055. * Determine if an element has non-empty children.
  1056. */
  1057. function _field_group_is_empty_element($element, $entity, $childname, $groups) {
  1058. $exceptions = array('user__account', 'comment__author');
  1059. $exception = $entity . '__' . $childname;
  1060. if (in_array($exception, $exceptions)) {
  1061. return TRUE;
  1062. }
  1063. if (isset($element[$childname]['#type'])
  1064. || isset($element[$childname]['#markup'])
  1065. || isset($element[$childname]['#prefix'])
  1066. || isset($element[$childname]['#suffix'])
  1067. ) {
  1068. return TRUE;
  1069. }
  1070. // Prevent a double recursive loop (groups are already recursive looped in field_group_remove_empty_form_groups.
  1071. if (in_array($childname, $groups)) {
  1072. return FALSE;
  1073. }
  1074. $children = element_children($element[$childname]);
  1075. foreach ($children as $child) {
  1076. if (_field_group_is_empty_element($element[$childname], $entity, $child, $groups)) {
  1077. return TRUE;
  1078. }
  1079. }
  1080. return FALSE;
  1081. }
  1082. /**
  1083. * Remove empty groups on entity display.
  1084. * @param array $element
  1085. * The element to check the empty state.
  1086. * @param array $groups
  1087. * Array of group objects.
  1088. */
  1089. function field_group_remove_empty_display_groups(& $element, $groups) {
  1090. $empty_child = TRUE;
  1091. $empty_group = TRUE;
  1092. // Loop through the children for current element.
  1093. foreach (element_children($element) as $name) {
  1094. // Descend if the child is a group.
  1095. if (in_array($name, $groups)) {
  1096. $empty_child = field_group_remove_empty_display_groups($element[$name], $groups);
  1097. if (!$empty_child) {
  1098. $empty_group = FALSE;
  1099. }
  1100. }
  1101. // Child is a field, the element is not empty and access is set to true (or empty).
  1102. elseif (!empty($element[$name]) && (!isset($element[$name]['#access']) || $element[$name]['#access'])) {
  1103. $empty_group = FALSE;
  1104. }
  1105. }
  1106. // Reset an empty group.
  1107. if ($empty_group) {
  1108. $element = NULL;
  1109. }
  1110. return $empty_group;
  1111. }
  1112. /**
  1113. * Implements hook_field_group_format_summary().
  1114. */
  1115. function field_group_field_group_format_summary($group) {
  1116. $group_form = module_invoke_all('field_group_format_settings', $group);
  1117. $output = '';
  1118. if (isset($group->format_settings['formatter'])) {
  1119. $output .= '<strong>' . $group->format_type . '</strong> ' . $group->format_settings['formatter'] . '';
  1120. }
  1121. if (isset($group->format_settings['instance_settings'])) {
  1122. $last = end($group->format_settings['instance_settings']);
  1123. $output .= '<br />';
  1124. foreach ($group->format_settings['instance_settings'] as $key => $value) {
  1125. if (empty($value)) {
  1126. continue;
  1127. }
  1128. $output .= '<strong>' . $key . '</strong> ';
  1129. if (isset($group_form['instance_settings'], $group_form['instance_settings'][$key]['#options'])) {
  1130. if (is_array($value)) {
  1131. $value = implode(array_filter($value), ', ');
  1132. }
  1133. else {
  1134. $value = $group_form['instance_settings'][$key]['#options'][$value];
  1135. }
  1136. }
  1137. // Shorten the string.
  1138. if (drupal_strlen($value) > 38) {
  1139. $value = truncate_utf8($value, 50, TRUE, TRUE);
  1140. }
  1141. // If still numeric, handle it as yes or no.
  1142. elseif (is_numeric($value)) {
  1143. $value = $value == '1' ? t('yes') : t('no');
  1144. }
  1145. $output .= check_plain($value);
  1146. $output .= $last == $value ? ' ' : '<br />';
  1147. }
  1148. }
  1149. return $output;
  1150. }
  1151. /**
  1152. * Implements hook_element_info().
  1153. */
  1154. function field_group_element_info() {
  1155. $types['horizontal_tabs'] = array(
  1156. '#theme_wrappers' => array('horizontal_tabs'),
  1157. '#default_tab' => '',
  1158. '#process' => array('form_process_horizontal_tabs'),
  1159. );
  1160. $types['multipage'] = array(
  1161. '#theme_wrappers' => array('multipage'),
  1162. '#default_tab' => '',
  1163. '#process' => array('form_process_multipage'),
  1164. );
  1165. $types['multipage_pane'] = array(
  1166. '#value' => NULL,
  1167. '#process' => array('form_process_fieldset', 'ajax_process_form'),
  1168. '#pre_render' => array('form_pre_render_fieldset'),
  1169. '#theme_wrappers' => array('multipage_pane'),
  1170. );
  1171. return $types;
  1172. }
  1173. /**
  1174. * Implements hook_library().
  1175. */
  1176. function field_group_library() {
  1177. $path = drupal_get_path('module', 'field_group');
  1178. // Horizontal Tabs.
  1179. $libraries['horizontal-tabs'] = array(
  1180. 'title' => 'Horizontal Tabs',
  1181. 'website' => 'http://drupal.org/node/323112',
  1182. 'version' => '1.0',
  1183. 'js' => array(
  1184. $path . '/horizontal-tabs/horizontal-tabs.js' => array(),
  1185. ),
  1186. 'css' => array(
  1187. $path . '/horizontal-tabs/horizontal-tabs.css' => array(),
  1188. ),
  1189. );
  1190. // Multipage Tabs.
  1191. $libraries['multipage'] = array(
  1192. 'title' => 'Multipage',
  1193. 'website' => 'http://drupal.org/node/323112',
  1194. 'version' => '1.0',
  1195. 'js' => array(
  1196. $path . '/multipage/multipage.js' => array(),
  1197. ),
  1198. 'css' => array(
  1199. $path . '/multipage/multipage.css' => array(),
  1200. ),
  1201. );
  1202. return $libraries;
  1203. }
  1204. /**
  1205. * Implements hook_field_extra_fields().
  1206. */
  1207. function field_group_field_extra_fields() {
  1208. $extra = array();
  1209. $extra['user']['user'] = array('form' => array());
  1210. // User picture field to integrate with user module.
  1211. if (variable_get('user_pictures', 0)) {
  1212. $extra['user']['user']['form']['picture'] = array(
  1213. 'label' => t('Picture'),
  1214. 'description' => t('User picture'),
  1215. 'weight' => 5,
  1216. );
  1217. }
  1218. // Field to itegrate with overlay module.
  1219. if (module_exists('overlay')) {
  1220. $extra['user']['user']['form']['overlay_control'] = array(
  1221. 'label' => t('Administrative overlay'),
  1222. 'description' => t('Administrative overlay'),
  1223. 'weight' => 5,
  1224. );
  1225. }
  1226. // Field to itegrate with contact module.
  1227. if (module_exists('contact')) {
  1228. $extra['user']['user']['form']['contact'] = array(
  1229. 'label' => t('Contact'),
  1230. 'description' => t('Contact user element'),
  1231. 'weight' => 5,
  1232. );
  1233. }
  1234. // Field to integrate with the locale module.
  1235. if (module_exists('locale')) {
  1236. $extra['user']['user']['form']['locale'] = array(
  1237. 'label' => t('Language settings'),
  1238. 'description' => t('Language settings for the user account.'),
  1239. 'weight' => 5,
  1240. );
  1241. }
  1242. // Field to integrate with the wysiwyg module on user settings.
  1243. if (module_exists('wysiwyg')) {
  1244. $extra['user']['user']['form']['wysiwyg'] = array(
  1245. 'label' => t('Wysiwyg status'),
  1246. 'description' => t('Text formats enabled for rich-text editing'),
  1247. 'weight' => 5,
  1248. );
  1249. }
  1250. return $extra;
  1251. }
  1252. /**
  1253. * Implements hook_field_attach_rename_bundle().
  1254. */
  1255. function field_group_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
  1256. db_query('UPDATE {field_group} SET bundle = :bundle WHERE bundle = :old_bundle AND entity_type = :entity_type', array(
  1257. ':bundle' => $bundle_new,
  1258. ':old_bundle' => $bundle_old,
  1259. ':entity_type' => $entity_type,
  1260. ));
  1261. }
  1262. /**
  1263. * Creates a group formatted as horizontal tabs.
  1264. * This function will never be callable from within field_group rendering. Other
  1265. * modules using #type horizontal_tabs will have the benefit of this processor.
  1266. *
  1267. * @param $element
  1268. * An associative array containing the properties and children of the
  1269. * fieldset.
  1270. * @param $form_state
  1271. * The $form_state array for the form this horizontal tab widget belongs to.
  1272. * @return
  1273. * The processed element.
  1274. */
  1275. function form_process_horizontal_tabs($element, &$form_state) {
  1276. // Inject a new fieldset as child, so that form_process_fieldset() processes
  1277. // this fieldset like any other fieldset.
  1278. $element['group'] = array(
  1279. '#type' => 'fieldset',
  1280. '#theme_wrappers' => array(),
  1281. '#parents' => $element['#parents'],
  1282. );
  1283. // The JavaScript stores the currently selected tab in this hidden
  1284. // field so that the active tab can be restored the next time the
  1285. // form is rendered, e.g. on preview pages or when form validation
  1286. // fails.
  1287. $name = implode('__', $element['#parents']);
  1288. if (isset($form_state['values'][$name . '__active_tab'])) {
  1289. $element['#default_tab'] = $form_state['values'][$name . '__active_tab'];
  1290. }
  1291. $element[$name . '__active_tab'] = array(
  1292. '#type' => 'hidden',
  1293. '#default_value' => $element['#default_tab'],
  1294. '#attributes' => array('class' => array('horizontal-tabs-active-tab')),
  1295. );
  1296. return $element;
  1297. }
  1298. /**
  1299. * Returns HTML for an element's children fieldsets as horizontal tabs.
  1300. *
  1301. * @param $variables
  1302. * An associative array containing:
  1303. * - element: An associative array containing the properties and children of the
  1304. * fieldset. Properties used: #children.
  1305. *
  1306. * @ingroup themeable
  1307. */
  1308. function theme_horizontal_tabs($variables) {
  1309. $element = $variables['element'];
  1310. // Add required JavaScript and Stylesheet.
  1311. drupal_add_library('field_group', 'horizontal-tabs');
  1312. $output = '<h2 class="element-invisible">' . (!empty($element['#title']) ? $element['#title'] : t('Horizontal Tabs')) . '</h2>';
  1313. $output .= '<div class="horizontal-tabs-panes">' . $element['#children'] . '</div>';
  1314. return $output;
  1315. }
  1316. /**
  1317. * Creates a group formatted as multipage.
  1318. * This function will never be callable from within field_group rendering. Other
  1319. * modules using #type multipage will have the benefit of this processor.
  1320. *
  1321. * @param $element
  1322. * An associative array containing the properties and children of the
  1323. * fieldset.
  1324. * @param $form_state
  1325. * The $form_state array for the form this multipage tab widget belongs to.
  1326. * @return
  1327. * The processed element.
  1328. */
  1329. function form_process_multipage($element, &$form_state) {
  1330. // Inject a new fieldset as child, so that form_process_fieldset() processes
  1331. // this fieldset like any other fieldset.
  1332. $element['group'] = array(
  1333. '#type' => 'fieldset',
  1334. '#theme_wrappers' => array(),
  1335. '#parents' => $element['#parents'],
  1336. );
  1337. // The JavaScript stores the currently selected tab in this hidden
  1338. // field so that the active control can be restored the next time the
  1339. // form is rendered, e.g. on preview pages or when form validation
  1340. // fails.
  1341. $name = implode('__', $element['#parents']);
  1342. if (isset($form_state['values'][$name . '__active_control'])) {
  1343. $element['#default_tab'] = $form_state['values'][$name . '__active_control'];
  1344. }
  1345. $element[$name . '__active_control'] = array(
  1346. '#type' => 'hidden',
  1347. '#default_value' => $element['#default_control'],
  1348. '#attributes' => array('class' => array('multipage-active-control')),
  1349. );
  1350. return $element;
  1351. }
  1352. /**
  1353. * Returns HTML for an element's children fieldsets as multipage.
  1354. *
  1355. * @param $variables
  1356. * An associative array containing:
  1357. * - element: An associative array containing the properties and children of the
  1358. * fieldset. Properties used: #children.
  1359. *
  1360. * @ingroup themeable
  1361. */
  1362. function theme_multipage($variables) {
  1363. $element = $variables['element'];
  1364. // Add required JavaScript and Stylesheet.
  1365. $element['#attached']['library'][] = array('field_group', 'multipage');
  1366. $output = '<h2 class="element-invisible">' . (!empty($element['#title']) ? $element['#title'] : t('Multipage')) . '</h2>';
  1367. $output .= '<div class="multipage-panes">';
  1368. $output .= $element['#children'];
  1369. $output .= '</div>';
  1370. return $output;
  1371. }
  1372. /**
  1373. * Returns HTML for multipage pane.
  1374. *
  1375. * @param $variables
  1376. * An associative array containing:
  1377. * - element: An associative array containing the properties and children of the
  1378. * fieldset. Properties used: #children.
  1379. *
  1380. * @ingroup themeable
  1381. */
  1382. function theme_multipage_pane($variables) {
  1383. $element = $variables['element'];
  1384. $group = $variables['element']['#group_object'];
  1385. $parent_group = $variables['element']['#parent_group_object'];
  1386. static $multipages;
  1387. if (!isset($multipages[$group->parent_name])) {
  1388. $multipages = array($group->parent_name => 0);
  1389. }
  1390. $multipages[$parent_group->group_name]++;
  1391. // Create a page title from the label.
  1392. $page_header = isset($parent_group->format_settings['instance_settings']['page_header']) ? $parent_group->format_settings['instance_settings']['page_header'] : 3;
  1393. switch ($page_header) {
  1394. case 1:
  1395. $title = $element['#title'];
  1396. break;
  1397. case 2:
  1398. $title = t('Step %count of %total', array('%count' => $multipages[$parent_group->group_name], '%total' => count($parent_group->children)));
  1399. break;
  1400. case 3:
  1401. $title = t('Step %count of %total !label', array('%count' => $multipages[$parent_group->group_name], '%total' => count($parent_group->children), '!label' => $element['#title']));
  1402. break;
  1403. case 0:
  1404. default:
  1405. $title = '';
  1406. break;
  1407. }
  1408. element_set_attributes($element, array('id'));
  1409. _form_set_class($element, array('form-wrapper'));
  1410. $output = '<div' . drupal_attributes($element['#attributes']) . '>';
  1411. if (!empty($element['#title'])) {
  1412. // Always wrap fieldset legends in a SPAN for CSS positioning.
  1413. $output .= '<h2 class="multipage-pane-title"><span>' . $title . '</span></h2>';
  1414. }
  1415. $output .= '<div class="fieldset-wrapper multipage-pane-wrapper">';
  1416. if (!empty($element['#description'])) {
  1417. $output .= '<div class="fieldset-description">' . $element['#description'] . '</div>';
  1418. }
  1419. $output .= $element['#children'];
  1420. if (isset($element['#value'])) {
  1421. $output .= $element['#value'];
  1422. }
  1423. // Add a page counter if needed.
  1424. // counter array(0 => t('No'), 1 => t('Format 1 / 10'), 2 => t('The count number only'));
  1425. $page_counter_format = isset($parent_group->format_settings['instance_settings']['page_counter']) ? $parent_group->format_settings['instance_settings']['page_counter'] : 1;
  1426. $multipage_element['#page_counter_rendered'] = '';
  1427. if ($page_counter_format == 1) {
  1428. $output .= t('<span class="multipage-counter">%count / %total</span>', array('%count' => $multipages[$parent_group->group_name], '%total' => count($parent_group->children)));
  1429. }
  1430. elseif ($page_counter_format == 2) {
  1431. $output .= t('<span class="multipage-counter">%count</span>', array('%count' => $multipages[$parent_group->group_name]));
  1432. }
  1433. $output .= '</div>';
  1434. $output .= "</div>\n";
  1435. return $output;
  1436. }
  1437. /**
  1438. * Get all groups.
  1439. *
  1440. * @param $entity_type
  1441. * The name of the entity.
  1442. * @param $bundle
  1443. * The name of the bundle.
  1444. * @param $view_mode
  1445. * The view mode.
  1446. * @param $reset.
  1447. * Whether to reset the cache or not.
  1448. */
  1449. function field_group_info_groups($entity_type = NULL, $bundle = NULL, $view_mode = NULL, $reset = FALSE) {
  1450. static $groups = FALSE;
  1451. if (!$groups || $reset) {
  1452. if (!$reset && $cached = cache_get('field_groups', 'cache_field')) {
  1453. $groups = $cached->data;
  1454. }
  1455. else {
  1456. $ctools_export_load_object = &drupal_static('ctools_export_load_object');
  1457. $ctools_export_load_object_all = &drupal_static('ctools_export_load_object_all');
  1458. unset($ctools_export_load_object['field_group']);
  1459. unset($ctools_export_load_object_all['field_group']);
  1460. $groups = field_group_read_groups();
  1461. cache_set('field_groups', $groups, 'cache_field');
  1462. }
  1463. }
  1464. if (!isset($entity_type)) {
  1465. return $groups;
  1466. }
  1467. elseif (!isset($bundle) && isset($groups[$entity_type])) {
  1468. return $groups[$entity_type];
  1469. }
  1470. elseif (!isset($view_mode) && isset($groups[$entity_type][$bundle])) {
  1471. return $groups[$entity_type][$bundle];
  1472. }
  1473. elseif (isset($groups[$entity_type][$bundle][$view_mode])) {
  1474. return $groups[$entity_type][$bundle][$view_mode];
  1475. }
  1476. return array();
  1477. }
  1478. /**
  1479. * Read all groups.
  1480. *
  1481. * @param array $conditions
  1482. * Parameters for the query, as elements of the $conditions array.
  1483. * 'entity_type' The name of the entity type.
  1484. * 'bundle' The name of the bundle.
  1485. * 'mode' The view mode.
  1486. *
  1487. * @param boolean $enabled
  1488. * Return enabled or disabled groups.
  1489. *
  1490. * @return array
  1491. * Array of groups.
  1492. */
  1493. function field_group_read_groups($conditions = array(), $enabled = TRUE) {
  1494. $groups = array();
  1495. ctools_include('export');
  1496. if (empty($conditions)) {
  1497. $records = ctools_export_load_object('field_group');
  1498. }
  1499. else {
  1500. $records = ctools_export_load_object('field_group', 'conditions', $conditions);
  1501. }
  1502. foreach ($records as $group) {
  1503. // Return only enabled groups.
  1504. if ($enabled && isset($group->disabled) && $group->disabled) {
  1505. continue;
  1506. }
  1507. // Return only disabled groups.
  1508. elseif (!$enabled && (!isset($group->disabled) || !$group->disabled)) {
  1509. continue;
  1510. }
  1511. $groups[$group->entity_type][$group->bundle][$group->mode][$group->group_name] = field_group_unpack($group);
  1512. }
  1513. drupal_alter('field_group_info', $groups);
  1514. return $groups;
  1515. }
  1516. /**
  1517. * Utility function to recreate identifiers.
  1518. */
  1519. function _field_group_recreate_identifiers() {
  1520. // Migrate the field groups so they have a unique identifier.
  1521. $result = db_select('field_group', 'fg')
  1522. ->fields('fg')
  1523. ->execute();
  1524. $rows = array();
  1525. foreach($result as $row) {
  1526. $row->identifier = $row->group_name . '|' . $row->entity_type . '|' . $row->bundle . '|' . $row->mode;
  1527. $row->data = unserialize($row->data);
  1528. $rows[] = $row;
  1529. }
  1530. foreach ($rows as $row) {
  1531. drupal_write_record('field_group', $row, array('id'));
  1532. }
  1533. }
  1534. /**
  1535. * Checks if a field_group exists in required context.
  1536. *
  1537. * @param String $group_name
  1538. * The name of the group.
  1539. * @param String $entity_type
  1540. * The name of the entity.
  1541. * @param String $bundle
  1542. * The bundle for the entity.
  1543. * @param String $mode
  1544. * The view mode context the group will be rendered.
  1545. */
  1546. function field_group_exists($group_name, $entity_type, $bundle, $mode) {
  1547. $groups = field_group_read_groups();
  1548. return !empty($groups[$entity_type][$bundle][$mode][$group_name]);
  1549. }
  1550. /**
  1551. * Unpacks a database row in a FieldGroup object.
  1552. * @param $packed_group
  1553. * Database result object with stored group data.
  1554. * @return $group
  1555. * Field group object.
  1556. */
  1557. function field_group_unpack($packed_group) {
  1558. if (empty($packed_group->data)) {
  1559. return $packed_group;
  1560. }
  1561. // Extract unserialized data.
  1562. $group = clone $packed_group;
  1563. $data = $group->data;
  1564. unset($group->data);
  1565. $group->label = isset($data['label']) ? $data['label'] : '';
  1566. $group->weight = isset($data['weight']) ? $data['weight'] : '';
  1567. $group->children = isset($data['children']) ? $data['children'] : '';
  1568. $group->format_type = !empty($data['format_type']) ? $data['format_type'] : 'fieldset';
  1569. if (isset($data['format_settings'])) {
  1570. $group->format_settings = $data['format_settings'];
  1571. }
  1572. return $group;
  1573. }
  1574. /**
  1575. * Packs a FieldGroup object into a database row.
  1576. * @param $group
  1577. * FieldGroup object.
  1578. * @return $record
  1579. * Database row object, ready to be inserted/update
  1580. */
  1581. function field_group_pack($group) {
  1582. $record = clone $group;
  1583. $record->data = array(
  1584. 'label' => $record->label,
  1585. 'weight' => $record->weight,
  1586. 'children' => $record->children,
  1587. 'format_type' => !empty($record->format_type) ? $record->format_type : 'fieldset',
  1588. );
  1589. if (isset($record->format_settings)) {
  1590. $record->data['format_settings'] = $record->format_settings;
  1591. }
  1592. return $record;
  1593. }
  1594. /**
  1595. * Delete a field group.
  1596. * This function is also called by ctools export when calls are
  1597. * made through ctools_export_crud_delete().
  1598. *
  1599. * @param $group
  1600. * A group definition.
  1601. * @param $ctools_crud
  1602. * Is this function called by the ctools crud delete.
  1603. */
  1604. function field_group_group_export_delete($group, $ctools_crud = TRUE) {
  1605. $query = db_delete('field_group');
  1606. if (isset($group->identifier)) {
  1607. $query->condition('identifier', $group->identifier);
  1608. if (!$ctools_crud) {
  1609. ctools_export_crud_disable('field_group', $group->identifier);
  1610. }
  1611. }
  1612. elseif (isset($group->id)) {
  1613. $query->condition('id', $group->id);
  1614. }
  1615. if (!empty($group->mode)) {
  1616. $query->condition('mode', $group->mode);
  1617. }
  1618. $query->execute();
  1619. cache_clear_all('field_groups', 'cache_field');
  1620. module_invoke_all('field_group_delete_field_group', $group);
  1621. }
  1622. /**
  1623. * field_group_group_save().
  1624. *
  1625. * Saves a group definition.
  1626. * This function is called by ctools export when calls are made
  1627. * through ctools_export_crud_save().
  1628. *
  1629. * @param $group
  1630. * A group definition.
  1631. */
  1632. function field_group_group_save(& $group) {
  1633. // Prepare the record.
  1634. $object = field_group_pack($group);
  1635. if (isset($object->export_type) && $object->export_type & EXPORT_IN_DATABASE) {
  1636. // Existing record.
  1637. $update = array('id');
  1638. module_invoke_all('field_group_update_field_group', $object);
  1639. }
  1640. else {
  1641. // New record.
  1642. $update = array();
  1643. $object->export_type = EXPORT_IN_DATABASE;
  1644. module_invoke_all('field_group_create_field_group', $object);
  1645. }
  1646. return drupal_write_record('field_group', $object, $update);
  1647. }
  1648. /**
  1649. * Function to retrieve all format possibilities for the fieldgroups.
  1650. */
  1651. function field_group_formatter_info($display_overview = FALSE) {
  1652. $cache = &drupal_static(__FUNCTION__, array());
  1653. if (empty($cache)) {
  1654. if ($cached = cache_get('field_group_formatter_info', 'cache_field')) {
  1655. $formatters = $cached->data;
  1656. }
  1657. else {
  1658. $formatters = array();
  1659. $formatters += module_invoke_all('field_group_formatter_info');
  1660. $hidden_region = array(
  1661. 'label' => '<' . t('Hidden') . '>',
  1662. 'description' => '',
  1663. 'format_types' => array(),
  1664. 'instance_settings' => array(),
  1665. 'default_formatter' => '',
  1666. );
  1667. //$formatters['form']['hidden'] = $hidden_region;
  1668. $formatters['display']['hidden'] = $hidden_region;
  1669. cache_set('field_group_formatter_info', $formatters, 'cache_field');
  1670. }
  1671. $cache = $formatters;
  1672. }
  1673. return $cache;
  1674. }
  1675. /**
  1676. * Attach groups to the (form) build.
  1677. *
  1678. * @param Array $element
  1679. * The part of the form.
  1680. * @param String $view_mode
  1681. * The mode for the build.
  1682. * @param Array $form_state
  1683. * The optional form state when in view_mode = form context.
  1684. */
  1685. function field_group_attach_groups(&$element, $view_mode, $form_state = array()) {
  1686. $entity_type = $element['#entity_type'];
  1687. $bundle = $element['#bundle'];
  1688. $element['#groups'] = field_group_info_groups($entity_type, $bundle, $view_mode);
  1689. $element['#fieldgroups'] = $element['#groups'];
  1690. // Create a lookup array.
  1691. $group_children = array();
  1692. foreach ($element['#groups'] as $group_name => $group) {
  1693. if (!empty($group->children)) {
  1694. foreach ($group->children as $child) {
  1695. $group_children[$child] = $group_name;
  1696. }
  1697. }
  1698. }
  1699. $element['#group_children'] = $group_children;
  1700. }
  1701. /**
  1702. * Pre render callback for rendering groups.
  1703. * @see field_group_field_attach_form
  1704. * @param $element Form that is being rendered.
  1705. */
  1706. function field_group_form_pre_render(&$element) {
  1707. return field_group_build_entity_groups($element, 'form');
  1708. }
  1709. /**
  1710. * Preprocess/ Pre-render callback.
  1711. *
  1712. * @see field_group_form_pre_render()
  1713. * @see field_group_theme_registry_alter
  1714. * @see field_group_fields_nest()
  1715. * @param $vars preprocess vars or form element
  1716. * @param $type The type of object being rendered
  1717. * @return $element Array with re-arranged fields in forms.
  1718. */
  1719. function field_group_build_entity_groups(&$vars, $type) {
  1720. if ($type == 'form') {
  1721. $element = &$vars;
  1722. $nest_vars = NULL;
  1723. }
  1724. else {
  1725. $element = &$vars['elements'];
  1726. $nest_vars = &$vars;
  1727. }
  1728. // No groups on the entity.
  1729. if (empty($element['#fieldgroups'])) {
  1730. return $element;
  1731. }
  1732. // Nest the fields in the corresponding field groups.
  1733. field_group_fields_nest($element, $nest_vars);
  1734. // Allow others to alter the pre_rendered build.
  1735. drupal_alter('field_group_build_pre_render', $element);
  1736. // Return the element on forms.
  1737. if ($type == 'form') {
  1738. return $element;
  1739. }
  1740. // No groups on the entity. Prerender removed empty field groups.
  1741. if (empty($element['#fieldgroups'])) {
  1742. return $element;
  1743. }
  1744. // Put groups inside content if we are rendering an entity_view.
  1745. foreach ($element['#fieldgroups'] as $group) {
  1746. if (!empty($element[$group->group_name]) && $type != 'user_profile') {
  1747. $vars['content'][$group->group_name] = $element[$group->group_name];
  1748. }
  1749. elseif (!empty($element[$group->group_name])) {
  1750. $vars['user_profile'][$group->group_name] = $element[$group->group_name];
  1751. }
  1752. }
  1753. // New css / js can be attached.
  1754. drupal_process_attached($element);
  1755. }
  1756. /**
  1757. * Recursive function to nest fields in the field groups.
  1758. *
  1759. * This function will take out all the elements in the form and
  1760. * place them in the correct container element, a fieldgroup.
  1761. * The current group element in the loop is passed recursively so we can
  1762. * stash fields and groups in it while we go deeper in the array.
  1763. * @param Array $element
  1764. * The current element to analyse for grouping.
  1765. * @param Array $vars
  1766. * Rendering vars from the entity being viewed.
  1767. */
  1768. function field_group_fields_nest(&$element, &$vars = NULL) {
  1769. // Create all groups and keep a flat list of references to these groups.
  1770. $group_references = array();
  1771. foreach ($element['#fieldgroups'] as $group_name => $group) {
  1772. // check for any erroneous groups from other modules
  1773. if (is_string($group_name)) {
  1774. // Construct own weight, as some fields (for example preprocess fields) don't have weight set.
  1775. $element[$group_name] = array();
  1776. $group_references[$group_name] = &$element[$group_name];
  1777. // Get group parents
  1778. $parents = array();
  1779. $current_group = $group;
  1780. while (!empty($current_group)) {
  1781. array_unshift($parents, $current_group->group_name);
  1782. $current_group = isset($element['#fieldgroups'][$current_group->parent_name]) ?
  1783. $element['#fieldgroups'][$current_group->parent_name] : NULL;
  1784. }
  1785. $group_references[$group_name]['#array_parents'] = $parents;
  1786. $element['#fieldgroups'][$group_name]->array_parents = $parents;
  1787. // Remove self from parents and set #field_parents
  1788. array_pop($parents);
  1789. $group_references[$group_name]['#field_parents'] = $parents;
  1790. }
  1791. }
  1792. // Loop through all form children looking for those that are supposed to be
  1793. // in groups, and insert placeholder element for the new group field in the
  1794. // correct location within the form structure.
  1795. $element_clone = array();
  1796. foreach (element_children($element) as $child_name) {
  1797. $element_clone[$child_name] = $element[$child_name];
  1798. // If this element is in a group, create the placeholder element.
  1799. if (isset($element['#group_children'][$child_name])) {
  1800. $element_clone[$element['#group_children'][$child_name]] = array();
  1801. }
  1802. }
  1803. $element = array_merge($element_clone, $element);
  1804. // Move all children to their parents. Use the flat list of references for
  1805. // direct access as we don't know where in the root_element hierarchy the
  1806. // parent currently is situated.
  1807. foreach ($element['#group_children'] as $child_name => $parent_name) {
  1808. // Entity being viewed
  1809. if ($vars) {
  1810. // If not a group, check vars['content'] for empty field.
  1811. if (!isset($element['#fieldgroups'][$child_name]) && isset($vars['content'][$child_name])) {
  1812. $group_references[$parent_name][$child_name] = $vars['content'][$child_name];
  1813. unset($vars['content'][$child_name]);
  1814. }
  1815. elseif (!isset($element['#fieldgroups'][$child_name]) && isset($vars['user_profile'][$child_name])) {
  1816. $group_references[$parent_name][$child_name] = $vars['user_profile'][$child_name];
  1817. unset($vars['user_profile'][$child_name]);
  1818. }
  1819. // If this is a group, we have to use a reference to keep the reference
  1820. // list intact (but if it is a field we don't mind).
  1821. else {
  1822. $group_references[$parent_name][$child_name] = &$element[$child_name];
  1823. unset($element[$child_name]);
  1824. }
  1825. }
  1826. // Form being viewed
  1827. else {
  1828. // Block denied fields (#access) before they are put in groups.
  1829. // Fields (not groups) that don't have children (like field_permissions) are removed
  1830. // in field_group_field_group_build_pre_render_alter.
  1831. if (isset($element[$child_name]) && (!isset($element[$child_name]['#access']) || $element[$child_name]['#access'])) {
  1832. // If this is a group, we have to use a reference to keep the reference
  1833. // list intact (but if it is a field we don't mind).
  1834. $group_references[$parent_name][$child_name] = &$element[$child_name];
  1835. $group_references[$parent_name]['#weight'] = $element['#fieldgroups'][$parent_name]->weight;
  1836. // Prepend #array_parents & #field_parents of group child element & its element_children
  1837. // if those keys are set, and don't already include the group parents
  1838. $group_child = &$group_references[$parent_name][$child_name];
  1839. $group_parents = $group_references[$parent_name]['#array_parents'];
  1840. $process_elements = array_merge(array(&$group_child), _field_group_element_children_recursive_ref($group_child));
  1841. foreach ($process_elements as $key => $current_element) {
  1842. if (isset($current_element['#array_parents']) && !in_array($group_parents[0], $current_element['#array_parents'])) {
  1843. $process_elements[$key]['#array_parents'] = array_merge($group_parents, $current_element['#array_parents']);
  1844. }
  1845. if (isset($current_element['#field_parents']) && !in_array($group_parents[0], $current_element['#field_parents'])) {
  1846. $process_elements[$key]['#field_parents'] = array_merge($group_parents, $current_element['#field_parents']);
  1847. }
  1848. }
  1849. }
  1850. // The child has been copied to its parent: remove it from the root element.
  1851. unset($element[$child_name]);
  1852. }
  1853. }
  1854. // Bring extra element wrappers to achieve a grouping of fields.
  1855. // This will mainly be prefix and suffix altering.
  1856. foreach ($element['#fieldgroups'] as $group_name => $group) {
  1857. field_group_pre_render($group_references[$group_name], $group, $element);
  1858. }
  1859. }
  1860. /**
  1861. * Recursive element_children, returns children by reference
  1862. */
  1863. function _field_group_element_children_recursive_ref(&$element) {
  1864. $results = array();
  1865. $children = element_children($element);
  1866. foreach ($children as $key) {
  1867. $child = &$element[$key];
  1868. if (is_array($child)) {
  1869. $results[] = &$child;
  1870. $results = array_merge($results, _field_group_element_children_recursive_ref($child));
  1871. }
  1872. unset($child);
  1873. }
  1874. return $results;
  1875. }
  1876. /**
  1877. * Function to pre render the field group element.
  1878. *
  1879. * @see field_group_fields_nest()
  1880. *
  1881. * @param $element Array of group element that needs to be created!
  1882. * @param $group Object with the group information.
  1883. * @param $form The form object itself.
  1884. */
  1885. function field_group_pre_render(& $element, $group, & $form) {
  1886. // Only run the pre_render function if the group has elements.
  1887. // $group->group_name
  1888. if ($element == array()) {
  1889. return;
  1890. }
  1891. // Let modules define their wrapping element.
  1892. // Note that the group element has no properties, only elements.
  1893. foreach (module_implements('field_group_pre_render') as $module) {
  1894. $function = $module . '_field_group_pre_render';
  1895. if (function_exists($function)) {
  1896. // The intention here is to have the opportunity to alter the
  1897. // elements, as defined in hook_field_group_formatter_info.
  1898. // Note, implement $element by reference!
  1899. $function($element, $group, $form);
  1900. }
  1901. }
  1902. // Allow others to alter the pre_render.
  1903. drupal_alter('field_group_pre_render', $element, $group, $form);
  1904. }
  1905. /**
  1906. * Hides field groups including children in a render array.
  1907. *
  1908. * @param array $element
  1909. * A render array. Can be a form, node, user, ...
  1910. * @param array $group_names
  1911. * An array of field group names that should be hidden.
  1912. */
  1913. function field_group_hide_field_groups(&$element, $group_names) {
  1914. foreach ($group_names as $group_name) {
  1915. if (isset($element['#fieldgroups'][$group_name]) && isset($element['#group_children'])) {
  1916. // Hide the field group.
  1917. $element['#fieldgroups'][$group_name]->format_type = 'hidden';
  1918. // Hide the elements inside the field group.
  1919. $sub_groups = array();
  1920. foreach (array_keys($element['#group_children'], $group_name) as $field_name) {
  1921. if (isset($element['#fieldgroups'][$field_name])) {
  1922. $sub_groups[] = $field_name;
  1923. } else {
  1924. $element[$field_name]['#access'] = FALSE;
  1925. }
  1926. }
  1927. field_group_hide_field_groups($element, $sub_groups);
  1928. }
  1929. }
  1930. }
  1931. /**
  1932. * Calculates html classes for a group.
  1933. */
  1934. function _field_group_get_html_classes(&$group) {
  1935. if (isset($group->format_settings['formatter'])) {
  1936. $group->collapsible = in_array($group->format_settings['formatter'], array('collapsible', 'collapsed'));
  1937. // Open or closed horizontal or vertical tabs will be collapsible by default.
  1938. if ($group->format_type == 'tab' || $group->format_type == 'htab') {
  1939. $group->collapsible = TRUE;
  1940. }
  1941. $group->collapsed = in_array($group->format_settings['formatter'], array('collapsed', 'closed'));
  1942. }
  1943. $classes = new stdClass();
  1944. // Prepare extra classes, required and optional ones.
  1945. $optional = array(str_replace('_', '-', $group->group_name));
  1946. $required = array();
  1947. if ($group->format_type == 'multipage') {
  1948. $required[] = 'field-group-' . $group->format_type;
  1949. }
  1950. else {
  1951. $optional[] = 'field-group-' . $group->format_type;
  1952. }
  1953. if (isset($group->format_settings['formatter']) && $group->collapsible) {
  1954. $required[] = 'collapsible';
  1955. if ($group->collapsed) {
  1956. $required[] = 'collapsed';
  1957. }
  1958. }
  1959. if (isset($group->format_settings['instance_settings'])) {
  1960. // Add a required-fields class to trigger the js.
  1961. if (!empty($group->format_settings['instance_settings']['required_fields'])) {
  1962. $required[] = 'required-fields';
  1963. }
  1964. // Add user selected classes.
  1965. if (!empty($group->format_settings['instance_settings']['classes'])) {
  1966. $required[] = check_plain($group->format_settings['instance_settings']['classes']);
  1967. }
  1968. // Extra required classes for div.
  1969. if ($group->format_type == 'div') {
  1970. if ($group->format_settings['formatter'] != 'open') {
  1971. $speed = isset($group->format_settings['instance_settings']['speed']) ? $group->format_settings['instance_settings']['speed'] : 'none';
  1972. $required[] = 'speed-' . $speed;
  1973. $effect = isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : 'none';
  1974. $required[] = 'effect-' . $effect;
  1975. }
  1976. }
  1977. // Extra required classes for accordions.
  1978. elseif ($group->format_type == 'accordion') {
  1979. $required[] = 'field-group-' . $group->format_type . '-wrapper';
  1980. $effect = isset($group->format_settings['instance_settings']['effect']) ? $group->format_settings['instance_settings']['effect'] : 'none';
  1981. $required[] = 'effect-' . $effect;
  1982. }
  1983. }
  1984. $classes->required = $required;
  1985. $classes->optional = $optional;
  1986. return $classes;
  1987. }
  1988. /**
  1989. * Get the default formatter settings for a given formatter and a mode.
  1990. */
  1991. function _field_group_get_default_formatter_settings($format_type, $mode) {
  1992. $field_group_types = field_group_formatter_info();
  1993. $display_mode = $mode == 'form' ? 'form' : 'display';
  1994. $formatter = $field_group_types[$display_mode][$format_type];
  1995. return array(
  1996. 'formatter' => isset($formatter['default_formatter']) ? $formatter['default_formatter'] : '',
  1997. 'instance_settings' => $formatter['instance_settings']
  1998. );
  1999. }
  2000. /**
  2001. * Callback to bulk export field groups.
  2002. */
  2003. function field_group_field_group_to_hook_code($data, $module) {
  2004. ctools_include('export');
  2005. $schema = ctools_export_get_schema('field_group');
  2006. $export = $schema['export'];
  2007. $translatables = array();
  2008. $objects = ctools_export_load_object('field_group', 'names', array_values($data));
  2009. $code = "/**\n";
  2010. $code .= " * Implements hook_{$export['default hook']}()\n";
  2011. $code .= " */\n";
  2012. $code .= "function " . $module . "_{$export['default hook']}() {\n";
  2013. $code .= " \${$export['identifier']}s = array();\n\n";
  2014. foreach ($objects as $object) {
  2015. $code .= ctools_export_object('field_group', $object, ' ');
  2016. $code .= " \${$export['identifier']}s['" . check_plain($object->{$export['key']}) . "'] = \${$export['identifier']};\n\n";
  2017. if (!empty($object->data['label'])) {
  2018. $translatables[] = $object->data['label'];
  2019. }
  2020. if (!empty($object->data['description'])) {
  2021. $translatables[] = $object->data['description'];
  2022. }
  2023. }
  2024. if (!empty($translatables)) {
  2025. $code .= features_translatables_export($translatables, ' ') . "\n";
  2026. }
  2027. $code .= " return \${$export['identifier']}s;";
  2028. $code .= "}\n";
  2029. return $code;
  2030. }