field_group.module 77 KB

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