field_group.field_ui.inc 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. <?php
  2. /**
  3. * @file
  4. * Field_group.field_ui.inc is a file that contains most functions
  5. * needed on the Fields UI Manage forms (display and fields).
  6. */
  7. /**
  8. * Helper function to get the form parameters to use while
  9. * building the fields and display overview form.
  10. */
  11. function field_group_field_ui_form_params($form, $display_overview) {
  12. $params = new stdClass();
  13. $params->entity_type = $form['#entity_type'];
  14. $params->bundle = $form['#bundle'];
  15. $params->admin_path = _field_ui_bundle_admin_path($params->entity_type, $params->bundle);
  16. $params->display_overview = $display_overview;
  17. if ($display_overview) {
  18. $params->region_callback = 'field_group_display_overview_row_region';
  19. $params->mode = $form['#view_mode'];
  20. }
  21. else {
  22. $params->region_callback = 'field_group_field_overview_row_region';
  23. $params->mode = 'form';
  24. }
  25. $params->groups = field_group_info_groups($params->entity_type, $params->bundle, $params->mode, TRUE);
  26. // Gather parenting data.
  27. $params->parents = array();
  28. foreach ($params->groups as $name => $group) {
  29. foreach ($group->children as $child) {
  30. $params->parents[$child] = $name;
  31. }
  32. }
  33. return $params;
  34. }
  35. /**
  36. * Function to alter the fields overview and display overview screen.
  37. */
  38. function field_group_field_ui_overview_form_alter(&$form, &$form_state, $display_overview = FALSE) {
  39. // Only start altering the form if we need to.
  40. if ($display_overview && empty($form['#fields']) && empty($form['#extra'])) {
  41. return;
  42. }
  43. $params = field_group_field_ui_form_params($form, $display_overview);
  44. // Add some things to be able to preserve synced usage of field_ui.
  45. if (!$display_overview) {
  46. // This key is used to store the current updated field.
  47. $form_state += array(
  48. 'formatter_settings_edit' => NULL,
  49. );
  50. // Add AJAX wrapper.
  51. $form['fields']['#prefix'] = '<div id="field-display-overview-wrapper">';
  52. $form['fields']['#suffix'] = '</div>';
  53. }
  54. $form['#groups'] = array_keys($params->groups);
  55. $table = &$form['fields'];
  56. // Add a region for 'add_new' rows, but only when fields are
  57. // available and thus regions.
  58. if (isset($table['#regions'])) {
  59. $table['#regions'] += array(
  60. 'add_new' => array('title' => '&nbsp;'),
  61. );
  62. }
  63. // Extend available parenting options.
  64. foreach ($params->groups as $name => $group) {
  65. $table['#parent_options'][$name] = $group->label;
  66. }
  67. $table['#parent_options']['_add_new_group'] = t('Add new group');
  68. // Update existing rows accordingly to the parents.
  69. foreach (element_children($table) as $name) {
  70. $table[$name]['parent_wrapper']['parent']['#options'] = $table['#parent_options'];
  71. // Inherit the value of the parent when default value is empty.
  72. if (empty($table[$name]['parent_wrapper']['parent']['#default_value'])) {
  73. $table[$name]['parent_wrapper']['parent']['#default_value'] = isset($params->parents[$name]) ? $params->parents[$name] : '';
  74. }
  75. }
  76. $formatter_options = field_group_field_formatter_options($display_overview ? 'display' : 'form');
  77. $refresh_rows = isset($form_state['values']['refresh_rows']) ? $form_state['values']['refresh_rows'] : (isset($form_state['input']['refresh_rows']) ? $form_state['input']['refresh_rows'] : NULL);
  78. // Create the group rows and check actions.
  79. foreach (array_keys($params->groups) as $name) {
  80. // Play around with form_state so we only need to hold things
  81. // between requests, until the save button was hit.
  82. if (isset($form_state['field_group'][$name])) {
  83. $group = &$form_state['field_group'][$name];
  84. }
  85. else {
  86. $group = &$params->groups[$name];
  87. }
  88. // Check the currently selected formatter, and merge persisted values for
  89. // formatter settings for the group.
  90. // This needs to be done first, so all fields are updated before creating form elements.
  91. if (isset($refresh_rows) && $refresh_rows == $name) {
  92. $settings = isset($form_state['values']['fields'][$name]) ? $form_state['values']['fields'][$name] : (isset($form_state['input']['fields'][$name]) ? $form_state['input']['fields'][$name] : NULL);
  93. if (array_key_exists('settings_edit', $settings)) {
  94. //$group->format_type = $form_state['field_group'][$name]->format_type;
  95. $group = $form_state['field_group'][$name];
  96. }
  97. field_group_formatter_row_update($group, $settings);
  98. }
  99. // Save the group when the configuration is submitted.
  100. if (!empty($form_state['values'][$name . '_formatter_settings_update'])) {
  101. field_group_formatter_settings_update($group, $form_state['values']['fields'][$name]);
  102. }
  103. // After all updates are finished, let the form_state know.
  104. $form_state['field_group'][$name] = $group;
  105. $settings = field_group_format_settings_form($group);
  106. $id = strtr($name, '_', '-');
  107. $js_rows_data[$id] = array('type' => 'group', 'name' => $name);
  108. // A group cannot be selected as its own parent.
  109. $parent_options = $table['#parent_options'];
  110. unset($parent_options[$name]);
  111. $table[$name] = array(
  112. '#attributes' => array('class' => array('draggable', 'field-group'), 'id' => $id),
  113. '#row_type' => 'group',
  114. '#region_callback' => $params->region_callback,
  115. '#js_settings' => array('rowHandler' => 'group'),
  116. 'human_name' => array(
  117. '#markup' => check_plain(t($group->label)),
  118. '#prefix' => '<span class="group-label">',
  119. '#suffix' => '</span>',
  120. ),
  121. 'weight' => array(
  122. '#type' => 'textfield',
  123. '#default_value' => $group->weight,
  124. '#size' => 3,
  125. '#attributes' => array('class' => array('field-weight')),
  126. ),
  127. 'parent_wrapper' => array(
  128. 'parent' => array(
  129. '#type' => 'select',
  130. '#options' => $parent_options,
  131. '#empty_value' => '',
  132. '#default_value' => isset($params->parents[$name]) ? $params->parents[$name] : '',
  133. '#attributes' => array('class' => array('field-parent')),
  134. '#parents' => array('fields', $name, 'parent'),
  135. ),
  136. 'hidden_name' => array(
  137. '#type' => 'hidden',
  138. '#default_value' => $name,
  139. '#attributes' => array('class' => array('field-name')),
  140. ),
  141. ),
  142. );
  143. $table[$name] += array(
  144. 'group_name' => array(
  145. '#markup' => check_plain($name),
  146. ),
  147. 'format' => array(
  148. 'type' => array(
  149. '#type' => 'select',
  150. '#options' => $formatter_options,
  151. '#default_value' => $group->format_type,
  152. '#attributes' => array('class' => array('field-group-type')),
  153. ),
  154. ),
  155. );
  156. $base_button = array(
  157. '#submit' => array('field_ui_display_overview_multistep_submit'),
  158. '#ajax' => array(
  159. 'callback' => 'field_ui_display_overview_multistep_js',
  160. 'wrapper' => 'field-display-overview-wrapper',
  161. 'effect' => 'fade',
  162. ),
  163. '#field_name' => $name,
  164. );
  165. if ($form_state['formatter_settings_edit'] == $name) {
  166. $table[$name]['format']['#cell_attributes'] = array('colspan' => $display_overview ? 3 : 3);
  167. $table[$name]['format']['format_settings'] = array(
  168. '#type' => 'container',
  169. '#attributes' => array('class' => array('field-formatter-settings-edit-form')),
  170. '#parents' => array('fields', $name, 'format_settings'),
  171. '#weight' => -5,
  172. 'label' => array(
  173. '#markup' => t('Field group format:') . ' <span class="formatter-name">' . $group->format_type . '</span>',
  174. ),
  175. // Create a settings form where hooks can pick in.
  176. 'settings' => $settings,
  177. 'actions' => array(
  178. '#type' => 'actions',
  179. 'save_settings' => $base_button + array(
  180. '#type' => 'submit',
  181. '#name' => $name . '_formatter_settings_update',
  182. '#value' => t('Update'),
  183. '#op' => 'update',
  184. ),
  185. 'cancel_settings' => $base_button + array(
  186. '#type' => 'submit',
  187. '#name' => $name . '_formatter_settings_cancel',
  188. '#value' => t('Cancel'),
  189. '#op' => 'cancel',
  190. // Do not check errors for the 'Cancel' button.
  191. '#limit_validation_errors' => array(),
  192. ),
  193. ),
  194. );
  195. $table[$name]['#attributes']['class'][] = 'field-formatter-settings-editing';
  196. $table[$name]['format']['type']['#attributes']['class'] = array('element-invisible');
  197. }
  198. else {
  199. // After saving, the settings are updated here as well. First we create
  200. // the element for the table cell.
  201. $table[$name]['settings_summary'] = array('#markup' => '');
  202. if (!empty($group->format_settings)) {
  203. $table[$name]['settings_summary'] = field_group_format_settings_summary($name, $group);
  204. }
  205. // Add the configure button.
  206. $table[$name]['settings_edit'] = $base_button + array(
  207. '#type' => 'image_button',
  208. '#name' => $name . '_group_settings_edit',
  209. '#src' => 'misc/configure.png',
  210. '#attributes' => array('class' => array('field-formatter-settings-edit'), 'alt' => t('Edit')),
  211. '#op' => 'edit',
  212. // Do not check errors for the 'Edit' button.
  213. '#limit_validation_errors' => array(),
  214. '#prefix' => '<div class="field-formatter-settings-edit-wrapper">',
  215. '#suffix' => '</div>',
  216. );
  217. if ($display_overview) {
  218. $table[$name]['settings_edit']['#suffix'] .= l(t('delete'), $params->admin_path . '/groups/' . $name . '/delete/' . $params->mode);
  219. }
  220. }
  221. if (!$display_overview) {
  222. $table[$name] += array(
  223. 'delete' => array(
  224. '#markup' => l(t('delete'), $params->admin_path . '/groups/' . $name . '/delete/form'),
  225. ),
  226. );
  227. }
  228. }
  229. // Additional row: add new group.
  230. $parent_options = $table['#parent_options'];
  231. unset($parent_options['_add_new_group']);
  232. $table['_add_new_group'] = field_group_add_row('_add_new_group', $parent_options, $params);
  233. $table['_add_new_group'] += array(
  234. 'format' => array(
  235. 'type' => array(
  236. '#type' => 'select',
  237. '#options' => $formatter_options,
  238. '#default_value' => 'fieldset',
  239. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  240. ),
  241. '#cell_attributes' => array('colspan' => 3),
  242. ),
  243. );
  244. if (!$display_overview) {
  245. // See field_ui.admin.inc for more details on refresh rows.
  246. $form['refresh_rows'] = array('#type' => 'hidden');
  247. $form['refresh'] = array(
  248. '#type' => 'submit',
  249. '#value' => t('Refresh'),
  250. '#op' => 'refresh_table',
  251. '#submit' => array('field_ui_display_overview_multistep_submit'),
  252. '#ajax' => array(
  253. 'callback' => 'field_ui_display_overview_multistep_js',
  254. 'wrapper' => 'field-display-overview-wrapper',
  255. 'effect' => 'fade',
  256. // The button stays hidden, so we hide the AJAX spinner too. Ad-hoc
  257. // spinners will be added manually by the client-side script.
  258. 'progress' => 'none',
  259. ),
  260. );
  261. }
  262. $form['#attached']['css'][] = drupal_get_path('module', 'field_group') . '/field_group.field_ui.css';
  263. $form['#attached']['js'][] = drupal_get_path('module', 'field_group') . '/field_group.field_ui.js';
  264. $form['#validate'][] = 'field_group_field_overview_validate';
  265. $form['#submit'][] = 'field_group_field_overview_submit';
  266. // Create the settings for fieldgroup as vertical tabs (merged with DS).
  267. field_group_field_ui_create_vertical_tabs($form, $form_state, $params);
  268. // Show a warning if the user has not set up required containers
  269. if ($form['#groups']) {
  270. $parent_requirements = array(
  271. 'multipage' => array(
  272. 'parent' => 'multipage-group',
  273. 'message' => 'Each Multipage element needs to have a parent Multipage group element.',
  274. ),
  275. 'htab' => array(
  276. 'parent' => 'htabs',
  277. 'message' => 'Each Horizontal tab element needs to have a parent Horizontal tabs group element.',
  278. ),
  279. 'accordion-item' => array(
  280. 'parent' => 'accordion',
  281. 'message' => 'Each Accordion item element needs to have a parent Accordion group element.',
  282. ),
  283. );
  284. // On display overview tabs need to be checked.
  285. if ($display_overview) {
  286. $parent_requirements['tab'] = array(
  287. 'parent' => 'tabs',
  288. 'message' => 'Each Vertical tab element needs to have a parent Vertical tabs group element.',
  289. );
  290. }
  291. foreach ($form['#groups'] as $group_name) {
  292. $group_check = field_group_load_field_group($group_name, $params->entity_type, $params->bundle, $params->mode);
  293. if (isset($parent_requirements[$group_check->format_type])) {
  294. if (!$group_check->parent_name || field_group_load_field_group($group_check->parent_name, $params->entity_type, $params->bundle, $params->mode)->format_type != $parent_requirements[$group_check->format_type]['parent']) {
  295. drupal_set_message(t($parent_requirements[$group_check->format_type]['message']), 'warning', FALSE);
  296. }
  297. }
  298. }
  299. }
  300. }
  301. /**
  302. * Return an array of field_group_formatter options.
  303. */
  304. function field_group_field_formatter_options($type) {
  305. $options = &drupal_static(__FUNCTION__);
  306. if (!isset($options)) {
  307. $options = array();
  308. $field_group_types = field_group_formatter_info();
  309. foreach ($field_group_types[$type] as $name => $field_group_type) {
  310. $options[$name] = $field_group_type['label'];
  311. }
  312. }
  313. return $options;
  314. }
  315. /**
  316. * Helper function to add a row in the overview forms.
  317. */
  318. function field_group_add_row($name, $parent_options, $params) {
  319. return array(
  320. '#attributes' => array('class' => array('draggable', 'field-group', 'add-new')),
  321. '#row_type' => 'add_new_group',
  322. '#js_settings' => array('rowHandler' => 'group'),
  323. '#region_callback' => $params->region_callback,
  324. 'label' => array(
  325. '#title_display' => 'invisible',
  326. '#title' => t('Label for new group'),
  327. '#type' => 'textfield',
  328. '#size' => 15,
  329. '#description' => t('Label'),
  330. '#prefix' => '<div class="label-input"><div class="add-new-placeholder">' . t('Add new group') . '</div>',
  331. '#suffix' => '</div>',
  332. ),
  333. 'weight' => array(
  334. '#type' => 'textfield',
  335. '#default_value' => field_info_max_weight($params->entity_type, $params->bundle, $params->mode) + 3,
  336. '#size' => 3,
  337. '#title_display' => 'invisible',
  338. '#title' => t('Weight for new group'),
  339. '#attributes' => array('class' => array('field-weight')),
  340. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  341. ),
  342. 'parent_wrapper' => array(
  343. 'parent' => array(
  344. '#title_display' => 'invisible',
  345. '#title' => t('Parent for new group'),
  346. '#type' => 'select',
  347. '#options' => $parent_options,
  348. '#empty_value' => '',
  349. '#attributes' => array('class' => array('field-parent')),
  350. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  351. '#parents' => array('fields', $name, 'parent'),
  352. ),
  353. 'hidden_name' => array(
  354. '#type' => 'hidden',
  355. '#default_value' => $name,
  356. '#attributes' => array('class' => array('field-name')),
  357. ),
  358. ),
  359. 'group_name' => array(
  360. '#type' => 'textfield',
  361. '#title_display' => 'invisible',
  362. '#title' => t('Machine name for new group'),
  363. // This field should stay LTR even for RTL languages.
  364. '#field_prefix' => '<span dir="ltr">group_',
  365. '#field_suffix' => '</span>&lrm;',
  366. '#attributes' => array('dir' => 'ltr'),
  367. '#size' => 15,
  368. '#description' => t('Group name (a-z, 0-9, _)'),
  369. '#prefix' => '<div class="add-new-placeholder">&nbsp;</div>',
  370. '#cell_attributes' => array('colspan' => $params->display_overview ? 1 : 2),
  371. ),
  372. );
  373. }
  374. /**
  375. * Creates a form for field_group formatters.
  376. * @param Object $group The FieldGroup object.
  377. */
  378. function field_group_format_settings_form(&$group) {
  379. $form = array();
  380. $form['label'] = array(
  381. '#type' => 'textfield',
  382. '#title' => t('Field group label'),
  383. '#default_value' => $group->label,
  384. '#weight' => -5,
  385. '#element_validate' => array('field_group_format_settings_label_validate'),
  386. );
  387. $addition = module_invoke_all('field_group_format_settings', $group);
  388. $form += $addition;
  389. // Give fieldgroup implementations the chance to alter the description.
  390. if (!empty($addition['label']['#description'])) {
  391. $form['label']['#description'] = $addition['label']['#description'];
  392. }
  393. $form['#validate'] = array('field_group_format_settings_form_validate');
  394. return $form;
  395. }
  396. /**
  397. * Validate the label. Label is required for fieldsets that are collapsible.
  398. */
  399. function field_group_format_settings_label_validate($element, &$form_state) {
  400. $group = $form_state['values']['fields'][$element['#parents'][1]];
  401. $settings = $group['format_settings']['settings'];
  402. $name = $form_state['formatter_settings_edit'];
  403. $form_state['values']['fields'][$name]['settings_edit_form']['settings'] = $settings;
  404. if ($group['format']['type'] == 'fieldset' && ($settings['formatter'] == 'collapsed' || $settings['formatter'] == 'collapsible') && empty($settings['label'])) {
  405. form_error($element, t('The label is required when formatter is collapsible or collapsed'));
  406. }
  407. }
  408. /**
  409. * Update the row so that the group variables are updated.
  410. * The rendering of the elements needs the updated defaults.
  411. * @param Object $group
  412. * @param array $settings
  413. */
  414. function field_group_formatter_row_update(&$group, $settings) {
  415. // if the row has changed formatter type, update the group object
  416. if (!empty($settings['format']['type']) && $settings['format']['type'] != $group->format_type) {
  417. $group->format_type = $settings['format']['type'];
  418. field_group_formatter_settings_update($group, $settings);
  419. }
  420. }
  421. /**
  422. * Update handler for field_group configuration settings.
  423. * @param Object $group The group object
  424. * @param Array $settings Configuration settings
  425. */
  426. function field_group_formatter_settings_update(&$group, $settings) {
  427. // for format changes we load the defaults.
  428. if (empty($settings['format_settings']['settings'])) {
  429. $mode = $group->mode == 'form' ? 'form' : 'display';
  430. $group->format_settings = _field_group_get_default_formatter_settings($group->format_type, $mode);
  431. }
  432. else {
  433. $group->format_type = $settings['format']['type'];
  434. $group->label = $settings['format_settings']['settings']['label'];
  435. $group->format_settings = $settings['format_settings']['settings'];
  436. }
  437. }
  438. /**
  439. * Creates a summary for the field format configuration summary.
  440. * @param String $group_name The name of the group
  441. * @param Object $group The group object
  442. * @return Array ready to be rendered.
  443. */
  444. function field_group_format_settings_summary($group_name, $group) {
  445. $summary = implode('<br />', module_invoke_all('field_group_format_summary', $group));
  446. return array(
  447. '#markup' => '<div class="field-formatter-summary">' . $summary . '</div>',
  448. '#cell_attributes' => array('class' => array('field-formatter-summary-cell')),
  449. );
  450. }
  451. /**
  452. * Returns the region to which a row in the 'Manage fields' screen belongs.
  453. * @param Array $row A field or field_group row
  454. * @return String the current region.
  455. */
  456. function field_group_field_overview_row_region($row) {
  457. switch ($row['#row_type']) {
  458. case 'group':
  459. return 'main';
  460. case 'add_new_group':
  461. // If no input in 'label', assume the row has not been dragged out of the
  462. // 'add new' section.
  463. if (empty($row['label']['#value'])) {
  464. return 'add_new';
  465. }
  466. return 'main';
  467. }
  468. }
  469. /**
  470. * Returns the region to which a row in the 'Manage display' screen belongs.
  471. * @param Array $row A field or field_group row
  472. * @return String the current region.
  473. */
  474. function field_group_display_overview_row_region($row) {
  475. switch ($row['#row_type']) {
  476. case 'group':
  477. return ($row['format']['type']['#value'] == 'hidden' ? 'hidden' : 'visible');
  478. case 'add_new_group':
  479. // If no input in 'label', assume the row has not been dragged out of the
  480. // 'add new' section.
  481. if (empty($row['label']['#value'])) {
  482. return 'add_new';
  483. }
  484. return ($row['format']['type']['#value'] == 'hidden' ? 'hidden' : 'visible');
  485. }
  486. }
  487. /**
  488. * Validate handler for the overview screens.
  489. * @param Array $form The complete form.
  490. * @param Array $form_state The state of the form.
  491. */
  492. function field_group_field_overview_validate($form, &$form_state) {
  493. $form_values = $form_state['values']['fields'];
  494. $entity_type = $form['#entity_type'];
  495. $bundle = $form['#bundle'];
  496. $mode = (isset($form['#view_mode']) ? $form['#view_mode'] : 'form');
  497. $group = $form_values['_add_new_group'];
  498. // Validate if any information was provided in the 'add new group' row.
  499. if (array_filter(array($group['label'], $group['group_name']))) {
  500. // Missing group name.
  501. if (!$group['group_name']) {
  502. form_set_error('fields][_add_new_group][group_name', t('Add new group: you need to provide a group name.'));
  503. }
  504. // Group name validation.
  505. else {
  506. $group_name = $group['group_name'];
  507. // Add the 'group_' prefix.
  508. if (drupal_substr($group_name, 0, 6) != 'group_') {
  509. $group_name = 'group_' . $group_name;
  510. form_set_value($form['fields']['_add_new_group']['group_name'], $group_name, $form_state);
  511. }
  512. // Invalid group name.
  513. if (!preg_match('!^group_[a-z0-9_]+$!', $group_name)) {
  514. form_set_error('fields][_add_new_group][group_name', t('Add new group: the group name %group_name is invalid. The name must include only lowercase unaccentuated letters, numbers, and underscores.', array('%group_name' => $group_name)));
  515. }
  516. if (drupal_strlen($group_name) > 32) {
  517. form_set_error('fields][_add_new_group][group_name', t("Add new group: the group name %group_name is too long. The name is limited to 32 characters, including the 'group_' prefix.", array('%group_name' => $group_name)));
  518. }
  519. // Group name already exists.
  520. if (field_group_exists($group_name, $entity_type, $bundle, $mode)) {
  521. form_set_error('fields][_add_new_group][group_name', t('Add new group: the group name %group_name already exists.', array('%group_name' => $group_name)));
  522. }
  523. }
  524. }
  525. }
  526. /**
  527. * Submit handler for the overview screens.
  528. * @param Array $form The complete form.
  529. * @param Array $form_state The state of the form.
  530. */
  531. function field_group_field_overview_submit($form, &$form_state) {
  532. $form_values = $form_state['values']['fields'];
  533. $entity_type = $form['#entity_type'];
  534. $bundle = $form['#bundle'];
  535. $mode = (isset($form['#view_mode']) ? $form['#view_mode'] : 'form');
  536. // Collect children.
  537. $children = array_fill_keys($form['#groups'], array());
  538. foreach ($form_values as $name => $value) {
  539. if (!empty($value['parent'])) {
  540. // Substitute newly added fields, in case they were dragged
  541. // directly in a group.
  542. if ($name == '_add_new_field' && isset($form_state['fields_added']['_add_new_field'])) {
  543. $name = $form_state['fields_added']['_add_new_field'];
  544. }
  545. elseif ($name == '_add_existing_field' && isset($form_state['fields_added']['_add_existing_field'])) {
  546. $name = $form_state['fields_added']['_add_existing_field'];
  547. }
  548. $children[$value['parent']][$name] = $name;
  549. }
  550. }
  551. // Prepare storage with ctools.
  552. ctools_include('export');
  553. // Create new group.
  554. if (!empty($form_values['_add_new_group']['group_name'])) {
  555. $values = $form_values['_add_new_group'];
  556. $field_group_types = field_group_formatter_info();
  557. $formatter = $field_group_types[($mode == 'form' ? 'form' : 'display')][$values['format']['type']];
  558. $new_group = (object) array(
  559. 'identifier' => $values['group_name'] . '|' . $entity_type . '|' . $bundle . '|' . $mode,
  560. 'group_name' => $values['group_name'],
  561. 'entity_type' => $entity_type,
  562. 'bundle' => $bundle,
  563. 'mode' => $mode,
  564. 'children' => isset($children['_add_new_group']) ? array_keys($children['_add_new_group']) : array(),
  565. 'parent_name' => $values['parent'],
  566. 'weight' => $values['weight'],
  567. 'label' => $values['label'],
  568. 'format_type' => $values['format']['type'],
  569. 'disabled' => FALSE,
  570. );
  571. $new_group->format_settings = array('formatter' => isset($formatter['default_formatter']) ? $formatter['default_formatter'] : '');
  572. if (isset($formatter['instance_settings'])) {
  573. $new_group->format_settings['instance_settings'] = $formatter['instance_settings'];
  574. }
  575. $classes = _field_group_get_html_classes($new_group);
  576. $new_group->format_settings['instance_settings']['classes'] = implode(' ', $classes->optional);
  577. // Save and enable it in ctools.
  578. ctools_export_crud_save('field_group', $new_group);
  579. ctools_export_crud_enable('field_group', $new_group->identifier);
  580. // Store new group information for any additional submit handlers.
  581. $form_state['groups_added']['_add_new_group'] = $new_group->group_name;
  582. drupal_set_message(t('New group %label successfully created.', array('%label' => $new_group->label)));
  583. // Replace the newly created group in the $children array, in case it was
  584. // dragged directly in an existing field.
  585. foreach (array_keys($children) as $parent) {
  586. if (isset($children[$parent]['_add_new_group'])) {
  587. unset($children[$parent]['_add_new_group']);
  588. $children[$parent][$new_group->group_name] = $new_group->group_name;
  589. }
  590. }
  591. }
  592. // Update existing groups.
  593. $groups = field_group_info_groups($entity_type, $bundle, $mode, TRUE);
  594. foreach ($form['#groups'] as $group_name) {
  595. $group = $groups[$group_name];
  596. $group->label = $form_state['field_group'][$group_name]->label;
  597. $group->children = array_keys($children[$group_name]);
  598. $group->parent_name = $form_values[$group_name]['parent'];
  599. $group->weight = $form_values[$group_name]['weight'];
  600. $old_format_type = $group->format_type;
  601. $group->format_type = isset($form_values[$group_name]['format']['type']) ? $form_values[$group_name]['format']['type'] : 'visible';
  602. if (isset($form_state['field_group'][$group_name]->format_settings)) {
  603. $group->format_settings = $form_state['field_group'][$group_name]->format_settings;
  604. }
  605. // If the format type is changed, make sure we have all required format settings.
  606. if ($group->format_type != $old_format_type) {
  607. $mode = $group->mode == 'form' ? 'form' : 'display';
  608. $default_formatter_settings = _field_group_get_default_formatter_settings($group->format_type, $mode);
  609. $group->format_settings += $default_formatter_settings;
  610. $group->format_settings['instance_settings'] += $default_formatter_settings['instance_settings'];
  611. }
  612. ctools_export_crud_save('field_group', $group);
  613. }
  614. cache_clear_all('field_groups', 'cache_field');
  615. }
  616. /**
  617. * Validate the entered css class from the submitted format settings.
  618. * @param Array $element The validated element
  619. * @param Array $form_state The state of the form.
  620. */
  621. function field_group_validate_css_class($element, &$form_state) {
  622. if (!empty($form_state['values']['fields'][$form_state['formatter_settings_edit']]['format_settings']['settings']['instance_settings']['classes']) && !preg_match('!^[A-Za-z0-9-_ ]+$!', $form_state['values']['fields'][$form_state['formatter_settings_edit']]['format_settings']['settings']['instance_settings']['classes'])) {
  623. form_error($element, t('The css class must include only letters, numbers, underscores and dashes.'));
  624. }
  625. }
  626. /**
  627. * Validate the entered id attribute from the submitted format settings.
  628. * @param Array $element The validated element
  629. * @param Array $form_state The state of the form.
  630. */
  631. function field_group_validate_id($element, &$form_state) {
  632. if (!empty($form_state['values']['fields'][$form_state['formatter_settings_edit']]['format_settings']['settings']['instance_settings']['id']) && !preg_match('!^[A-Za-z0-9-_]+$!', $form_state['values']['fields'][$form_state['formatter_settings_edit']]['format_settings']['settings']['instance_settings']['id'])) {
  633. form_error($element, t('The id must include only letters, numbers, underscores and dashes.'));
  634. }
  635. }
  636. /**
  637. * Implements hook_field_info_max_weight().
  638. */
  639. function field_group_field_info_max_weight($entity_type, $bundle, $context) {
  640. $weights = array();
  641. foreach (field_group_info_groups($entity_type, $bundle, $context) as $group) {
  642. $weights[] = $group->weight;
  643. }
  644. return $weights ? max($weights) : NULL;
  645. }
  646. /**
  647. * Menu callback; present a form for removing a group.
  648. */
  649. function field_group_delete_form($form, &$form_state, $group, $view_mode = 'form') {
  650. $form['#group'] = $group;
  651. $admin_path = _field_ui_bundle_admin_path($group->entity_type, $group->bundle);
  652. if ($view_mode == 'form') {
  653. $admin_path .= '/fields';
  654. }
  655. else {
  656. $admin_path .= '/display/' . $view_mode;
  657. }
  658. $form['#redirect'] = array($admin_path);
  659. $output = confirm_form($form,
  660. t('Are you sure you want to delete the group %group?', array('%group' => t($group->label))),
  661. $admin_path,
  662. t('This action cannot be undone.'),
  663. t('Delete'), t('Cancel'),
  664. 'confirm'
  665. );
  666. return $output;
  667. }
  668. /**
  669. * Remove group from bundle.
  670. *
  671. * @todo we'll have to reset all view mode settings - that will be fun :)
  672. */
  673. function field_group_delete_form_submit($form, &$form_state) {
  674. $group = $form['#group'];
  675. $bundle = $group->bundle;
  676. $entity_type = $group->entity_type;
  677. $group->mode = $form_state['build_info']['args'][1];
  678. $bundles = field_info_bundles();
  679. $bundle_label = $bundles[$entity_type][$bundle]['label'];
  680. ctools_include('export');
  681. field_group_group_export_delete($group, FALSE);
  682. drupal_set_message(t('The group %group has been deleted from the %type content type.', array('%group' => t($group->label), '%type' => $bundle_label)));
  683. // Redirect.
  684. $form_state['redirect'] = $form['#redirect'];
  685. }
  686. /**
  687. * Menu callback; present a form for re-enabling a group.
  688. */
  689. function field_group_enable_form($form, &$form_state, $group, $view_mode = 'form') {
  690. $form['#group'] = $group;
  691. $admin_path = _field_ui_bundle_admin_path($group->entity_type, $group->bundle);
  692. if ($view_mode == 'form') {
  693. $admin_path .= '/fields';
  694. }
  695. else {
  696. $admin_path .= '/display/' . $view_mode;
  697. }
  698. $form['#redirect'] = array($admin_path);
  699. $output = confirm_form($form,
  700. t('Are you sure you want to enable the group %group?', array('%group' => t($group->label))),
  701. $admin_path,
  702. '',
  703. t('Enable'), t('Cancel'),
  704. 'confirm'
  705. );
  706. return $output;
  707. }
  708. /**
  709. * Re-enable the group on a bundle.
  710. */
  711. function field_group_enable_form_submit($form, &$form_state) {
  712. $group = $form['#group'];
  713. $bundle = $group->bundle;
  714. $entity_type = $group->entity_type;
  715. $group->mode = $form_state['build_info']['args'][1];
  716. $bundles = field_info_bundles();
  717. $bundle_label = $bundles[$entity_type][$bundle]['label'];
  718. ctools_include('export');
  719. ctools_export_crud_enable('field_group', $group->identifier);
  720. drupal_set_message(t('The group %group has been enabled on the %type content type.', array('%group' => t($group->label), '%type' => $bundle_label)));
  721. // Redirect.
  722. $form_state['redirect'] = $form['#redirect'];
  723. }
  724. /**
  725. * Create vertical tabs.
  726. */
  727. function field_group_field_ui_create_vertical_tabs(&$form, &$form_state, $params) {
  728. $form_state['field_group_params'] = $params;
  729. $entity_info = entity_get_info($params->entity_type);
  730. $view_modes = array();
  731. if ($params->mode != 'default') {
  732. $view_modes['default'] = t('Default');
  733. }
  734. if ($params->mode != 'form') {
  735. $view_modes['0'] = t('Form');
  736. }
  737. foreach ($entity_info['view modes'] as $view_mode => $data) {
  738. if ($data['custom settings'] && $params->mode != $view_mode) {
  739. $view_modes[$view_mode] = $data['label'];
  740. }
  741. }
  742. // Add additional settings vertical tab.
  743. if (!isset($form['additional_settings'])) {
  744. $form['additional_settings'] = array(
  745. '#type' => 'vertical_tabs',
  746. '#theme_wrappers' => array('vertical_tabs'),
  747. '#prefix' => '<div>',
  748. '#suffix' => '</div>',
  749. '#tree' => TRUE,
  750. );
  751. $form['#attached']['js'][] = 'misc/form.js';
  752. $form['#attached']['js'][] = 'misc/collapse.js';
  753. }
  754. // Add extra guidelines for webmaster.
  755. $form['additional_settings']['field_group'] = array(
  756. '#type' => 'fieldset',
  757. '#title' => t('Fieldgroups'),
  758. '#description' => t('<p class="fieldgroup-help">Fields can be dragged into groups with unlimited nesting. Each fieldgroup format comes with a configuration form, specific for that format type.<br />Note that some formats come in pair. These types have a html wrapper to nest its fieldgroup children. E.g. Place accordion items into the accordion, vertical tabs in vertical tab group and horizontal tabs in the horizontal tab group. There is one exception to this rule, you can use a vertical tab without a wrapper when the additional settings tabs are available. E.g. node forms.</p>'),
  759. '#collapsible' => TRUE,
  760. '#collapsed' => FALSE,
  761. '#parents' => array('additional_settings'),
  762. );
  763. $form['additional_settings']['field_group']['fieldgroup_clone'] = array(
  764. '#title' => t('Select source view mode or form'),
  765. '#description' => t('Clone fieldgroups from selected view mode to the current display'),
  766. '#type' => 'select',
  767. '#options' => $view_modes,
  768. '#default_value' => 'none'
  769. );
  770. $form['additional_settings']['field_group']['fieldgroup_submit'] = array(
  771. '#type' => 'submit',
  772. '#value' => t('Clone'),
  773. '#validate' => array('field_group_field_ui_clone_field_groups_validate'),
  774. '#submit' => array('field_group_field_ui_clone_field_groups')
  775. );
  776. $disabled_groups = field_group_read_groups(array(), FALSE);
  777. // Show disabled fieldgroups, and make it possible to enable them again.
  778. if ($disabled_groups && isset($disabled_groups[$params->entity_type][$params->bundle][$params->mode])) {
  779. $form['additional_settings']['disabled_field_groups'] = array(
  780. '#type' => 'fieldset',
  781. '#title' => t('Disabled fieldgroups'),
  782. '#collapsible' => TRUE,
  783. '#collapsed' => FALSE,
  784. '#parents' => array('additional_settings'),
  785. );
  786. $form['additional_settings']['disabled_field_groups']['overview'] = field_group_disabled_groups_overview($disabled_groups[$params->entity_type][$params->bundle][$params->mode], $entity_info, $params);
  787. }
  788. }
  789. /**
  790. * Validate handler to validate saving existing fieldgroups from one view mode or form to another.
  791. */
  792. function field_group_field_ui_clone_field_groups_validate($form, &$form_state) {
  793. $source_mode = $form_state['#source_mode'] = $form_state['values']['additional_settings']['fieldgroup_clone'] == '0' ? 'form' : $form_state['values']['additional_settings']['fieldgroup_clone'];
  794. $groups_to_clone = $form_state['#groups_to_clone'] = field_group_read_groups(array('bundle' => $form_state['field_group_params']->bundle, 'entity_type' => $form_state['field_group_params']->entity_type));
  795. $form_state['#source_groups'] = array();
  796. if (!empty($groups_to_clone) && isset($groups_to_clone[$form_state['field_group_params']->entity_type], $groups_to_clone[$form_state['field_group_params']->entity_type][$form_state['field_group_params']->bundle], $groups_to_clone[$form_state['field_group_params']->entity_type][$form_state['field_group_params']->bundle][$source_mode])) {
  797. $form_state['#source_groups'] = $groups_to_clone[$form_state['field_group_params']->entity_type][$form_state['field_group_params']->bundle][$source_mode];
  798. }
  799. // Check for types are not known in current mode.
  800. if ($form_state['field_group_params']->mode != 'form') {
  801. $non_existing_types = array('multipage', 'multipage-group');
  802. }
  803. else {
  804. $non_existing_types = array('div');
  805. }
  806. foreach ($form_state['#source_groups'] as $key => $group) {
  807. if (in_array($group->format_type, $non_existing_types)) {
  808. unset($form_state['#source_groups'][$key]);
  809. drupal_set_message(t('Skipping @group because this type does not exist in current mode', array('@group' => $group->label)), 'warning');
  810. }
  811. }
  812. if (empty($form_state['#source_groups'])) {
  813. // Report error found with selection.
  814. form_set_error('additional_settings][fieldgroup_clone', t('No field groups were found in selected view mode.'));
  815. return;
  816. }
  817. }
  818. /**
  819. * Submit handler to save existing fieldgroups from one view mode or form to another.
  820. */
  821. function field_group_field_ui_clone_field_groups($form, &$form_state) {
  822. $source_mode = $form_state['#source_mode'];
  823. $groups_to_clone = $form_state['#groups_to_clone'];
  824. $fields = array_keys($form_state['values']['fields']);
  825. if (!empty($form_state['#source_groups'])) {
  826. foreach ($form_state['#source_groups'] as $source_group) {
  827. if (in_array($source_group->group_name, $fields)) {
  828. drupal_set_message(t('Fieldgroup @group is not cloned since a group already exists with the same name.', array('@group' => $source_group->group_name)), 'warning');
  829. continue;
  830. }
  831. // Recreate the identifier and reset the id.
  832. $source_group->id = NULL;
  833. $source_group->mode = $form_state['field_group_params']->mode;
  834. $source_group->identifier = $source_group->group_name . '|' . $source_group->entity_type . '|' . $source_group->bundle . '|' . $form_state['field_group_params']->mode;
  835. $source_group->disabled = FALSE;
  836. $source_group->children = array();
  837. unset($source_group->export_type, $source_group->type, $source_group->table);
  838. // Save and enable it in ctools.
  839. ctools_include('export');
  840. ctools_export_crud_save('field_group', $source_group);
  841. ctools_export_crud_enable('field_group', $source_group->identifier);
  842. drupal_set_message(t('Fieldgroup @group cloned successfully.', array('@group' => $source_group->group_name)));
  843. }
  844. }
  845. }
  846. /**
  847. * Show an overview of all the disabled fieldgroups, and make it possible to activate them again.
  848. * @param $disabled_groups Array with all disabled groups.
  849. */
  850. function field_group_disabled_groups_overview($disabled_groups, $entity_info, $params) {
  851. $formatter_options = field_group_field_formatter_options($params->mode != 'form' ? 'display' : 'form');
  852. $table = array(
  853. '#theme' => 'table',
  854. '#header' => array(
  855. t('Label'),
  856. t('Machine name'),
  857. t('Field'),
  858. t('Widget'),
  859. t('Operations'),
  860. ),
  861. '#attributes' => array(
  862. 'class' => array('field-ui-overview'),
  863. ),
  864. '#rows' => array(),
  865. );
  866. // Add all of the disabled groups as a row on the table.
  867. foreach ($disabled_groups as $group) {
  868. $summary = field_group_format_settings_summary($group->group_name, $group);
  869. $row = array();
  870. $row[] = $group->label;
  871. $row[] = $group->group_name;
  872. $row[] = $formatter_options[$group->format_type];
  873. $row[] = render($summary);
  874. $path = (isset($entity_info['bundles'][$params->bundle]['admin']['real path']) ? $entity_info['bundles'][$params->bundle]['admin']['real path'] : $entity_info['bundles'][$params->bundle]['admin']['path']);
  875. $row[] = l(t('Enable'), $path . '/groups/' . $group->group_name . '/enable/' . $group->mode);
  876. $table['#rows'][] = $row;
  877. }
  878. return $table;
  879. }
  880. /**
  881. * eof().
  882. */