ds.field_ui.inc

  1. cis7 sites/all/modules/ulmus/ds/includes/ds.field_ui.inc
  2. cle7 sites/all/modules/ulmus/ds/includes/ds.field_ui.inc
  3. ecd7 sites/all/modules/ulmus/ds/includes/ds.field_ui.inc
  4. elmsmedia7 sites/all/modules/ulmus/ds/includes/ds.field_ui.inc
  5. harmony7 sites/all/modules/ulmus/ds/includes/ds.field_ui.inc
  6. icor7 sites/all/modules/ulmus/ds/includes/ds.field_ui.inc
  7. meedjum_blog7 sites/all/modules/ulmus/ds/includes/ds.field_ui.inc
  8. mooc7 sites/all/modules/ulmus/ds/includes/ds.field_ui.inc

Field UI functions for Display Suite.

Functions

Namesort descending Description
ds_ctools_content Return the configuration settings for the CTools field.
ds_ctools_content_next Handle the 'next' click on the add/edit field form wizard.
ds_ctools_content_select Select content.
ds_ctools_get_category Helper function to get the category.
ds_disable_layout_field_settings_form Menu callback: Disable layout and field settings form.
ds_disable_layout_field_settings_form_submit Submit callback: disable layout and field settings.
ds_ds_field_format_summary Implements hook_ds_field_format_summary().
ds_ds_field_settings_form Implements hook_ds_field_settings_form().
ds_enable_layout_field_settings_form Menu callback: Enable layout and field settings form.
ds_enable_layout_field_settings_form_submit Submit callback: enable layout and field settings.
ds_field_formatter_settings_form Implements hook_field_formatter_settings_form().
ds_field_formatter_settings_summary Implements hook_field_formatter_settings_summary().
ds_field_row_form_format_construct Helper function for building the formatter settings.
ds_field_row_form_format_summary_construct Helper function for formatter summary settings.
ds_field_settings_form Creates a form for Display Suite fields. .
ds_field_settings_summary Creates a summary for the field format configuration summary.
ds_field_ui_change_layout_submit Form submission handler for _ds_field_ui_table_layouts_preview().
ds_field_ui_change_layout_validate Form validation handler for _ds_field_ui_table_layouts_preview().
ds_field_ui_create_vertical_tabs Create vertical tabs.
ds_field_ui_fields_layouts Adds the Display Suite fields and layouts to the form.
ds_field_ui_fields_save Save the field settings from the 'Manage display' screen.
ds_field_ui_fields_validate Form validation handler for _ds_field_ui_fields().
ds_field_ui_layouts_save Save the layout settings from the 'Manage display' screen.
ds_field_ui_layouts_validate Move the view modes so Field UI can handle them.
ds_field_ui_layout_change Change a layout for a given entity.
ds_field_ui_layout_change_submit Submit callback: save the layout change.
ds_field_ui_layout_clone Clone a fields layout.
ds_field_ui_regions Add Regions to 'Manage fields' or 'Manage display' screen.
ds_field_ui_row_region Returns the region to which a row in the Field UI screen belongs.
ds_field_ui_table_layouts_preview_callback Ajax callback for _ds_field_ui_table_layouts_preview().
ds_get_entity_context Add entity contexts.
ds_revert_layout_field_settings_form Menu callback: Revert layout and field settings form.
ds_revert_layout_field_settings_form_submit Submit callback: revert layout and field settings.
_ds_classes Utility function to return CSS classes.
_ds_field_group_field_ui_fix_notices Add fake field group value in.
_ds_field_ui_check_hidden_region Helper function to check if we are in a hidden region or not.
_ds_field_ui_clone_view_mode_settings Populates display settings for a new view mode from the another view mode.
_ds_field_ui_core_fields Alter the core field on the the Field UI form.
_ds_field_ui_custom_fields Add tab for adding new fields on the fly.
_ds_field_ui_fields Add the fields to the Field UI form.
_ds_field_ui_table_layouts Add the layouts fieldset on the Field UI screen.
_ds_field_ui_table_layouts_preview Add the layout previews to the Field UI screen.
_ds_field_valid Utility function to check if we need to save anything for this field.
_ds_sort_fields Utility function to sort a multidimensional array by a value in a sub-array.

File

