ds.field_ui.inc 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642
  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. // Additional settings
  658. if (isset($form_state['values']['additional_settings']['preview']['info']['settings']['disable_css'])) {
  659. $record->settings['layout_disable_css'] = $form_state['values']['additional_settings']['preview']['info']['settings']['disable_css'];
  660. }
  661. else {
  662. $record->settings['layout_disable_css'] = FALSE;
  663. }
  664. $record->settings = $record->settings;
  665. // Let other modules alter the layout settings.
  666. drupal_alter('ds_layout_settings', $record, $form_state);
  667. $l = $form['#ds_layout'];
  668. if ($l->export_type == 2) {
  669. drupal_write_record('ds_layout_settings', $record);
  670. }
  671. else {
  672. drupal_write_record('ds_layout_settings', $record, array('id'));
  673. }
  674. // Clear entity info cache.
  675. cache_clear_all('entity_info', 'cache', TRUE);
  676. }
  677. }
  678. /**
  679. * Form validation handler for _ds_field_ui_fields().
  680. */
  681. function ds_field_ui_fields_validate($form, &$form_state) {
  682. foreach (element_children($form['fields']) as $key) {
  683. if (isset($form_state['values']['fields'][$key]['settings_edit_form'])) {
  684. $settings = isset($form_state['values']['fields'][$key]['settings_edit_form']['settings']['ft']) ? $form_state['values']['fields'][$key]['settings_edit_form']['settings']['ft'] : array();
  685. if (!empty($settings)) {
  686. $merge = isset($form_state['formatter_settings'][$key]['ft']) ? $form_state['formatter_settings'][$key]['ft'] : array();
  687. $form_state['formatter_settings'][$key]['ft'] = array_merge($merge, $settings);
  688. }
  689. }
  690. }
  691. }
  692. /**
  693. * Save the field settings from the 'Manage display' screen.
  694. */
  695. function ds_field_ui_fields_save($form, &$form_state) {
  696. // Setup some variables.
  697. $entity_type = $form['#entity_type'];
  698. $bundle = $form['#bundle'];
  699. $view_mode = $form['#view_mode'];
  700. // Delete previous field configuration configuration.
  701. db_delete('ds_field_settings')
  702. ->condition('entity_type', $entity_type)
  703. ->condition('bundle', $bundle)
  704. ->condition('view_mode', $view_mode)
  705. ->execute();
  706. if (empty($form_state['layout_saved'])) {
  707. return;
  708. }
  709. $field_settings = array();
  710. // Save settings for each field.
  711. $fields = $form['#ds_fields'];
  712. foreach ($fields as $key => $field) {
  713. // Field settings.
  714. $field_values = $form_state['values']['fields'][$field];
  715. // In case the region is hidden, do not save.
  716. if (isset($field_values['region']) && $field_values['region'] == 'hidden') {
  717. continue;
  718. }
  719. // Build settings.
  720. $settings = array();
  721. $settings['weight'] = $field_values['weight'];
  722. $settings['label'] = $field_values['label'];
  723. $settings['format'] = $field_values['format']['type'];
  724. // Any formatter settings.
  725. if (isset($form_state['formatter_settings'][$field])) {
  726. $settings['formatter_settings'] = $form_state['formatter_settings'][$field];
  727. }
  728. $field_settings[$field] = $settings;
  729. }
  730. // Allow other modules to modify the field settings before they get saved.
  731. drupal_alter('ds_field_settings', $field_settings, $form, $form_state);
  732. // Save the record.
  733. if (!empty($field_settings)) {
  734. $record = new stdClass;
  735. $record->id = $form['#export_id'];
  736. $record->entity_type = $entity_type;
  737. $record->bundle = $bundle;
  738. $record->view_mode = $view_mode;
  739. $record->settings = $field_settings;
  740. drupal_write_record('ds_field_settings', $record);
  741. }
  742. // Clear the ds_fields cache.
  743. cache_clear_all('ds_fields:', 'cache', TRUE);
  744. cache_clear_all('ds_field_settings', 'cache');
  745. }
  746. /**
  747. * Clone a fields layout.
  748. */
  749. function ds_field_ui_layout_clone($form, &$form_state) {
  750. $clone = $form_state['values']['additional_settings']['clone'];
  751. $entity_type = $form['#entity_type'];
  752. $bundle = $form['#bundle'];
  753. $view_mode = $form['#view_mode'];
  754. ctools_include('export');
  755. $layout = ctools_export_crud_load('ds_layout_settings', $clone);
  756. // Delete previous layout settings configuration.
  757. db_delete('ds_layout_settings')
  758. ->condition('entity_type', $entity_type)
  759. ->condition('bundle', $bundle)
  760. ->condition('view_mode', $view_mode)
  761. ->execute();
  762. // Delete previous field configuration configuration.
  763. db_delete('ds_field_settings')
  764. ->condition('entity_type', $entity_type)
  765. ->condition('bundle', $bundle)
  766. ->condition('view_mode', $view_mode)
  767. ->execute();
  768. // Save new layout record for ds.
  769. if ($layout) {
  770. $record = new stdClass();
  771. $record->id = $form['#export_id'];
  772. $record->entity_type = $entity_type;
  773. $record->bundle = $bundle;
  774. $record->view_mode = $view_mode;
  775. $record->layout = $layout->layout;
  776. $record->settings = $layout->settings;
  777. // Let other modules alter the layout settings.
  778. drupal_alter('ds_layout_settings', $record, $form_state);
  779. // Save layout record.
  780. drupal_write_record('ds_layout_settings', $record);
  781. // Copy the view mode settings.
  782. list($ce, $cb, $cv) = explode('|', $clone);
  783. _ds_field_ui_clone_view_mode_settings($entity_type, $bundle, $view_mode, $cv);
  784. // Clear entity info cache.
  785. cache_clear_all('entity_info', 'cache', TRUE);
  786. // Show message.
  787. drupal_set_message(t('The layout has been cloned.'));
  788. }
  789. else {
  790. drupal_set_message(t('No layout was cloned.'));
  791. }
  792. }
  793. /**
  794. * Populates display settings for a new view mode from the another view mode.
  795. *
  796. * This is almost a straight copy from Field UI, but with the addition
  797. * that we can pass the view mode from which we want to clone from.
  798. */
  799. function _ds_field_ui_clone_view_mode_settings($entity_type, $bundle, $view_mode, $copy_view_mode) {
  800. $settings = field_bundle_settings($entity_type, $bundle);
  801. // Update display settings for field instances.
  802. $instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle));
  803. foreach ($instances as $instance) {
  804. // If this field instance has display settings defined for this view mode,
  805. // respect those settings.
  806. if (isset($instance['display'][$copy_view_mode])) {
  807. $instance['display'][$view_mode] = $instance['display'][$copy_view_mode];
  808. field_update_instance($instance);
  809. }
  810. }
  811. // Update display settings for 'extra fields'.
  812. foreach (array_keys($settings['extra_fields']['display']) as $name) {
  813. if (isset($settings['extra_fields']['display'][$name][$copy_view_mode])) {
  814. $settings['extra_fields']['display'][$name][$view_mode] = $settings['extra_fields']['display'][$name][$copy_view_mode];
  815. }
  816. }
  817. // Save the settings.
  818. field_bundle_settings($entity_type, $bundle, $settings);
  819. }
  820. /**
  821. * Implements hook_field_formatter_settings_form().
  822. */
  823. function ds_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  824. $display = $instance['display'][$view_mode];
  825. $settings = $display['settings'];
  826. // Taxonomy view modes.
  827. if ($display['type'] === 'ds_taxonomy_view_mode') {
  828. $options = array();
  829. $view_modes = ds_entity_view_modes('taxonomy_term');
  830. foreach ($view_modes as $key => $info) {
  831. $options[$key] = $info['label'];
  832. }
  833. $element['taxonomy_term_reference_view_mode'] = array(
  834. '#title' => t('View mode'),
  835. '#type' => 'select',
  836. '#options' => $options,
  837. '#default_value' => $settings['taxonomy_term_reference_view_mode'],
  838. );
  839. $element['use_content_language'] = array(
  840. '#type' => 'checkbox',
  841. '#title' => t('Use current content language'),
  842. '#default_value' => $settings['use_content_language'],
  843. );
  844. return $element;
  845. }
  846. // Taxonomy separated.
  847. if ($display['type'] === 'ds_taxonomy_separator' || $display['type'] == 'ds_taxonomy_separator_localized') {
  848. $element['taxonomy_term_link'] = array(
  849. '#title' => t('Link to term'),
  850. '#type' => 'checkbox',
  851. '#size' => 10,
  852. '#default_value' => $settings['taxonomy_term_link'],
  853. );
  854. $separators = array(
  855. ' ' => t('space'),
  856. ', ' => t('comma'),
  857. ' - ' => t('dash'),
  858. ' / ' => t('slash'),
  859. );
  860. drupal_alter('ds_taxonomy_term_separators', $separators);
  861. $element['taxonomy_term_separator'] = array(
  862. '#title' => t('Separator'),
  863. '#type' => 'select',
  864. '#options' => $separators,
  865. '#default_value' => $settings['taxonomy_term_separator'],
  866. '#states' => array(
  867. 'visible' => array(
  868. 'select[name="fields[field_tags][settings_edit_form][settings][taxonomy_term_list]"]' => array('value' => 'separated_list'),
  869. ),
  870. ),
  871. );
  872. return $element;
  873. }
  874. }
  875. /**
  876. * Implements hook_field_formatter_settings_summary().
  877. */
  878. function ds_field_formatter_settings_summary($field, $instance, $view_mode) {
  879. $summary = '';
  880. $display = $instance['display'][$view_mode];
  881. $settings = $display['settings'];
  882. if ($display['type'] === 'ds_taxonomy_view_mode') {
  883. $entity_info = entity_get_info('taxonomy_term');
  884. $modes = $entity_info['view modes'];
  885. $mode = $modes[$settings['taxonomy_term_reference_view_mode']]['label'];
  886. $summary .= t('View mode: %mode', array('%mode' => $mode)) . '<br />';;
  887. $summary .= !empty($settings['use_content_language']) ? t('Use current content language') : t('Use field language');
  888. }
  889. if ($display['type'] === 'ds_taxonomy_separator' || $display['type'] == 'ds_taxonomy_separator_localized') {
  890. $separators = array(
  891. ' ' => t('space'),
  892. ', ' => t('comma'),
  893. ' - ' => t('dash'),
  894. ' / ' => t('slash'),
  895. );
  896. drupal_alter('ds_taxonomy_term_separators', $separators);
  897. $summary .= t('Separated by !sep', array('!sep' => $separators[$settings['taxonomy_term_separator']]));
  898. $summary .= $settings['taxonomy_term_link'] ? ', ' . t('linked') : ', ' . t('not linked');
  899. }
  900. return $summary;
  901. }
  902. /**
  903. * Creates a summary for the field format configuration summary.
  904. *
  905. * @param $field
  906. * The configuration of the field.
  907. *
  908. * @return $summary
  909. * An markup array.
  910. */
  911. function ds_field_settings_summary($field, $form_state, $form, $view_mode) {
  912. $summary = '';
  913. // Not all fields have settings.
  914. if (isset($field['properties']['settings'])) {
  915. $summary = module_invoke($field['module'], 'ds_field_format_summary', $field);
  916. }
  917. if (module_exists('ds_extras') && variable_get('ds_extras_field_template', FALSE)) {
  918. module_load_include('inc', 'ds_extras', 'includes/ds_extras.admin');
  919. // Field template summary
  920. if (!in_array($field['field_type'], array(DS_FIELD_TYPE_IGNORE, DS_FIELD_TYPE_PREPROCESS))) {
  921. $functions = module_invoke_all('ds_field_theme_functions_info');
  922. $default_field_function = variable_get('ft-default', 'theme_field');
  923. $field_function = isset($form_state['formatter_settings'][$field['name']]['ft']['func']) ? $form_state['formatter_settings'][$field['name']]['ft']['func'] : $default_field_function;
  924. $summary .= 'Field template: ' . check_plain($functions[$field_function]) . '<br />';
  925. }
  926. }
  927. if (!empty($form_state['complete form'])) {
  928. $formatter_name = $form_state['complete form']['fields'][$field['name']]['format']['type']['#value'];
  929. }
  930. else {
  931. $formatter_name = $form['fields'][$field['name']]['format']['type']['#default_value'];
  932. }
  933. // Allow other modules to alter the formatter summary.
  934. $context = array(
  935. 'formatter' => $formatter_name,
  936. 'field' => $field,
  937. 'instance' => array(
  938. 'display' => array(
  939. $view_mode => array(
  940. 'label' => '',
  941. 'type' => '',
  942. 'weight' => '',
  943. 'settings' => isset($field['formatter_settings']) ? $field['formatter_settings'] : array(),
  944. 'module' => '',
  945. )
  946. )
  947. ),
  948. 'view_mode' => $view_mode,
  949. 'ds' => TRUE,
  950. );
  951. drupal_alter('field_formatter_settings_summary', $summary, $context);
  952. if (empty($summary)) {
  953. return NULL; // no summary return nothing
  954. }
  955. return array(
  956. '#markup' => '<div class="field-formatter-summary">' . $summary . '</div>',
  957. '#cell_attributes' => array('class' => array('field-formatter-summary-cell')),
  958. );
  959. }
  960. /**
  961. * Creates a form for Display Suite fields.
  962. * .
  963. * @param $field
  964. * The field definition.
  965. *
  966. * @return $form
  967. * A form definition.
  968. */
  969. function ds_field_settings_form($field, &$form_state, $entity_form, $view_mode) {
  970. $form = module_invoke($field['module'], 'ds_field_settings_form', $field);
  971. // Add field template settings to every field if enabled.
  972. if (module_exists('ds_extras') && variable_get('ds_extras_field_template', FALSE)) {
  973. $context = array(
  974. 'instance' => array(
  975. 'entity_type' => $field['entity_type'],
  976. 'bundle' => $field['bundle'],
  977. 'field_name' => $field['name'],
  978. ),
  979. 'view_mode' => $field['view_mode'],
  980. );
  981. // Load the formatter settings form
  982. module_load_include('inc', 'ds_extras', 'includes/ds_extras.admin');
  983. // Protect against empty $form.
  984. if (!is_array($form)) $form = array();
  985. if (!in_array($field['field_type'], array(DS_FIELD_TYPE_IGNORE, DS_FIELD_TYPE_PREPROCESS))) {
  986. ds_extras_field_template_settings_form($form, $form_state, $context);
  987. }
  988. else {
  989. $form['#markup'] = t('This field does not support Field templates.');
  990. }
  991. }
  992. $formatter_name = $form_state['complete form']['fields'][$field['name']]['format']['type']['#value'];
  993. // Allow other modules to alter the formatter settings form.
  994. $context = array(
  995. 'ds' => TRUE,
  996. 'formatter' => $formatter_name,
  997. 'field' => $field,
  998. 'instance' => array(
  999. 'label' => $field['title'],
  1000. 'bundle' => $field['bundle'],
  1001. 'entity_type' => $field['entity_type'],
  1002. 'display' => array(
  1003. 'default' => array(
  1004. 'settings' => isset($field['formatter_settings']) ? $field['formatter_settings'] : array(),
  1005. ),
  1006. ),
  1007. ),
  1008. 'view_mode' => $view_mode,
  1009. 'form' => $entity_form,
  1010. 'form_state' => $form_state,
  1011. );
  1012. drupal_alter('field_formatter_settings_form', $form, $context);
  1013. return $form;
  1014. }
  1015. /**
  1016. * Implements hook_ds_field_format_summary().
  1017. */
  1018. function ds_ds_field_format_summary($field) {
  1019. $summary = '';
  1020. $settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : $field['properties']['default'];
  1021. $functions = module_invoke_all('ds_field_theme_functions_info');
  1022. foreach ($settings as $key => $value) {
  1023. // Ignore Field Formatter conditions.
  1024. if ($key == 'conditions') {
  1025. continue;
  1026. }
  1027. if ($key == 'ctools') {
  1028. $conf = unserialize($value);
  1029. $summary .= t('Type: !type', array('!type' => check_plain(drupal_ucfirst(str_replace('_', ' ', $conf['subtype'])))));
  1030. }
  1031. elseif ($key == 'ft' || is_array($value)) {
  1032. // Do nothing
  1033. }
  1034. elseif (!empty($value)) {
  1035. $value = is_numeric($value) ? ($value ? t('Yes') : t('No')) : check_plain($value);
  1036. $summary .= ' ' . str_replace('_', ' ', drupal_ucfirst(check_plain($key))) . ': ' . check_plain($value) . '<br />';
  1037. }
  1038. }
  1039. if (empty($summary) && ($field['field_type'] == DS_FIELD_TYPE_CTOOLS)) {
  1040. $summary .= t('Not configured yet.') . '<br />';
  1041. }
  1042. return $summary;
  1043. }
  1044. /**
  1045. * Implements hook_ds_field_settings_form().
  1046. */
  1047. function ds_ds_field_settings_form($field) {
  1048. $form = array();
  1049. $settings = !empty($field['formatter_settings']) ? $field['formatter_settings'] : (!empty($field['properties']['default']) ? $field['properties']['default'] : array());
  1050. if (empty($field['properties']['settings'])) {
  1051. return $form;
  1052. }
  1053. foreach ($field['properties']['settings'] as $key => $value) {
  1054. switch ($value['type']) {
  1055. case 'textfield':
  1056. $form[$key] = array(
  1057. '#type' => 'textfield',
  1058. '#title' => str_replace('_', ' ', check_plain(drupal_ucfirst($key))),
  1059. '#default_value' => isset($settings[$key]) ? $settings[$key] : '',
  1060. '#size' => 40,
  1061. '#description' => isset($value['description']) ? check_plain($value['description']) : '',
  1062. );
  1063. break;
  1064. case 'select':
  1065. $form[$key] = array(
  1066. '#type' => 'select',
  1067. '#title' => check_plain(drupal_ucfirst($key)),
  1068. '#default_value' => isset($settings[$key]) ? $settings[$key] : '',
  1069. '#options' => $value['options'],
  1070. '#description' => isset($value['description']) ? check_plain($value['description']) : '',
  1071. );
  1072. break;
  1073. case 'checkbox':
  1074. $form[$key] = array(
  1075. '#type' => 'checkbox',
  1076. '#title' => str_replace('_', ' ', check_plain(drupal_ucfirst($key))),
  1077. '#default_value' => isset($settings[$key]) ? $settings[$key] : '',
  1078. '#description' => isset($value['description']) ? check_plain($value['description']) : '',
  1079. );
  1080. break;
  1081. case 'ctools':
  1082. ctools_include('modal');
  1083. ctools_include('object-cache');
  1084. ctools_modal_add_js();
  1085. $form[$key] = array(
  1086. '#type' => 'hidden',
  1087. '#default_value' => isset($settings[$key]) ? $settings[$key] : '',
  1088. '#weight' => 2,
  1089. );
  1090. $action = 'add';
  1091. $args = '';
  1092. $conf = array();
  1093. $query = array('query' => array('selection' => 1));
  1094. $title = t('Select content');
  1095. if (isset($settings[$key])) {
  1096. $query = array();
  1097. $ctools = unserialize($settings['ctools']);
  1098. $type = $ctools['type'];
  1099. $subtype = $ctools['subtype'];
  1100. $args = '/' . $type . '/' . $subtype;
  1101. $action = 'edit';
  1102. $conf = $ctools['conf'];
  1103. $title = t('Edit content');
  1104. }
  1105. $form['select'] = array(
  1106. '#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>',
  1107. '#weight' => -10,
  1108. );
  1109. $form['load_terms'] = array(
  1110. '#type' => 'checkbox',
  1111. '#title' => t('Load terms'),
  1112. '#description' => t('Toggle if you are embedding a view with term fields.'),
  1113. '#default_value' => isset($settings['load_terms']) ? $settings['load_terms'] : '',
  1114. '#weight' => -1,
  1115. );
  1116. $form['show_title']['#weight'] = 0;
  1117. $form['title_wrapper']['#weight'] = 1;
  1118. ctools_object_cache_set($field['name'], $field['name'], $conf);
  1119. break;
  1120. }
  1121. }
  1122. return $form;
  1123. }
  1124. /**
  1125. * Add entity contexts.
  1126. */
  1127. function ds_get_entity_context($entity_type) {
  1128. ctools_include('context');
  1129. $arguments = array(
  1130. array(
  1131. 'keyword' => $entity_type,
  1132. 'identifier' => drupal_ucfirst($entity_type) . ' being viewed',
  1133. 'id' => 1,
  1134. 'name' => 'entity_id:' . $entity_type,
  1135. 'settings' => array(),
  1136. ),
  1137. );
  1138. return ctools_context_get_placeholders_from_argument($arguments);
  1139. }
  1140. /**
  1141. * Return the configuration settings for the CTools field.
  1142. */
  1143. function ds_ctools_content($action = 'add', $entity_type = '', $field_name = '', $type_name = '', $subtype_name = '', $step = NULL) {
  1144. ctools_include('modal');
  1145. ctools_include('ajax');
  1146. ctools_include('content');
  1147. ctools_include('object-cache');
  1148. $commands = array();
  1149. $content_type = ctools_get_content_type($type_name);
  1150. $subtype = ctools_content_get_subtype($content_type, $subtype_name);
  1151. if ($data = ctools_object_cache_get($field_name, $field_name)) {
  1152. $conf = $data;
  1153. }
  1154. else {
  1155. $conf = ctools_content_get_defaults($content_type, $subtype);
  1156. }
  1157. $url = 'admin/structure/ds/fields/manage_ctools/content/' . $action . '/' . $entity_type . '/' . $field_name;
  1158. $base_url = $url;
  1159. if (!empty($type_name) && !empty($subtype_name)) {
  1160. $url .= '/' . $type_name . '/' . $subtype_name . '/%step';
  1161. }
  1162. $form_info = array(
  1163. 'path' => $url,
  1164. 'show cancel' => TRUE,
  1165. 'next callback' => 'ds_ctools_content_next',
  1166. );
  1167. // Get entity context.
  1168. $contexts = ds_get_entity_context($entity_type);
  1169. $form_state = array(
  1170. 'contexts' => $contexts,
  1171. 'ajax' => TRUE,
  1172. 'modal' => TRUE,
  1173. 'modal return' => TRUE,
  1174. 'field_name' => $field_name,
  1175. );
  1176. // Call the content form.
  1177. $output = ctools_content_form($action, $form_info, $form_state, $content_type, $subtype_name, $subtype, $conf, $step);
  1178. if (!empty($form_state['complete']) || isset($_GET['dismiss'])) {
  1179. $configuration = array(
  1180. 'conf' => $form_state['conf'],
  1181. 'type' => $type_name,
  1182. 'subtype' => $subtype_name,
  1183. );
  1184. $commands[] = ctools_modal_command_dismiss();
  1185. $commands[] = ajax_command_invoke('input[name="fields[' . $field_name . '][settings_edit_form][settings][ctools]"]', 'dsCtoolsContentConfiguration', array(serialize($configuration)));
  1186. $commands[] = ajax_command_invoke('.select-content-link', 'dsCtoolsContentUpdate', array(serialize($configuration)));
  1187. ctools_object_cache_clear($field_name, $field_name);
  1188. }
  1189. // Content selection
  1190. elseif (!empty($form_state['cancel']) || isset($_GET['selection'])) {
  1191. ctools_object_cache_clear($field_name, $field_name);
  1192. $commands[] = ds_ctools_content_select($contexts, $field_name, $action, $entity_type);
  1193. }
  1194. // No configuration anymore.
  1195. elseif ($output === FALSE && !isset($_GET['dismiss'])) {
  1196. $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)))));
  1197. $commands[] = ctools_modal_command_display(t('Edit content'), $output);
  1198. }
  1199. // Form render.
  1200. else {
  1201. $commands = ctools_modal_form_render($form_state, $output);
  1202. }
  1203. print ajax_render($commands);
  1204. ajax_footer();
  1205. exit;
  1206. }
  1207. /**
  1208. * Handle the 'next' click on the add/edit field form wizard.
  1209. */
  1210. function ds_ctools_content_next(&$form_state) {
  1211. ctools_object_cache_set($form_state['field_name'], $form_state['field_name'], $form_state['conf']);
  1212. }
  1213. /**
  1214. * Select content.
  1215. *
  1216. * @param $contexts
  1217. * A collection of contexts, usually the entity.
  1218. * @param $field_name
  1219. * The name of the field.
  1220. * @param $action
  1221. * The name of the action.
  1222. * @param $entity_type
  1223. * The name of the entity type.
  1224. */
  1225. function ds_ctools_content_select($contexts, $field_name, $action, $entity_type) {
  1226. // Get content types.
  1227. $content_types = ctools_content_get_available_types($contexts);
  1228. $categories = $category_names = $ordered = array();
  1229. foreach ($content_types as $type_name => $subtypes) {
  1230. foreach ($subtypes as $subtype_name => $content_type) {
  1231. list($category_key, $category) = ds_ctools_get_category($content_type);
  1232. if (empty($categories[$category_key])) {
  1233. $categories[$category_key] = array(
  1234. 'title' => $category,
  1235. 'content' => array(),
  1236. );
  1237. $category_names[$category_key] = $category;
  1238. }
  1239. $content_title = filter_xss_admin($content_type['title']);
  1240. // Ensure content with the same title doesn't overwrite each other.
  1241. while (isset($categories[$category_key]['content'][$content_title])) {
  1242. $content_title .= '-';
  1243. }
  1244. $categories[$category_key]['content'][$content_title] = $content_type;
  1245. $categories[$category_key]['content'][$content_title]['type_name'] = $type_name;
  1246. $categories[$category_key]['content'][$content_title]['subtype_name'] = $subtype_name;
  1247. }
  1248. }
  1249. // Now sort
  1250. natcasesort($category_names);
  1251. foreach ($category_names as $category => $name) {
  1252. $ordered[$category] = $categories[$category];
  1253. }
  1254. $left = '';
  1255. $right = '<div class="content">' . t('Content options are divided by category. Please select a category from the left to proceed.') . '</div>';
  1256. foreach ($ordered as $section => $section_content) {
  1257. // Section.
  1258. if ($section == 'root') {
  1259. $section_content['title'] = t('Content');
  1260. }
  1261. $left .= '<div class="section"><a href="" id="' . $section . '" class="section-link">' . $section_content['title'] . '</a></div>';
  1262. // Content.
  1263. $right .= '<div id="' . $section . '-container" class="selection-hide content">';
  1264. $right .= '<h2>' . $section_content['title'] . '</h2>';
  1265. foreach ($section_content['content'] as $key => $value) {
  1266. $right .= '<div class="content-item">';
  1267. $variables = array(
  1268. 'path' => ctools_content_admin_icon($value),
  1269. );
  1270. $right .= theme('image', $variables) . '&nbsp;';
  1271. $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);
  1272. $right .= '</div>';
  1273. }
  1274. $right .= '</div>';
  1275. }
  1276. // Create output.
  1277. $output = '<div id="ctools-content-selection">';
  1278. $output .= '<div id="ds-left">' . $left . '</div>';
  1279. $output .= '<div id="ds-right">' . $right . '</div>';
  1280. $output .= '</div>';
  1281. return ctools_modal_command_display(t('Select content'), $output);
  1282. }
  1283. /**
  1284. * Helper function to get the category.
  1285. */
  1286. function ds_ctools_get_category($content_type) {
  1287. if (isset($content_type['top level'])) {
  1288. $category = 'root';
  1289. }
  1290. elseif (isset($content_type['category'])) {
  1291. if (is_array($content_type['category'])) {
  1292. list($category, $weight) = $content_type['category'];
  1293. }
  1294. else {
  1295. $category = $content_type['category'];
  1296. }
  1297. }
  1298. else {
  1299. $category = t('Uncategorized');
  1300. }
  1301. return array(preg_replace('/[^a-z0-9]/', '-', drupal_strtolower($category)), $category);
  1302. }
  1303. /**
  1304. * Add fake field group value in.
  1305. */
  1306. function _ds_field_group_field_ui_fix_notices($form, &$form_state) {
  1307. $field_group = array(
  1308. 'group_name' => '',
  1309. 'label' => '',
  1310. );
  1311. $form_state['values']['fields']['_add_new_group'] = $field_group;
  1312. }
  1313. /**
  1314. * Add the layouts fieldset on the Field UI screen.
  1315. *
  1316. * @param $entity_type
  1317. * The name of the entity type.
  1318. * @param $bundle
  1319. * The name of the bundle
  1320. * @param $view_mode
  1321. * The name of the view_mode
  1322. * @param $form
  1323. * A collection of form properties.
  1324. */
  1325. function _ds_field_ui_table_layouts($entity_type, $bundle, $view_mode, &$form, $form_state) {
  1326. $layout_options = array();
  1327. $ds_layouts = ds_get_layout_info();
  1328. $layout_options = array('' => t('- None -'));
  1329. foreach ($ds_layouts as $key => $layout) {
  1330. $optgroup = 'Display Suite';
  1331. // Panels can not be used on Views fields and forms.
  1332. if (!empty($layout['module']) && $layout['module'] == 'panels' && isset($form_state['no_panels'])) {
  1333. continue;
  1334. }
  1335. // Create new layout option group.
  1336. if (!empty($layout['module'])) {
  1337. $optgroup = drupal_ucfirst($layout['module']);
  1338. }
  1339. if (!isset($layout_options[$optgroup])) {
  1340. $layout_options[$optgroup] = array();
  1341. }
  1342. // Stack the layout.
  1343. $layout_options[$optgroup][$key] = $layout['label'];
  1344. }
  1345. // If there is only one $optgroup, move it to the root.
  1346. if (count($layout_options) == 2) {
  1347. $options = $layout_options[$optgroup];
  1348. $layout_options = array_merge(array('' => t('- None -')), $options);
  1349. }
  1350. // Add layouts form.
  1351. $form['additional_settings']['ds_layouts'] = array(
  1352. '#type' => 'fieldset',
  1353. '#title' => t('Layout for !bundle in !view_mode', array('!bundle' => str_replace('_', ' ', $bundle), '!view_mode' => str_replace('_', ' ', $view_mode))),
  1354. '#collapsible' => TRUE,
  1355. '#collapsed' => FALSE,
  1356. '#parents' => array('additional_settings'),
  1357. '#weight' => -100,
  1358. );
  1359. ctools_include('export');
  1360. $layout = new stdClass();
  1361. $ds_layout_settings = ctools_export_crud_load_all('ds_layout_settings');
  1362. if (isset($ds_layout_settings[$form['#export_id']])) {
  1363. $layout = $ds_layout_settings[$form['#export_id']];
  1364. }
  1365. if (!empty($layout) && isset($layout->layout) && isset($ds_layouts[$layout->layout]) && empty($layout->disabled)) {
  1366. $layout->settings = $layout->settings;
  1367. $layout->regions = $ds_layouts[$layout->layout]['regions'];
  1368. $form['#ds_layout'] = $layout;
  1369. }
  1370. // The layout is defined in code.
  1371. if (isset($layout->export_type)) {
  1372. // Enabled/disable the layout.
  1373. if (empty($layout->disabled)) {
  1374. $link = t('This layout is defined in code') . ': ' . l(t('disable layout.'), 'admin/structure/ds/disable/' . $form['#export_id'], array('query' => drupal_get_destination()));
  1375. }
  1376. else {
  1377. $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()));
  1378. }
  1379. $form['additional_settings']['ds_layouts']['enable_disable'] = array(
  1380. '#markup' => $link,
  1381. '#weight' => 2,
  1382. );
  1383. // Overridden in database.
  1384. if ($layout->export_type == 3) {
  1385. $form['additional_settings']['ds_layouts']['revert'] = array(
  1386. '#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())),
  1387. '#weight' => 1,
  1388. );
  1389. }
  1390. }
  1391. // Load the layout preview form
  1392. $layout->layout_options = $layout_options;
  1393. _ds_field_ui_table_layouts_preview($form, $form_state, $ds_layouts, $layout, $entity_type, $bundle, $view_mode);
  1394. if (!empty($layout) && isset($layout->regions)) {
  1395. // Add wrappers
  1396. $wrapper_options = array(
  1397. 'div' => 'Div',
  1398. 'span' => 'Span',
  1399. 'section' => 'Section',
  1400. 'article' => 'Article',
  1401. 'header' => 'Header',
  1402. 'footer' => 'Footer',
  1403. 'aside' => 'Aside',
  1404. 'figure' => 'Figure'
  1405. );
  1406. $form['additional_settings']['region_wrapper'] = array(
  1407. '#type' => 'fieldset',
  1408. '#title' => t('Custom wrappers'),
  1409. '#description' => t('Choose a wrapper. All Display Suite layouts support this option.')
  1410. );
  1411. // Hide the fieldset in case of the reset layout.
  1412. if ($layout->layout === 'ds_reset') {
  1413. $form['additional_settings']['region_wrapper']['#access'] = FALSE;
  1414. }
  1415. foreach (array_keys($layout->regions) as $region) {
  1416. $form['additional_settings']['region_wrapper'][$region] = array(
  1417. '#type' => 'select',
  1418. '#options' => $wrapper_options,
  1419. '#title' => t('Wrapper for @region', array('@region' => $layout->regions[$region])),
  1420. '#default_value' => isset($layout->settings['wrappers'], $layout->settings['wrappers'][$region]) ? $layout->settings['wrappers'][$region] : 'div',
  1421. );
  1422. }
  1423. $form['additional_settings']['region_wrapper']['layout_wrapper'] = array(
  1424. '#type' => 'select',
  1425. '#options' => $wrapper_options,
  1426. '#title' => t('Layout wrapper'),
  1427. '#default_value' => isset($layout->settings['layout_wrapper']) ? $layout->settings['layout_wrapper'] : 'div',
  1428. '#weight' => 10,
  1429. );
  1430. $form['additional_settings']['region_wrapper']['layout_attributes'] = array(
  1431. '#type' => 'textfield',
  1432. '#title' => t('Layout attributes'),
  1433. '#description' => 'E.g. role="navigation"',
  1434. '#default_value' => isset($layout->settings['layout_attributes']) ? $layout->settings['layout_attributes'] : '',
  1435. '#weight' => 11,
  1436. );
  1437. $form['additional_settings']['region_wrapper']['layout_attributes_merge'] = array(
  1438. '#type' => 'checkbox',
  1439. '#title' => t('Merge other layout attributes'),
  1440. '#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.',
  1441. '#default_value' => isset($layout->settings['layout_attributes_merge']) ? $layout->settings['layout_attributes_merge'] : variable_get('ds_layout_attributes_merge', TRUE),
  1442. '#weight' => 12,
  1443. );
  1444. $form['additional_settings']['region_wrapper']['layout_link_attribute'] = array(
  1445. '#type' => 'select',
  1446. '#options' => array(
  1447. '' => t('No link'),
  1448. 'content' => t('Link to content'),
  1449. 'custom' => t('Custom'),
  1450. 'tokens' => t('Tokens')
  1451. ),
  1452. '#title' => t('Add link'),
  1453. '#description' => t('This will add an onclick attribute on the layout wrapper.'),
  1454. '#default_value' => isset($layout->settings['layout_link_attribute']) ? $layout->settings['layout_link_attribute'] : FALSE,
  1455. '#weight' => 12,
  1456. );
  1457. $form['additional_settings']['region_wrapper']['layout_link_custom'] = array(
  1458. '#type' => 'textfield',
  1459. '#title' => t('Custom link'),
  1460. '#description' => t('You may use tokens for this link if you selected tokens.'),
  1461. '#default_value' => isset($layout->settings['layout_link_custom']) ? $layout->settings['layout_link_custom'] : FALSE,
  1462. '#weight' => 13,
  1463. '#states' => array(
  1464. 'visible' => array(array(
  1465. ':input[name="additional_settings[region_wrapper][layout_link_attribute]"]' => array(array("value" => "tokens"), array("value" => "custom")),
  1466. )),
  1467. ),
  1468. );
  1469. if (module_exists('token')) {
  1470. $form['additional_settings']['region_wrapper']['tokens'] = array(
  1471. '#title' => t('Tokens'),
  1472. '#type' => 'container',
  1473. '#weight' => 14,
  1474. '#states' => array(
  1475. 'visible' => array(
  1476. ':input[name="additional_settings[region_wrapper][layout_link_attribute]"]' => array("value" => "tokens"),
  1477. ),
  1478. ),
  1479. );
  1480. $form['additional_settings']['region_wrapper']['tokens']['help'] = array(
  1481. '#theme' => 'token_tree',
  1482. '#token_types' => 'all',
  1483. '#global_types' => FALSE,
  1484. '#dialog' => TRUE,
  1485. );
  1486. }
  1487. // Add extra classes for the regions to have more control while theming.
  1488. $form['additional_settings']['ds_classes'] = array(
  1489. '#type' => 'fieldset',
  1490. '#title' => t('Custom classes'),
  1491. '#collapsible' => TRUE,
  1492. '#collapsed' => TRUE,
  1493. '#parents' => array('additional_settings'),
  1494. '#access' => empty($chosen_layout['flexible']),
  1495. );
  1496. $classes_access = (user_access('admin_classes') && module_exists('ds_ui'));
  1497. $classes = _ds_classes();
  1498. if (!empty($classes)) {
  1499. $form['additional_settings']['ds_classes']['layout_class'] = array(
  1500. '#type' => 'select',
  1501. '#multiple' => TRUE,
  1502. '#options' => $classes,
  1503. '#title' => t('Class for layout'),
  1504. '#default_value' => isset($layout->settings['classes']['layout_class']) ? $layout->settings['classes']['layout_class'] : '',
  1505. );
  1506. foreach (array_keys($layout->regions) as $region) {
  1507. $form['additional_settings']['ds_classes'][$region] = array(
  1508. '#type' => 'select',
  1509. '#multiple' => TRUE,
  1510. '#options' => $classes,
  1511. '#title' => t('Class for @region', array('@region' => $region)),
  1512. '#default_value' => isset($layout->settings['classes'][$region]) ? $layout->settings['classes'][$region] : '',
  1513. );
  1514. }
  1515. if ($classes_access) {
  1516. $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())));
  1517. }
  1518. elseif (user_access('admin_classes')) {
  1519. $form['additional_settings']['ds_classes']['info'] = array('#markup' => l(t('To manage region and field CSS classes, the Display Suite UI module should be enabled.'), 'admin/modules', array('query' => drupal_get_destination(), 'fragment' => 'edit-modules-display-suite')));
  1520. }
  1521. }
  1522. else {
  1523. if ($classes_access) {
  1524. $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>');
  1525. }
  1526. else {
  1527. $form['additional_settings']['ds_classes']['#access'] = FALSE;
  1528. }
  1529. }
  1530. }
  1531. else {
  1532. if ($view_mode != 'form') {
  1533. // See if we can clone from another view mode.
  1534. $options = array();
  1535. $ds_layout_settings = ctools_export_crud_load_all('ds_layout_settings');
  1536. foreach ($ds_layout_settings as $row) {
  1537. // Do not clone from form layouts.
  1538. if ($row->view_mode == 'form') {
  1539. continue;
  1540. }
  1541. if ($row->entity_type == $entity_type && $row->bundle == $bundle) {
  1542. $name = drupal_ucfirst(str_replace('_', ' ', $row->entity_type)) . ' > ' . drupal_ucfirst(str_replace('_', ' ', $row->bundle)) . ' > ' . drupal_ucfirst(str_replace('_', ' ', $row->view_mode));
  1543. if (!empty($row->disabled)) {
  1544. $name .= ' ' . t('(disabled)');
  1545. }
  1546. $options[$row->id] = $name;
  1547. }
  1548. }
  1549. if (!empty($options)) {
  1550. // Clone from another layout.
  1551. $form['additional_settings']['ds_clone'] = array(
  1552. '#type' => 'fieldset',
  1553. '#title' => t('Clone layout'),
  1554. '#collapsible' => TRUE,
  1555. '#collapsed' => TRUE,
  1556. '#parents' => array('additional_settings'),
  1557. );
  1558. $form['additional_settings']['ds_clone']['clone'] = array(
  1559. '#title' => t('Select an existing layout to clone.'),
  1560. '#type' => 'select',
  1561. '#options' => $options,
  1562. '#weight' => 20,
  1563. );
  1564. $form['additional_settings']['ds_clone']['clone_submit'] = array(
  1565. '#type' => 'submit',
  1566. '#value' => t('Clone layout'),
  1567. '#submit' => array('ds_field_ui_layout_clone'),
  1568. '#weight' => 21,
  1569. );
  1570. }
  1571. }
  1572. }
  1573. $form['additional_settings']['ds_layouts']['id'] = array(
  1574. '#type' => 'value',
  1575. '#value' => isset($layout->id) ? $layout->id : $form['#export_id'],
  1576. );
  1577. $form['additional_settings']['ds_layouts']['old_layout'] = array(
  1578. '#type' => 'value',
  1579. '#value' => isset($layout->layout) ? $layout->layout : 0,
  1580. );
  1581. // Add validate and submit handlers. Layout needs be first so
  1582. // we can reset the type key for Field API fields.
  1583. $form['#validate'][] = 'ds_field_ui_layouts_validate';
  1584. $submit = $form['#submit'];
  1585. $form['#submit'] = array('ds_field_ui_layouts_save');
  1586. $form['#submit'] = array_merge($form['#submit'], $submit);
  1587. }
  1588. /**
  1589. * Add the layout previews to the Field UI screen.
  1590. *
  1591. * @param $form
  1592. * A collection of form properties.
  1593. * @param $form_state
  1594. * The state of the form
  1595. * @param $ds_layouts
  1596. * Collection of all the layouts
  1597. * @param $layout
  1598. * Current selected layout
  1599. * @param $entity_type
  1600. * The name of the entity type.
  1601. * @param $bundle
  1602. * The name of the bundle
  1603. * @param $view_mode
  1604. * The name of the view_mode
  1605. */
  1606. function _ds_field_ui_table_layouts_preview(&$form, &$form_state, $ds_layouts, $layout, $entity_type, $bundle, $view_mode) {
  1607. $layout_string = '';
  1608. $form['additional_settings']['ds_layouts']['layout'] = array(
  1609. '#type' => 'select',
  1610. '#title' => t('Select a layout'),
  1611. '#options' => $layout->layout_options,
  1612. '#default_value' => isset($layout->layout) && empty($layout->disabled) ? $layout->layout : '',
  1613. '#prefix' => '<div class="ds-select-layout">',
  1614. '#suffix' => '</div>',
  1615. '#weight' => -1,
  1616. '#ajax' => array(
  1617. 'callback' => 'ds_field_ui_table_layouts_preview_callback',
  1618. 'wrapper' => 'ds_layout_wrapper',
  1619. ),
  1620. );
  1621. if (!isset($layout->layout)) {
  1622. $form['additional_settings']['ds_layouts']['layout']['#description'] = t("A layout must be selected to enable Display Suite functionality.");
  1623. }
  1624. $form['additional_settings']['ds_layouts']['preview'] = array(
  1625. '#type' => 'container',
  1626. '#prefix' => '<div id="ds_layout_wrapper">',
  1627. '#suffix' => '</div>',
  1628. '#weight' => -3,
  1629. );
  1630. if (isset($layout->layout) || isset($form_state['values']['additional_settings']['layout'])) {
  1631. $layout_string = isset($form_state['values']['additional_settings']['layout']) ? $form_state['values']['additional_settings']['layout'] : $layout->layout;
  1632. }
  1633. if (!empty($layout_string)) {
  1634. $chosen_layout = $ds_layouts[$layout_string];
  1635. if (empty($chosen_layout['flexible'])) {
  1636. $selected = '<strong>' . $chosen_layout['label'] . '</strong>';
  1637. $selected .= '<br/>' . t('The default template can be found in %path', array('%path' => $chosen_layout['path']));
  1638. $suggestions = t('Template suggestions') . ':<ul>';
  1639. $suggestions_array = array();
  1640. $suggestions_array[0] = $layout_string . '--' . $entity_type;
  1641. $suggestions_array[2] = $layout_string . '--' . $entity_type . '-' . $bundle;
  1642. $suggestions_array[4] = $layout_string . '--' . $entity_type . '--{id}';
  1643. if (!isset($form_state['no_view_mode_suggestions']) && $view_mode != 'default') {
  1644. $suggestions_array[1] = $layout_string . '--' . $entity_type . '-' . $view_mode;
  1645. $suggestions_array[3] = $layout_string . '--' . $entity_type . '-' . $bundle . '-' . $view_mode;
  1646. }
  1647. ksort($suggestions_array);
  1648. $suggestions .= '<ul><li>' . implode('.tpl.php</li><li>', $suggestions_array) . '.tpl.php</li></ul>';
  1649. }
  1650. else {
  1651. $suggestions = '';
  1652. $selected = t('You have selected the flexible %layout_label layout.', array('%layout_label' => $chosen_layout['label'], '%path' => $chosen_layout['path']));
  1653. }
  1654. if (isset($form_state['values']['additional_settings']['layout']) || (!empty($layout) && isset($layout->regions))) {
  1655. $fallback_image = drupal_get_path('module', 'ds') . '/images/preview.png';
  1656. $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');
  1657. $image = (isset($chosen_layout['image']) && !empty($chosen_layout['image'])) ? $chosen_layout['path'] . '/' . $layout_string . '.png' : $fallback_image;
  1658. if (isset($chosen_layout['panels']) && !empty($chosen_layout['panels']['icon'])) {
  1659. $image = $chosen_layout['panels']['path'] . '/' . $chosen_layout['panels']['icon'];
  1660. }
  1661. $form['additional_settings']['ds_layouts']['preview'] ['title'] = array(
  1662. '#markup' => '<div class="ds-layout-preview-title">' . $current_layout . '</div>',
  1663. );
  1664. $form['additional_settings']['ds_layouts']['preview'] ['image'] = array(
  1665. '#markup' => '<div class="ds-layout-preview-image"><img src="' . base_path() . $image . '"/></div>',
  1666. );
  1667. $form['additional_settings']['ds_layouts']['preview']['info'] = array(
  1668. '#type' => 'container',
  1669. '#attributes' => array(
  1670. 'class' => array('ds-layout-preview-suggestion'),
  1671. ),
  1672. );
  1673. $form['additional_settings']['ds_layouts']['preview']['info']['suggestions'] = array(
  1674. '#markup' => '<p>' . $selected . '</p><p>' . t('!suggestions', array('!suggestions' => strtr($suggestions, '_', '-'))) . '</p>',
  1675. );
  1676. if (!empty($chosen_layout['css'])) {
  1677. $disable_css = FALSE;
  1678. if (isset($layout->settings['layout_disable_css'])) {
  1679. $disable_css = $layout->settings['layout_disable_css'];
  1680. }
  1681. $form['additional_settings']['ds_layouts']['preview']['info']['settings']['disable_css'] = array(
  1682. '#type' => 'checkbox',
  1683. '#title' => t('Disable layout CSS styles'),
  1684. '#default_value' => $disable_css,
  1685. );
  1686. }
  1687. }
  1688. if (isset($form_state['values']['additional_settings']['layout']) && (!isset($layout->layout) || $form_state['values']['additional_settings']['layout'] != $layout->layout)) {
  1689. // Get admin path.
  1690. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  1691. if ($view_mode != 'form') {
  1692. $admin_path .= '/display';
  1693. }
  1694. else {
  1695. $admin_path .= '/fields';
  1696. }
  1697. // If regions aren't set we don't have to move fields.
  1698. if (isset($layout->regions)) {
  1699. $url = 'admin/structure/ds/change-layout/' . $entity_type . '/' . $bundle . '/' . $view_mode . '/' . $layout_string . '?destination=' . $admin_path;
  1700. $form['#validate'][] = 'ds_field_ui_change_layout_validate';
  1701. }
  1702. else {
  1703. $url = $admin_path;
  1704. }
  1705. if ($view_mode != 'default' && $view_mode != 'form') {
  1706. $url .= '/' . $view_mode;
  1707. }
  1708. $form['layout_changed_url'] = array(
  1709. '#type' => 'value',
  1710. '#value' => $url,
  1711. );
  1712. $form['#submit'][] = 'ds_field_ui_change_layout_submit';
  1713. }
  1714. }
  1715. }
  1716. /**
  1717. * Ajax callback for _ds_field_ui_table_layouts_preview().
  1718. */
  1719. function ds_field_ui_table_layouts_preview_callback($form, $form_state) {
  1720. return $form['additional_settings']['ds_layouts']['preview'];
  1721. }
  1722. /**
  1723. * Form validation handler for _ds_field_ui_table_layouts_preview().
  1724. */
  1725. function ds_field_ui_change_layout_validate(&$form, &$form_state) {
  1726. $key1 = array_search('ds_field_ui_layouts_save', $form['#submit']);
  1727. $key2 = array_search('ds_field_ui_fields_save', $form['#submit']);
  1728. unset($form['#submit'][$key1]);
  1729. unset($form['#submit'][$key2]);
  1730. }
  1731. /**
  1732. * Form submission handler for _ds_field_ui_table_layouts_preview().
  1733. */
  1734. function ds_field_ui_change_layout_submit($form, &$form_state) {
  1735. $values = $form_state['values'];
  1736. if (isset($values['additional_settings']['preview']['info']['settings']['disable_css'])) {
  1737. $disable_css = $values['additional_settings']['preview']['info']['settings']['disable_css'];
  1738. }
  1739. else {
  1740. $disable_css = FALSE;
  1741. }
  1742. $record = db_select('ds_layout_settings')
  1743. ->fields('ds_layout_settings')
  1744. ->condition('entity_type', $values['ds_entity_type'])
  1745. ->condition('bundle', $values['ds_bundle'])
  1746. ->condition('view_mode', $values['ds_view_mode'])
  1747. ->execute()
  1748. ->fetchObject();
  1749. $record->settings = unserialize($record->settings);
  1750. $record->settings['layout_disable_css'] = $disable_css;
  1751. drupal_write_record('ds_layout_settings', $record, array('id'));
  1752. unset($_GET['destination']);
  1753. global $base_url;
  1754. $url = $base_url . '/' . $values['layout_changed_url'];
  1755. $form_state['redirect'] = $url;
  1756. }
  1757. /**
  1758. * Add the fields to the Field UI form.
  1759. *
  1760. * @param $entity_type
  1761. * The name of the entity type.
  1762. * @param $bundle
  1763. * The name of the bundle
  1764. * @param $view_mode
  1765. * The name of the view_mode
  1766. * @param $form
  1767. * A collection of form properties.
  1768. * @param $form_state
  1769. * A collection of form_state properties.
  1770. */
  1771. function _ds_field_ui_fields($entity_type, $bundle, $view_mode, &$form, &$form_state) {
  1772. // Do not add the fields if there is no layout.
  1773. if (!isset($form['#ds_layout'])) {
  1774. return;
  1775. }
  1776. // Get the fields and put them on the form.
  1777. $fields = ds_get_fields($entity_type, FALSE);
  1778. // Ultimate alter on Field UI fields, only used for edge cases.
  1779. $context = array(
  1780. 'entity_type' => $entity_type,
  1781. 'bundle' => $bundle,
  1782. 'view_mode' => $view_mode,
  1783. );
  1784. // Load views file if entity type is not ds_views.
  1785. // We need to cache the hook it's implementing.
  1786. if ($entity_type != 'ds_views' && module_exists('ds_extras') && variable_get('ds_extras_vd', FALSE)) {
  1787. module_load_include('inc', 'ds_extras', 'includes/ds_extras.vd');
  1788. }
  1789. drupal_alter('ds_fields_ui', $fields, $context);
  1790. // Get field settings.
  1791. $field_settings = ds_get_field_settings($entity_type, $bundle, $view_mode, FALSE);
  1792. $form['#field_settings'] = $field_settings;
  1793. $table = &$form['fields'];
  1794. $form['#ds_fields'] = array();
  1795. $field_label_options = array(
  1796. 'above' => t('Above'),
  1797. 'inline' => t('Inline'),
  1798. 'hidden' => t('<Hidden>'),
  1799. );
  1800. drupal_alter('ds_label_options', $field_label_options);
  1801. // Regions for fields.
  1802. $field_regions = array();
  1803. if (isset($form['#ds_layout']->settings['fields'])) {
  1804. $field_regions = $form['#ds_layout']->settings['fields'];
  1805. }
  1806. foreach ($fields as $key => $field) {
  1807. // Check on ui_limit.
  1808. if (!empty($field['ui_limit'])) {
  1809. $continue = TRUE;
  1810. foreach ($field['ui_limit'] as $limitation) {
  1811. list($limit_bundle, $limit_view_mode) = explode('|', $limitation);
  1812. if ($limit_bundle == '*' || $limit_bundle == $bundle) {
  1813. if ($limit_view_mode == '*' || $limit_view_mode == $view_mode) {
  1814. $continue = FALSE;
  1815. }
  1816. }
  1817. }
  1818. if ($continue) {
  1819. continue;
  1820. }
  1821. }
  1822. $form['#ds_fields'][] = $key;
  1823. // Check on formatter settings.
  1824. if (isset($form_state['formatter_settings'][$key])) {
  1825. $field['formatter_settings'] = $form_state['formatter_settings'][$key];
  1826. }
  1827. elseif (isset($field_settings[$key]['formatter_settings'])) {
  1828. $field['formatter_settings'] = $field_settings[$key]['formatter_settings'];
  1829. $form_state['formatter_settings'][$key] = $field['formatter_settings'];
  1830. }
  1831. if (!isset($field_settings[$key]['ft']) && isset($field_settings[$key]['ft'])) {
  1832. $form_state['formatter_settings'][$key]['ft'] = $field_settings[$key]['ft'];
  1833. }
  1834. $value = isset($form_state['formatter_settings']) ? $form_state['formatter_settings'] : array();
  1835. $hidden = array('hidden' => t('<Hidden>'));
  1836. $formatters = isset($field['properties']['formatters']) ? $hidden + $field['properties']['formatters'] : $hidden + array('default' => t('Default'));
  1837. $table[$key] = array(
  1838. '#row_type' => 'field',
  1839. '#js_settings' => array('field'),
  1840. '#region_callback' => 'field_ui_display_overview_row_region',
  1841. '#attributes' => array('class' => array('draggable', 'tabledrag-leaf')),
  1842. 'human_name' => array(
  1843. '#markup' => check_plain($field['title']),
  1844. ),
  1845. 'weight' => array(
  1846. '#type' => 'textfield',
  1847. '#default_value' => isset($field_settings[$key]['weight']) ? $field_settings[$key]['weight'] : 0,
  1848. '#size' => 3,
  1849. '#attributes' => array('class' => array('field-weight')),
  1850. ),
  1851. 'parent_wrapper' => array(
  1852. 'parent' => array(
  1853. '#type' => 'select',
  1854. '#empty_value' => '',
  1855. '#options' => array(),
  1856. '#attributes' => array('class' => array('field-parent')),
  1857. '#parents' => array('fields', $key, 'parent'),
  1858. ),
  1859. 'hidden_name' => array(
  1860. '#type' => 'hidden',
  1861. '#default_value' => $key,
  1862. '#attributes' => array('class' => array('field-name')),
  1863. ),
  1864. ),
  1865. 'label' => array(
  1866. '#type' => 'select',
  1867. '#options' => $field_label_options,
  1868. '#default_value' => isset($field_settings[$key]['label']) ? $field_settings[$key]['label'] : 'hidden',
  1869. ),
  1870. 'format' => array(
  1871. 'type' => array(
  1872. '#type' => 'select',
  1873. '#options' => $formatters,
  1874. '#default_value' => isset($field_settings[$key]['format']) ? $field_settings[$key]['format'] : 'hidden',
  1875. '#attributes' => array('class' => array('field-formatter-type')),
  1876. ),
  1877. ),
  1878. 'settings_summary' => array(),
  1879. 'settings_edit' => array(),
  1880. );
  1881. // Don't show summary or cogwheel in hidden region.
  1882. if (_ds_field_ui_check_hidden_region($key, $form_state, $field_regions)) {
  1883. continue;
  1884. }
  1885. $field['name'] = $key;
  1886. $field['entity_type'] = $entity_type;
  1887. $field['bundle'] = $bundle;
  1888. $field['view_mode'] = $view_mode;
  1889. if ($form_state['formatter_settings_edit'] == $key) {
  1890. $table[$key]['settings_summary']['#attributes']['colspan'] = 2;
  1891. $settings_form = ds_field_settings_form($field, $form_state, $form, $view_mode);
  1892. ds_field_row_form_format_construct($table, $key, $settings_form);
  1893. }
  1894. else {
  1895. // After saving, the settings are updated here as well. First we create
  1896. // the element for the table cell.
  1897. $summary = ds_field_settings_summary($field, $form_state, $form, $view_mode);
  1898. if (isset($summary)) {
  1899. $table[$key]['settings_summary'] = $summary;
  1900. ds_field_row_form_format_summary_construct($table, $key);
  1901. }
  1902. }
  1903. }
  1904. // Add fields submit handler.
  1905. $form['#submit'][] = 'ds_field_ui_fields_save';
  1906. }
  1907. /**
  1908. * Alter the core field on the the Field UI form.
  1909. *
  1910. * @param $entity_type
  1911. * The name of the entity type.
  1912. * @param $bundle
  1913. * The name of the bundle
  1914. * @param $view_mode
  1915. * The name of the view_mode
  1916. * @param $form
  1917. * A collection of form properties.
  1918. * @param $form_state
  1919. * A collection of form_state properties.
  1920. */
  1921. function _ds_field_ui_core_fields($entity_type, $bundle, $view_mode, &$form, &$form_state) {
  1922. $entity_type = $form['#entity_type'];
  1923. $bundle = $form['#bundle'];
  1924. $view_mode = $form['#view_mode'];
  1925. // Gather type information.
  1926. $instances = field_info_instances($entity_type, $bundle);
  1927. $field_types = field_info_field_types();
  1928. $extra_fields = field_info_extra_fields($entity_type, $bundle, 'display');
  1929. $table = &$form['fields'];
  1930. // Regions for fields.
  1931. $field_regions = array();
  1932. if (isset($form['#ds_layout']->settings['fields'])) {
  1933. $field_regions = $form['#ds_layout']->settings['fields'];
  1934. }
  1935. // Field rows.
  1936. foreach ($instances as $key => $instance) {
  1937. // Don't show summary or cogwheel in hidden region.
  1938. if (_ds_field_ui_check_hidden_region($key, $form_state, $field_regions)) {
  1939. $table[$key]['settings_summary']['#markup'] = '';
  1940. $table[$key]['settings_edit'] = array();
  1941. continue;
  1942. }
  1943. $field = field_info_field($instance['field_name']);
  1944. $display = $instance['display'][$view_mode];
  1945. // Check the currently selected formatter, and merge persisted values for
  1946. // formatter settings.
  1947. if (isset($form_state['values']['fields'][$key]['type'])) {
  1948. $formatter_type = $form_state['values']['fields'][$key]['type'];
  1949. }
  1950. else {
  1951. $formatter_type = $display['type'];
  1952. }
  1953. $settings = $display['settings'];
  1954. if (isset($form_state['formatter_settings'][$key])) {
  1955. $settings = array_merge($settings, $form_state['formatter_settings'][$key]);
  1956. }
  1957. $settings += field_info_formatter_settings($formatter_type);
  1958. // Import field settings and merge with Field API settings.
  1959. if (!isset($form_state['formatter_settings'][$key]) && !empty($form['#field_settings'][$key]['formatter_settings']['ft'])) {
  1960. $form_state['formatter_settings'][$key] = $settings;
  1961. $form_state['formatter_settings'][$key]['ft'] = $form['#field_settings'][$key]['formatter_settings']['ft'];
  1962. }
  1963. // Change default value or Field API format, so we can change the right
  1964. // settings form when clicking on the cogwheel.
  1965. $form['fields'][$key]['format']['type']['#default_value'] = $formatter_type;
  1966. $instance['display'][$view_mode]['type'] = $formatter_type;
  1967. $formatter = field_info_formatter_types($formatter_type);
  1968. $instance['display'][$view_mode]['module'] = $formatter['module'];
  1969. $instance['display'][$view_mode]['settings'] = $settings;
  1970. // Base button element for the various formatter settings actions.
  1971. $base_button = array(
  1972. '#submit' => array(
  1973. 'field_ui_display_overview_multistep_submit',
  1974. ),
  1975. '#ajax' => array(
  1976. 'callback' => 'field_ui_display_overview_multistep_js',
  1977. 'wrapper' => 'field-display-overview-wrapper',
  1978. 'effect' => 'fade',
  1979. ),
  1980. '#field_name' => $key,
  1981. );
  1982. if ($form_state['formatter_settings_edit'] == $key) {
  1983. $formatter_type = $form_state['complete form']['fields'][$key]['format']['type']['#default_value'];
  1984. $formatter = field_info_formatter_types($formatter_type);
  1985. $instance['display'][$view_mode]['type'] = $formatter_type;
  1986. $instance['display'][$view_mode]['module'] = $formatter['module'];
  1987. $instance['display'][$view_mode]['settings'] = $settings + field_info_formatter_settings($formatter_type);
  1988. $function = $formatter['module'] . '_field_formatter_settings_form';
  1989. // Add the default formatter settings if any.
  1990. $settings_form = array();
  1991. if (function_exists($function)) {
  1992. $settings_form = $function($field, $instance, $view_mode, $form, $form_state);
  1993. }
  1994. // Add the field templates form when needed
  1995. if (module_exists('ds_extras') && variable_get('ds_extras_field_template', FALSE)) {
  1996. $context = array(
  1997. 'instance' => $instance,
  1998. 'view_mode' => $view_mode,
  1999. );
  2000. // Load the form
  2001. module_load_include('inc', 'ds_extras', 'includes/ds_extras.admin');
  2002. if (!is_array($settings_form)) $settings_form = array();
  2003. ds_extras_field_template_settings_form($settings_form, $form_state, $context);
  2004. }
  2005. // Allow other modules to alter the formatter settings form.
  2006. $context = array(
  2007. 'module' => $formatter['module'],
  2008. 'formatter' => $formatter,
  2009. 'field' => $field,
  2010. 'instance' => $instance,
  2011. 'view_mode' => $view_mode,
  2012. 'form' => $form,
  2013. 'form_state' => $form_state,
  2014. );
  2015. drupal_alter('field_formatter_settings_form', $settings_form, $context);
  2016. if ($settings_form) {
  2017. $table[$key]['format']['#cell_attributes'] = array('colspan' => 3);
  2018. $table[$key]['format']['settings_edit_form'] = array(
  2019. '#type' => 'container',
  2020. '#attributes' => array('class' => array('field-formatter-settings-edit-form')),
  2021. '#parents' => array('fields', $key, 'settings_edit_form'),
  2022. 'label' => array(
  2023. '#markup' => t('Format settings:') . ' <span class="formatter-name">' . $formatter['label'] . '</span>',
  2024. ),
  2025. 'settings' => $settings_form,
  2026. 'actions' => array(
  2027. '#type' => 'actions',
  2028. 'save_settings' => $base_button + array(
  2029. '#type' => 'submit',
  2030. '#name' => $key . '_formatter_settings_update',
  2031. '#value' => t('Update'),
  2032. '#op' => 'update',
  2033. ),
  2034. 'cancel_settings' => $base_button + array(
  2035. '#type' => 'submit',
  2036. '#name' => $key . '_formatter_settings_cancel',
  2037. '#value' => t('Cancel'),
  2038. '#op' => 'cancel',
  2039. // Do not check errors for the 'Cancel' button, but make sure we
  2040. // get the value of the 'formatter type' select.
  2041. '#limit_validation_errors' => array(array('fields', $key, 'type')),
  2042. ),
  2043. ),
  2044. );
  2045. $table[$key]['#attributes']['class'][] = 'field-formatter-settings-editing';
  2046. $table[$key]['format']['type']['#attributes']['class'] = array('element-invisible');
  2047. }
  2048. }
  2049. else {
  2050. $summary = module_invoke($formatter['module'], 'field_formatter_settings_summary', $field, $instance, $view_mode);
  2051. // Allow other modules to alter the formatter summary.
  2052. $context = array(
  2053. 'module' => $formatter['module'],
  2054. 'formatter' => $formatter,
  2055. 'field' => $field,
  2056. 'instance' => $instance,
  2057. 'view_mode' => $view_mode,
  2058. );
  2059. drupal_alter('field_formatter_settings_summary', $summary, $context);
  2060. if (module_exists('ds_extras') && variable_get('ds_extras_field_template', FALSE)) {
  2061. module_load_include('inc', 'ds_extras', 'includes/ds_extras.admin');
  2062. // Field template summary
  2063. $functions = module_invoke_all('ds_field_theme_functions_info');
  2064. $default_field_function = variable_get('ft-default', 'theme_field');
  2065. $field_function = isset($form_state['formatter_settings'][$key]['ft']['func']) ? $form_state['formatter_settings'][$key]['ft']['func'] : $default_field_function;
  2066. if (!empty($summary)) {
  2067. $summary .= '<br />';
  2068. }
  2069. $summary .= 'Field template: ' . check_plain($functions[$field_function]) . '<br />';
  2070. }
  2071. if (!empty($summary)) {
  2072. $table[$key]['settings_summary'] = array();
  2073. $table[$key]['settings_edit'] = array();
  2074. $table[$key]['settings_summary'] = array(
  2075. '#markup' => '<div class="field-formatter-summary">' . $summary . '</div>',
  2076. '#cell_attributes' => array('class' => array('field-formatter-summary-cell')),
  2077. );
  2078. // Render the other part of the summary
  2079. ds_field_row_form_format_summary_construct($table, $key);
  2080. }
  2081. }
  2082. }
  2083. }
  2084. /**
  2085. * Helper function to check if we are in a hidden region or not.
  2086. */
  2087. function _ds_field_ui_check_hidden_region($key, $form_state, $field_regions) {
  2088. $continue = FALSE;
  2089. if (isset($form_state['input']['fields'][$key]['region'])) {
  2090. if ($form_state['input']['fields'][$key]['region'] == 'hidden') {
  2091. $continue = TRUE;
  2092. }
  2093. }
  2094. elseif (!isset($field_regions[$key]) || $field_regions[$key] == 'hidden') {
  2095. $continue = TRUE;
  2096. }
  2097. return $continue;
  2098. }
  2099. /**
  2100. * Helper function for building the formatter settings.
  2101. */
  2102. function ds_field_row_form_format_construct(&$table, $key, $settings_form) {
  2103. $base_button = array(
  2104. '#submit' => array(
  2105. 'field_ui_display_overview_multistep_submit',
  2106. ),
  2107. '#validate' => array(
  2108. 'ds_field_ui_fields_validate',
  2109. ),
  2110. '#ajax' => array(
  2111. 'callback' => 'field_ui_display_overview_multistep_js',
  2112. 'wrapper' => 'field-display-overview-wrapper',
  2113. 'effect' => 'fade',
  2114. ),
  2115. '#field_name' => $key,
  2116. );
  2117. $table[$key]['format']['settings_edit'] = array(
  2118. '#type' => 'container',
  2119. '#attributes' => array('class' => array('field-formatter-settings-edit-form')),
  2120. '#parents' => array('fields', $key, 'settings_edit_form'),
  2121. '#weight' => -5,
  2122. // Create a settings form where hooks can pick in.
  2123. 'settings' => $settings_form,
  2124. 'actions' => array(
  2125. '#type' => 'actions',
  2126. 'save_settings' => $base_button + array(
  2127. '#type' => 'submit',
  2128. '#name' => $key . '_formatter_settings_update',
  2129. '#value' => t('Update'),
  2130. '#op' => 'update',
  2131. ),
  2132. 'cancel_settings' => $base_button + array(
  2133. '#type' => 'submit',
  2134. '#name' => $key . '_formatter_settings_cancel',
  2135. '#value' => t('Cancel'),
  2136. '#op' => 'cancel',
  2137. // Do not check errors for the 'Cancel' button.
  2138. '#limit_validation_errors' => array(),
  2139. ),
  2140. ),
  2141. );
  2142. $table[$key]['#attributes']['class'][] = 'field-formatter-settings-editing';
  2143. $table[$key]['format']['type']['#attributes']['class'] = array('element-invisible');
  2144. }
  2145. /**
  2146. * Helper function for formatter summary settings.
  2147. */
  2148. function ds_field_row_form_format_summary_construct(&$table, $key) {
  2149. $base_button = array(
  2150. '#submit' => array('field_ui_display_overview_multistep_submit'),
  2151. '#ajax' => array(
  2152. 'callback' => 'field_ui_display_overview_multistep_js',
  2153. 'wrapper' => 'field-display-overview-wrapper',
  2154. 'effect' => 'fade',
  2155. ),
  2156. '#field_name' => $key,
  2157. );
  2158. // Add the configure button.
  2159. $table[$key]['settings_edit'] = $base_button + array(
  2160. '#type' => 'image_button',
  2161. '#name' => $key . '_formatter_settings_edit',
  2162. '#src' => 'misc/configure.png',
  2163. '#attributes' => array('class' => array('field-formatter-settings-edit'), 'alt' => t('Edit')),
  2164. '#op' => 'edit',
  2165. // Do not check errors for the 'Edit' button.
  2166. '#limit_validation_errors' => array(),
  2167. '#prefix' => '<div class="field-formatter-settings-edit-wrapper">',
  2168. '#suffix' => '</div>',
  2169. );
  2170. }
  2171. /**
  2172. * Add tab for adding new fields on the fly.
  2173. *
  2174. * @param $entity_type
  2175. * The name of the entity type.
  2176. * @param $bundle
  2177. * The name of the bundle
  2178. * @param $view_mode
  2179. * The name of the view_mode
  2180. * @param $form
  2181. * A collection of form properties.
  2182. * @param $form_state
  2183. * A collection of form_state properties.
  2184. */
  2185. function _ds_field_ui_custom_fields($entity_type, $bundle, $view_mode, &$form, $form_state) {
  2186. $form['additional_settings']['add_custom_fields'] = array(
  2187. '#type' => 'fieldset',
  2188. '#title' => t('Custom fields'),
  2189. '#description' => t('Click on one of the buttons to create a new field.') . '<p></p>',
  2190. '#access' => user_access('admin_fields'),
  2191. );
  2192. // Include the CTools tools that we need.
  2193. ctools_include('ajax');
  2194. ctools_include('modal');
  2195. // Add CTools' javascript to the page.
  2196. ctools_modal_add_js();
  2197. $field_types = array(
  2198. 'custom_field' => t('Add a code field'),
  2199. 'manage_ctools' => t('Add a dynamic field'),
  2200. );
  2201. $field_types['manage_block'] = t('Add a block field');
  2202. $field_types['manage_preprocess'] = t('Add a preprocess field');
  2203. foreach ($field_types as $field_key => $field_title) {
  2204. $form['ctools_add_field_' . $field_key . '_url'] = array(
  2205. '#type' => 'hidden',
  2206. '#attributes' => array('class' => array('ctools_add_field_' . $field_key . '-url')),
  2207. '#value' => url('admin/structure/ds/nojs/add_field/' . $field_key),
  2208. );
  2209. $form['additional_settings']['add_custom_fields']['ctools_add_field_' . $field_key] = array(
  2210. '#type' => 'button',
  2211. '#value' => $field_title,
  2212. '#attributes' => array('class' => array('ctools-use-modal')),
  2213. '#id' => 'ctools_add_field_' . $field_key,
  2214. );
  2215. }
  2216. $form['additional_settings']['add_custom_fields']['manage_fields'] = array(
  2217. '#type' => 'link',
  2218. '#title' => 'Manage fields',
  2219. '#href' => 'admin/structure/ds/fields',
  2220. '#prefix' => '<div>',
  2221. '#suffix' => '</div>',
  2222. );
  2223. }
  2224. /**
  2225. * Utility function to check if we need to save anything for this field.
  2226. */
  2227. function _ds_field_valid($key, $field, &$form_state, $view_mode = 'default') {
  2228. $continue = FALSE;
  2229. // Ignore the Field group module and the region to block plugin.
  2230. if ($key == '_add_new_group' || $key == '_add_new_field' || $key == '_add_new_block_region') {
  2231. $continue = TRUE;
  2232. }
  2233. // If the field is in hidden region, do not save. Check if the
  2234. // field has a type key which means it's from Field API and
  2235. // we need to reset that type to 'hidden' so it doesn't get
  2236. // fired by Field API in the frontend.
  2237. if (isset($field['region']) && $field['region'] == 'hidden') {
  2238. if (isset($field['type']) && $view_mode != 'form') {
  2239. $form_state['values']['fields'][$key]['type'] = 'hidden';
  2240. }
  2241. // In case of a form, fields will be set with #access to FALSE.
  2242. if ($view_mode != 'form') {
  2243. $continue = TRUE;
  2244. }
  2245. }
  2246. return $continue;
  2247. }
  2248. /**
  2249. * Utility function to return CSS classes.
  2250. */
  2251. function _ds_classes($name = 'ds_classes_regions') {
  2252. static $classes = array();
  2253. if (!isset($classes[$name])) {
  2254. $classes[$name] = array();
  2255. $custom_classes = trim(variable_get($name, ''));
  2256. if (!empty($custom_classes)) {
  2257. $classes[$name][''] = t('None');
  2258. $custom_classes = explode("\n", $custom_classes);
  2259. foreach ($custom_classes as $key => $value) {
  2260. $classes_splitted = explode("|", $value);
  2261. $key = trim($classes_splitted[0]);
  2262. $friendly_name = isset($classes_splitted[1]) ? trim($classes_splitted[1]) : $key;
  2263. $classes[$name][check_plain($key)] = $friendly_name;
  2264. }
  2265. }
  2266. $name_clone = $name; // Prevent the name from being changed.
  2267. drupal_alter('ds_classes', $classes[$name], $name_clone);
  2268. }
  2269. return $classes[$name];
  2270. }
  2271. /**
  2272. * Utility function to sort a multidimensional array by a value in a sub-array.
  2273. *
  2274. * @param $a
  2275. * The array to sort.
  2276. * @param $subkey
  2277. * The subkey to sort by.
  2278. */
  2279. function _ds_sort_fields($a, $subkey) {
  2280. foreach ($a as $k => $v) {
  2281. if (isset($v[$subkey])) {
  2282. $b[$k] = $v[$subkey];
  2283. }
  2284. }
  2285. asort($b);
  2286. foreach ($b as $key => $val) {
  2287. $c[$key] = $a[$key];
  2288. }
  2289. return $c;
  2290. }