field_group.module 75 KB

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