fieldgroup.module

Tracking 5.x-1.x branch
  1. drupal
    1. 5 contributions/cck/fieldgroup.module
    2. 6 contributions/cck/modules/fieldgroup/fieldgroup.module

Create field groups for CCK fields.

Functions & methods

NameDescription
fieldgroup_content_admin_field_remove_submit
fieldgroup_content_admin_form_submit
fieldgroup_display_overview_form
fieldgroup_display_overview_form_row
fieldgroup_display_overview_form_submit
fieldgroup_edit_group
fieldgroup_edit_group_form
fieldgroup_edit_group_submit
fieldgroup_edit_group_validateGroup name validation for programmatic group addition.
fieldgroup_form_alter
fieldgroup_get_group
fieldgroup_groups
fieldgroup_menu
fieldgroup_nodeapi
fieldgroup_node_typeImplementation of hook_node_type() React to change in node types
fieldgroup_panels_content_typesImplementation of hook_panels_content_types()
fieldgroup_panels_edit_fieldgroup'Edit' callback for the 'fieldgroup' panel.
fieldgroup_panels_fieldgroup_content_typesReturn all fieldgroup panel types available.
fieldgroup_panels_fieldgroup_title'Title' callback for the 'fieldgroup' panel.
fieldgroup_panels_render_fieldgroup'Render' callback for the 'fieldgroup' panel.
fieldgroup_remove_group
fieldgroup_remove_group_submit
fieldgroup_save_group
theme_fieldgroup_display_overview_form
theme_fieldgroup_panelAllows users to theme the panels group.
theme_fieldgroup_simple
_fieldgroup_field_get_group
_fieldgroup_groups_label

File

