field_group.module 80 KB

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