ds.module 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366
  1. <?php
  2. /**
  3. * @file
  4. * Display Suite core functions.
  5. */
  6. /**
  7. * Constants for field types.
  8. */
  9. define('DS_FIELD_TYPE_THEME', 1);
  10. define('DS_FIELD_TYPE_FUNCTION', 2);
  11. define('DS_FIELD_TYPE_PREPROCESS', 3);
  12. define('DS_FIELD_TYPE_IGNORE', 4);
  13. define('DS_FIELD_TYPE_CODE', 5);
  14. define('DS_FIELD_TYPE_BLOCK', 6);
  15. define('DS_FIELD_TYPE_CTOOLS', 7);
  16. /**
  17. * Constants for block fields rendering.
  18. */
  19. define('DS_BLOCK_TEMPLATE', 1);
  20. define('DS_BLOCK_TITLE_CONTENT', 2);
  21. define('DS_BLOCK_CONTENT', 3);
  22. /**
  23. * Implements hook_permission().
  24. */
  25. function ds_permission() {
  26. return array(
  27. 'admin_display_suite' => array(
  28. 'title' => t('Administer Display Suite'),
  29. 'description' => t('General permission for Display Suite settings pages.')
  30. ),
  31. );
  32. }
  33. /**
  34. * Implements hook_hook_info().
  35. */
  36. function ds_hook_info() {
  37. $hooks['ds_fields_info'] = array(
  38. 'group' => 'ds_fields_info',
  39. );
  40. return $hooks;
  41. }
  42. /**
  43. * Implements hook_menu().
  44. */
  45. function ds_menu() {
  46. module_load_include('inc', 'ds', 'includes/ds.registry');
  47. return _ds_menu();
  48. }
  49. /**
  50. * Implements hook_menu_alter().
  51. */
  52. function ds_menu_alter(&$items) {
  53. module_load_include('inc', 'ds', 'includes/ds.registry');
  54. return _ds_menu_alter($items);
  55. }
  56. /**
  57. * Implements hook_theme().
  58. */
  59. function ds_theme() {
  60. module_load_include('inc', 'ds', 'includes/ds.registry');
  61. return _ds_theme();
  62. }
  63. /**
  64. * Implements hook_ds_layout_info().
  65. */
  66. function ds_ds_layout_info() {
  67. module_load_include('inc', 'ds', 'includes/ds.registry');
  68. return _ds_ds_layout_info();
  69. }
  70. /**
  71. * Implements hook_ctools_plugin_api().
  72. */
  73. function ds_ctools_plugin_api($owner, $api) {
  74. if ($owner == 'ds' && ($api == 'ds' || $api == 'plugins')) {
  75. return array('version' => 1);
  76. }
  77. }
  78. /**
  79. * Implements hook_ctools_plugin_directory().
  80. */
  81. function ds_ctools_plugin_directory($owner, $plugin_type) {
  82. if ($owner == 'ctools' && $plugin_type == 'content_types') {
  83. return 'plugins/' . $plugin_type;
  84. }
  85. }
  86. /**
  87. * Implements hook_views_api().
  88. */
  89. function ds_views_api() {
  90. return array('api' => 3);
  91. }
  92. /**
  93. * Implements hook_node_type_update().
  94. */
  95. function ds_node_type_update($info) {
  96. if (!empty($info->old_type) && $info->old_type != $info->type) {
  97. module_load_include('inc', 'ds', 'includes/ds.registry');
  98. _ds_entity_type_update('node', $info, 'update');
  99. }
  100. }
  101. /**
  102. * Implements hook_node_type_delete().
  103. */
  104. function ds_node_type_delete($info) {
  105. module_load_include('inc', 'ds', 'includes/ds.registry');
  106. _ds_entity_type_update('node', $info, 'delete');
  107. }
  108. /**
  109. * Implements hook_theme_registry_alter().
  110. */
  111. function ds_theme_registry_alter(&$theme_registry) {
  112. module_load_include('inc', 'ds', 'includes/ds.registry');
  113. _ds_theme_registry_alter($theme_registry);
  114. }
  115. /**
  116. * Implements hook_entity_info_alter().
  117. */
  118. function ds_entity_info_alter(&$entity_info) {
  119. module_load_include('inc', 'ds', 'includes/ds.registry');
  120. _ds_entity_info_alter($entity_info);
  121. }
  122. /**
  123. * Implements hook_form_FORM_ID_alter().
  124. */
  125. function ds_form_field_ui_display_overview_form_alter(&$form, &$form_state) {
  126. form_load_include($form_state, 'inc', 'ds', 'includes/ds.field_ui');
  127. // Also load admin on behalf of DS extras when enabled.
  128. if (module_exists('ds_extras')) {
  129. form_load_include($form_state, 'inc', 'ds_extras', 'includes/ds_extras.admin');
  130. }
  131. ds_field_ui_fields_layouts($form, $form_state);
  132. }
  133. /**
  134. * Implements hook_module_implements_alter().
  135. */
  136. function ds_module_implements_alter(&$implementations, $hook) {
  137. // node_field_display_module_alter() disables all labels on all fields
  138. // when the view mode is 'search_index'. If you set display modes for
  139. // this view mode by hand, then the hook isn't needed. Since this
  140. // may be called hundreds of times on some pages, it's worth disabling it.
  141. // See http://drupal.org/node/834278
  142. // This code is also in Performance hacks module, but it's not bad to
  143. // disable this too in Display Suite by default.
  144. if ($hook == 'field_display_node_alter') {
  145. unset($implementations['node']);
  146. }
  147. }
  148. /**
  149. * Implements hook_features_api().
  150. */
  151. function ds_features_api() {
  152. module_load_include('inc', 'ds', 'includes/ds.registry');
  153. return _ds_features_api();
  154. }
  155. /**
  156. * Function to check if we go on with Display Suite.
  157. */
  158. function ds_kill_switch() {
  159. if (variable_get('ds_disable', FALSE)) {
  160. return TRUE;
  161. }
  162. return FALSE;
  163. }
  164. /**
  165. * Get entity view modes.
  166. *
  167. * @param $entity_type
  168. * The name of the entity type.
  169. */
  170. function ds_entity_view_modes($entity_type = NULL) {
  171. if (!empty($entity_type)) {
  172. switch ($entity_type) {
  173. // For taxonomy terms the base table and the entity type are different
  174. case 'taxonomy_term_data':
  175. $entity_info = entity_get_info('taxonomy_term');
  176. break;
  177. default:
  178. $entity_info = entity_get_info($entity_type);
  179. break;
  180. }
  181. return $entity_info['view modes'];
  182. }
  183. }
  184. /**
  185. * Get Display Suite layouts.
  186. */
  187. function ds_get_layout_info() {
  188. static $layouts = FALSE;
  189. if (!$layouts) {
  190. $errors = array();
  191. $layouts = module_invoke_all('ds_layout_info');
  192. // Give modules a chance to alter the layouts information.
  193. drupal_alter('ds_layout_info', $layouts);
  194. // Check that there is no 'content' region, but ignore panel layouts.
  195. // Because when entities are rendered, the field items are stored into a
  196. // 'content' key so fields would be overwritten before they're all moved.
  197. foreach ($layouts as $key => $info) {
  198. if (isset($info['panels'])) {
  199. continue;
  200. }
  201. if (isset($info['regions']['content'])) {
  202. $errors[] = $key;
  203. }
  204. }
  205. if (!empty($errors)) {
  206. drupal_set_message(t('Following layouts have a "content" region key which is invalid: %layouts.', array('%layouts' => implode(', ', $errors))), 'error');
  207. }
  208. }
  209. return $layouts;
  210. }
  211. /**
  212. * Get a layout for a given entity.
  213. *
  214. * @param $entity_type
  215. * The name of the entity.
  216. * @param $bundle
  217. * The name of the bundle.
  218. * @param $view_mode
  219. * The name of the view mode.
  220. * @param $fallback
  221. * Whether to fallback to default or not.
  222. *
  223. * @return $layout
  224. * Array of layout variables for the regions.
  225. */
  226. function ds_get_layout($entity_type, $bundle, $view_mode, $fallback = TRUE) {
  227. static $layouts = array();
  228. $layout_key = $entity_type . '_' . $bundle . '_' . $view_mode;
  229. if (!isset($layouts[$layout_key])) {
  230. $entity_info = entity_get_info();
  231. $overridden = TRUE;
  232. if ($view_mode != 'form') {
  233. $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  234. $overridden = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? TRUE : FALSE);
  235. }
  236. // Check if a layout is configured.
  237. if (isset($entity_info[$entity_type]['bundles'][$bundle]['layouts'][$view_mode]) && ($overridden || $view_mode == 'default')) {
  238. $layouts[$layout_key] = $entity_info[$entity_type]['bundles'][$bundle]['layouts'][$view_mode];
  239. $layouts[$layout_key]['view_mode'] = $view_mode;
  240. }
  241. // In case $view_mode is not found, check if we have a default layout,
  242. // but only if the view mode settings aren't overridden for this view mode.
  243. if ($view_mode != 'default' && !$overridden && $fallback && isset($entity_info[$entity_type]['bundles'][$bundle]['layouts']['default'])) {
  244. $layouts[$layout_key] = $entity_info[$entity_type]['bundles'][$bundle]['layouts']['default'];
  245. $layouts[$layout_key]['view_mode'] = 'default';
  246. }
  247. // Register the false return value as well.
  248. if (!isset($layouts[$layout_key])) {
  249. $layouts[$layout_key] = FALSE;
  250. }
  251. }
  252. return $layouts[$layout_key];
  253. }
  254. /**
  255. * Get all fields.
  256. *
  257. * @param $entity_type
  258. * The name of the entity.
  259. * @param $cache
  260. * Whether we need to get the fields from cache or not.
  261. * @return
  262. * Collection of fields.
  263. */
  264. function ds_get_fields($entity_type, $cache = TRUE) {
  265. global $language;
  266. static $static_fields, $fields_cached = array();
  267. static $loaded = FALSE;
  268. // Load the ds file that handles ds call of hook_ds_fields_info, otherwise it
  269. // doesn't get loaded
  270. module_load_include('inc', 'ds', 'ds.ds_fields_info');
  271. // Get fields from cache.
  272. if (!$loaded) {
  273. $loaded = TRUE;
  274. if ($cache && $cached_fields = cache_get('ds_fields:' . $language->language)) {
  275. $fields_cached = $static_fields = $cached_fields->data;
  276. }
  277. }
  278. if (!isset($static_fields[$entity_type])) {
  279. // Do we have them cached or not ?
  280. if (!isset($fields_cached[$entity_type])) {
  281. // Get all fields for this entity type.
  282. $fields = array();
  283. foreach (module_implements('ds_fields_info') as $module) {
  284. $function = $module . '_ds_fields_info';
  285. $all_fields = $function($entity_type);
  286. if (!empty($all_fields)) {
  287. foreach ($all_fields as $key => $field_results) {
  288. if ($key === $entity_type) {
  289. // Add module key to field definition.
  290. foreach ($field_results as $f => $d) {
  291. $field_results[$f]['module'] = $module;
  292. }
  293. $fields = array_merge($field_results, $fields);
  294. }
  295. }
  296. }
  297. }
  298. // Give modules a change to alter fields.
  299. drupal_alter('ds_fields_info', $fields, $entity_type);
  300. $fields_cached[$entity_type] = $fields;
  301. // Cache the fields.
  302. if ($cache) {
  303. cache_set('ds_fields:' . $language->language, $fields_cached, 'cache');
  304. }
  305. }
  306. else {
  307. $fields = $fields_cached[$entity_type];
  308. }
  309. // Store the fields statically.
  310. $static_fields[$entity_type] = $fields;
  311. }
  312. return isset($static_fields[$entity_type]) ? $static_fields[$entity_type] : array();
  313. }
  314. /**
  315. * Get the field settings.
  316. *
  317. * @param $entity_type
  318. * The name of the entity.
  319. * @param $bundle
  320. * The name of bundle (ie, page or story for node types, profile for users)
  321. * @param $view_mode
  322. * The name of view mode.
  323. */
  324. function ds_get_field_settings($entity_type, $bundle, $view_mode, $default = TRUE) {
  325. static $field_settings = NULL;
  326. if (!isset($field_settings)) {
  327. if ($cache = cache_get('ds_field_settings')) {
  328. $field_settings = $cache->data;
  329. }
  330. else {
  331. ctools_include('export');
  332. $ds_field_settings = ctools_export_crud_load_all('ds_field_settings');
  333. foreach ($ds_field_settings as $layout => $layout_settings) {
  334. // Do not store configuration when the field settings is disabled.
  335. if (!empty($layout_settings->disabled)) {
  336. continue;
  337. }
  338. // Do not store configuration if settings key is not set.
  339. if (!isset($layout_settings->settings)) {
  340. continue;
  341. }
  342. foreach ($layout_settings->settings as $field => $settings) {
  343. $field_settings[$layout_settings->entity_type][$layout_settings->bundle][$layout_settings->view_mode][$field] = $settings;
  344. }
  345. }
  346. cache_set('ds_field_settings', $field_settings, 'cache');
  347. }
  348. }
  349. return (isset($field_settings[$entity_type][$bundle][$view_mode])) ? $field_settings[$entity_type][$bundle][$view_mode] : (isset($field_settings[$entity_type][$bundle]['default']) && $default ? $field_settings[$entity_type][$bundle]['default'] : array());
  350. }
  351. /**
  352. * Get the value for a Display Suite field.
  353. *
  354. * @param $key
  355. * The key of the field.
  356. * @param $field
  357. * The configuration of a DS field.
  358. * @param $entity
  359. * The current entity.
  360. * @param $entity_type
  361. * The name of the entity.
  362. * @param $bundle
  363. * The name of the bundle.
  364. * @param $view_mode
  365. * The name of the view mode.
  366. * @param $build
  367. * The current built of the entity.
  368. * @return $markup
  369. * The markup of the field used for output.
  370. */
  371. function ds_get_field_value($key, $field, $entity, $entity_type, $bundle, $view_mode, $build = array()) {
  372. $field['field_name'] = $key;
  373. $field['entity'] = $entity;
  374. $field['entity_type'] = $entity_type;
  375. $field['bundle'] = $bundle;
  376. $field['view_mode'] = $view_mode;
  377. $field['build'] = $build;
  378. // Special case for ds_views which can handle custom fields now.
  379. if ($field['field_type'] != DS_FIELD_TYPE_PREPROCESS && $entity_type == 'ds_views') {
  380. $entity->preprocess_fields[] = $key;
  381. }
  382. switch ($field['field_type']) {
  383. case DS_FIELD_TYPE_PREPROCESS:
  384. $entity->preprocess_fields[] = $key;
  385. break;
  386. case DS_FIELD_TYPE_FUNCTION:
  387. if (isset($field['file'])) {
  388. include_once $field['file'];
  389. }
  390. return $field['function']($field);
  391. case DS_FIELD_TYPE_THEME:
  392. $format = (isset($field['formatter'])) ? $field['formatter'] : key($field['properties']['formatters']);
  393. return theme($format, $field);
  394. case DS_FIELD_TYPE_CODE:
  395. return ds_render_code_field($field);
  396. case DS_FIELD_TYPE_CTOOLS:
  397. return ds_render_ctools_field($field);
  398. case DS_FIELD_TYPE_BLOCK:
  399. return ds_render_block_field($field);
  400. }
  401. }
  402. /**
  403. * Implements hook_field_attach_view_alter().
  404. */
  405. function ds_field_attach_view_alter(&$build, $context) {
  406. static $loaded_css = array();
  407. // Global kill switch. In some edge cases, a view might
  408. // be inserted into the view of an entity, in which the
  409. // same entity is available as well. This is simply not
  410. // possible, so you can temporarily disable DS completely
  411. // by setting this variable, either from code or via
  412. // the UI through admin/structure/ds/
  413. if (ds_kill_switch()) {
  414. return;
  415. }
  416. // If views and core doesn't send information along on the entity,
  417. // Display Suite doesn't have a context to render the fields.
  418. if (!isset($build['#entity_type']) && !isset($build['#bundle'])) {
  419. return;
  420. }
  421. // If no layout is configured, stop as well.
  422. if (!ds_get_layout($build['#entity_type'], $build['#bundle'], $context['view_mode'])) {
  423. return;
  424. }
  425. $entity_type = $build['#entity_type'];
  426. $bundle = $build['#bundle'];
  427. $view_mode = $context['view_mode'];
  428. $entity = $context['entity'];
  429. $layout = ds_get_layout($entity_type, $bundle, $view_mode);
  430. // Check on field/delta limit.
  431. if (isset($layout['settings']['limit'])) {
  432. foreach ($layout['settings']['limit'] as $field => $limit) {
  433. if (isset($build[$field])) {
  434. if ($limit === 'delta' && isset($entity->ds_delta) && isset($entity->ds_delta[$field])) {
  435. $delta = $entity->ds_delta[$field];
  436. foreach ($build[$field]['#items'] as $key => $item) {
  437. if ($key != $delta) {
  438. unset($build[$field][$key]);
  439. }
  440. }
  441. }
  442. else {
  443. $count = count($build[$field]['#items']);
  444. if ($count > $limit) {
  445. $build[$field]['#items'] = array_slice($build[$field]['#items'], 0, $limit);
  446. }
  447. }
  448. }
  449. }
  450. }
  451. // Add Display Suite display fields.
  452. $fields = ds_get_fields($entity_type);
  453. $field_values = ds_get_field_settings($entity_type, $bundle, $layout['view_mode']);
  454. foreach ($field_values as $key => $field) {
  455. // Ignore if this field is not a DS field.
  456. if (!isset($fields[$key])) {
  457. continue;
  458. }
  459. $field = $fields[$key];
  460. if (isset($field_values[$key]['format'])) {
  461. $field['formatter'] = $field_values[$key]['format'];
  462. }
  463. if (isset($field_values[$key]['formatter_settings'])) {
  464. $field['formatter_settings'] = $field_values[$key]['formatter_settings'];
  465. }
  466. $field_value = ds_get_field_value($key, $field, $entity, $entity_type, $bundle, $view_mode, $build);
  467. // Title label.
  468. if ($key == 'title' && $entity_type == 'node') {
  469. $node_type = node_type_get_type($entity);
  470. $field['title'] = function_exists('i18n_node_translate_type') ? i18n_node_translate_type($node_type->type, 'title_label', $node_type->title_label) : $node_type->title_label;
  471. }
  472. if (!empty($field_value) || (string) $field_value === '0') {
  473. // Special case for views.
  474. if (!empty($build['render_code_fields'])) {
  475. $build[$key] = $field_value;
  476. }
  477. else {
  478. $build[$key] = array(
  479. '#theme' => 'field',
  480. '#field_type' => 'ds',
  481. '#skip_edit' => TRUE,
  482. '#title' => $field['title'],
  483. '#weight' => isset($field_values[$key]['weight']) ? $field_values[$key]['weight'] : 0,
  484. '#label_display' => isset($field_values[$key]['label']) ? $field_values[$key]['label'] : 'inline',
  485. '#field_name' => $key,
  486. '#bundle' => $bundle,
  487. '#object' => $entity,
  488. '#entity_type' => $entity_type,
  489. '#view_mode' => $view_mode,
  490. '#access' => (variable_get('ds_extras_field_permissions', FALSE) && function_exists('ds_extras_ds_field_access')) ? ds_extras_ds_field_access($key, $entity_type) : TRUE,
  491. '#items' => array(
  492. 0 => array(
  493. 'value' => $field_value,
  494. ),
  495. ),
  496. 0 => array(
  497. '#markup' => $field_value,
  498. ),
  499. );
  500. }
  501. }
  502. }
  503. $disable_css = FALSE;
  504. if (!empty($layout['settings']['layout_disable_css'])) {
  505. $disable_css = TRUE;
  506. }
  507. // Add path to css file for this layout and disable block regions if necessary.
  508. if (!$disable_css && isset($layout['css']) && !isset($loaded_css[$layout['path'] . '/' . $layout['layout'] . '.css'])) {
  509. // Register css file.
  510. $loaded_css[$layout['path'] . '/' . $layout['layout'] . '.css'] = TRUE;
  511. // Add css file.
  512. if (isset($layout['module']) && $layout['module'] == 'panels') {
  513. $build['#attached']['css'][] = $layout['path'] . '/' . $layout['panels']['css'];
  514. }
  515. else {
  516. $build['#attached']['css'][] = $layout['path'] . '/' . $layout['layout'] . '.css';
  517. }
  518. }
  519. }
  520. /**
  521. * Add variables to an entity.
  522. *
  523. * This function is added in ds_theme_registry_alter().
  524. */
  525. function ds_entity_variables(&$vars) {
  526. if (isset($vars['elements']) && isset($vars['elements']['#bundle']) && $layout = ds_get_layout($vars['elements']['#entity_type'], $vars['elements']['#bundle'], $vars['elements']['#view_mode'])) {
  527. $render_container = 'content';
  528. // User uses user_profile as container.
  529. if ($vars['elements']['#entity_type'] == 'user') {
  530. $render_container = 'user_profile';
  531. }
  532. // Move any preprocess fields to render container.
  533. // Inconsistency in taxonomy term naming.
  534. $entity_type = $vars['elements']['#entity_type'];
  535. if ($vars['elements']['#entity_type'] == 'taxonomy_term') {
  536. $entity_type = 'term';
  537. }
  538. // Get entity id and bundle
  539. $id = NULL;
  540. $bundle = $vars['elements']['#bundle'];
  541. $entity = isset($vars[$entity_type]) ? $vars[$entity_type] : (isset($vars['elements']['#' . $entity_type]) ? $vars['elements']['#' . $entity_type] : NULL);
  542. list($id,, $bundle) = entity_extract_ids($entity_type, $entity);
  543. if (isset($vars[$entity_type]->preprocess_fields)) {
  544. foreach ($vars[$entity_type]->preprocess_fields as $field) {
  545. // Process RDF if the module is enabled before moving preprocess fields.
  546. if (module_exists('rdf')) {
  547. rdf_process($vars, $field);
  548. // Remove it so we can unset the field later on.
  549. unset($vars['rdf_template_variable_attributes_array'][$field]);
  550. }
  551. // Move the field to content so it renders, remove it
  552. // because we don't need it anymore.
  553. if (isset($vars[$field])) {
  554. $vars[$render_container][$field] = array('#markup' => $vars[$field]);
  555. if (!isset($vars['preprocess_keep'])) {
  556. unset($vars[$field]);
  557. }
  558. }
  559. }
  560. }
  561. // Check if this is a flexible panels layout.
  562. if (!empty($layout['flexible'])) {
  563. ctools_include('plugins', 'panels');
  564. $vars['css_id'] = $vars['settings'] = $vars['display'] = $vars['renderer'] = '';
  565. $vars['layout'] = panels_get_layout($layout['panels']['name']);
  566. $vars['theme_hook_suggestion'] = 'panels_flexible';
  567. }
  568. else {
  569. // Template layout.
  570. if (!isset($vars['classes_array'])) {
  571. $vars['classes_array'] = array();
  572. }
  573. // Add view-mode-{name} to classes.
  574. if (!in_array('view-mode-' . $vars['elements']['#view_mode'], $vars['classes_array'])) {
  575. $vars['classes_array'][] = 'view-mode-' . $vars['elements']['#view_mode'];
  576. }
  577. // In case this is a panels layout, use panels info array.
  578. if (isset($layout['module']) && $layout['module'] == 'panels') {
  579. $layout['layout'] = $layout['panels']['theme'];
  580. }
  581. $bundle = strtr($bundle, '-', '_');
  582. $vars['theme_hook_suggestions'][] = $layout['layout'];
  583. $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'];
  584. $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $vars['elements']['#view_mode'];
  585. $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $bundle;
  586. $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '_' . $bundle . '_' . $vars['elements']['#view_mode'];
  587. if (!empty($id)) {
  588. $vars['theme_hook_suggestions'][] = $layout['layout'] . '__' . $vars['elements']['#entity_type'] . '__' . $id;
  589. }
  590. }
  591. // If the layout has wrapper class lets add it.
  592. if (isset($layout['settings']['classes']['layout_class'])) {
  593. foreach ($layout['settings']['classes']['layout_class'] as $layout_class) {
  594. $vars['classes_array'][] = $layout_class;
  595. }
  596. }
  597. $layout_render_array = array();
  598. // Create region variables based on the layout settings.
  599. foreach ($layout['regions'] as $region_name => $region) {
  600. // Create the region content.
  601. $layout_render_array[$region_name] = array();
  602. if (isset($layout['settings']['regions'][$region_name])) {
  603. foreach ($layout['settings']['regions'][$region_name] as $key => $field) {
  604. // Make sure the field exists.
  605. if (!isset($vars[$render_container][$field])) {
  606. continue;
  607. }
  608. if (!isset($vars[$render_container][$field]['#weight'])) {
  609. $vars[$render_container][$field]['#weight'] = $key;
  610. }
  611. $layout_render_array[$region_name][$key] = $vars[$render_container][$field];
  612. }
  613. }
  614. // Add extras classes to the region.
  615. if (empty($layout['flexible'])) {
  616. $vars[$region_name . '_classes'] = !empty($layout['settings']['classes'][$region_name]) ? ' ' . implode(' ', $layout['settings']['classes'][$region_name]) : '';
  617. // Token support for region classes.
  618. if (module_exists('token') && isset($vars[$entity_type])) {
  619. $vars[$region_name . '_classes'] = token_replace($vars[$region_name . '_classes'], array($entity_type => $vars[$entity_type]), array('clear' => TRUE, 'sanitize' => TRUE));
  620. }
  621. }
  622. // Add a wrapper to the region.
  623. if (empty($layout['flexible'])) {
  624. $vars[$region_name . '_wrapper'] = isset($layout['settings']['wrappers'][$region_name]) ? $layout['settings']['wrappers'][$region_name] : 'div';
  625. }
  626. }
  627. // Let other modules know we have rendered.
  628. $vars['rendered_by_ds'] = TRUE;
  629. // Add a layout wrapper.
  630. $vars['layout_wrapper'] = isset($layout['settings']['layout_wrapper']) ? $layout['settings']['layout_wrapper'] : 'div';
  631. // Add layout attributes if any.
  632. $vars['layout_attributes'] = '';
  633. if (!empty($layout['settings']['layout_attributes'])) {
  634. if (isset($vars[$entity_type])) {
  635. $vars['layout_attributes'] .= ' ' . token_replace($layout['settings']['layout_attributes'], array($entity_type => $vars[$entity_type]), array('clear' => TRUE, 'sanitize' => TRUE));
  636. }
  637. else {
  638. $vars['layout_attributes'] .= ' ' . $layout['settings']['layout_attributes'];
  639. }
  640. }
  641. // Merge in other attributes which were passed to the template.
  642. if (!empty($layout['settings']['layout_attributes_merge'])) {
  643. // Handle classes separately.
  644. if (isset($vars['attributes_array']['class'])) {
  645. $vars['classes_array'] += $vars['attributes_array']['class'];
  646. unset($vars['attributes_array']['class']);
  647. }
  648. $vars['layout_attributes'] .= ' ' . drupal_attributes($vars['attributes_array']);
  649. }
  650. // Token support for layout classes.
  651. if (module_exists('token') && isset($vars[$entity_type])) {
  652. foreach ($vars['classes_array'] as &$class) {
  653. $class = token_replace($class, array($entity_type => $vars[$entity_type]), array('clear' => TRUE, 'sanitize' => TRUE));
  654. }
  655. }
  656. // Add an onclick attribute on the wrapper.
  657. if (!empty($layout['settings']['layout_link_attribute'])) {
  658. $url = '';
  659. switch ($layout['settings']['layout_link_attribute']) {
  660. case 'content':
  661. if ($entity_type == 'user') {
  662. $uri = entity_uri($vars['elements']['#entity_type'], $vars['elements']['#account']);
  663. }
  664. else {
  665. $uri = entity_uri($vars['elements']['#entity_type'], $vars[$entity_type]);
  666. }
  667. if (!empty($uri)) {
  668. $url = $uri['path'];
  669. }
  670. break;
  671. case 'custom':
  672. $url = $layout['settings']['layout_link_custom'];
  673. break;
  674. case 'tokens':
  675. $url = token_replace($layout['settings']['layout_link_custom'], array($vars['elements']['#entity_type'] => $vars[$entity_type]), array('clear' => TRUE));
  676. break;
  677. }
  678. if (!empty($url)) {
  679. $vars['layout_attributes'] .= ' onclick="location.href=\'' . url($url) . '\'"';
  680. }
  681. }
  682. // Set field weights for all fields, including pre-process.
  683. foreach ($layout_render_array as $region => &$fields) {
  684. foreach ($fields as $field_key => &$field) {
  685. $field['#weight'] = $field_key;
  686. }
  687. }
  688. // Let other modules alter the ds array before creating the region variables.
  689. $context = array('entity' => isset($vars[$entity_type]) ? $vars[$entity_type] : (isset($vars['elements']['#' . $entity_type]) ? $vars['elements']['#' . $entity_type] : ''), 'entity_type' => $vars['elements']['#entity_type'], 'bundle' => $vars['elements']['#bundle'], 'view_mode' => $vars['elements']['#view_mode']);
  690. drupal_alter('ds_pre_render', $layout_render_array, $context, $vars);
  691. foreach ($layout_render_array as $region_name => $content) {
  692. // In case this is a panels layout, add the region to the $content variable.
  693. if (isset($layout['module']) && $layout['module'] == 'panels') {
  694. $vars['content'][$region_name] = drupal_render($content);
  695. }
  696. else {
  697. $vars[$region_name] = drupal_render($content);
  698. }
  699. }
  700. }
  701. }
  702. /**
  703. * Create entity context.
  704. */
  705. function ds_create_entity_context($entity_type, $entity, &$contexts, $context_arguments = array()) {
  706. ctools_include('context');
  707. if (empty($context_arguments)) {
  708. $context_arguments = array(array(
  709. 'keyword' => $entity_type,
  710. 'identifier' => drupal_ucfirst($entity_type) . ' being viewed',
  711. 'id' => 1,
  712. 'name' => 'entity_id:' . $entity_type,
  713. 'settings' => array(),
  714. ));
  715. }
  716. ctools_context_get_context_from_arguments($context_arguments, $contexts, array($entity));
  717. }
  718. /**
  719. * Render a code field.
  720. */
  721. function ds_render_code_field($field) {
  722. if (isset($field['properties']['code'])) {
  723. $value = $field['properties']['code']['value'];
  724. // Token support - check on token property so we don't run every single field through token.
  725. if (isset($field['properties']['use_token']) && $field['properties']['use_token']) {
  726. $value = token_replace($value, array($field['entity_type'] => $field['entity']), array('clear' => TRUE));
  727. }
  728. $format = (isset($field['properties']['code']['format'])) ? $field['properties']['code']['format'] : 'plain_text';
  729. if ($format == 'ds_code' && module_exists('ds_format')) {
  730. $value = ds_format_php_eval($value, $field['entity'], isset($field['build']) ? $field['build'] : array());
  731. }
  732. else {
  733. $value = check_markup($value, $format);
  734. }
  735. return $value;
  736. }
  737. }
  738. /**
  739. * Render a CTools field.
  740. */
  741. function ds_render_ctools_field($field) {
  742. if (isset($field['formatter_settings']['ctools'])) {
  743. // Extreme use case where a taxonomy_term object is not
  744. // loaded on the entity and triggers notices if a view is embedded
  745. // with taxonomy term fields from the same object.
  746. // see http://drupal.org/node/1238132 - To reproduce:
  747. // 1) add 2 taxonomy field instances on a bundle
  748. // 2) configure a ds layout showing only one
  749. // 3) embed a view with the 2 taxonomies as fields.
  750. if (isset($field['formatter_settings']['load_terms']) && $field['formatter_settings']['load_terms']) {
  751. static $terms_loaded = array();
  752. if (isset($field['entity']->language)) {
  753. $language = $field['entity']->language;
  754. }
  755. else {
  756. $language = LANGUAGE_NONE;
  757. }
  758. $instances = field_info_instances($field['entity_type'], $field['bundle']);
  759. foreach ($instances as $key => $instance) {
  760. $info = field_info_field($key);
  761. if ($info['module'] == 'taxonomy') {
  762. if (empty($field['entity']->{$key})) {
  763. continue;
  764. }
  765. if (!isset($field['entity']->{$key}[$language])) {
  766. $language = LANGUAGE_NONE;
  767. }
  768. foreach ($field['entity']->{$key}[$language] as $tkey => $item) {
  769. if (isset($item['tid']) && !isset($item['taxonomy_term'])) {
  770. if (!isset($terms_loaded[$item['tid']])) {
  771. $term = taxonomy_term_load($item['tid']);
  772. if (!is_object($term)) {
  773. // This term is missing in the database.
  774. continue;
  775. }
  776. $terms_loaded[$item['tid']] = $term;
  777. }
  778. $field['entity']->{$key}[$language][$tkey]['taxonomy_term'] = $terms_loaded[$item['tid']];
  779. }
  780. }
  781. }
  782. }
  783. }
  784. ctools_include('content');
  785. ctools_include('context');
  786. // Get variables.
  787. $show_title = $field['formatter_settings']['show_title'];
  788. $title_wrapper = trim($field['formatter_settings']['title_wrapper']);
  789. $ctools = unserialize($field['formatter_settings']['ctools']);
  790. $type = $ctools['type'];
  791. $subtype = $ctools['subtype'];
  792. $conf = $ctools['conf'];
  793. $entity_type = $field['entity_type'];
  794. $keywords = $arguments = $contexts = array();
  795. // Create entity context.
  796. ds_create_entity_context($entity_type, $field['entity'], $contexts);
  797. // Build the content.
  798. $data = ctools_content_render($type, $subtype, $conf, $keywords, $arguments, $contexts);
  799. // Return content.
  800. if (!empty($data->content)) {
  801. $content = '';
  802. if ($show_title) {
  803. if (empty($title_wrapper)) $title_wrapper = 'div';
  804. $content .= '<' . check_plain($title_wrapper) . ' class="title">' . $data->title . '</' . check_plain($title_wrapper) . '>';
  805. }
  806. if (is_array($data->content)) {
  807. $content .= drupal_render($data->content);
  808. }
  809. else {
  810. $content .= $data->content;
  811. }
  812. return $content;
  813. }
  814. }
  815. }
  816. /**
  817. * Render a block field.
  818. */
  819. function ds_render_block_field($field) {
  820. // Invoke the block_view hook of the module.
  821. list($module, $delta) = explode('|', $field['properties']['block']);
  822. $block = module_invoke($module, 'block_view', $delta);
  823. // Get contextual links.
  824. $contextual_links = array();
  825. $contextual = module_exists('contextual') && user_access('access contextual links');
  826. if ($contextual) {
  827. if (isset($block['content']) && is_array($block['content']) && isset($block['content']['#contextual_links'])) {
  828. $contextual_links = $block['content']['#contextual_links'];
  829. }
  830. }
  831. // Render the block content.
  832. if (isset($block['content']) && is_array($block['content'])) {
  833. $block['content'] = drupal_render($block['content']);
  834. }
  835. // Go through in case we have actual content.
  836. if (!empty($block['content'])) {
  837. // Make sure subject is set.
  838. if (!isset($block['subject'])) {
  839. $block['subject'] = '';
  840. }
  841. global $theme_key;
  842. if (module_exists('block')) {
  843. $full_block = db_query("SELECT * FROM {block} WHERE module = :module AND delta = :delta AND theme = :theme", array(':module' => $module, ':delta' => $delta, ':theme' => $theme_key))->fetchObject();
  844. }
  845. if (!empty($full_block)) {
  846. if ($full_block->title == '<none>') {
  847. $block['subject'] = '';
  848. }
  849. elseif (!empty($full_block->title)) {
  850. $block['subject'] = $full_block->title;
  851. }
  852. }
  853. // i18n support.
  854. if (function_exists('i18n_block_block_view_alter')) {
  855. // Check language visibility.
  856. global $language;
  857. static $block_languages = FALSE;
  858. if (!$block_languages) {
  859. $block_languages = array();
  860. $result = db_query('SELECT module, delta, language FROM {i18n_block_language}');
  861. foreach ($result as $record) {
  862. $block_languages[$record->module][$record->delta][$record->language] = TRUE;
  863. }
  864. }
  865. if (isset($block_languages[$module][$delta]) && !isset($block_languages[$module][$delta][$language->language])) {
  866. return;
  867. }
  868. // Translate.
  869. if (!empty($full_block->i18n_mode)) {
  870. i18n_block_block_view_alter($block, $full_block);
  871. if (!empty($block['title'])) {
  872. $block['subject'] = $block['title'];
  873. }
  874. }
  875. }
  876. $block = (object) $block;
  877. switch ($field['properties']['block_render']) {
  878. case DS_BLOCK_TEMPLATE:
  879. $block->region = NULL;
  880. $block->module = $module;
  881. $block->delta = $delta;
  882. $elements = array('elements' => array('#block' => $block, '#children' => $block->content));
  883. // Add contextual links.
  884. if ($contextual) {
  885. $elements['elements'] += array('#contextual_links' => array_merge($contextual_links, array('block' => array('admin/structure/block/manage', array($block->module, $block->delta)))));
  886. }
  887. return theme('block', $elements);
  888. break;
  889. case DS_BLOCK_TITLE_CONTENT:
  890. return '<h2 class="block-title">' . $block->subject . '</h2>' . $block->content;
  891. break;
  892. case DS_BLOCK_CONTENT:
  893. return $block->content;
  894. break;
  895. }
  896. }
  897. }
  898. /**
  899. * Render a field.
  900. */
  901. function ds_render_field($field) {
  902. $title_field = FALSE;
  903. $output = '';
  904. $settings = isset($field['formatter_settings']) ? $field['formatter_settings'] : array();
  905. $settings += $field['properties']['default'];
  906. // Basic string.
  907. if (isset($settings['link text'])) {
  908. $output = t($settings['link text']);
  909. }
  910. elseif (isset($field['properties']['entity_render_key']) && isset($field['entity']->{$field['properties']['entity_render_key']})) {
  911. if ($field['entity_type'] == 'user' && $field['properties']['entity_render_key'] == 'name') {
  912. $output = format_username($field['entity']);
  913. }
  914. else {
  915. $title_field = $field['properties']['entity_render_key'] == 'title' && $field['entity_type'] == 'node';
  916. $output = $field['entity']->{$field['properties']['entity_render_key']};
  917. }
  918. }
  919. if (empty($output)) {
  920. return;
  921. }
  922. // Link.
  923. if ($settings['link']) {
  924. if (isset($field['entity']->uri)) {
  925. $uri_info = $field['entity']->uri;
  926. }
  927. else {
  928. $uri_info = entity_uri($field['entity_type'], $field['entity']);
  929. }
  930. if (isset($settings['link class'])) {
  931. $uri_info['options']['attributes']['class'][] = $settings['link class'];
  932. }
  933. $output = l($output, $uri_info['path'], $uri_info['options']);
  934. if ($title_field) {
  935. $output = ds_edit_support('title', $output, $field);
  936. }
  937. }
  938. else {
  939. $output = check_plain($output);
  940. if ($title_field) {
  941. $output = ds_edit_support('title', $output, $field);
  942. }
  943. }
  944. // Wrapper and class.
  945. if (!empty($settings['wrapper'])) {
  946. $wrapper = check_plain($settings['wrapper']);
  947. $class = (!empty($settings['class'])) ? ' class="' . check_plain($settings['class']) . '"' : '';
  948. $output = '<' . $wrapper . $class . '>' . $output . '</' . $wrapper . '>';
  949. }
  950. return $output;
  951. }
  952. /**
  953. * Support for edit module.
  954. *
  955. * @param $field_name
  956. * The name of the field.
  957. * @param $output
  958. * The output of the field.
  959. * @param $field
  960. * The complete field array.
  961. *
  962. * @return
  963. * The field ready for edit module or the same value in case
  964. * the edit module is not enabled.
  965. */
  966. function ds_edit_support($field_name, $output, $field) {
  967. if (module_exists('edit')) {
  968. $edit_id = "node/" . $field['entity']->nid . "/" . $field_name . "/" . $field['entity']->language . "/" . $field['view_mode'];
  969. $output = edit_wrap_pseudofield($output, $edit_id);
  970. }
  971. return $output;
  972. }
  973. /**
  974. * Render an author field.
  975. */
  976. function ds_render_author_field($field) {
  977. // Users without a user name are anonymous users. These are never linked.
  978. if (empty($field['entity']->name)) {
  979. $output = check_plain(variable_get('anonymous', t('Anonymous')));
  980. }
  981. if ($field['formatter'] == 'author') {
  982. $output = format_username($field['entity']);
  983. }
  984. if ($field['formatter'] == 'author_linked') {
  985. $output = theme('username', array('account' => $field['entity']));
  986. }
  987. return ds_edit_support('author', $output, $field);
  988. }
  989. /**
  990. * Render a markup field.
  991. */
  992. function ds_render_markup($field) {
  993. if (isset($field['entity']->{$field['properties']['key']})) {
  994. // Check for format, and let's take filtered_html as a sane default.
  995. $format = isset($field['entity']->{$field['properties']['format']}) ? $field['entity']->{$field['properties']['format']} : 'filtered_html';
  996. return check_markup($field['entity']->{$field['properties']['key']}, $format, '', TRUE);;
  997. }
  998. }
  999. /**
  1000. * Return the picture.
  1001. */
  1002. function ds_return_picture($entity) {
  1003. // Gravatar support.
  1004. if (module_exists('gravatar')) {
  1005. $entity = _gravatar_load_account($entity);
  1006. $entity->picture = _gravatar_get_account_user_picture($entity);
  1007. }
  1008. if (!empty($entity->picture)) {
  1009. if (is_numeric($entity->picture)) {
  1010. return file_load($entity->picture);
  1011. }
  1012. else {
  1013. return $entity->picture;
  1014. }
  1015. }
  1016. elseif (variable_get('user_picture_default', '')) {
  1017. return variable_get('user_picture_default', '');
  1018. }
  1019. }
  1020. /**
  1021. * Render a user picture.
  1022. */
  1023. function ds_render_user_picture($field) {
  1024. $picture = ds_return_picture($field['entity']);
  1025. if (!empty($picture)) {
  1026. $filepath = (isset($picture->uri)) ? $picture->uri : $picture;
  1027. $name = format_username($field['entity']);
  1028. $alt = t("@user's picture", array('@user' => $name));
  1029. $vars = array('path' => $filepath, 'alt' => $alt, 'title' => $alt);
  1030. // If the image does not have a valid Drupal scheme (for eg. HTTP),
  1031. // don't load image styles.
  1032. if (module_exists('image') && file_valid_uri($filepath)) {
  1033. $vars['style_name'] = str_replace('ds_picture_', '', $field['formatter']);
  1034. $image = theme('image_style', $vars);
  1035. }
  1036. else {
  1037. $image = theme('image', $vars);
  1038. }
  1039. if (!empty($field['entity']->uid) && user_access('access user profiles')) {
  1040. return l($image, 'user/' . $field['entity']->uid, array('html' => TRUE));
  1041. }
  1042. else {
  1043. return $image;
  1044. }
  1045. }
  1046. }
  1047. /**
  1048. * Render a date field.
  1049. */
  1050. function ds_render_date_field($field) {
  1051. $date_format = str_replace('ds_post_date_', '', $field['formatter']);
  1052. return ds_edit_support($field['properties']['entity_render_key'], format_date($field['entity']->{$field['properties']['entity_render_key']}, $date_format), $field);
  1053. }
  1054. /**
  1055. * Render a "Submitted by"-line.
  1056. */
  1057. function ds_render_submitted_by($field) {
  1058. $account = user_load($field['entity']->uid);
  1059. switch ($field['formatter']) {
  1060. case 'ds_time_ago':
  1061. $interval = REQUEST_TIME - $field['entity']->created;
  1062. return t('Submitted !interval ago by !user.', array('!interval' => format_interval($interval), '!user' => theme('username', array('account' => $account))));
  1063. default:
  1064. $date_format = str_replace('ds_post_date_', '', $field['formatter']);
  1065. return t('Submitted by !user on !date.', array('!user' => theme('username', array('account' => $account)), '!date' => format_date($field['entity']->created, $date_format)));
  1066. }
  1067. }
  1068. /**
  1069. * Implements hook_field_formatter_info().
  1070. */
  1071. function ds_field_formatter_info() {
  1072. $formatters = array();
  1073. if (module_exists('taxonomy')) {
  1074. $formatters['ds_taxonomy_view_mode'] = array(
  1075. 'label' => t('Rendered taxonomy term'),
  1076. 'description' => t('Display the referenced term in a specific view mode'),
  1077. 'field types' => array('taxonomy_term_reference'),
  1078. 'settings' => array(
  1079. 'taxonomy_term_reference_view_mode' => 'full',
  1080. 'use_content_language' => TRUE,
  1081. ),
  1082. );
  1083. $formatters['ds_taxonomy_separator'] = array(
  1084. 'label' => t('Separated'),
  1085. 'description' => t('Display the referenced term with a separator.'),
  1086. 'field types' => array('taxonomy_term_reference'),
  1087. 'settings' => array(
  1088. 'taxonomy_term_link' => TRUE,
  1089. 'taxonomy_term_separator' => ', ',
  1090. ),
  1091. );
  1092. if (module_exists('i18n_taxonomy')) {
  1093. $formatters['ds_taxonomy_separator_localized'] = array(
  1094. 'label' => t('Separated (localized)'),
  1095. 'description' => t('Display the referenced term with a separator. Use this with the "localize" translation mode for vocabularies.'),
  1096. 'field types' => array('taxonomy_term_reference'),
  1097. 'settings' => array(
  1098. 'taxonomy_term_link' => TRUE,
  1099. 'taxonomy_term_separator' => ', ',
  1100. ),
  1101. );
  1102. }
  1103. }
  1104. return $formatters;
  1105. }
  1106. /**
  1107. * Implements hook_field_formatter_view().
  1108. */
  1109. function ds_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  1110. global $language;
  1111. $element = array();
  1112. if ($display['type'] === 'ds_taxonomy_view_mode') {
  1113. $view_mode = $display['settings']['taxonomy_term_reference_view_mode'];
  1114. foreach ($items as $delta => $item) {
  1115. if ($item['tid'] == 'autocreate') {
  1116. // We don't necessarily have a link when this is
  1117. // an autocreated term, usually in preview.
  1118. // So just send the term as check plained markup.
  1119. $build['#markup'] = check_plain($item['name']);
  1120. }
  1121. else {
  1122. $term = taxonomy_term_load($item['tid']);
  1123. if (module_exists('i18n_taxonomy')) {
  1124. $term = i18n_taxonomy_localize_terms($term);
  1125. }
  1126. if (!is_object($term)) {
  1127. // This term is missing in the database.
  1128. continue;
  1129. }
  1130. $settings = $display['settings'];
  1131. if ($settings['use_content_language'] && !empty($GLOBALS['language_content']->language)) {
  1132. $langcode = $GLOBALS['language_content']->language;
  1133. }
  1134. if (!empty($term)) {
  1135. $build = taxonomy_term_view($term, $view_mode, $langcode);
  1136. }
  1137. else {
  1138. $build['#markup'] = '';
  1139. }
  1140. }
  1141. $element[$delta] = $build;
  1142. }
  1143. }
  1144. if ($display['type'] === 'ds_taxonomy_separator' || $display['type'] == 'ds_taxonomy_separator_localized') {
  1145. $linked = $display['settings']['taxonomy_term_link'];
  1146. $terms = array();
  1147. foreach ($items as $delta => $item) {
  1148. if ($item['tid'] == 'autocreate') {
  1149. // We don't necessarily have a link when this is
  1150. // an autocreated term, usually in preview.
  1151. // So just send the term as check plained markup.
  1152. $item_display = check_plain($item['name']);
  1153. }
  1154. else {
  1155. $term = taxonomy_term_load($item['tid']);
  1156. if (module_exists('i18n_taxonomy')) {
  1157. $term = i18n_taxonomy_localize_terms($term);
  1158. }
  1159. if (!is_object($term)) {
  1160. // This term is missing in the database.
  1161. continue;
  1162. }
  1163. if ($display['type'] == 'ds_taxonomy_separator_localized' && function_exists('i18n_taxonomy_term_name')) {
  1164. $term->name = i18n_taxonomy_term_name($term, $language->language);
  1165. }
  1166. $item_display = check_plain($term->name);
  1167. if ($linked) {
  1168. $uri = entity_uri('taxonomy_term', $term);
  1169. $item_display = l($term->name, $uri['path'], $uri['options']);
  1170. }
  1171. }
  1172. $terms[] = $item_display;
  1173. }
  1174. if (!empty($terms)) {
  1175. $output = '';
  1176. $separator = $display['settings']['taxonomy_term_separator'];
  1177. $output = implode($separator, $terms);
  1178. $element[0] = array('#markup' => $output);
  1179. }
  1180. }
  1181. return $element;
  1182. }
  1183. /**
  1184. * Implements hook_contextual_links_view_alter().
  1185. */
  1186. function ds_contextual_links_view_alter(&$element, $items) {
  1187. if (!empty($element['#element']['#entity_type']) && !empty($element['#element']['#bundle']) && module_exists('field_ui') && user_access('administer content types')) {
  1188. $entity_type = $element['#element']['#entity_type'];
  1189. $bundle = $element['#element']['#bundle'];
  1190. $view_mode = isset($element['#element']['#view_mode']) ? $element['#element']['#view_mode'] : 'default';
  1191. // Get the manage display URI.
  1192. $admin_path = _field_ui_bundle_admin_path($entity_type, $bundle);
  1193. // Check view mode settings.
  1194. $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  1195. $overridden = (!empty($view_mode_settings[$view_mode]['custom_settings']) ? TRUE : FALSE);
  1196. if (!$overridden) {
  1197. $admin_path .= '/display';
  1198. }
  1199. else {
  1200. $admin_path .= '/display/' . $view_mode;
  1201. }
  1202. $element['#links']['manage-display'] = array(
  1203. 'title' => t('Manage display'),
  1204. 'href' => $admin_path,
  1205. 'query' => drupal_get_destination(),
  1206. );
  1207. }
  1208. }