sites/all/modules/ulmus/ds/includes/ds.field_ui.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Field UI functions for Display Suite.
  5. */
  6. /**
  7. * Adds the Display Suite fields and layouts to the form.
  8. */
  9. function ds_field_ui_fields_layouts(&$form, &$form_state) {
  10. global $base_root, $base_path;
  11. // Get the entity_type, bundle and view mode.
  12. $entity_type = $form['#entity_type'];
  13. $bundle = $form['#bundle'];
  14. $view_mode = $form['#view_mode'];
  15. $form['#export_id'] = $entity_type . '|' . $bundle . '|' . $view_mode;
  16. // Create vertical tabs.
  17. ds_field_ui_create_vertical_tabs($form);
  18. // Add layout fieldset.
  19. _ds_field_ui_table_layouts($entity_type, $bundle, $view_mode, $form, $form_state);
  20. // Add/alter fields on the table, but only if a layout is selected.
  21. if ($view_mode != 'form' && !empty($form['#ds_layout'])) {
  22. _ds_field_ui_fields($entity_type, $bundle, $view_mode, $form, $form_state);
  23. // Also alter core fields
  24. _ds_field_ui_core_fields($entity_type, $bundle, $view_mode, $form, $form_state);
  25. }
  26. // Add buttons to add fields in overlay.
  27. if (isset($form['#ds_layout']) && user_access('admin_fields') && $view_mode != 'form' && module_exists('ds_ui')) {
  28. _ds_field_ui_custom_fields($entity_type, $bundle, $view_mode, $form, $form_state);
  29. }
  30. // Special validate function for field group.
  31. if (isset($form_state['no_field_group'])) {
  32. array_unshift($form['#validate'], '_ds_field_group_field_ui_fix_notices');
  33. }
  34. // Attach js.
  35. $form['#attached']['js'][] = drupal_get_path('module', 'ds') . '/js/ds.admin.js';
  36. // Attach css.
  37. $form['#attached']['css'][] = drupal_get_path('module', 'ds') . '/css/ds.admin.css';
  38. // Add process function to add the regions.
  39. $form['#process'][] = 'ds_field_ui_regions';
  40. // Add a destination so we can get back if layout has been changed.
  41. $form['ds_source'] = array(
  42. '#type' => 'hidden',
  43. '#value' => $base_root . $base_path,
  44. );
  45. $form['ds_destination'] = array(
  46. '#type' => 'hidden',
  47. '#value' => drupal_get_destination(),
  48. );
  49. $form['ds_entity_type'] = array(
  50. '#type' => 'hidden',
  51. '#value' => $entity_type,
  52. );
  53. $form['ds_bundle'] = array(
  54. '#type' => 'hidden',
  55. '#value' => $bundle,
  56. );
  57. $form['ds_view_mode'] = array(
  58. '#type' => 'hidden',
  59. '#value' => $view_mode,
  60. );
  61. }
  62. /**
  63. * Create vertical tabs.
  64. */
  65. function ds_field_ui_create_vertical_tabs(&$form) {
  66. // Add additional settings vertical tab.
  67. if (!isset($form['additional_settings'])) {
  68. $form['additional_settings'] = array(
  69. '#type' => 'vertical_tabs',
  70. '#theme_wrappers' => array('vertical_tabs'),
  71. '#prefix' => '<div>',
  72. '#suffix' => '</div>',
  73. '#tree' => TRUE,
  74. );
  75. $form['#attached']['js'][] = 'misc/form.js';
  76. $form['#attached']['js'][] = 'misc/collapse.js';
  77. }
  78. $view_mode_admin_access = user_access('admin_view_modes') && module_exists('ds_ui');
  79. if (isset($form['modes'])) {
  80. if ($view_mode_admin_access) {
  81. $form['modes']['view_modes_custom']['#description'] = l(t('Manage view modes'), 'admin/structure/ds/view_modes');
  82. }
  83. $form['additional_settings']['modes'] = $form['modes'];
  84. $form['additional_settings']['modes']['#weight'] = -10;
  85. unset($form['modes']);
  86. }
  87. else {
  88. if ($view_mode_admin_access) {
  89. $form['additional_settings']['modes']['view_modes_custom']['#description'] = l(t('Manage view modes'), 'admin/structure/ds/view_modes');
  90. }
  91. }
  92. }
  93. /**
  94. * Menu callback: Disable layout and field settings form.
  95. */
  96. function ds_disable_layout_field_settings_form($form, &$form_state, $id = '') {
  97. $layout = new stdClass();
  98. ctools_include('export');
  99. $ds_layout_settings = ctools_export_crud_load_all('ds_layout_settings');
  100. if (isset($ds_layout_settings[$id])) {
  101. $layout = $ds_layout_settings[$id];
  102. }
  103. if (isset($layout) && $layout->export_type != 1 && empty($layout->disable)) {
  104. $form['#layout'] = $layout;
  105. $form['#export_id'] = $id;
  106. return confirm_form($form,
  107. t('Are you sure you want to disable the layout and field settings for %layout?', array('%layout' => implode(', ', explode('|', $layout->id)))),
  108. drupal_get_destination(),
  109. t('This action cannot be undone.'),
  110. t('Disable'),
  111. t('Cancel')
  112. );
  113. }
  114. else {
  115. drupal_set_message(t('This operation is not possible.'));
  116. }
  117. }
  118. /**
  119. * Submit callback: disable layout and field settings.
  120. */
  121. function ds_disable_layout_field_settings_form_submit(&$form, &$form_state) {
  122. $layout = $form['#layout'];
  123. ctools_include('export');
  124. ctools_export_crud_disable('ds_layout_settings', $form['#export_id']);
  125. ctools_export_crud_disable('ds_field_settings', $form['#export_id']);
  126. // @todo layout fields
  127. // Clear the ds_fields cache.
  128. cache_clear_all('ds_fields:', 'cache', TRUE);
  129. cache_clear_all('ds_field_settings', 'cache');
  130. // Clear entity info cache.
  131. cache_clear_all('entity_info', 'cache', TRUE);
  132. drupal_set_message(t('Layout has been disabled.'));
  133. $form_state['redirect'] = isset($_GET['destination']) ? $_GET['destination'] : drupal_get_destination();
  134. }
  135. /**
  136. * Menu callback: Enable layout and field settings form.
  137. */
  138. function ds_enable_layout_field_settings_form($form, &$form_state, $id = '') {
  139. $layout = new stdClass();
  140. ctools_include('export');
  141. $ds_layout_settings = ctools_export_crud_load_all('ds_layout_settings');
  142. if (isset($ds_layout_settings[$id])) {
  143. $layout = $ds_layout_settings[$id];
  144. }
  145. if (isset($layout) && $layout->export_type != 1 && !empty($layout->disabled)) {
  146. $form['#layout'] = $layout;
  147. $form['#export_id'] = $id;
  148. return confirm_form($form,
  149. t('Are you sure you want to enable the layout and field settings for %layout?', array('%layout' => implode(', ', explode('|', $layout->id)))),
  150. drupal_get_destination(),
  151. t('This action cannot be undone.'),
  152. t('Enable'),
  153. t('Cancel')
  154. );
  155. }
  156. else {
  157. drupal_set_message(t('This operation is not possible.'));
  158. }
  159. }
  160. /**
  161. * Submit callback: enable layout and field settings.
  162. */
  163. function ds_enable_layout_field_settings_form_submit(&$form, &$form_state) {
  164. $layout = $form['#layout'];
  165. ctools_include('export');
  166. ctools_export_crud_enable('ds_layout_settings', $form['#export_id']);
  167. ctools_export_crud_enable('ds_field_settings', $form['#export_id']);
  168. // Clear the ds_fields cache.
  169. cache_clear_all('ds_fields:', 'cache', TRUE);
  170. cache_clear_all('ds_field_settings', 'cache');
  171. // Clear entity info cache.
  172. cache_clear_all('entity_info', 'cache', TRUE);
  173. drupal_set_message(t('Layout has been enabled'));
  174. $form_state['redirect'] = isset($_GET['destination']) ? $_GET['destination'] : drupal_get_destination();
  175. }
  176. /**
  177. * Menu callback: Revert layout and field settings form.
  178. */
  179. function ds_revert_layout_field_settings_form($form, &$form_state, $id = '') {
  180. $layout = new stdClass();
  181. ctools_include('export');
  182. $ds_layout_settings = ctools_export_crud_load_all('ds_layout_settings');
  183. if (isset($ds_layout_settings[$id])) {
  184. $layout = $ds_layout_settings[$id];
  185. }
  186. if (isset($layout) && $layout->export_type == 3) {
  187. $form['#layout'] = $layout;
  188. return confirm_form($form,
  189. t('Are you sure you want to revert the layout for %layout?', array('%layout' => implode(', ', explode('|', $layout->id)))),
  190. drupal_get_destination(),
  191. t('This action cannot be undone.'),
  192. t('Revert'),
  193. t('Cancel')
  194. );
  195. }
  196. else {
  197. drupal_set_message(t('This operation is not possible.'));
  198. }
  199. }
  200. /**
  201. * Submit callback: revert layout and field settings.
  202. */
  203. function ds_revert_layout_field_settings_form_submit(&$form, &$form_state) {
  204. $layout = $form['#layout'];
  205. db_delete('ds_field_settings')
  206. ->condition('id', $layout->id)
  207. ->execute();
  208. db_delete('ds_layout_settings')
  209. ->condition('id', $layout->id)
  210. ->execute();
  211. // Clear the ds_fields cache.
  212. cache_clear_all('ds_fields:', 'cache', TRUE);
  213. cache_clear_all('ds_field_settings', 'cache');
  214. // Clear entity info cache.
  215. cache_clear_all('entity_info', 'cache', TRUE);
  216. drupal_set_message(t('Layout has been reverted'));
  217. $form_state['redirect'] = isset($_GET['destination']) ? $_GET['destination'] : drupal_get_destination();
  218. }
  219. /**
  220. * Add Regions to 'Manage fields' or 'Manage display' screen.
  221. *
  222. * @param $form
  223. * The form to add layout fieldset and extra Display Suite fields.
  224. * @param $form_state
  225. * The current form state.
  226. */
  227. function ds_field_ui_regions($form, $form_state) {
  228. // Get the entity_type, bundle and view mode.
  229. $entity_type = $form['#entity_type'];
  230. $bundle = $form['#bundle'];
  231. $view_mode = $form['#view_mode'];
  232. // Ignore fieldgroup options.
  233. if (isset($form_state['no_field_group'])) {
  234. unset($form['fields']['_add_new_group']);
  235. $form['additional_settings']['field_group']['#access'] = FALSE;
  236. }
  237. // Check layout.
  238. $layout = isset($form['#ds_layout']) ? $form['#ds_layout'] : FALSE;
  239. // Change UI to add Region column if we have a layout.
  240. if ($layout) {
  241. $table = &$form['fields'];
  242. if ($view_mode != 'form') {
  243. $table['#header'] = array(
  244. t('Field'),
  245. t('Weight'),
  246. t('Parent'),
  247. t('Region'),
  248. t('Label'),
  249. array('data' => t('Format'), 'colspan' => 3),
  250. );
  251. }
  252. else {
  253. $table['#header'] = array(
  254. t('Label'),
  255. t('Weight'),
  256. t('Parent'),
  257. t('Region'),
  258. t('Name'),
  259. t('Field'),
  260. t('Widget'),
  261. array('data' => t('Operations'), 'colspan' => 2),
  262. );
  263. }
  264. // Remove label and format for views.
  265. if ($entity_type == 'ds_views') {
  266. $table['#header'][4] = '';
  267. }
  268. $table['#regions'] = array();
  269. foreach ($layout->regions as $region_key => $region_title) {
  270. $region_options[$region_key] = $region_title;
  271. $table['#regions'][$region_key] = array(
  272. 'title' => $region_title,
  273. 'message' => t('No fields are displayed in this region'),
  274. );
  275. }
  276. // Let other modules alter the regions.
  277. $context = array(
  278. 'entity_type' => $entity_type,
  279. 'bundle' => $bundle,
  280. 'view_mode' => $view_mode
  281. );
  282. $region_info = array(
  283. 'region_options' => &$region_options,
  284. 'table_regions' => &$table['#regions'],
  285. );
  286. drupal_alter('ds_layout_region', $context, $region_info);
  287. $region_options['hidden'] = $view_mode != 'form' ? t('Disabled') : t('Hidden');
  288. $table['#regions']['hidden'] = array(
  289. 'title' => $view_mode != 'form' ? t('Disabled') : t('Hidden'),
  290. 'message' => t('No fields are hidden.')
  291. );
  292. $region = array(
  293. '#type' => 'select',
  294. '#options' => $region_options,
  295. '#default_value' => 'hidden',
  296. '#attributes' => array(
  297. 'class' => array('ds-field-region'),
  298. )
  299. );
  300. $limit_items = array(
  301. '#type' => 'textfield',
  302. '#size' => 2,
  303. '#default_value' => '',
  304. '#weight' => 10,
  305. '#default_value' => '#',
  306. '#prefix' => '<div class="limit-float">',
  307. '#suffix' => '</div><div class="clearfix"></div>',
  308. '#attributes' => array(
  309. 'alt' => t('Enter a number to limit the number of items or \'delta\' to print a specific delta (usually configured in views or found in entity->ds_delta). Leave empty to display them all. Note that depending on the formatter settings, this option might not always work.'),
  310. 'title' => t('Enter a number to limit the number of items or \'delta\' to print a specific delta (usually configured in views or found in entity->ds_delta). Leave empty to display them all. Note that depending on the formatter settings, this option might not always work.'),
  311. ),
  312. );
  313. // Hide this if we formatter_settings_edit is not empty so it doesn't confuse users.
  314. if (!empty($form_state['formatter_settings_edit'])) {
  315. $limit_items['#access'] = FALSE;
  316. }
  317. // Update existing rows by changing rowHandler and adding regions.
  318. foreach (element_children($table) as $name) {
  319. $row = &$table[$name];
  320. $row['#js_settings'] = array('rowHandler' => 'ds');
  321. $row['#region_callback'] = 'ds_field_ui_row_region';
  322. // Remove hidden format.
  323. if (isset($row['format']['type']['#options']['hidden'])) {
  324. unset($row['format']['type']['#options']['hidden']);
  325. }
  326. // Add label class.
  327. if (isset($row['label'])) {
  328. if (isset($form_state['formatter_settings']) && isset($form_state['formatter_settings'][$name]['ft'])) {
  329. if (!empty($form_state['formatter_settings'][$name]['ft']['lb'])) {
  330. $row['human_name']['#markup'] = check_plain($form_state['formatter_settings'][$name]['ft']['lb']) . ' ' . t('(Original: !orig)', array('!orig' => $row['human_name']['#markup']));
  331. }
  332. }
  333. }
  334. // Limit items.
  335. $field_info = field_info_field($name);
  336. if (isset($field_info['cardinality']) && $field_info['cardinality'] != 1 && $view_mode != 'form') {
  337. $row['format']['type']['#prefix'] = '<div class="limit-float">';
  338. $row['format']['type']['#suffix'] = '</div>';
  339. $row['format']['limit'] = $limit_items;
  340. $row['format']['limit']['#default_value'] = (isset($layout->settings['limit']) && isset($layout->settings['limit'][$name])) ? $layout->settings['limit'][$name] : '#';
  341. }
  342. // Disable label and format for views.
  343. if ($entity_type == 'ds_views') {
  344. $row['label']['#access'] = FALSE;
  345. }
  346. // Add region.
  347. $split = ($view_mode != 'form') ? 7 : 6;
  348. if ($row['#row_type'] == 'group' && $view_mode == 'form') {
  349. $split = ($view_mode != 'form') ? 8 : 7;
  350. }
  351. $second = array_splice($row, $split);
  352. $row['region'] = $region;
  353. $row['region']['#default_value'] = (isset($layout->settings['fields'][$name]) && isset($region_options[$layout->settings['fields'][$name]])) ? $layout->settings['fields'][$name] : 'hidden';
  354. $row = array_merge($row, $second);
  355. }
  356. }
  357. return $form;
  358. }
  359. /**
  360. * Returns the region to which a row in the Field UI screen belongs.
  361. *
  362. * @param $row
  363. * The current row that is being rendered in the Field UI screen.
  364. */
  365. function ds_field_ui_row_region($row) {
  366. return isset($row['region']['#value']) ? $row['region']['#value'] : 'hidden';
  367. }
  368. /**
  369. * Move the view modes so Field UI can handle them.
  370. */
  371. function ds_field_ui_layouts_validate($form, &$form_state) {
  372. if (isset($form_state['values']['additional_settings']['modes']['view_modes_custom'])) {
  373. $form_state['values']['view_modes_custom'] = $form_state['values']['additional_settings']['modes']['view_modes_custom'];
  374. }
  375. }
  376. /**
  377. * Change a layout for a given entity.
  378. *
  379. * @param $entity_type
  380. * The name of the entity.
  381. * @param $bundle
  382. * The name of the bundle.
  383. * @param $view_mode
  384. * The name of the view mode.
  385. */
  386. function ds_field_ui_layout_change($form, $form_state, $entity_type = '', $bundle = '', $view_mode = '', $new_layout = '') {
  387. $old_layout = NULL;
  388. $all_layouts = ds_get_layout_info();
  389. if (!empty($entity_type) && !empty($bundle) && !empty($view_mode)) {
  390. $old_layout = ds_get_layout($entity_type, $bundle, $view_mode, FALSE);
  391. }
  392. if ($old_layout && isset($all_layouts[$new_layout])) {
  393. $new_layout_key = $new_layout;
  394. $new_layout = $all_layouts[$new_layout];
  395. $form['#entity_type'] = $entity_type;
  396. $form['#bundle'] = $bundle;
  397. $form['#view_mode'] = $view_mode;
  398. $form['#old_layout'] = $old_layout;
  399. $form['#new_layout'] = $new_layout;
  400. $form['#new_layout_key'] = $new_layout_key;
  401. $form['#export_id'] = $entity_type . '|' . $bundle . '|' . $view_mode;
  402. $form['info'] = array(
  403. '#markup' => t('You are changing from %old to %new layout for !bundle in !view_mode view mode.', array('%old' => $old_layout['label'], '%new' => $new_layout['label'], '!bundle' => $bundle, '!view_mode' => $view_mode)),
  404. '#prefix' => "<div class='change_ds_layout_info'>",
  405. '#suffix' => "</div>",
  406. );
  407. // Old region options.
  408. $regions = array();
  409. foreach ($old_layout['regions'] as $key => $title) {
  410. $regions[$key] = $title;
  411. }
  412. // Let other modules alter the regions.
  413. // For old regions.
  414. $context = array(
  415. 'entity_type' => $entity_type,
  416. 'bundle' => $bundle,
  417. 'view_mode' => $view_mode,
  418. );
  419. $region_info = array(
  420. 'region_options' => $regions,
  421. );
  422. drupal_alter('ds_layout_region', $context, $region_info);
  423. $regions = $region_info['region_options'];
  424. $form['#old_layout']['regions'] = $regions;
  425. // For new regions.
  426. $region_info = array(
  427. 'region_options' => $new_layout['regions'],
  428. );
  429. drupal_alter('ds_layout_region', $context, $region_info);
  430. $new_layout['regions'] = $region_info['region_options'];
  431. $form['#new_layout']['regions'] = $new_layout['regions'];
  432. // Display the region options
  433. $selectable_regions = array('' => t('- None -')) + $new_layout['regions'];
  434. $form['regions_pre']['#markup'] = '<div class="ds-layout-regions">';
  435. foreach ($regions as $region => $region_title) {
  436. $form['region_' . $region] = array(
  437. '#type' => 'container',
  438. );
  439. $form['region_' . $region]['ds_label_' . $region] = array(
  440. '#markup' => 'Fields in <span class="change_ds_layout_old_region"> ' . $region_title . '</span> go into',
  441. );
  442. $form['region_' . $region]['ds_' . $region] = array(
  443. '#type' => 'select',
  444. '#options' => $layout_options = $selectable_regions,
  445. '#default_value' => $region,
  446. );
  447. }
  448. $form['regions_post']['#markup'] = '</div>';
  449. // Show previews from old and new layouts
  450. $form['preview'] = array(
  451. '#type' => 'container',
  452. '#prefix' => '<div class="ds-layout-preview"/>',
  453. '#suffix' => '</div>',
  454. );
  455. $fallback_image = drupal_get_path('module', 'ds') . '/images/preview.png';
  456. $old_image = (isset($old_layout['image']) && !empty($old_layout['image'])) ? $old_layout['path'] . '/' . $old_layout['layout'] . '.png' : $fallback_image;
  457. if (isset($old_layout['panels']) && !empty($old_layout['panels']['icon'])) {
  458. $old_image = $old_layout['panels']['path'] . '/' . $old_layout['panels']['icon'];
  459. }
  460. $new_image = (isset($new_layout['image']) && !empty($new_layout['image'])) ? $new_layout['path'] . '/' . $new_layout_key . '.png' : $fallback_image;
  461. if (isset($new_layout['panels']) && !empty($new_layout['panels']['icon'])) {
  462. $new_image = $new_layout['panels']['path'] . '/' . $new_layout['panels']['icon'];
  463. }
  464. $arrow = drupal_get_path('module', 'ds') . '/images/arrow.png';
  465. $form['preview']['old_layout'] = array(
  466. '#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $old_image . '"/></div>',
  467. );
  468. $form['preview']['arrow'] = array(
  469. '#markup' => '<div class="ds-layout-preview-arrow"><img src="' . base_path() . $arrow . '"/></div>',
  470. );
  471. $form['preview']['new_layout'] = array(
  472. '#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $new_image . '"/></div>',
  473. );
  474. $form['#attached']['css'][] = drupal_get_path('module', 'ds') . '/css/ds.admin.css';
  475. // Submit button
  476. $form['actions'] = array('#type' => 'actions');
  477. $form['actions']['submit'] = array(
  478. '#type' => 'submit',
  479. '#value' => t('Save'),
  480. '#prefix' => '<div class="ds-layout-change-save">',
  481. '#suffix' => '</div>',
  482. );
  483. }
  484. else {
  485. $form['nothing'] = array('#markup' => t('No valid configuration found.'));
  486. }
  487. return $form;
  488. }
  489. /**
  490. * Submit callback: save the layout change.
  491. */
  492. function ds_field_ui_layout_change_submit($form, &$form_state) {
  493. // Prepare some variables.
  494. $old_layout = $form['#old_layout'];
  495. $new_layout = $form['#new_layout'];
  496. $new_layout_key = $form['#new_layout_key'];
  497. $entity_type = $form['#entity_type'];
  498. $bundle = $form['#bundle'];
  499. $view_mode = $form['#view_mode'];
  500. // Create new record.
  501. $record = new stdClass();
  502. $record->id = $form['#export_id'];
  503. $record->entity_type = $entity_type;
  504. $record->bundle = $bundle;
  505. $record->view_mode = $view_mode;
  506. $record->layout = $new_layout_key;
  507. $record->settings = $old_layout['settings'];
  508. unset($record->settings['regions']);
  509. unset($record->settings['fields']);
  510. // map old regions to new ones
  511. foreach ($old_layout['regions'] as $region => $region_title) {
  512. $new_region = $form_state['values']['ds_' . $region];
  513. if ($new_region != '' && isset($old_layout['settings']['regions'][$region])) {
  514. foreach ($old_layout['settings']['regions'][$region] as $field_key => $field) {
  515. if (!isset($record->settings['regions'][$new_region])) {
  516. $record->settings['regions'][$new_region] = array();
  517. }
  518. $record->settings['regions'][$new_region][] = $field;
  519. $record->settings['fields'][$field] = $new_region;
  520. }
  521. }
  522. }
  523. // Remove old record.
  524. db_delete('ds_layout_settings')
  525. ->condition('entity_type', $entity_type)
  526. ->condition('bundle', $bundle)
  527. ->condition('view_mode', $view_mode)
  528. ->execute();
  529. // Save new record.
  530. drupal_write_record('ds_layout_settings', $record);
  531. // Clear entity info cache.
  532. cache_clear_all('entity_info', 'cache', TRUE);
  533. // Show message.
  534. drupal_set_message(t('The layout change has been saved.'));
  535. }
  536. /**
  537. * Save the layout settings from the 'Manage display' screen.
  538. */
  539. function ds_field_ui_layouts_save($form, &$form_state) {
  540. $weight = 0;
  541. // Get default values.
  542. $entity_type = $form['#entity_type'];
  543. $bundle = $form['#bundle'];
  544. $view_mode = $form['#view_mode'];
  545. // Determine layout variables.
  546. $layout = $form_state['values']['additional_settings']['layout'];
  547. $old_layout = $form_state['values']['additional_settings']['old_layout'];
  548. $new_layout = ($layout != $old_layout) || empty($old_layout);
  549. // Save layout and add regions if necessary.
  550. $record = new stdClass;
  551. $record->id = $form['#export_id'];
  552. $record->entity_type = $entity_type;
  553. $record->bundle = $bundle;
  554. $record->view_mode = $view_mode;
  555. $record->layout = $layout;
  556. $record->settings = array();
  557. $form_state['layout_saved'] = FALSE;
  558. // Remove old layout if necessary.
  559. if ($new_layout && !empty($old_layout)) {
  560. db_delete('ds_layout_settings')
  561. ->condition('entity_type', $entity_type)
  562. ->condition('bundle', $bundle)
  563. ->condition('view_mode', $view_mode)
  564. ->execute();
  565. }
  566. if ($new_layout && !empty($layout)) {
  567. $form_state['layout_saved'] = TRUE;
  568. // Save new layout.
  569. $record->settings = $record->settings;
  570. // Let other modules alter the layout settings.
  571. drupal_alter('ds_layout_settings', $record, $form_state);
  572. // Move current visible fields into a default region, so
  573. // we keep their current settings.
  574. $layouts = ds_get_layout_info();
  575. $sl = $layouts[$layout];
  576. $first_region = key($sl['regions']);
  577. $record->settings['regions'] = array();
  578. $record->settings['fields'] = array();
  579. $record->settings['classes'] = array();
  580. $record->settings['wrappers'] = array();
  581. $record->settings['layout_wrapper'] = 'div';
  582. $record->settings['layout_attributes'] = '';
  583. $record->settings['layout_attributes_merge'] = variable_get('ds_layout_attributes_merge', TRUE);
  584. $record->settings['layout_link_attribute'] = FALSE;
  585. $record->settings['layout_link_custom'] = '';
  586. $fields = _ds_sort_fields($form_state['values']['fields'], 'weight');
  587. foreach ($fields as $field_key => $field) {
  588. // Ignore new fieldgroup, new field or existing field.
  589. if (in_array($field_key, array('_add_new_field', '_add_existing_field', '_add_new_group'))) {
  590. continue;
  591. }
  592. // Can either be form or display.
  593. if ((isset($field['type']) && $field['type'] != 'hidden') || $record->view_mode == 'form') {
  594. $record->settings['regions'][$first_region][$weight++] = $field_key;
  595. $record->settings['fields'][$field_key] = $first_region;
  596. }
  597. }
  598. // In case this is the full node view mode and if the comment module
  599. // is enabled for this content type, add it as well.
  600. if ($record->entity_type == 'node' && $record->view_mode == 'full' && module_exists('comment')) {
  601. $record->settings['regions'][$first_region][] = 'comments';
  602. $record->settings['fields']['comments'] = $first_region;
  603. }
  604. // Save the record.
  605. drupal_write_record('ds_layout_settings', $record);
  606. }
  607. // Update existing layout.
  608. elseif (!empty($layout)) {
  609. $form_state['layout_saved'] = TRUE;
  610. $fields = _ds_sort_fields($form_state['values']['fields'], 'weight');
  611. foreach ($fields as $key => $field) {
  612. // Make sure we need to save anything for this field.
  613. if (_ds_field_valid($key, $field, $form_state, $view_mode)) {
  614. continue;
  615. }
  616. if (!isset($record->settings['regions'][$field['region']])) {
  617. $record->settings['regions'][$field['region']] = array();
  618. }
  619. $record->settings['regions'][$field['region']][$weight++] = $key;
  620. $record->settings['fields'][$key] = $field['region'];
  621. // Save limit.
  622. $limit = isset($field['format']['limit']) ? trim($field['format']['limit']) : '';
  623. if (is_numeric($limit) || $limit === 'delta') {
  624. $record->settings['limit'][$key] = $limit;
  625. }
  626. }
  627. // Save the region classes.
  628. $record->settings['classes'] = array();
  629. foreach (array_keys($form['fields']['#regions']) as $region) {
  630. // Ignore hidden region.
  631. if ($region == 'hidden') {
  632. continue;
  633. }
  634. if (isset($form_state['values']['additional_settings']['layout_class'])) {
  635. $record->settings['classes']['layout_class'] = $form_state['values']['additional_settings']['layout_class'];
  636. }
  637. // Additional classes on regions.
  638. if (isset($form_state['values']['additional_settings'][$region])) {
  639. // Do not save empty string.
  640. $classes = is_array($form_state['values']['additional_settings'][$region]) ? implode(' ', $form_state['values']['additional_settings'][$region]) : array();
  641. if (!empty($classes)) {
  642. $record->settings['classes'][$region] = $form_state['values']['additional_settings'][$region];
  643. }
  644. }
  645. // Additional wrappers on regions.
  646. if (isset($form_state['values']['additional_settings']['region_wrapper'][$region])) {
  647. $record->settings['wrappers'][$region] = $form_state['values']['additional_settings']['region_wrapper'][$region];
  648. }
  649. }
  650. // Layout wrapper
  651. $record->settings['layout_wrapper'] = $form_state['values']['additional_settings']['region_wrapper']['layout_wrapper'];
  652. $record->settings['layout_attributes'] = filter_xss_admin($form_state['values']['additional_settings']['region_wrapper']['layout_attributes']);
  653. $record->settings['layout_attributes_merge'] = $form_state['values']['additional_settings']['region_wrapper']['layout_attributes_merge'];
  654. // Link attribute.
  655. $record->settings['layout_link_attribute'] = $form_state['values']['additional_settings']['region_wrapper']['layout_link_attribute'];
  656. $record->settings['layout_link_custom'] = $form_state['values']['additional_settings']['region_wrapper']['layout_link_custom'];
  657. $record->settings = $record->settings;
  658. // Let other modules alter the layout settings.
  659. drupal_alter('ds_layout_settings', $record, $form_state);
  660. $l = $form['#ds_layout'];
  661. if ($l->export_type == 2) {
  662. drupal_write_record('ds_layout_settings', $record);
  663. }
  664. else {
  665. drupal_write_record('ds_layout_settings', $record, array('id'));
  666. }
  667. // Clear entity info cache.
  668. cache_clear_all('entity_info', 'cache', TRUE);
  669. }
  670. }
  671. /**
  672. * Form validation handler for _ds_field_ui_fields().
  673. */
  674. function ds_field_ui_fields_validate($form, &$form_state) {
  675. foreach (element_children($form['fields']) as $key) {
  676. if (isset($form_state['values']['fields'][$key]['settings_edit_form'])) {
  677. $settings = isset($form_state['values']['fields'][$key]['settings_edit_form']['settings']['ft']) ? $form_state['values']['fields'][$key]['settings_edit_form']['settings']['ft'] : array();
  678. if (!empty($settings)) {
  679. $merge = isset($form_state['formatter_settings'][$key]['ft']) ? $form_state['formatter_settings'][$key]['ft'] : array();
  680. $form_state['formatter_settings'][$key]['ft'] = array_merge($merge, $settings);
  681. }
  682. }
  683. }
  684. }
  685. /**
  686. * Save the field settings from the 'Manage display' screen.
  687. */
  688. function ds_field_ui_fields_save($form, &$form_state) {
  689. // Setup some variables.
  690. $entity_type = $form['#entity_type'];
  691. $bundle = $form['#bundle'];
  692. $view_mode = $form['#view_mode'];
  693. // Delete previous field configuration configuration.
  694. db_delete('ds_field_settings')
  695. ->condition('entity_type', $entity_type)
  696. ->condition('bundle', $bundle)
  697. ->condition('view_mode', $view_mode)
  698. ->execute();
  699. if (empty($form_state['layout_saved'])) {
  700. return;
  701. }
  702. $field_settings = array();
  703. // Save settings for each field.
  704. $fields = $form['#ds_fields'];
  705. foreach ($fields as $key => $field) {
  706. // Field settings.
  707. $field_values = $form_state['values']['fields'][$field];
  708. // In case the region is hidden, do not save.
  709. if (isset($field_values['region']) && $field_values['region'] == 'hidden') {
  710. continue;
  711. }
  712. // Build settings.
  713. $settings = array();
  714. $settings['weight'] = $field_values['weight'];
  715. $settings['label'] = $field_values['label'];
  716. $settings['format'] = $field_values['format']['type'];
  717. // Any formatter settings.
  718. if (isset($form_state['formatter_settings'][$field])) {
  719. $settings['formatter_settings'] = $form_state['formatter_settings'][$field];
  720. }
  721. $field_settings[$field] = $settings;
  722. }
  723. // Allow other modules to modify the field settings before they get saved.
  724. drupal_alter('ds_field_settings', $field_settings, $form, $form_state);
  725. // Save the record.
  726. if (!empty($field_settings)) {
  727. $record = new stdClass;
  728. $record->id = $form['#export_id'];
  729. $record->entity_type = $entity_type;
  730. $record->bundle = $bundle;
  731. $record->view_mode = $view_mode;
  732. $record->settings = $field_settings;
  733. drupal_write_record('ds_field_settings', $record);
  734. }
  735. // Clear the ds_fields cache.
  736. cache_clear_all('ds_fields:', 'cache', TRUE);
  737. cache_clear_all('ds_field_settings', 'cache');
  738. }
  739. /**
  740. * Clone a fields layout.
  741. */
  742. function ds_field_ui_layout_clone($form, &$form_state) {
  743. $clone = $form_state['values']['additional_settings']['clone'];
  744. $entity_type = $form['#entity_type'];
  745. $bundle = $form['#bundle'];
  746. $view_mode = $form['#view_mode'];
  747. ctools_include('export');
  748. $layout = ctools_export_crud_load('ds_layout_settings', $clone);
  749. // Delete previous layout settings configuration.
  750. db_delete('ds_layout_settings')
  751. ->condition('entity_type', $entity_type)
  752. ->condition('bundle', $bundle)
  753. ->condition('view_mode', $view_mode)
  754. ->execute();
  755. // Delete previous field configuration configuration.
  756. db_delete('ds_field_settings')
  757. ->condition('entity_type', $entity_type)
  758. ->condition('bundle', $bundle)
  759. ->condition('view_mode', $view_mode)
  760. ->execute();
  761. // Save new layout record for ds.
  762. if ($layout) {
  763. $record = new stdClass();
  764. $record->id = $form['#export_id'];
  765. $record->entity_type = $entity_type;
  766. $record->bundle = $bundle;
  767. $record->view_mode = $view_mode;
  768. $record->layout = $layout->layout;
  769. $record->settings = $layout->settings;
  770. // Let other modules alter the layout settings.
  771. drupal_alter('ds_layout_settings', $record, $form_state);
  772. // Save layout record.
  773. drupal_write_record('ds_layout_settings', $record);
  774. // Copy the view mode settings.
  775. list($ce, $cb, $cv) = explode('|', $clone);
  776. _ds_field_ui_clone_view_mode_settings($entity_type, $bundle, $view_mode, $cv);
  777. // Clear entity info cache.
  778. cache_clear_all('entity_info', 'cache', TRUE);
  779. // Show message.
  780. drupal_set_message(t('The layout has been cloned.'));
  781. }
  782. else {
  783. drupal_set_message(t('No layout was cloned.'));
  784. }
  785. }
  786. /**
  787. * Populates display settings for a new view mode from the another view mode.
  788. *
  789. * This is almost a straight copy from Field UI, but with the addition
  790. * that we can pass the view mode from which we want to clone from.
  791. */
  792. function _ds_field_ui_clone_view_mode_settings($entity_type, $bundle, $view_mode, $copy_view_mode) {
  793. $settings = field_bundle_settings($entity_type, $bundle);
  794. // Update display settings for field instances.
  795. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle));
  796. foreach ($instances as $instance) {
  797. // If this field instance has display settings defined for this view mode,
  798. // respect those settings.
  799. if (isset($instance['display'][$copy_view_mode])) {
  800. $instance['display'][$view_mode] = $instance['display'][$copy_view_mode];
  801. field_update_instance($instance);
  802. }
  803. }
  804. // Update display settings for 'extra fields'.
  805. foreach (array_keys($settings['extra_fields']['display']) as $name) {
  806. if (isset($settings['extra_fields']['display'][$name][$copy_view_mode])) {
  807. $settings['extra_fields']['display'][$name][$view_mode] = $settings['extra_fields']['display'][$name][$copy_view_mode];
  808. }
  809. }
  810. // Save the settings.
  811. field_bundle_settings($entity_type, $bundle, $settings);
  812. }
  813. /**
  814. * Implements hook_field_formatter_settings_form().
  815. */
  816. function ds_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  817. $display = $instance['display'][$view_mode];
  818. $settings = $display['settings'];
  819. // Taxonomy view modes.
  820. if ($display['type'] === 'ds_taxonomy_view_mode') {
  821. $options = array();
  822. $view_modes = ds_entity_view_modes('taxonomy_term');
  823. foreach ($view_modes as $key => $info) {
  824. $options[$key] = $info['label'];
  825. }
  826. $element['taxonomy_term_reference_view_mode'] = array(
  827. '#title' => t('View mode'),
  828. '#type' => 'select',
  829. '#options' => $options,
  830. '#default_value' => $settings['taxonomy_term_reference_view_mode'],
  831. );
  832. return $element;
  833. }
  834. // Taxonomy separated.
  835. if ($display['type'] === 'ds_taxonomy_separator' || $display['type'] == 'ds_taxonomy_separator_localized') {
  836. $element['taxonomy_term_link'] = array(
  837. '#title' => t('Link to term'),
  838. '#type' => 'checkbox',
  839. '#size' => 10,
  840. '#default_value' => $settings['taxonomy_term_link'],
  841. );
  842. $separators = array(
  843. ' ' => t('space'),
  844. ', ' => t('comma'),
  845. ' - ' => t('dash'),
  846. ' / ' => t('slash'),
  847. );
  848. drupal_alter('ds_taxonomy_term_separators', $separators);
  849. $element['taxonomy_term_separator'] = array(
  850. '#title' => t('Separator'),
  851. '#type' => 'select',
  852. '#options' => $separators,
  853. '#default_value' => $settings['taxonomy_term_separator'],
  854. '#states' => array(
  855. 'visible' => array(
  856. 'select[name="fields[field_tags][settings_edit_form][settings][taxonomy_term_list]"]' => array('value' => 'separated_list'),
  857. ),
  858. ),
  859. );
  860. return $element;
  861. }
  862. }
  863. /**
  864. * Implements hook_field_formatter_settings_summary().
  865. */
  866. function ds_field_formatter_settings_summary($field, $instance, $view_mode) {
  867. $summary = '';
  868. $display = $instance['display'][$view_mode];
  869. $settings = $display['settings'];
  870. if ($display['type'] === 'ds_taxonomy_view_mode') {
  871. $entity_info = entity_get_info('taxonomy_term');
  872. $modes = $entity_info['view modes'];
  873. $mode = $modes[$settings['taxonomy_term_reference_view_mode']]['label'];
  874. $summary = t('View mode: %mode', array('%mode' => $mode));
  875. }
  876. if ($display['type'] === 'ds_taxonomy_separator' || $display['type'] == 'ds_taxonomy_separator_localized') {
  877. $separators = array(
  878. ' ' => t('space'),
  879. ', ' => t('comma'),
  880. ' - ' => t('dash'),
  881. ' / ' => t('slash'),
  882. );
  883. drupal_alter('ds_taxonomy_term_separators', $separators);
  884. $summary .= t('Separated by !sep', array('!sep' => $separators[$settings['taxonomy_term_separator']]));
  885. $summary .= $settings['taxonomy_term_link'] ? ', ' . t('linked') : ', ' . t('not linked');
  886. }
  887. return $summary;
  888. }
  889. /**
  890. * Creates a summary for the field format configuration summary.
  891. *
  892. * @param $field
  893. * The configuration of the field.
  894. *
  895. * @return $summary
  896. * An markup array.
  897. */
  898. function ds_field_settings_summary($field, $form_state, $form, $view_mode) {
  899. $summary = '';
  900. // Not all fields have settings.
  901. if (isset($field['properties']['settings'])) {
  902. $summary = module_invoke($field['module'], 'ds_field_format_summary', $field);
  903. }
  904. if (module_exists('ds_extras') && variable_get('ds_extras_field_template', FALSE)) {
  905. module_load_include('inc', 'ds_extras', 'includes/ds_extras.admin');
  906. // Field template summary
  907. if (!in_array($field['field_type'], array(DS_FIELD_TYPE_IGNORE, DS_FIELD_TYPE_PREPROCESS))) {
  908. $functions = module_invoke_all('ds_field_theme_functions_info');
  909. $default_field_function = variable_get('ft-default', 'theme_field');
  910. $field_function = isset($form_state['formatter_settings'][$field['name']]['ft']['func']) ? $form_state['formatter_settings'][$field['name']]['ft']['func'] : $default_field_function;
  911. $summary .= 'Field template: ' . check_plain($functions[$field_function]) . '<br />';
  912. }
  913. }
  914. if (!empty($form_state['complete form'])) {
  915. $formatter_name = $form_state['complete form']['fields'][$field['name']]['format']['type']['#value'];
  916. }
  917. else {
  918. $formatter_name = $form['fields'][$field['name']]['format']['type']['#default_value'];
  919. }
  920. // Allow other modules to alter the formatter summary.
  921. $context = array(
  922. 'formatter' => $formatter_name,
  923. 'field' => $field,
  924. 'instance' => array(
  925. 'display' => array(
  926. $view_mode => array(
  927. 'label' => '',
  928. 'type' => '',
  929. 'weight' => '',
  930. 'settings' => isset($field['formatter_settings']) ? $field['formatter_settings'] : array(),
  931. 'module' => '',
  932. )
  933. )
  934. ),
  935. 'view_mode' => $view_mode,
  936. 'ds' => TRUE,
  937. );
  938. drupal_alter('field_formatter_settings_summary', $summary, $context);
  939. if (empty($summary)) {
  940. return NULL; // no summary return nothing
  941. }
  942. return array(
  943. '#markup' => '<div class="field-formatter-summary">' . $summary . '</div>',
  944. '#cell_attributes' => array('class' => array('field-formatter-summary-cell')),
  945. );
  946. }
  947. /**
  948. * Creates a form for Display Suite fields.
  949. * .
  950. * @param $field
  951. * The field definition.
  952. *
  953. * @return $form
  954. * A form definition.
  955. */
  956. function ds_field_settings_form($field, &$form_state, $entity_form, $view_mode) {
  957. $form = module_invoke($field['module'], 'ds_field_settings_form', $field);
  958. // Add field template settings to every field if enabled.
  959. if (module_exists('ds_extras') && variable_get('ds_extras_field_template', FALSE)) {
  960. $context = array(
  961. 'instance' => array(
  962. 'entity_type' => $field['entity_type'],
  963. 'bundle' => $field['bundle'],
  964. 'field_name' => $field['name'],
  965. ),
  966. 'view_mode' => $field['view_mode'],
  967. );
  968. // Load the formatter settings form
  969. module_load_include('inc', 'ds_extras', 'includes/ds_extras.admin');
  970. // Protect against empty $form.
  971. if (!is_array($form)) $form = array();
  972. if (!in_array($field['field_type'], array(DS_FIELD_TYPE_IGNORE, DS_FIELD_TYPE_PREPROCESS))) {
  973. ds_extras_field_template_settings_form($form, $form_state, $context);
  974. }
  975. else {
  976. $form['#markup'] = t('This field does not support Field templates.');
  977. }
  978. }
  979. $formatter_name = $form_state['complete form']['fields'][$field['name']]['format']['type']['#value'];
  980. // Allow other modules to alter the formatter settings form.
  981. $context = array(
  982. 'ds' => TRUE,
  983. 'formatter' => $formatter_name,
  984. 'field' => $field,
  985. 'instance' => array(
  986. 'label' => $field['title'],
  987. 'bundle' => $field['bundle'],
  988. 'entity_type' => $field['entity_type'],
  989. 'display' => array(
  990. 'default' => array(
  991. 'settings' => isset($field['formatter_settings']) ? $field['formatter_settings'] : array(),
  992. ),
  993. ),
  994. ),
  995. 'view_mode' => $view_mode,
  996. 'form' => $entity_form,
  997. 'form_state' => $form_state,
  998. );
  999. drupal_alter('field_formatter_settings_form', $form, $context);
  1000. return $form;
  1001. }
  1002. /**
  1003. * Implements hook_ds_field_format_summary().
  1004. */
  1005. function ds_ds_field_format_summary($field) {
  1006. $summary = '';
  1007. $settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : $field['properties']['default'];
  1008. $functions = module_invoke_all('ds_field_theme_functions_info');
  1009. foreach ($settings as $key => $value) {
  1010. // Ignore Field Formatter conditions.
  1011. if ($key == 'conditions') {
  1012. continue;
  1013. }
  1014. if ($key == 'ctools') {
  1015. $conf = unserialize($value);
  1016. $summary .= t('Type: !type', array('!type' => check_plain(drupal_ucfirst(str_replace('_', ' ', $conf['subtype'])))));
  1017. }
  1018. elseif ($key == 'ft' || is_array($value)) {
  1019. // Do nothing
  1020. }
  1021. elseif (!empty($value)) {
  1022. $value = is_numeric($value) ? ($value ? t('Yes') : t('No')) : check_plain($value);
  1023. $summary .= ' ' . str_replace('_', ' ', drupal_ucfirst(check_plain($key))) . ': ' . check_plain($value) . '<br />';
  1024. }
  1025. }
  1026. if (empty($summary) && ($field['field_type'] == DS_FIELD_TYPE_CTOOLS)) {
  1027. $summary .= t('Not configured yet.') . '<br />';
  1028. }
  1029. return $summary;
  1030. }
  1031. /**
  1032. * Implements hook_ds_field_settings_form().
  1033. */
  1034. function ds_ds_field_settings_form($field) {
  1035. $form = array();
  1036. $settings = !empty($field['formatter_settings']) ? $field['formatter_settings'] : (!empty($field['properties']['default']) ? $field['properties']['default'] : array());
  1037. if (empty($field['properties']['settings'])) {
  1038. return $form;
  1039. }
  1040. foreach ($field['properties']['settings'] as $key => $value) {
  1041. switch ($value['type']) {
  1042. case 'textfield':
  1043. $form[$key] = array(
  1044. '#type' => 'textfield',
  1045. '#title' => str_replace('_', ' ', check_plain(drupal_ucfirst($key))),
  1046. '#default_value' => isset($settings[$key]) ? $settings[$key] : '',
  1047. '#size' => 40,
  1048. '#description' => isset($value['description']) ? check_plain($value['description']) : '',
  1049. );
  1050. break;
  1051. case 'select':
  1052. $form[$key] = array(
  1053. '#type' => 'select',
  1054. '#title' => check_plain(drupal_ucfirst($key)),
  1055. '#default_value' => isset($settings[$key]) ? $settings[$key] : '',
  1056. '#options' => $value['options'],
  1057. '#description' => isset($value['description']) ? check_plain($value['description']) : '',
  1058. );
  1059. break;
  1060. case 'checkbox':
  1061. $form[$key] = array(
  1062. '#type' => 'checkbox',
  1063. '#title' => str_replace('_', ' ', check_plain(drupal_ucfirst($key))),
  1064. '#default_value' => isset($settings[$key]) ? $settings[$key] : '',
  1065. '#description' => isset($value['description']) ? check_plain($value['description']) : '',
  1066. );
  1067. break;
  1068. case 'ctools':
  1069. ctools_include('modal');
  1070. ctools_include('object-cache');
  1071. ctools_modal_add_js();
  1072. $form[$key] = array(
  1073. '#type' => 'hidden',
  1074. '#default_value' => isset($settings[$key]) ? $settings[$key] : '',
  1075. '#weight' => 2,
  1076. );
  1077. $action = 'add';
  1078. $args = '';
  1079. $conf = array();
  1080. $query = array('query' => array('selection' => 1));
  1081. $title = t('Select content');
  1082. if (isset($settings[$key])) {
  1083. $query = array();
  1084. $ctools = unserialize($settings['ctools']);
  1085. $type = $ctools['type'];
  1086. $subtype = $ctools['subtype'];
  1087. $args = '/' . $type . '/' . $subtype;
  1088. $action = 'edit';
  1089. $conf = $ctools['conf'];
  1090. $title = t('Edit content');
  1091. }
  1092. $form['select'] = array(
  1093. '#markup' => '<div class="select-content-link">' . l($title, 'admin/structure/ds/fields/manage_ctools/content/' . $action . '/' . $field['entity_type'] . '/' . $field['name'] . $args, array('attributes' => array('class' => array('ctools-use-modal'))) + $query) . '</div>',
  1094. '#weight' => -10,
  1095. );
  1096. $form['load_terms'] = array(
  1097. '#type' => 'checkbox',
  1098. '#title' => t('Load terms'),
  1099. '#description' => t('Toggle if you are embedding a view with term fields.'),
  1100. '#default_value' => isset($settings['load_terms']) ? $settings['load_terms'] : '',
  1101. '#weight' => -1,
  1102. );
  1103. $form['show_title']['#weight'] = 0;
  1104. $form['title_wrapper']['#weight'] = 1;
  1105. ctools_object_cache_set($field['name'], $field['name'], $conf);
  1106. break;
  1107. }
  1108. }
  1109. return $form;
  1110. }
  1111. /**
  1112. * Add entity contexts.
  1113. */
  1114. function ds_get_entity_context($entity_type) {
  1115. ctools_include('context');
  1116. $arguments = array(
  1117. array(
  1118. 'keyword' => $entity_type,
  1119. 'identifier' => drupal_ucfirst($entity_type) . ' being viewed',
  1120. 'id' => 1,
  1121. 'name' => 'entity_id:' . $entity_type,
  1122. 'settings' => array(),
  1123. ),
  1124. );
  1125. return ctools_context_get_placeholders_from_argument($arguments);
  1126. }
  1127. /**
  1128. * Return the configuration settings for the CTools field.
  1129. */
  1130. function ds_ctools_content($action = 'add', $entity_type = '', $field_name = '', $type_name = '', $subtype_name = '', $step = NULL) {
  1131. ctools_include('modal');
  1132. ctools_include('ajax');
  1133. ctools_include('content');
  1134. ctools_include('object-cache');
  1135. $commands = array();
  1136. $content_type = ctools_get_content_type($type_name);
  1137. $subtype = ctools_content_get_subtype($content_type, $subtype_name);
  1138. if ($data = ctools_object_cache_get($field_name, $field_name)) {
  1139. $conf = $data;
  1140. }
  1141. else {
  1142. $conf = ctools_content_get_defaults($content_type, $subtype);
  1143. }
  1144. $url = 'admin/structure/ds/fields/manage_ctools/content/' . $action . '/' . $entity_type . '/' . $field_name;
  1145. $base_url = $url;
  1146. if (!empty($type_name) && !empty($subtype_name)) {
  1147. $url .= '/' . $type_name . '/' . $subtype_name . '/%step';
  1148. }
  1149. $form_info = array(
  1150. 'path' => $url,
  1151. 'show cancel' => TRUE,
  1152. 'next callback' => 'ds_ctools_content_next',
  1153. );
  1154. // Get entity context.
  1155. $contexts = ds_get_entity_context($entity_type);
  1156. $form_state = array(
  1157. 'contexts' => $contexts,
  1158. 'ajax' => TRUE,
  1159. 'modal' => TRUE,
  1160. 'modal return' => TRUE,
  1161. 'field_name' => $field_name,
  1162. );
  1163. // Call the content form.
  1164. $output = ctools_content_form($action, $form_info, $form_state, $content_type, $subtype_name, $subtype, $conf, $step);
  1165. if (!empty($form_state['complete']) || isset($_GET['dismiss'])) {
  1166. $configuration = array(
  1167. 'conf' => $form_state['conf'],
  1168. 'type' => $type_name,
  1169. 'subtype' => $subtype_name,
  1170. );
  1171. $commands[] = ctools_modal_command_dismiss();
  1172. $commands[] = ajax_command_invoke('input[name="fields[' . $field_name . '][settings_edit_form][settings][ctools]"]', 'dsCtoolsContentConfiguration', array(serialize($configuration)));
  1173. $commands[] = ajax_command_invoke('.select-content-link', 'dsCtoolsContentUpdate', array(serialize($configuration)));
  1174. ctools_object_cache_clear($field_name, $field_name);
  1175. }
  1176. // Content selection
  1177. elseif (!empty($form_state['cancel']) || isset($_GET['selection'])) {
  1178. ctools_object_cache_clear($field_name, $field_name);
  1179. $commands[] = ds_ctools_content_select($contexts, $field_name, $action, $entity_type);
  1180. }
  1181. // No configuration anymore.
  1182. elseif ($output === FALSE && !isset($_GET['dismiss'])) {
  1183. $output = t('No further configuration exists for this content type.<br/><br/><a href="!close_modal" class="use-ajax">Click here to close the modal and save the settings.</a><br/><br/><a href="!new_content" class="use-ajax">Click here to select new content</a>.', array('!new_content' => url($base_url, array('query' => array('selection' => TRUE))), '!close_modal' => url($url, array('query' => array('dismiss' => 1)))));
  1184. $commands[] = ctools_modal_command_display(t('Edit content'), $output);
  1185. }
  1186. // Form render.
  1187. else {
  1188. $commands = ctools_modal_form_render($form_state, $output);
  1189. }
  1190. print ajax_render($commands);
  1191. ajax_footer();
  1192. exit;
  1193. }
  1194. /**
  1195. * Handle the 'next' click on the add/edit field form wizard.
  1196. */
  1197. function ds_ctools_content_next(&$form_state) {
  1198. ctools_object_cache_set($form_state['field_name'], $form_state['field_name'], $form_state['conf']);
  1199. }
  1200. /**
  1201. * Select content.
  1202. *
  1203. * @param $contexts
  1204. * A collection of contexts, usually the entity.
  1205. * @param $field_name
  1206. * The name of the field.
  1207. * @param $action
  1208. * The name of the action.
  1209. * @param $entity_type
  1210. * The name of the entity type.
  1211. */
  1212. function ds_ctools_content_select($contexts, $field_name, $action, $entity_type) {
  1213. // Get content types.
  1214. $content_types = ctools_content_get_available_types($contexts);
  1215. $categories = $category_names = $ordered = array();
  1216. foreach ($content_types as $type_name => $subtypes) {
  1217. foreach ($subtypes as $subtype_name => $content_type) {
  1218. list($category_key, $category) = ds_ctools_get_category($content_type);
  1219. if (empty($categories[$category_key])) {
  1220. $categories[$category_key] = array(
  1221. 'title' => $category,
  1222. 'content' => array(),
  1223. );
  1224. $category_names[$category_key] = $category;
  1225. }
  1226. $content_title = filter_xss_admin($content_type['title']);
  1227. // Ensure content with the same title doesn't overwrite each other.
  1228. while (isset($categories[$category_key]['content'][$content_title])) {
  1229. $content_title .= '-';
  1230. }
  1231. $categories[$category_key]['content'][$content_title] = $content_type;
  1232. $categories[$category_key]['content'][$content_title]['type_name'] = $type_name;
  1233. $categories[$category_key]['content'][$content_title]['subtype_name'] = $subtype_name;
  1234. }
  1235. }
  1236. // Now sort
  1237. natcasesort($category_names);
  1238. foreach ($category_names as $category => $name) {
  1239. $ordered[$category] = $categories[$category];
  1240. }
  1241. $left = '';
  1242. $right = '<div class="content">' . t('Content options are divided by category. Please select a category from the left to proceed.') . '</div>';
  1243. foreach ($ordered as $section => $section_content) {
  1244. // Section.
  1245. if ($section == 'root') {
  1246. $section_content['title'] = t('Content');
  1247. }
  1248. $left .= '<div class="section"><a href="" id="' . $section . '" class="section-link">' . $section_content['title'] . '</a></div>';
  1249. // Content.
  1250. $right .= '<div id="' . $section . '-container" class="selection-hide content">';
  1251. $right .= '<h2>' . $section_content['title'] . '</h2>';
  1252. foreach ($section_content['content'] as $key => $value) {
  1253. $right .= '<div class="content-item">';
  1254. $variables = array(
  1255. 'path' => ctools_content_admin_icon($value),
  1256. );
  1257. $right .= theme('image', $variables) . '&nbsp;';
  1258. $right .= ctools_ajax_text_button($key, 'admin/structure/ds/fields/manage_ctools/content/' . $action . '/' . $entity_type . '/' . $field_name . '/' . $value['type_name'] . '/' . $value['subtype_name'], $key);
  1259. $right .= '</div>';
  1260. }
  1261. $right .= '</div>';
  1262. }
  1263. // Create output.
  1264. $output = '<div id="ctools-content-selection">';
  1265. $output .= '<div id="ds-left">' . $left . '</div>';
  1266. $output .= '<div id="ds-right">' . $right . '</div>';
  1267. $output .= '</div>';
  1268. return ctools_modal_command_display(t('Select content'), $output);
  1269. }
  1270. /**
  1271. * Helper function to get the category.
  1272. */
  1273. function ds_ctools_get_category($content_type) {
  1274. if (isset($content_type['top level'])) {
  1275. $category = 'root';
  1276. }
  1277. elseif (isset($content_type['category'])) {
  1278. if (is_array($content_type['category'])) {
  1279. list($category, $weight) = $content_type['category'];
  1280. }
  1281. else {
  1282. $category = $content_type['category'];
  1283. }
  1284. }
  1285. else {
  1286. $category = t('Uncategorized');
  1287. }
  1288. return array(preg_replace('/[^a-z0-9]/', '-', drupal_strtolower($category)), $category);
  1289. }
  1290. /**
  1291. * Add fake field group value in.
  1292. */
  1293. function _ds_field_group_field_ui_fix_notices($form, &$form_state) {
  1294. $field_group = array(
  1295. 'group_name' => '',
  1296. 'label' => '',
  1297. );
  1298. $form_state['values']['fields']['_add_new_group'] = $field_group;
  1299. }
  1300. /**
  1301. * Add the layouts fieldset on the Field UI screen.
  1302. *
  1303. * @param $entity_type
  1304. * The name of the entity type.
  1305. * @param $bundle
  1306. * The name of the bundle
  1307. * @param $view_mode
  1308. * The name of the view_mode
  1309. * @param $form
  1310. * A collection of form properties.
  1311. */
  1312. function _ds_field_ui_table_layouts($entity_type, $bundle, $view_mode, &$form, $form_state) {
  1313. $layout_options = array();
  1314. $ds_layouts = ds_get_layout_info();
  1315. $layout_options = array('' => t('- None -'));
  1316. foreach ($ds_layouts as $key => $layout) {
  1317. $optgroup = 'Display Suite';
  1318. // Panels can not be used on Views fields and forms.
  1319. if (!empty($layout['module']) && $layout['module'] == 'panels' && isset($form_state['no_panels'])) {
  1320. continue;
  1321. }
  1322. // Create new layout option group.
  1323. if (!empty($layout['module'])) {
  1324. $optgroup = drupal_ucfirst($layout['module']);
  1325. }
  1326. if (!isset($layout_options[$optgroup])) {
  1327. $layout_options[$optgroup] = array();
  1328. }
  1329. // Stack the layout.
  1330. $layout_options[$optgroup][$key] = $layout['label'];
  1331. }
  1332. // If there is only one $optgroup, move it to the root.
  1333. if (count($layout_options) == 2) {
  1334. $options = $layout_options[$optgroup];
  1335. $layout_options = array_merge(array('' => t('- None -')), $options);
  1336. }
  1337. // Add layouts form.
  1338. $form['additional_settings']['ds_layouts'] = array(
  1339. '#type' => 'fieldset',
  1340. '#title' => t('Layout for !bundle in !view_mode', array('!bundle' => str_replace('_', ' ', $bundle), '!view_mode' => str_replace('_', ' ', $view_mode))),
  1341. '#collapsible' => TRUE,
  1342. '#collapsed' => FALSE,
  1343. '#parents' => array('additional_settings'),
  1344. '#weight' => -100,
  1345. );
  1346. ctools_include('export');
  1347. $layout = new stdClass();
  1348. $ds_layout_settings = ctools_export_crud_load_all('ds_layout_settings');
  1349. if (isset($ds_layout_settings[$form['#export_id']])) {
  1350. $layout = $ds_layout_settings[$form['#export_id']];
  1351. }
  1352. if (!empty($layout) && isset($layout->layout) && isset($ds_layouts[$layout->layout]) && empty($layout->disabled)) {
  1353. $layout->settings = $layout->settings;
  1354. $layout->regions = $ds_layouts[$layout->layout]['regions'];
  1355. $form['#ds_layout'] = $layout;
  1356. }
  1357. // The layout is defined in code.
  1358. if (isset($layout->export_type)) {
  1359. // Enabled/disable the layout.
  1360. if (empty($layout->disabled)) {
  1361. $link = t('This layout is defined in code') . ': ' . l(t('disable layout.'), 'admin/structure/ds/disable/' . $form['#export_id'], array('query' => drupal_get_destination()));
  1362. }
  1363. else {
  1364. $link = t('A layout is defined in code but has been disabled') . ': ' . l(t('enable layout.'), 'admin/structure/ds/enable/' . $form['#export_id'], array('query' => drupal_get_destination()));
  1365. }
  1366. $form['additional_settings']['ds_layouts']['enable_disable'] = array(
  1367. '#markup' => $link,
  1368. '#weight' => 2,
  1369. );
  1370. // Overridden in database.
  1371. if ($layout->export_type == 3) {
  1372. $form['additional_settings']['ds_layouts']['revert'] = array(
  1373. '#markup' => l(t('This layout is overridden. Click to revert to default settings.'), 'admin/structure/ds/revert-layout/' . $form['#export_id'], array('query' => drupal_get_destination())),
  1374. '#weight' => 1,
  1375. );
  1376. }
  1377. }
  1378. // Load the layout preview form
  1379. $layout->layout_options = $layout_options;
  1380. _ds_field_ui_table_layouts_preview($form, $form_state, $ds_layouts, $layout, $entity_type, $bundle, $view_mode);
  1381. if (!empty($layout) && isset($layout->regions)) {
  1382. // Add wrappers
  1383. $wrapper_options = array('div' => 'Div', 'span' => 'Span', 'section' => 'Section', 'article' => 'Article', 'header' => 'Header', 'footer' => 'Footer', 'aside' => 'Aside');
  1384. $form['additional_settings']['region_wrapper'] = array(
  1385. '#type' => 'fieldset',
  1386. '#title' => t('Custom wrappers'),
  1387. '#description' => t('Choose a wrapper. All Display Suite layouts support this option.')
  1388. );
  1389. // Hide the fieldset in case of the reset layout.
  1390. if ($layout->layout === 'ds_reset') {
  1391. $form['additional_settings']['region_wrapper']['#access'] = FALSE;
  1392. }
  1393. foreach (array_keys($layout->regions) as $region) {
  1394. $form['additional_settings']['region_wrapper'][$region] = array(
  1395. '#type' => 'select',
  1396. '#options' => $wrapper_options,
  1397. '#title' => t('Wrapper for @region', array('@region' => $layout->regions[$region])),
  1398. '#default_value' => isset($layout->settings['wrappers'], $layout->settings['wrappers'][$region]) ? $layout->settings['wrappers'][$region] : 'div',
  1399. );
  1400. }
  1401. $form['additional_settings']['region_wrapper']['layout_wrapper'] = array(
  1402. '#type' => 'select',
  1403. '#options' => $wrapper_options,
  1404. '#title' => t('Layout wrapper'),
  1405. '#default_value' => isset($layout->settings['layout_wrapper']) ? $layout->settings['layout_wrapper'] : 'div',
  1406. '#weight' => 10,
  1407. );
  1408. $form['additional_settings']['region_wrapper']['layout_attributes'] = array(
  1409. '#type' => 'textfield',
  1410. '#title' => t('Layout attributes'),
  1411. '#description' => 'E.g. role="navigation"',
  1412. '#default_value' => isset($layout->settings['layout_attributes']) ? $layout->settings['layout_attributes'] : '',
  1413. '#weight' => 11,
  1414. );
  1415. $form['additional_settings']['region_wrapper']['layout_attributes_merge'] = array(
  1416. '#type' => 'checkbox',
  1417. '#title' => t('Merge other layout attributes'),
  1418. '#description' => 'Certain modules might provide additional attributes for the template wrapper, e.g. RDFa. Disable this option if you prefer to specify these attributes above.',
  1419. '#default_value' => isset($layout->settings['layout_attributes_merge']) ? $layout->settings['layout_attributes_merge'] : variable_get('ds_layout_attributes_merge', TRUE),
  1420. '#weight' => 12,
  1421. );
  1422. $form['additional_settings']['region_wrapper']['layout_link_attribute'] = array(
  1423. '#type' => 'select',
  1424. '#options' => array(
  1425. '' => t('No link'),
  1426. 'content' => t('Link to content'),
  1427. 'custom' => t('Custom'),
  1428. 'tokens' => t('Tokens')
  1429. ),
  1430. '#title' => t('Add link'),
  1431. '#description' => t('This will add an onclick attribute on the layout wrapper.'),
  1432. '#default_value' => isset($layout->settings['layout_link_attribute']) ? $layout->settings['layout_link_attribute'] : FALSE,
  1433. '#weight' => 12,
  1434. );
  1435. $form['additional_settings']['region_wrapper']['layout_link_custom'] = array(
  1436. '#type' => 'textfield',
  1437. '#title' => t('Custom link'),
  1438. '#description' => t('You may use tokens for this link if you selected tokens.'),
  1439. '#default_value' => isset($layout->settings['layout_link_custom']) ? $layout->settings['layout_link_custom'] : FALSE,
  1440. '#weight' => 13,
  1441. '#states' => array(
  1442. 'visible' => array(array(
  1443. ':input[name="additional_settings[region_wrapper][layout_link_attribute]"]' => array(array("value" => "tokens"), array("value" => "custom")),
  1444. )),
  1445. ),
  1446. );
  1447. if (module_exists('token')) {
  1448. $form['additional_settings']['region_wrapper']['tokens'] = array(
  1449. '#title' => t('Tokens'),
  1450. '#type' => 'container',
  1451. '#weight' => 14,
  1452. '#states' => array(
  1453. 'visible' => array(
  1454. ':input[name="additional_settings[region_wrapper][layout_link_attribute]"]' => array("value" => "tokens"),
  1455. ),
  1456. ),
  1457. );
  1458. $form['additional_settings']['region_wrapper']['tokens']['help'] = array(
  1459. '#theme' => 'token_tree',
  1460. '#token_types' => 'all',
  1461. '#global_types' => FALSE,
  1462. '#dialog' => TRUE,
  1463. );
  1464. }
  1465. // Add extra classes for the regions to have more control while theming.
  1466. $form['additional_settings']['ds_classes'] = array(
  1467. '#type' => 'fieldset',
  1468. '#title' => t('Custom classes'),
  1469. '#collapsible' => TRUE,
  1470. '#collapsed' => TRUE,
  1471. '#parents' => array('additional_settings'),
  1472. '#access' => empty($chosen_layout['flexible']),
  1473. );
  1474. $classes_access = (user_access('admin_classes') && module_exists('ds_ui'));
  1475. $classes = _ds_classes();
  1476. if (!empty($classes)) {
  1477. $form['additional_settings']['ds_classes']['layout_class'] = array(
  1478. '#type' => 'select',
  1479. '#multiple' => TRUE,
  1480. '#options' => $classes,
  1481. '#title' => t('Class for layout'),
  1482. '#default_value' => isset($layout->settings['classes']['layout_class']) ? $layout->settings['classes']['layout_class'] : '',
  1483. );
  1484. foreach (array_keys($layout->regions) as $region) {
  1485. $form['additional_settings']['ds_classes'][$region] = array(
  1486. '#type' => 'select',
  1487. '#multiple' => TRUE,
  1488. '#options' => $classes,
  1489. '#title' => t('Class for @region', array('@region' => $region)),
  1490. '#default_value' => isset($layout->settings['classes'][$region]) ? $layout->settings['classes'][$region] : '',
  1491. );
  1492. }
  1493. if ($classes_access) {
  1494. $form['additional_settings']['ds_classes']['info'] = array('#markup' => l(t('Manage region and field CSS classes'), 'admin/structure/ds/classes', array('query' => drupal_get_destination())));
  1495. }
  1496. }
  1497. else {
  1498. if ($classes_access) {
  1499. $form['additional_settings']['ds_classes']['info'] = array('#markup' => '<p>' . t('You have not defined any CSS classes which can be used on regions.') . '</p><p>' . l(t('Manage region and field CSS classes'), 'admin/structure/ds/classes', array('query' => drupal_get_destination())) . '</p>');
  1500. }
  1501. else {
  1502. $form['additional_settings']['ds_classes']['#access'] = FALSE;
  1503. }
  1504. }
  1505. }
  1506. else {
  1507. if ($view_mode != 'form') {
  1508. // See if we can clone from another view mode.
  1509. $options = array();
  1510. $ds_layout_settings = ctools_export_crud_load_all('ds_layout_settings');
  1511. foreach ($ds_layout_settings as $row) {
  1512. // Do not clone from form layouts.
  1513. if ($row->view_mode == 'form') {
  1514. continue;
  1515. }
  1516. if ($row->entity_type == $entity_type && $row->bundle == $bundle) {
  1517. $name = drupal_ucfirst(str_replace('_', ' ', $row->entity_type)) . ' > ' . drupal_ucfirst(str_replace('_', ' ', $row->bundle)) . ' > ' . drupal_ucfirst(str_replace('_', ' ', $row->view_mode));
  1518. if (!empty($row->disabled)) {
  1519. $name .= ' ' . t('(disabled)');
  1520. }
  1521. $options[$row->id] = $name;
  1522. }
  1523. }
  1524. if (!empty($options)) {
  1525. // Clone from another layout.
  1526. $form['additional_settings']['ds_clone'] = array(
  1527. '#type' => 'fieldset',
  1528. '#title' => t('Clone layout'),
  1529. '#collapsible' => TRUE,
  1530. '#collapsed' => TRUE,
  1531. '#parents' => array('additional_settings'),
  1532. );
  1533. $form['additional_settings']['ds_clone']['clone'] = array(
  1534. '#title' => t('Select an existing layout to clone.'),
  1535. '#type' => 'select',
  1536. '#options' => $options,
  1537. '#weight' => 20,
  1538. );
  1539. $form['additional_settings']['ds_clone']['clone_submit'] = array(
  1540. '#type' => 'submit',
  1541. '#value' => t('Clone layout'),
  1542. '#submit' => array('ds_field_ui_layout_clone'),
  1543. '#weight' => 21,
  1544. );
  1545. }
  1546. }
  1547. }
  1548. $form['additional_settings']['ds_layouts']['id'] = array(
  1549. '#type' => 'value',
  1550. '#value' => isset($layout->id) ? $layout->id : $form['#export_id'],
  1551. );
  1552. $form['additional_settings']['ds_layouts']['old_layout'] = array(
  1553. '#type' => 'value',
  1554. '#value' => isset($layout->layout) ? $layout->layout : 0,
  1555. );
  1556. // Add validate and submit handlers. Layout needs be first so
  1557. // we can reset the type key for Field API fields.
  1558. $form['#validate'][] = 'ds_field_ui_layouts_validate';
  1559. $submit = $form['#submit'];
  1560. $form['#submit'] = array('ds_field_ui_layouts_save');
  1561. $form['#submit'] = array_merge($form['#submit'], $submit);
  1562. }
  1563. /**
  1564. * Add the layout previews to the Field UI screen.
  1565. *
  1566. * @param $form
  1567. * A collection of form properties.
  1568. * @param $form_state
  1569. * The state of the form
  1570. * @param $ds_layouts
  1571. * Collection of all the layouts
  1572. * @param $layout
  1573. * Current selected layout
  1574. * @param $entity_type
  1575. * The name of the entity type.
  1576. * @param $bundle
  1577. * The name of the bundle
  1578. * @param $view_mode
  1579. * The name of the view_mode
  1580. */
  1581. function _ds_field_ui_table_layouts_preview(&$form, &$form_state, $ds_layouts, $layout, $entity_type, $bundle, $view_mode) {
  1582. $layout_string = '';
  1583. $form['additional_settings']['ds_layouts']['layout'] = array(
  1584. '#type' => 'select',
  1585. '#title' => t('Select a layout'),
  1586. '#options' => $layout->layout_options,
  1587. '#default_value' => isset($layout->layout) && empty($layout->disabled) ? $layout->layout : '',
  1588. '#prefix' => '<div class="ds-select-layout">',
  1589. '#suffix' => '</div>',
  1590. '#weight' => -1,
  1591. '#ajax' => array(
  1592. 'callback' => 'ds_field_ui_table_layouts_preview_callback',
  1593. 'wrapper' => 'ds_layout_wrapper',
  1594. ),
  1595. );
  1596. if (!isset($layout->layout)) {
  1597. $form['additional_settings']['ds_layouts']['layout']['#description'] = t("A layout must be selected to enable Display Suite functionality.");
  1598. }
  1599. $form['additional_settings']['ds_layouts']['preview'] = array(
  1600. '#type' => 'container',
  1601. '#prefix' => '<div id="ds_layout_wrapper">',
  1602. '#suffix' => '</div>',
  1603. '#weight' => -3,
  1604. );
  1605. if (isset($layout->layout) || isset($form_state['values']['additional_settings']['layout'])) {
  1606. $layout_string = isset($form_state['values']['additional_settings']['layout']) ? $form_state['values']['additional_settings']['layout'] : $layout->layout;
  1607. }
  1608. if (!empty($layout_string)) {
  1609. $chosen_layout = $ds_layouts[$layout_string];
  1610. if (empty($chosen_layout['flexible'])) {
  1611. $selected = '<strong>' . $chosen_layout['label'] . '</strong>';
  1612. $selected .= '<br/>' . t('The default template can be found in %path', array('%path' => $chosen_layout['path']));
  1613. $suggestions = t('Template suggestions') . ':<ul>';
  1614. $suggestions_array = array();
  1615. $suggestions_array[0] = $layout_string . '--' . $entity_type;
  1616. $suggestions_array[2] = $layout_string . '--' . $entity_type . '-' . $bundle;
  1617. if (!isset($form_state['no_view_mode_suggestions']) && $view_mode != 'default') {
  1618. $suggestions_array[1] = $layout_string . '--' . $entity_type . '-' . $view_mode;
  1619. $suggestions_array[3] = $layout_string . '--' . $entity_type . '-' . $bundle . '-' . $view_mode;
  1620. }
  1621. ksort($suggestions_array);
  1622. $suggestions = '<ul><li>' . implode('.tpl.php</li><li>', $suggestions_array) . '.tpl.php</li></ul>';
  1623. }
  1624. else {
  1625. $suggestions = '';
  1626. $selected = t('You have selected the flexible %layout_label layout.', array('%layout_label' => $chosen_layout['label'], '%path' => $chosen_layout['path']));
  1627. }
  1628. if (isset($form_state['values']['additional_settings']['layout']) || (!empty($layout) && isset($layout->regions))) {
  1629. $fallback_image = drupal_get_path('module', 'ds') . '/images/preview.png';
  1630. $current_layout = isset($form_state['values']['additional_settings']['layout']) && (!isset($layout->layout) || $form_state['values']['additional_settings']['layout'] != $layout->layout) ? t('Current layout (after save)') : t('Current layout');
  1631. $image = (isset($chosen_layout['image']) && !empty($chosen_layout['image'])) ? $chosen_layout['path'] . '/' . $layout_string . '.png' : $fallback_image;
  1632. if (isset($chosen_layout['panels']) && !empty($chosen_layout['panels']['icon'])) {
  1633. $image = $chosen_layout['panels']['path'] . '/' . $chosen_layout['panels']['icon'];
  1634. }
  1635. $form['additional_settings']['ds_layouts']['preview'] ['title'] = array(
  1636. '#markup' => '<div class="ds-layout-preview-title">' . $current_layout . '</div>',
  1637. );
  1638. $form['additional_settings']['ds_layouts']['preview'] ['image'] = array(
  1639. '#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $image . '"/></div>',
  1640. );
  1641. $form['additional_settings']['ds_layouts']['preview'] ['suggestions'] = array(
  1642. '#markup' => '<div class="ds-layout-preview-suggestion"><p>' . $selected . '</p><p>' . t('!suggestions', array('!suggestions' => strtr($suggestions, '_', '-'))) . '</p></div>',
  1643. );
  1644. }
  1645. if (isset($form_state['values']['additional_settings']['layout']) && (!isset($layout->layout) || $form_state['values']['additional_settings']['layout'] != $layout->layout)) {
  1646. // Get admin path.
  1647. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  1648. if ($view_mode != 'form') {
  1649. $admin_path .= '/display';
  1650. }
  1651. else {
  1652. $admin_path .= '/fields';
  1653. }
  1654. // If regions aren't set we don't have to move fields.
  1655. if (isset($layout->regions)) {
  1656. $url = 'admin/structure/ds/change-layout/' . $entity_type . '/' . $bundle . '/' . $view_mode . '/' . $layout_string . '?destination=' . $admin_path;
  1657. $form['#validate'][] = 'ds_field_ui_change_layout_validate';
  1658. }
  1659. else {
  1660. $url = $admin_path;
  1661. }
  1662. if ($view_mode != 'default' && $view_mode != 'form') {
  1663. $url .= '/' . $view_mode;
  1664. }
  1665. $form['layout_changed_url'] = array(
  1666. '#type' => 'value',
  1667. '#value' => $url,
  1668. );
  1669. $form['#submit'][] = 'ds_field_ui_change_layout_submit';
  1670. }
  1671. }
  1672. }
  1673. /**
  1674. * Ajax callback for _ds_field_ui_table_layouts_preview().
  1675. */
  1676. function ds_field_ui_table_layouts_preview_callback($form, $form_state) {
  1677. return $form['additional_settings']['ds_layouts']['preview'];
  1678. }
  1679. /**
  1680. * Form validation handler for _ds_field_ui_table_layouts_preview().
  1681. */
  1682. function ds_field_ui_change_layout_validate(&$form, &$form_state) {
  1683. $key1 = array_search('ds_field_ui_layouts_save', $form['#submit']);
  1684. $key2 = array_search('ds_field_ui_fields_save', $form['#submit']);
  1685. unset($form['#submit'][$key1]);
  1686. unset($form['#submit'][$key2]);
  1687. }
  1688. /**
  1689. * Form submission handler for _ds_field_ui_table_layouts_preview().
  1690. */
  1691. function ds_field_ui_change_layout_submit($form, &$form_state) {
  1692. unset($_GET['destination']);
  1693. global $base_url;
  1694. $url = $base_url . '/' . $form_state['values']['layout_changed_url'];
  1695. $form_state['redirect'] = $url;
  1696. }
  1697. /**
  1698. * Add the fields to the Field UI form.
  1699. *
  1700. * @param $entity_type
  1701. * The name of the entity type.
  1702. * @param $bundle
  1703. * The name of the bundle
  1704. * @param $view_mode
  1705. * The name of the view_mode
  1706. * @param $form
  1707. * A collection of form properties.
  1708. * @param $form_state
  1709. * A collection of form_state properties.
  1710. */
  1711. function _ds_field_ui_fields($entity_type, $bundle, $view_mode, &$form, &$form_state) {
  1712. // Do not add the fields if there is no layout.
  1713. if (!isset($form['#ds_layout'])) {
  1714. return;
  1715. }
  1716. // Get the fields and put them on the form.
  1717. $fields = ds_get_fields($entity_type, FALSE);
  1718. // Ultimate alter on Field UI fields, only used for edge cases.
  1719. $context = array(
  1720. 'entity_type' => $entity_type,
  1721. 'bundle' => $bundle,
  1722. 'view_mode' => $view_mode,
  1723. );
  1724. // Load views file if entity type is not ds_views.
  1725. // We need to cache the hook it's implementing.
  1726. if ($entity_type != 'ds_views' && module_exists('ds_extras') && variable_get('ds_extras_vd', FALSE)) {
  1727. module_load_include('inc', 'ds_extras', 'includes/ds_extras.vd');
  1728. }
  1729. drupal_alter('ds_fields_ui', $fields, $context);
  1730. // Get field settings.
  1731. $field_settings = ds_get_field_settings($entity_type, $bundle, $view_mode, FALSE);
  1732. $form['#field_settings'] = $field_settings;
  1733. $table = &$form['fields'];
  1734. $form['#ds_fields'] = array();
  1735. $field_label_options = array(
  1736. 'above' => t('Above'),
  1737. 'inline' => t('Inline'),
  1738. 'hidden' => t('<Hidden>'),
  1739. );
  1740. drupal_alter('ds_label_options', $field_label_options);
  1741. // Regions for fields.
  1742. $field_regions = array();
  1743. if (isset($form['#ds_layout']->settings['fields'])) {
  1744. $field_regions = $form['#ds_layout']->settings['fields'];
  1745. }
  1746. foreach ($fields as $key => $field) {
  1747. // Check on ui_limit.
  1748. if (isset($field['ui_limit'])) {
  1749. $continue = TRUE;
  1750. foreach ($field['ui_limit'] as $limitation) {
  1751. list($limit_bundle, $limit_view_mode) = explode('|', $limitation);
  1752. if ($limit_bundle == '*' || $limit_bundle == $bundle) {
  1753. if ($limit_view_mode == '*' || $limit_view_mode == $view_mode) {
  1754. $continue = FALSE;
  1755. }
  1756. }
  1757. }
  1758. if ($continue) {
  1759. continue;
  1760. }
  1761. }
  1762. $form['#ds_fields'][] = $key;
  1763. // Check on formatter settings.
  1764. if (isset($form_state['formatter_settings'][$key])) {
  1765. $field['formatter_settings'] = $form_state['formatter_settings'][$key];
  1766. }
  1767. elseif (isset($field_settings[$key]['formatter_settings'])) {
  1768. $field['formatter_settings'] = $field_settings[$key]['formatter_settings'];
  1769. $form_state['formatter_settings'][$key] = $field['formatter_settings'];
  1770. }
  1771. if (!isset($field_settings[$key]['ft']) && isset($field_settings[$key]['ft'])) {
  1772. $form_state['formatter_settings'][$key]['ft'] = $field_settings[$key]['ft'];
  1773. }
  1774. $value = isset($form_state['formatter_settings']) ? $form_state['formatter_settings'] : array();
  1775. $hidden = array('hidden' => t('<Hidden>'));
  1776. $formatters = isset($field['properties']['formatters']) ? $hidden + $field['properties']['formatters'] : $hidden + array('default' => t('Default'));
  1777. $table[$key] = array(
  1778. '#row_type' => 'field',
  1779. '#js_settings' => array('field'),
  1780. '#region_callback' => 'field_ui_display_overview_row_region',
  1781. '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
  1782. 'human_name' => array(
  1783. '#markup' => check_plain($field['title']),
  1784. ),
  1785. 'weight' => array(
  1786. '#type' => 'textfield',
  1787. '#default_value' => isset($field_settings[$key]['weight']) ? $field_settings[$key]['weight'] : 0,
  1788. '#size' => 3,
  1789. '#attributes' => array('class' => array('field-weight')),
  1790. ),
  1791. 'parent_wrapper' => array(
  1792. 'parent' => array(
  1793. '#type' => 'select',
  1794. '#empty_value' => '',
  1795. '#options' => array(),
  1796. '#attributes' => array('class' => array('field-parent')),
  1797. '#parents' => array('fields', $key, 'parent'),
  1798. ),
  1799. 'hidden_name' => array(
  1800. '#type' => 'hidden',
  1801. '#default_value' => $key,
  1802. '#attributes' => array('class' => array('field-name')),
  1803. ),
  1804. ),
  1805. 'label' => array(
  1806. '#type' => 'select',
  1807. '#options' => $field_label_options,
  1808. '#default_value' => isset($field_settings[$key]['label']) ? $field_settings[$key]['label'] : 'hidden',
  1809. ),
  1810. 'format' => array(
  1811. 'type' => array(
  1812. '#type' => 'select',
  1813. '#options' => $formatters,
  1814. '#default_value' => isset($field_settings[$key]['format']) ? $field_settings[$key]['format'] : 'hidden',
  1815. '#attributes' => array('class' => array('field-formatter-type')),
  1816. ),
  1817. ),
  1818. 'settings_summary' => array(),
  1819. 'settings_edit' => array(),
  1820. );
  1821. // Don't show summary or cogwheel in hidden region.
  1822. if (_ds_field_ui_check_hidden_region($key, $form_state, $field_regions)) {
  1823. continue;
  1824. }
  1825. $field['name'] = $key;
  1826. $field['entity_type'] = $entity_type;
  1827. $field['bundle'] = $bundle;
  1828. $field['view_mode'] = $view_mode;
  1829. if ($form_state['formatter_settings_edit'] == $key) {
  1830. $table[$key]['settings_summary']['#attributes']['colspan'] = 2;
  1831. $settings_form = ds_field_settings_form($field, $form_state, $form, $view_mode);
  1832. ds_field_row_form_format_construct($table, $key, $settings_form);
  1833. }
  1834. else {
  1835. // After saving, the settings are updated here as well. First we create
  1836. // the element for the table cell.
  1837. $summary = ds_field_settings_summary($field, $form_state, $form, $view_mode);
  1838. if (isset($summary)) {
  1839. $table[$key]['settings_summary'] = $summary;
  1840. ds_field_row_form_format_summary_construct($table, $key);
  1841. }
  1842. }
  1843. }
  1844. // Add fields submit handler.
  1845. $form['#submit'][] = 'ds_field_ui_fields_save';
  1846. }
  1847. /**
  1848. * Alter the core field on the the Field UI form.
  1849. *
  1850. * @param $entity_type
  1851. * The name of the entity type.
  1852. * @param $bundle
  1853. * The name of the bundle
  1854. * @param $view_mode
  1855. * The name of the view_mode
  1856. * @param $form
  1857. * A collection of form properties.
  1858. * @param $form_state
  1859. * A collection of form_state properties.
  1860. */
  1861. function _ds_field_ui_core_fields($entity_type, $bundle, $view_mode, &$form, &$form_state) {
  1862. $entity_type = $form['#entity_type'];
  1863. $bundle = $form['#bundle'];
  1864. $view_mode = $form['#view_mode'];
  1865. // Gather type information.
  1866. $instances = field_info_instances($entity_type, $bundle);
  1867. $field_types = field_info_field_types();
  1868. $extra_fields = field_info_extra_fields($entity_type, $bundle, 'display');
  1869. $table = &$form['fields'];
  1870. // Regions for fields.
  1871. $field_regions = array();
  1872. if (isset($form['#ds_layout']->settings['fields'])) {
  1873. $field_regions = $form['#ds_layout']->settings['fields'];
  1874. }
  1875. // Field rows.
  1876. foreach ($instances as $key => $instance) {
  1877. // Don't show summary or cogwheel in hidden region.
  1878. if (_ds_field_ui_check_hidden_region($key, $form_state, $field_regions)) {
  1879. $table[$key]['settings_summary']['#markup'] = '';
  1880. $table[$key]['settings_edit'] = array();
  1881. continue;
  1882. }
  1883. $field = field_info_field($instance['field_name']);
  1884. $display = $instance['display'][$view_mode];
  1885. // Check the currently selected formatter, and merge persisted values for
  1886. // formatter settings.
  1887. if (isset($form_state['values']['fields'][$key]['type'])) {
  1888. $formatter_type = $form_state['values']['fields'][$key]['type'];
  1889. }
  1890. else {
  1891. $formatter_type = $display['type'];
  1892. }
  1893. $settings = $display['settings'];
  1894. if (isset($form_state['formatter_settings'][$key])) {
  1895. $settings = array_merge($settings, $form_state['formatter_settings'][$key]);
  1896. }
  1897. $settings += field_info_formatter_settings($formatter_type);
  1898. // Import field settings and merge with Field API settings.
  1899. if (!isset($form_state['formatter_settings'][$key]) && !empty($form['#field_settings'][$key]['formatter_settings']['ft'])) {
  1900. $form_state['formatter_settings'][$key] = $settings;
  1901. $form_state['formatter_settings'][$key]['ft'] = $form['#field_settings'][$key]['formatter_settings']['ft'];
  1902. }
  1903. // Change default value or Field API format, so we can change the right
  1904. // settings form when clicking on the cogwheel.
  1905. $form['fields'][$key]['format']['type']['#default_value'] = $formatter_type;
  1906. $instance['display'][$view_mode]['type'] = $formatter_type;
  1907. $formatter = field_info_formatter_types($formatter_type);
  1908. $instance['display'][$view_mode]['module'] = $formatter['module'];
  1909. $instance['display'][$view_mode]['settings'] = $settings;
  1910. // Base button element for the various formatter settings actions.
  1911. $base_button = array(
  1912. '#submit' => array(
  1913. 'field_ui_display_overview_multistep_submit',
  1914. ),
  1915. '#ajax' => array(
  1916. 'callback' => 'field_ui_display_overview_multistep_js',
  1917. 'wrapper' => 'field-display-overview-wrapper',
  1918. 'effect' => 'fade',
  1919. ),
  1920. '#field_name' => $key,
  1921. );
  1922. if ($form_state['formatter_settings_edit'] == $key) {
  1923. $formatter_type = $form_state['complete form']['fields'][$key]['format']['type']['#default_value'];
  1924. $formatter = field_info_formatter_types($formatter_type);
  1925. $instance['display'][$view_mode]['type'] = $formatter_type;
  1926. $instance['display'][$view_mode]['module'] = $formatter['module'];
  1927. $instance['display'][$view_mode]['settings'] = $settings + field_info_formatter_settings($formatter_type);
  1928. $function = $formatter['module'] . '_field_formatter_settings_form';
  1929. // Add the default formatter settings if any.
  1930. $settings_form = array();
  1931. if (function_exists($function)) {
  1932. $settings_form = $function($field, $instance, $view_mode, $form, $form_state);
  1933. }
  1934. // Add the field templates form when needed
  1935. if (module_exists('ds_extras') && variable_get('ds_extras_field_template', FALSE)) {
  1936. $context = array(
  1937. 'instance' => $instance,
  1938. 'view_mode' => $view_mode,
  1939. );
  1940. // Load the form
  1941. module_load_include('inc', 'ds_extras', 'includes/ds_extras.admin');
  1942. ds_extras_field_template_settings_form($settings_form, $form_state, $context);
  1943. }
  1944. // Allow other modules to alter the formatter settings form.
  1945. $context = array(
  1946. 'module' => $formatter['module'],
  1947. 'formatter' => $formatter,
  1948. 'field' => $field,
  1949. 'instance' => $instance,
  1950. 'view_mode' => $view_mode,
  1951. 'form' => $form,
  1952. 'form_state' => $form_state,
  1953. );
  1954. drupal_alter('field_formatter_settings_form', $settings_form, $context);
  1955. if ($settings_form) {
  1956. $table[$key]['format']['#cell_attributes'] = array('colspan' => 3);
  1957. $table[$key]['format']['settings_edit_form'] = array(
  1958. '#type' => 'container',
  1959. '#attributes' => array('class' => array('field-formatter-settings-edit-form')),
  1960. '#parents' => array('fields', $key, 'settings_edit_form'),
  1961. 'label' => array(
  1962. '#markup' => t('Format settings:') . ' <span class="formatter-name">' . $formatter['label'] . '</span>',
  1963. ),
  1964. 'settings' => $settings_form,
  1965. 'actions' => array(
  1966. '#type' => 'actions',
  1967. 'save_settings' => $base_button + array(
  1968. '#type' => 'submit',
  1969. '#name' => $key . '_formatter_settings_update',
  1970. '#value' => t('Update'),
  1971. '#op' => 'update',
  1972. ),
  1973. 'cancel_settings' => $base_button + array(
  1974. '#type' => 'submit',
  1975. '#name' => $key . '_formatter_settings_cancel',
  1976. '#value' => t('Cancel'),
  1977. '#op' => 'cancel',
  1978. // Do not check errors for the 'Cancel' button, but make sure we
  1979. // get the value of the 'formatter type' select.
  1980. '#limit_validation_errors' => array(array('fields', $key, 'type')),
  1981. ),
  1982. ),
  1983. );
  1984. $table[$key]['#attributes']['class'][] = 'field-formatter-settings-editing';
  1985. $table[$key]['format']['type']['#attributes']['class'] = array('element-invisible');
  1986. }
  1987. }
  1988. else {
  1989. $summary = module_invoke($formatter['module'], 'field_formatter_settings_summary', $field, $instance, $view_mode);
  1990. // Allow other modules to alter the formatter summary.
  1991. $context = array(
  1992. 'module' => $formatter['module'],
  1993. 'formatter' => $formatter,
  1994. 'field' => $field,
  1995. 'instance' => $instance,
  1996. 'view_mode' => $view_mode,
  1997. );
  1998. drupal_alter('field_formatter_settings_summary', $summary, $context);
  1999. if (module_exists('ds_extras') && variable_get('ds_extras_field_template', FALSE)) {
  2000. module_load_include('inc', 'ds_extras', 'includes/ds_extras.admin');
  2001. // Field template summary
  2002. $functions = module_invoke_all('ds_field_theme_functions_info');
  2003. $default_field_function = variable_get('ft-default', 'theme_field');
  2004. $field_function = isset($form_state['formatter_settings'][$key]['ft']['func']) ? $form_state['formatter_settings'][$key]['ft']['func'] : $default_field_function;
  2005. if (!empty($summary)) {
  2006. $summary .= '<br />';
  2007. }
  2008. $summary .= 'Field template: ' . check_plain($functions[$field_function]) . '<br />';
  2009. }
  2010. if (!empty($summary)) {
  2011. $table[$key]['settings_summary'] = array();
  2012. $table[$key]['settings_edit'] = array();
  2013. $table[$key]['settings_summary'] = array(
  2014. '#markup' => '<div class="field-formatter-summary">' . $summary . '</div>',
  2015. '#cell_attributes' => array('class' => array('field-formatter-summary-cell')),
  2016. );
  2017. // Render the other part of the summary
  2018. ds_field_row_form_format_summary_construct($table, $key);
  2019. }
  2020. }
  2021. }
  2022. }
  2023. /**
  2024. * Helper function to check if we are in a hidden region or not.
  2025. */
  2026. function _ds_field_ui_check_hidden_region($key, $form_state, $field_regions) {
  2027. $continue = FALSE;
  2028. if (isset($form_state['input']['fields'][$key]['region'])) {
  2029. if ($form_state['input']['fields'][$key]['region'] == 'hidden') {
  2030. $continue = TRUE;
  2031. }
  2032. }
  2033. elseif (!isset($field_regions[$key]) || $field_regions[$key] == 'hidden') {
  2034. $continue = TRUE;
  2035. }
  2036. return $continue;
  2037. }
  2038. /**
  2039. * Helper function for building the formatter settings.
  2040. */
  2041. function ds_field_row_form_format_construct(&$table, $key, $settings_form) {
  2042. $base_button = array(
  2043. '#submit' => array(
  2044. 'field_ui_display_overview_multistep_submit',
  2045. ),
  2046. '#validate' => array(
  2047. 'ds_field_ui_fields_validate',
  2048. ),
  2049. '#ajax' => array(
  2050. 'callback' => 'field_ui_display_overview_multistep_js',
  2051. 'wrapper' => 'field-display-overview-wrapper',
  2052. 'effect' => 'fade',
  2053. ),
  2054. '#field_name' => $key,
  2055. );
  2056. $table[$key]['format']['settings_edit'] = array(
  2057. '#type' => 'container',
  2058. '#attributes' => array('class' => array('field-formatter-settings-edit-form')),
  2059. '#parents' => array('fields', $key, 'settings_edit_form'),
  2060. '#weight' => -5,
  2061. // Create a settings form where hooks can pick in.
  2062. 'settings' => $settings_form,
  2063. 'actions' => array(
  2064. '#type' => 'actions',
  2065. 'save_settings' => $base_button + array(
  2066. '#type' => 'submit',
  2067. '#name' => $key . '_formatter_settings_update',
  2068. '#value' => t('Update'),
  2069. '#op' => 'update',
  2070. ),
  2071. 'cancel_settings' => $base_button + array(
  2072. '#type' => 'submit',
  2073. '#name' => $key . '_formatter_settings_cancel',
  2074. '#value' => t('Cancel'),
  2075. '#op' => 'cancel',
  2076. // Do not check errors for the 'Cancel' button.
  2077. '#limit_validation_errors' => array(),
  2078. ),
  2079. ),
  2080. );
  2081. $table[$key]['#attributes']['class'][] = 'field-formatter-settings-editing';
  2082. $table[$key]['format']['type']['#attributes']['class'] = array('element-invisible');
  2083. }
  2084. /**
  2085. * Helper function for formatter summary settings.
  2086. */
  2087. function ds_field_row_form_format_summary_construct(&$table, $key) {
  2088. $base_button = array(
  2089. '#submit' => array('field_ui_display_overview_multistep_submit'),
  2090. '#ajax' => array(
  2091. 'callback' => 'field_ui_display_overview_multistep_js',
  2092. 'wrapper' => 'field-display-overview-wrapper',
  2093. 'effect' => 'fade',
  2094. ),
  2095. '#field_name' => $key,
  2096. );
  2097. // Add the configure button.
  2098. $table[$key]['settings_edit'] = $base_button + array(
  2099. '#type' => 'image_button',
  2100. '#name' => $key . '_formatter_settings_edit',
  2101. '#src' => 'misc/configure.png',
  2102. '#attributes' => array('class' => array('field-formatter-settings-edit'), 'alt' => t('Edit')),
  2103. '#op' => 'edit',
  2104. // Do not check errors for the 'Edit' button.
  2105. '#limit_validation_errors' => array(),
  2106. '#prefix' => '<div class="field-formatter-settings-edit-wrapper">',
  2107. '#suffix' => '</div>',
  2108. );
  2109. }
  2110. /**
  2111. * Add tab for adding new fields on the fly.
  2112. *
  2113. * @param $entity_type
  2114. * The name of the entity type.
  2115. * @param $bundle
  2116. * The name of the bundle
  2117. * @param $view_mode
  2118. * The name of the view_mode
  2119. * @param $form
  2120. * A collection of form properties.
  2121. * @param $form_state
  2122. * A collection of form_state properties.
  2123. */
  2124. function _ds_field_ui_custom_fields($entity_type, $bundle, $view_mode, &$form, $form_state) {
  2125. $form['additional_settings']['add_custom_fields'] = array(
  2126. '#type' => 'fieldset',
  2127. '#title' => t('Custom fields'),
  2128. '#description' => t('Click on one of the buttons to create a new field.') . '<p></p>',
  2129. '#access' => user_access('admin_fields'),
  2130. );
  2131. // Include the CTools tools that we need.
  2132. ctools_include('ajax');
  2133. ctools_include('modal');
  2134. // Add CTools' javascript to the page.
  2135. ctools_modal_add_js();
  2136. $field_types = array(
  2137. 'custom_field' => t('Add a code field'),
  2138. 'manage_ctools' => t('Add a dynamic field'),
  2139. );
  2140. if (module_exists('block')) {
  2141. $field_types['manage_block'] = t('Add a block field');
  2142. }
  2143. $field_types['manage_preprocess'] = t('Add a preprocess field');
  2144. foreach ($field_types as $field_key => $field_title) {
  2145. $form['ctools_add_field_' . $field_key . '_url'] = array(
  2146. '#type' => 'hidden',
  2147. '#attributes' => array('class' => array('ctools_add_field_' . $field_key . '-url')),
  2148. '#value' => url('admin/structure/ds/nojs/add_field/' . $field_key),
  2149. );
  2150. $form['additional_settings']['add_custom_fields']['ctools_add_field_' . $field_key] = array(
  2151. '#type' => 'button',
  2152. '#value' => $field_title,
  2153. '#attributes' => array('class' => array('ctools-use-modal')),
  2154. '#id' => 'ctools_add_field_' . $field_key,
  2155. );
  2156. }
  2157. $form['additional_settings']['add_custom_fields']['manage_fields'] = array(
  2158. '#type' => 'link',
  2159. '#title' => 'Manage fields',
  2160. '#href' => 'admin/structure/ds/fields',
  2161. '#prefix' => '<div>',
  2162. '#suffix' => '</div>',
  2163. );
  2164. }
  2165. /**
  2166. * Utility function to check if we need to save anything for this field.
  2167. */
  2168. function _ds_field_valid($key, $field, &$form_state, $view_mode = 'default') {
  2169. $continue = FALSE;
  2170. // Ignore the Field group module and the region to block plugin.
  2171. if ($key == '_add_new_group' || $key == '_add_new_field' || $key == '_add_new_block_region') {
  2172. $continue = TRUE;
  2173. }
  2174. // If the field is in hidden region, do not save. Check if the
  2175. // field has a type key which means it's from Field API and
  2176. // we need to reset that type to 'hidden' so it doesn't get
  2177. // fired by Field API in the frontend.
  2178. if (isset($field['region']) && $field['region'] == 'hidden') {
  2179. if (isset($field['type']) && $view_mode != 'form') {
  2180. $form_state['values']['fields'][$key]['type'] = 'hidden';
  2181. }
  2182. // In case of a form, fields will be set with #access to FALSE.
  2183. if ($view_mode != 'form') {
  2184. $continue = TRUE;
  2185. }
  2186. }
  2187. return $continue;
  2188. }
  2189. /**
  2190. * Utility function to return CSS classes.
  2191. */
  2192. function _ds_classes($name = 'ds_classes_regions') {
  2193. static $classes = array();
  2194. if (!isset($classes[$name])) {
  2195. $classes[$name] = array();
  2196. $custom_classes = trim(variable_get($name, ''));
  2197. if (!empty($custom_classes)) {
  2198. $classes[$name][''] = t('None');
  2199. $custom_classes = explode("\n", $custom_classes);
  2200. foreach ($custom_classes as $key => $value) {
  2201. $classes_splitted = explode("|", $value);
  2202. $key = trim($classes_splitted[0]);
  2203. $friendly_name = isset($classes_splitted[1]) ? trim($classes_splitted[1]) : $key;
  2204. $classes[$name][check_plain($key)] = $friendly_name;
  2205. }
  2206. }
  2207. $name_clone = $name; // Prevent the name from being changed.
  2208. drupal_alter('ds_classes', $classes[$name], $name_clone);
  2209. }
  2210. return $classes[$name];
  2211. }
  2212. /**
  2213. * Utility function to sort a multidimensional array by a value in a sub-array.
  2214. *
  2215. * @param $a
  2216. * The array to sort.
  2217. * @param $subkey
  2218. * The subkey to sort by.
  2219. */
  2220. function _ds_sort_fields($a, $subkey) {
  2221. foreach ($a as $k => $v) {
  2222. if (isset($v[$subkey])) {
  2223. $b[$k] = $v[$subkey];
  2224. }
  2225. }
  2226. asort($b);
  2227. foreach ($b as $key => $val) {
  2228. $c[$key] = $a[$key];
  2229. }
  2230. return $c;
  2231. }
Error | ELMSLN API

Error

×

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/elmsln_community/api.elmsln.org/includes/common.inc:2791) in drupal_send_headers() (line 1499 of /var/www/html/elmsln_community/api.elmsln.org/includes/bootstrap.inc).
  • Error: Call to undefined function apc_delete() in DrupalAPCCache->clear() (line 289 of /var/www/html/elmsln_community/api.elmsln.org/sites/all/modules/apc/drupal_apc_cache.inc).
The website encountered an unexpected error. Please try again later.