View source
  1. <?php
  2. /**
  3. * @file
  4. * Create field groups for CCK fields.
  5. */
  6. function fieldgroup_menu($may_cache) {
  7. if (!$may_cache) {
  8. if (arg(0) == 'admin' && arg(1) == 'content' && arg(2) == 'types' && arg(3)) {
  9. $content_type = content_types(arg(3));
  10. if (!empty($content_type) && arg(3) && arg(3) == $content_type['url_str']) {
  11. $items[] = array(
  12. 'path' => 'admin/content/types/'. arg(3) .'/add_group',
  13. 'title' => t('Add group'),
  14. 'callback' => 'fieldgroup_edit_group',
  15. 'access' => user_access('administer content types'),
  16. 'callback arguments' => array($content_type, '', 'add'),
  17. 'type' => MENU_LOCAL_TASK,
  18. 'weight' => 3,
  19. );
  20. if (arg(4) == 'groups' && arg(5)) {
  21. $items[] = array(
  22. 'path' => 'admin/content/types/'. arg(3) .'/groups/'. arg(5) .'/edit',
  23. 'title' => t('Edit group'),
  24. 'callback' => 'fieldgroup_edit_group',
  25. 'access' => user_access('administer content types'),
  26. 'callback arguments' => array($content_type, arg(5), 'edit'),
  27. 'type' => MENU_CALLBACK_ITEM,
  28. );
  29. $items[] = array(
  30. 'path' => 'admin/content/types/'. arg(3) .'/groups/'. arg(5) .'/remove',
  31. 'title' => t('Edit group'),
  32. 'callback' => 'drupal_get_form',
  33. 'callback arguments' => array('fieldgroup_remove_group', $content_type, arg(5)),
  34. 'access' => user_access('administer content types'),
  35. 'type' => MENU_CALLBACK_ITEM,
  36. );
  37. }
  38. }
  39. }
  40. drupal_add_css(drupal_get_path('module', 'fieldgroup') .'/fieldgroup.css');
  41. }
  42. return $items;
  43. }
  44. function fieldgroup_edit_group($content_type, $group_name, $action) {
  45. return drupal_get_form('fieldgroup_edit_group_form', $content_type, $group_name, $action);
  46. }
  47. function fieldgroup_edit_group_form($content_type, $group_name, $action) {
  48. $groups = fieldgroup_groups($content_type['type']);
  49. $group = $groups[$group_name];
  50. if ($action == 'add') {
  51. //adding a new one
  52. $group = array();
  53. $form['submit'] = array(
  54. '#type' => 'submit',
  55. '#value' => t('Add'),
  56. '#weight' => 10,
  57. );
  58. }
  59. else if ($group) {
  60. $form['submit'] = array(
  61. '#type' => 'submit',
  62. '#value' => t('Save'),
  63. '#weight' => 10,
  64. );
  65. }
  66. else {
  67. drupal_not_found();
  68. exit;
  69. }
  70. $form['label'] = array(
  71. '#type' => 'textfield',
  72. '#title' => t('Label'),
  73. '#default_value' => $group['label'],
  74. '#required' => TRUE,
  75. );
  76. $form['settings']['#tree'] = TRUE;
  77. $form['settings']['form'] = array(
  78. '#type' => 'fieldset',
  79. '#title' => 'Form settings',
  80. '#description' => t('These settings apply to the group in the node editing form'),
  81. );
  82. $form['settings']['form']['style'] = array(
  83. '#type' => 'radios',
  84. '#title' => t('Style'),
  85. '#default_value' => $group['settings']['form']['style'] ? $group['settings']['form']['style'] : 'fieldset',
  86. '#options' => array(
  87. 'fieldset' => t('always open'),
  88. 'fieldset_collapsible' => t('collapsible'),
  89. 'fieldset_collapsed' => t('collapsed'),
  90. )
  91. );
  92. $form['settings']['form']['description'] = array(
  93. '#type' => 'textarea',
  94. '#title' => t('Help text'),
  95. '#default_value' => $group['settings']['form']['description'],
  96. '#rows' => 5,
  97. '#description' => t('Instructions to present to the user on the editing form.'),
  98. '#required' => FALSE,
  99. );
  100. $form['settings']['display'] = array(
  101. '#type' => 'fieldset',
  102. '#title' => 'Display settings',
  103. '#description' => t('These settings apply to the group on node display.'),
  104. );
  105. $form['settings']['display']['description'] = array(
  106. '#type' => 'textarea',
  107. '#title' => t('Description'),
  108. '#default_value' => $group['settings']['display']['description'],
  109. '#rows' => 5,
  110. '#description' => t('A description of the group.'),
  111. '#required' => FALSE,
  112. );
  113. foreach (array_merge(array_keys(_content_admin_display_contexts()), array('label')) as $key) {
  114. $form['settings']['display'][$key] = array('#type' => 'value', '#value' => $group['settings']['display'][$key]);
  115. }
  116. $form['weight'] = array('#type' => 'hidden', '#default_value' => $group['weight']);
  117. $form['group_name'] = array('#type' => 'hidden', '#default_value' => $group_name);
  118. $form['#submit'] = array('fieldgroup_edit_group_submit' => array($content_type, $action));
  119. return $form;
  120. }
  121. /**
  122. * Group name validation for programmatic group addition.
  123. */
  124. function fieldgroup_edit_group_validate($form_id, $form_values, $content_type, $action) {
  125. if (!empty($form_values['group_name']) && $action == 'add') {
  126. $groups = fieldgroup_groups($content_type['type']);
  127. $group = $groups[$form_values['group_name']];
  128. if (isset($group[$form_values['group_name']])) {
  129. form_set_error('group_name', t('The group name %name already exists.', array(
  130. '%group_name' => $form_values['group_name'])));
  131. }
  132. if (!preg_match('!^[a-z0-9_]+$!', $form_values['group_name'])) {
  133. form_set_error('group_name', t('The group name %name is invalid.', array(
  134. '%group_name' => $form_values['group_name'])));
  135. }
  136. }
  137. }
  138. function fieldgroup_edit_group_submit($form_id, &$form_values, $content_type, $action) {
  139. $groups = fieldgroup_groups($content_type['type']);
  140. $group = $groups[$form_values['group_name']];
  141. fieldgroup_save_group($content_type['type'], $form_values);
  142. cache_clear_all('fieldgroup_data', 'cache_content');
  143. return 'admin/content/types/'. $content_type['url_str'] .'/fields';
  144. }
  145. /*
  146. * Saves the given group for this content-type
  147. */
  148. function fieldgroup_save_group($type_name, $group) {
  149. $groups = fieldgroup_groups($type_name);
  150. $old_group = $groups[$group['group_name']];
  151. if (!$old_group) {
  152. // Accept group name from programmed submissions if valid.
  153. if (!empty($group['group_name'])) {
  154. $group_name = $group['group_name'];
  155. }
  156. else {
  157. // Otherwise, find a valid, computer-friendly name.
  158. $group_name = trim($group['label']);
  159. $group_name = drupal_strtolower($group_name);
  160. $group_name = str_replace(array(' ', '-'), '_', $group_name);
  161. $group_name = preg_replace('/[^a-z0-9_]/', '', $group_name);
  162. $group_name = 'group_'. $group_name;
  163. $group_name = substr($group_name, 0, 30);
  164. if (isset($groups[$group_name])) {
  165. $group_name_base = $group_name;
  166. $counter = 0;
  167. while (isset($groups[$group_name])) {
  168. $group_name = $group_name_base .'_'. $counter++;
  169. }
  170. }
  171. }
  172. db_query("INSERT INTO {node_group} (type_name, group_name, label, settings, weight)
  173. VALUES ('%s', '%s', '%s', '%s', %d)", $type_name, $group_name, $group['label'], serialize($group['settings']), $group['weight']);
  174. return SAVED_NEW;
  175. }
  176. else {
  177. db_query("UPDATE {node_group} SET label = '%s', settings = '%s', weight = %d ".
  178. "WHERE type_name = '%s' AND group_name = '%s'",
  179. $group['label'], serialize($group['settings']), $group['weight'], $type_name, $group['group_name']);
  180. return SAVED_UPDATED;
  181. }
  182. }
  183. function fieldgroup_remove_group($content_type, $group_name) {
  184. $groups = fieldgroup_groups($content_type['type']);
  185. $group = $groups[$group_name];
  186. if (!$group) {
  187. drupal_not_found();
  188. exit;
  189. }
  190. $form['#submit'] = array(fieldgroup_remove_group_submit => array($content_type, $group_name));
  191. return confirm_form($form,
  192. t('Are you sure you want to remove the group %label?',
  193. array('%label' => t($group['label']))),
  194. 'admin/content/types/'. $content_type['url_str'] .'/fields', t('This action cannot be undone.'),
  195. t('Remove'), t('Cancel'));
  196. }
  197. function fieldgroup_remove_group_submit($form_id, &$form_values, $content_type, $group_name) {
  198. db_query("DELETE FROM {node_group} WHERE type_name = '%s' AND group_name = '%s'", $content_type['type'], $group_name);
  199. db_query("DELETE FROM {node_group_fields} WHERE type_name = '%s' AND group_name = '%s'", $content_type['type'], $group_name);
  200. cache_clear_all('fieldgroup_data', 'cache_content');
  201. drupal_set_message(t('The group %group_name has been removed.', array('%group_name' => $group_name)));
  202. return 'admin/content/types/'. $content_type['url_str'] .'/fields';
  203. }
  204. /*
  205. * Returns all groups for a content type
  206. */
  207. function fieldgroup_groups($content_type = '', $sorted = FALSE, $reset = FALSE) {
  208. static $groups, $groups_sorted;
  209. if (!isset($groups) || $reset) {
  210. if ($cached = cache_get('fieldgroup_data', 'cache_content')) {
  211. $data = unserialize($cached->data);
  212. $groups = $data['groups'];
  213. $groups_sorted = $data['groups_sorted'];
  214. }
  215. else {
  216. $result = db_query("SELECT * FROM {node_group} ORDER BY weight, group_name");
  217. $groups = array();
  218. $groups_sorted = array();
  219. while ($group = db_fetch_array($result)) {
  220. $group['settings'] = unserialize($group['settings']);
  221. $group['fields'] = array();
  222. $groups[$group['type_name']][$group['group_name']] = $group;
  223. $groups_sorted[$group['type_name']][] = &$groups[$group['type_name']][$group['group_name']];
  224. }
  225. //load fields
  226. $result = db_query("SELECT nfi.*, ng.group_name FROM {node_group} ng ".
  227. "INNER JOIN {node_group_fields} ngf ON ngf.type_name = ng.type_name AND ngf.group_name = ng.group_name ".
  228. "INNER JOIN {node_field_instance} nfi ON nfi.field_name = ngf.field_name AND nfi.type_name = ngf.type_name ".
  229. "ORDER BY nfi.weight");
  230. while ($field = db_fetch_array($result)) {
  231. $groups[$field['type_name']][$field['group_name']]['fields'][$field['field_name']] = $field;
  232. }
  233. cache_set('fieldgroup_data', 'cache_content', serialize(array('groups' => $groups, 'groups_sorted' => $groups_sorted)));
  234. }
  235. }
  236. if (empty($content_type)) {
  237. return $groups;
  238. }
  239. elseif (!$groups[$content_type]) {
  240. return array();
  241. }
  242. return $sorted ? $groups_sorted[$content_type] : $groups[$content_type];
  243. }
  244. function _fieldgroup_groups_label($content_type) {
  245. $groups = fieldgroup_groups($content_type);
  246. $labels[0] = t('No group');
  247. foreach ($groups as $group_name => $group) {
  248. $labels[$group_name] = t($group['label']);
  249. }
  250. return $labels;
  251. }
  252. function _fieldgroup_field_get_group($content_type, $field_name) {
  253. return db_result(db_query("SELECT group_name FROM {node_group_fields} WHERE type_name = '%s' AND field_name = '%s'", $content_type, $field_name));
  254. }
  255. /*
  256. * Implementation of hook_form_alter()
  257. */
  258. function fieldgroup_form_alter($form_id, &$form) {
  259. if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
  260. foreach (fieldgroup_groups($form['type']['#value']) as $group_name => $group) {
  261. $form[$group_name] = array(
  262. '#type' => 'fieldset',
  263. '#title' => check_plain(t($group['label'])),
  264. '#collapsed' => $group['settings']['form']['style'] == 'fieldset_collapsed',
  265. '#collapsible' => in_array($group['settings']['form']['style'], array('fieldset_collapsed', 'fieldset_collapsible')),
  266. '#weight' => $group['weight'],
  267. '#description' => content_filter_xss(t($group['settings']['form']['description'])),
  268. '#attributes' => array('class' => strtr($group['group_name'], '_', '-')),
  269. );
  270. foreach ($group['fields'] as $field_name => $field) {
  271. if (isset($form[$field_name])) {
  272. $form[$group_name][$field_name] = $form[$field_name];
  273. unset($form[$field_name]);
  274. }
  275. }
  276. if (!empty($group['fields']) && !element_children($form[$group_name])) {
  277. //hide the fieldgroup, because the fields are hidden too
  278. unset($form[$group_name]);
  279. }
  280. }
  281. }
  282. else if ($form_id == '_content_admin_field') {
  283. $content_type = content_types($form['type_name']['#value']);
  284. $form['widget']['group'] = array(
  285. '#type' => 'select',
  286. '#title' => t('Display in group'),
  287. '#options' => _fieldgroup_groups_label($content_type['type']),
  288. '#default_value' => _fieldgroup_field_get_group($content_type['type'], $form['field_name']['#value']),
  289. '#description' => t('Select a group, in which the field will be displayed on the editing form.'),
  290. '#weight' => 5,
  291. );
  292. $form['widget']['weight']['#weight'] = 5;
  293. $form['widget']['description']['#weight'] = 7;
  294. $form['#submit']['fieldgroup_content_admin_form_submit'] = array($form['widget']['group']['#default_value']);
  295. }
  296. else if ($form_id == 'content_admin_display_overview_form') {
  297. $form['groups'] = fieldgroup_display_overview_form($form['type_name']['#value']);
  298. $form['groups']['#theme'] = 'fieldgroup_display_overview_form';
  299. $form['#submit']['fieldgroup_display_overview_form_submit'] = array();
  300. if (!is_array($form['submit']) && count(fieldgroup_groups($form['type_name']['#value']))) {
  301. $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'), '#weight' => 10);
  302. }
  303. }
  304. else if ($form_id == '_content_admin_field_remove') {
  305. $form['#submit']['fieldgroup_content_admin_field_remove_submit'] = array();
  306. }
  307. }
  308. function fieldgroup_content_admin_form_submit($form_id, &$form_values, $default) {
  309. if ($default != $form_values['group']) {
  310. if ($form_values['group'] && !$default) {
  311. db_query("INSERT INTO {node_group_fields} (type_name, group_name, field_name) VALUES ('%s', '%s', '%s')",
  312. $form_values['type_name'], $form_values['group'], $form_values['field_name']);
  313. }
  314. else if ($form_values['group']) {
  315. db_query("UPDATE {node_group_fields} SET group_name = '%s' WHERE type_name = '%s' AND field_name = '%s'",
  316. $form_values['group'], $form_values['type_name'], $form_values['field_name']);
  317. }
  318. else {
  319. db_query("DELETE FROM {node_group_fields} WHERE type_name = '%s' AND field_name = '%s'", $form_values['type_name'], $form_values['field_name']);
  320. }
  321. cache_clear_all('fieldgroup_data', 'cache_content');
  322. }
  323. }
  324. function fieldgroup_content_admin_field_remove_submit($form_id, &$form_values) {
  325. db_query("DELETE FROM {node_group_fields} WHERE type_name = '%s' AND field_name = '%s'", $form_values['type_name'], $form_values['field_name']);
  326. }
  327. function fieldgroup_nodeapi(&$node, $op, $teaser, $page) {
  328. switch ($op) {
  329. case 'view':
  330. case 'print':
  331. $context = $teaser ? 'teaser' : 'full';
  332. foreach (fieldgroup_groups($node->type) as $group_name => $group) {
  333. $label = $group['settings']['display']['label'] == 'above';
  334. $element = array(
  335. '#title' => $label ? check_plain(t($group['label'])) : '',
  336. '#description' => $label ? content_filter_xss(t($group['settings']['display']['description'])) : '',
  337. '#weight' => $group['weight'],
  338. '#attributes' => array('class' => 'fieldgroup '. strtr($group['group_name'], '_', '-')),
  339. );
  340. switch ($group['settings']['display'][$context]) {
  341. case 'simple':
  342. $element['#type'] = 'fieldgroup_simple';
  343. break;
  344. case 'hidden':
  345. $element['#access'] = FALSE;
  346. break;
  347. case 'fieldset_collapsed':
  348. $element['#collapsed'] = TRUE;
  349. case 'fieldset_collapsible':
  350. $element['#collapsible'] = TRUE;
  351. case 'fieldset':
  352. $element['#type'] = 'fieldset';
  353. break;
  354. }
  355. $node->content[$group_name] = $element;
  356. $visible = FALSE;
  357. foreach ($group['fields'] as $field_name => $field) {
  358. if (isset($node->content[$field_name])) {
  359. $node->content[$group_name][$field_name] = $node->content[$field_name];
  360. if ($node->content[$field_name]['#access'] && !empty($node->content[$field_name]['#value'])) {
  361. $visible = TRUE;
  362. }
  363. unset($node->content[$field_name]);
  364. }
  365. }
  366. if (!$visible) {
  367. // Hide the group, because the fields are empty or not accessible.
  368. unset($node->content[$group_name]);
  369. }
  370. }
  371. break;
  372. }
  373. }
  374. /*
  375. * Themes a simple fieldgroup on node view
  376. */
  377. function theme_fieldgroup_simple($element) {
  378. $output = '<div'. drupal_attributes($element['#attributes']) .'>';
  379. if ($element['#title']) {
  380. $output .= '<h2>'. $element['#title'] .'</h2>';
  381. $output .= ($element['#description'] ? '<div class="description">'. $element['#description'] .'</div>' : '');
  382. }
  383. $output .= '<div class="content">'. $element['#children'] .'</div>';
  384. $output .= '</div>';
  385. return $output;
  386. }
  387. /*
  388. * Gets the group name for a field
  389. * If the field isn't in a group, FALSE will be returned.
  390. * @return The name of the group, or FALSE.
  391. */
  392. function fieldgroup_get_group($content_type, $field_name) {
  393. foreach (fieldgroup_groups($content_type) as $group_name => $group) {
  394. if (in_array($field_name, array_keys($group['fields']))) {
  395. return $group_name;
  396. }
  397. }
  398. return FALSE;
  399. }
  400. /**
  401. * Implementation of hook_node_type()
  402. * React to change in node types
  403. */
  404. function fieldgroup_node_type($op, $info) {
  405. if ($op == 'update' && $info->type != $info->old_type) {
  406. // update the tables
  407. db_query("UPDATE {node_group} SET type_name='%s' WHERE type_name='%s'", array($info->type, $info->old_type));
  408. db_query("UPDATE {node_group_fields} SET type_name='%s' WHERE type_name='%s'", array($info->type, $info->old_type));
  409. cache_clear_all('fieldgroup_data', 'cache_content');
  410. }
  411. else if ($op == 'delete') {
  412. db_query("DELETE FROM {node_group_fields} WHERE type_name = '%s'", $info->type);
  413. db_query("DELETE FROM {node_group} WHERE type_name = '%s'", $info->type);
  414. }
  415. }
  416. /*
  417. * Adds a own table to the "display fields" tab
  418. */
  419. function fieldgroup_display_overview_form($content_type) {
  420. $groups = fieldgroup_groups($content_type);
  421. $form = array();
  422. $form['#tree'] = TRUE;
  423. foreach ($groups as $group_name => $group) {
  424. $form[$group_name] = fieldgroup_display_overview_form_row($group);
  425. }
  426. return $form;
  427. }
  428. function fieldgroup_display_overview_form_row($group) {
  429. $defaults = &$group['settings']['display']; //shortcut
  430. $label_options = array(
  431. 'above' => t('Above'),
  432. 'hidden' => t('<Hidden>'),
  433. );
  434. $options = array(
  435. 'no_style' => t('no styling'),
  436. 'simple' => t('simple'),
  437. 'fieldset' => t('fieldset'),
  438. 'fieldset_collapsible' => t('fieldset - collapsible'),
  439. 'fieldset_collapsed' => t('fieldset - collapsed'),
  440. 'hidden' => t('<Hidden>'),
  441. );
  442. $row = array();
  443. $row['group_label'] = array('#value' => check_plain($group['label']));
  444. $row['label'] = array(
  445. '#type' => 'select',
  446. '#options' => $label_options,
  447. '#default_value' => isset($defaults['label']) ? $defaults['label'] : 'above',
  448. );
  449. foreach (_content_admin_display_contexts() as $key => $title) {
  450. $row[$key] = array(
  451. '#type' => 'select',
  452. '#options' => $options,
  453. '#default_value' => isset($defaults[$key]) ? $defaults[$key] : 'fieldset',
  454. );
  455. }
  456. return $row;
  457. }
  458. /*
  459. * Themes the group part of the form as table
  460. */
  461. function theme_fieldgroup_display_overview_form($form) {
  462. $header = array(t('Group'), t('Label'));
  463. foreach (_content_admin_display_contexts() as $key => $title) {
  464. $header[] = $title;
  465. }
  466. $rows = array();
  467. foreach (element_children($form) as $group) {
  468. $row = array();
  469. foreach (element_children($form[$group]) as $key) {
  470. $row[] = drupal_render($form[$group][$key]);
  471. }
  472. $rows[] = $row;
  473. }
  474. $output = '';
  475. if (!empty($rows)) {
  476. $output = theme('table', $header, $rows, array('class' => 'content-group-display-overview'));
  477. }
  478. $output .= drupal_render($form);
  479. return $output;
  480. }
  481. function fieldgroup_display_overview_form_submit($form_id, $form_values) {
  482. $type = $form_values['type_name'];
  483. $groups = fieldgroup_groups($type);
  484. if (isset($form_values['groups'])) {
  485. foreach ($form_values['groups'] as $group_name => $groupvalues) {
  486. $group = &$groups[$group_name];
  487. foreach ($groupvalues as $key => $value) {
  488. $group['settings']['display'][$key] = $value;
  489. }
  490. fieldgroup_save_group($type, $group);
  491. }
  492. cache_clear_all('fieldgroup_data', 'cache_content');
  493. }
  494. }
  495. /**
  496. * Implementation of hook_panels_content_types()
  497. */
  498. function fieldgroup_panels_content_types() {
  499. $items = array();
  500. $items['content_fieldgroup'] = array(
  501. 'title' => t('Content fieldgroup'),
  502. 'content_types' => 'fieldgroup_panels_fieldgroup_content_types',
  503. 'single' => TRUE, // only provides a single content type
  504. 'render callback' => 'fieldgroup_panels_render_fieldgroup',
  505. 'add callback' => 'fieldgroup_panels_edit_fieldgroup',
  506. 'edit callback' => 'fieldgroup_panels_edit_fieldgroup',
  507. 'title callback' => 'fieldgroup_panels_fieldgroup_title',
  508. );
  509. return $items;
  510. }
  511. /**
  512. * 'Render' callback for the 'fieldgroup' panel.
  513. */
  514. function fieldgroup_panels_render_fieldgroup($conf, $panel_args, $context) {
  515. $node = isset($context->data) ? drupal_clone($context->data) : NULL;
  516. $block = new stdClass();
  517. $block->module = 'fieldgroup';
  518. if ($node) {
  519. // Assemble the fields into groups
  520. $node = node_build_content($node);
  521. // Get the "machine name" of the group from the options
  522. $groupname = $conf['group'];
  523. // Print out the contents of the given group
  524. // Note, not using drupal_render($node->content[$groupname]) here to avoid printing the fieldset
  525. $vars = array();
  526. if (is_array($node->content[$groupname])) {
  527. foreach (element_children($node->content[$groupname]) as $key) {
  528. $vars[$key] = drupal_render($node->content[$groupname][$key]);
  529. }
  530. }
  531. $block->subject = $node->content[$groupname]['#title'];
  532. $block->content = $vars ? theme('fieldgroup_panel', $vars, $node->nid) : $conf['empty'];
  533. $block->delta = $node->nid;
  534. }
  535. else {
  536. $block->subject = $conf['group'];
  537. $block->content = t('Content fieldgroup content goes here.');
  538. $block->delta = 'unknown';
  539. }
  540. return $block;
  541. }
  542. /**
  543. * Allows users to theme the panels group.
  544. */
  545. function theme_fieldgroup_panel($vars, $nid) {
  546. return implode('', $vars);
  547. }
  548. /**
  549. * Return all fieldgroup panel types available.
  550. */
  551. function fieldgroup_panels_fieldgroup_content_types() {
  552. return array(
  553. 'description' => array(
  554. 'title' => t('Content fieldgroup'),
  555. 'icon' => 'icon_node.png',
  556. 'path' => panels_get_path('content_types/node'),
  557. 'description' => t('All fields from a fieldgroup on the referenced node.'),
  558. 'required context' => new panels_required_context(t('Node'), 'node'),
  559. 'category' => array(t('Node context'), -9),
  560. ),
  561. );
  562. }
  563. /**
  564. * 'Edit' callback for the 'fieldgroup' panel.
  565. */
  566. function fieldgroup_panels_edit_fieldgroup($id, $parents, $conf = array()) {
  567. // Apply defaults
  568. if (empty($conf)) {
  569. $conf = array('title' => '', 'group' => '', 'empty' => '');
  570. }
  571. // Retrieve the list of all groups on all content types
  572. $group_list = array();
  573. $types = fieldgroup_groups(NULL, FALSE, FALSE);
  574. // Add each group to the list with the content type it is from in parentheses
  575. foreach ($types as $type) {
  576. foreach ($type as $group) {
  577. $group_list[$group['group_name']] = $group['label'] . ' (' . $group['type_name'] . ')';
  578. }
  579. }
  580. $form['type_name'] = array(
  581. '#type' => 'value',
  582. '#value' => $group['type_name'],
  583. );
  584. $form['group'] = array(
  585. '#type' => 'select',
  586. '#title' => t('Fieldgroup'),
  587. '#options' => $group_list,
  588. '#default_value' => $conf['group'],
  589. '#prefix' => '<div class="clear-block no-float">',
  590. '#suffix' => '</div>',
  591. );
  592. $form['empty'] = array(
  593. '#type' => 'textarea',
  594. '#title' => 'Empty text',
  595. '#description' => t('Text to display if group has no data. Note that title will not display unless overridden.'),
  596. '#rows' => 5,
  597. '#default_value' => $conf['empty'],
  598. '#prefix' => '<div class="clear-block no-float">',
  599. '#suffix' => '</div>',
  600. );
  601. return $form;
  602. }
  603. /**
  604. * 'Title' callback for the 'fieldgroup' panel.
  605. */
  606. function fieldgroup_panels_fieldgroup_title($conf, $context) {
  607. $types = fieldgroup_groups(NULL, FALSE, FALSE);
  608. $type = $types[$conf['type_name']][$conf['group']];
  609. return t('"@s" fieldgroup @name', array('@s' => $context->identifier, '@name' => $type['label']));
  610. }