views_handler_field_field.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. <?php
  2. /**
  3. * @file
  4. * Definition of views_handler_field_field.
  5. */
  6. /**
  7. * Helper function: Return an array of formatter options for a field type.
  8. *
  9. * Borrowed from field_ui.
  10. */
  11. function _field_view_formatter_options($field_type = NULL) {
  12. $options = &drupal_static(__FUNCTION__);
  13. if (!isset($options)) {
  14. $field_types = field_info_field_types();
  15. $options = array();
  16. foreach (field_info_formatter_types() as $name => $formatter) {
  17. foreach ($formatter['field types'] as $formatter_field_type) {
  18. // Check that the field type exists.
  19. if (isset($field_types[$formatter_field_type])) {
  20. $options[$formatter_field_type][$name] = $formatter['label'];
  21. }
  22. }
  23. }
  24. }
  25. if ($field_type) {
  26. return !empty($options[$field_type]) ? $options[$field_type] : array();
  27. }
  28. return $options;
  29. }
  30. /**
  31. * A field that displays fieldapi fields.
  32. *
  33. * @ingroup views_field_handlers
  34. */
  35. class views_handler_field_field extends views_handler_field {
  36. /**
  37. * An array to store field renderable arrays for use by render_items.
  38. *
  39. * @var array
  40. */
  41. public $items = array();
  42. /**
  43. * Store the field information.
  44. *
  45. * @var array
  46. */
  47. public $field_info = array();
  48. /**
  49. * Does the field supports multiple field values.
  50. *
  51. * @var bool
  52. */
  53. public $multiple;
  54. /**
  55. * Does the rendered fields get limited.
  56. *
  57. * @var bool
  58. */
  59. public $limit_values;
  60. /**
  61. * A shortcut for $view->base_table.
  62. *
  63. * @var string
  64. */
  65. public $base_table;
  66. /**
  67. * Store the field instance.
  68. *
  69. * @var array
  70. */
  71. public $instance;
  72. function init(&$view, &$options) {
  73. parent::init($view, $options);
  74. $this->field_info = $field = field_info_field($this->definition['field_name']);
  75. $this->multiple = FALSE;
  76. $this->limit_values = FALSE;
  77. if ($field['cardinality'] > 1 || $field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
  78. $this->multiple = TRUE;
  79. // If "Display all values in the same row" is FALSE, then we always limit
  80. // in order to show a single unique value per row.
  81. if (!$this->options['group_rows']) {
  82. $this->limit_values = TRUE;
  83. }
  84. // If "First and last only" is chosen, limit the values
  85. if (!empty($this->options['delta_first_last'])) {
  86. $this->limit_values = TRUE;
  87. }
  88. // Otherwise, we only limit values if the user hasn't selected "all", 0, or
  89. // the value matching field cardinality.
  90. if ((intval($this->options['delta_limit']) && ($this->options['delta_limit'] != $field['cardinality'])) || intval($this->options['delta_offset'])) {
  91. $this->limit_values = TRUE;
  92. }
  93. }
  94. // Convert old style entity id group column to new format.
  95. // @todo Remove for next major version.
  96. if ($this->options['group_column'] == 'entity id') {
  97. $this->options['group_column'] = 'entity_id';
  98. }
  99. }
  100. /**
  101. * Check whether current user has access to this handler.
  102. *
  103. * @return bool
  104. * Return TRUE if the user has access to view this field.
  105. */
  106. function access() {
  107. $base_table = $this->get_base_table();
  108. return field_access('view', $this->field_info, $this->definition['entity_tables'][$base_table]);
  109. }
  110. /**
  111. * Set the base_table and base_table_alias.
  112. *
  113. * @return string
  114. * The base table which is used in the current view "context".
  115. */
  116. function get_base_table() {
  117. if (!isset($this->base_table)) {
  118. // This base_table is coming from the entity not the field.
  119. $this->base_table = $this->view->base_table;
  120. // If the current field is under a relationship you can't be sure that the
  121. // base table of the view is the base table of the current field.
  122. // For example a field from a node author on a node view does have users as base table.
  123. if (!empty($this->options['relationship']) && $this->options['relationship'] != 'none') {
  124. $relationships = $this->view->display_handler->get_option('relationships');
  125. if (!empty($relationships[$this->options['relationship']])) {
  126. $options = $relationships[$this->options['relationship']];
  127. $data = views_fetch_data($options['table']);
  128. $this->base_table = $data[$options['field']]['relationship']['base'];
  129. }
  130. }
  131. }
  132. return $this->base_table;
  133. }
  134. /**
  135. * Called to add the field to a query.
  136. *
  137. * By default, the only columns added to the query are entity_id and
  138. * entity_type. This is because other needed data is fetched by entity_load().
  139. * Other columns are added only if they are used in groupings, or if
  140. * 'add fields to query' is specifically set to TRUE in the field definition.
  141. *
  142. * The 'add fields to query' switch is used by modules which need all data
  143. * present in the query itself (such as "sphinx").
  144. */
  145. function query($use_groupby = FALSE) {
  146. $this->get_base_table();
  147. $params = array();
  148. if ($use_groupby) {
  149. // When grouping on a "field API" field (whose "real_field" is set to
  150. // entity_id), retrieve the minimum entity_id to have a valid entity_id to
  151. // pass to field_view_field().
  152. $params = array(
  153. 'function' => 'min',
  154. );
  155. $this->ensure_my_table();
  156. }
  157. // Get the entity type according to the base table of the field.
  158. // Then add it to the query as a formula. That way we can avoid joining
  159. // the field table if all we need is entity_id and entity_type.
  160. $entity_type = $this->definition['entity_tables'][$this->base_table];
  161. $entity_info = entity_get_info($entity_type);
  162. if (isset($this->relationship)) {
  163. $this->base_table_alias = $this->relationship;
  164. }
  165. else {
  166. $this->base_table_alias = $this->base_table;
  167. }
  168. // We always need the base field (entity_id / revision_id).
  169. if (empty($this->definition['is revision'])) {
  170. $this->field_alias = $this->query->add_field($this->base_table_alias, $entity_info['entity keys']['id'], '', $params);
  171. }
  172. else {
  173. $this->field_alias = $this->query->add_field($this->base_table_alias, $entity_info['entity keys']['revision'], '', $params);
  174. $this->aliases['entity_id'] = $this->query->add_field($this->base_table_alias, $entity_info['entity keys']['id'], '', $params);
  175. }
  176. // The alias needs to be unique, so we use both the field table and the entity type.
  177. $entity_type_alias = $this->definition['table'] . '_' . $entity_type . '_entity_type';
  178. $this->aliases['entity_type'] = $this->query->add_field(NULL, "'$entity_type'", $entity_type_alias);
  179. $fields = $this->additional_fields;
  180. // We've already added entity_type, so we can remove it from the list.
  181. $entity_type_key = array_search('entity_type', $fields);
  182. if ($entity_type_key !== FALSE) {
  183. unset($fields[$entity_type_key]);
  184. }
  185. if ($use_groupby) {
  186. // Add the fields that we're actually grouping on.
  187. $options = array();
  188. if ($this->options['group_column'] != 'entity_id') {
  189. $options = array($this->options['group_column'] => $this->options['group_column']);
  190. }
  191. $options += is_array($this->options['group_columns']) ? $this->options['group_columns'] : array();
  192. $fields = array();
  193. $rkey = $this->definition['is revision'] ? 'FIELD_LOAD_REVISION' : 'FIELD_LOAD_CURRENT';
  194. // Go through the list and determine the actual column name from field api.
  195. foreach ($options as $column) {
  196. $name = $column;
  197. if (isset($this->field_info['storage']['details']['sql'][$rkey][$this->table][$column])) {
  198. $name = $this->field_info['storage']['details']['sql'][$rkey][$this->table][$column];
  199. }
  200. $fields[$column] = $name;
  201. }
  202. $this->group_fields = $fields;
  203. }
  204. // Add additional fields (and the table join itself) if needed.
  205. if ($this->add_field_table($use_groupby)) {
  206. $this->ensure_my_table();
  207. $this->add_additional_fields($fields);
  208. // Filter by language, if field translation is enabled.
  209. $field = $this->field_info;
  210. if (field_is_translatable($entity_type, $field) && !empty($this->view->display_handler->options['field_language_add_to_query'])) {
  211. $column = $this->table_alias . '.language';
  212. // By the same reason as field_language the field might be LANGUAGE_NONE in reality so allow it as well.
  213. // @see this::field_language()
  214. global $language_content;
  215. $default_language = language_default('language');
  216. $language = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
  217. array($language_content->language, $default_language),
  218. $this->view->display_handler->options['field_language']);
  219. $placeholder = $this->placeholder();
  220. $language_fallback_candidates = array($language);
  221. if (variable_get('locale_field_language_fallback', TRUE)) {
  222. require_once DRUPAL_ROOT . '/includes/language.inc';
  223. $language_fallback_candidates = array_merge($language_fallback_candidates, language_fallback_get_candidates());
  224. }
  225. else {
  226. $language_fallback_candidates[] = LANGUAGE_NONE;
  227. }
  228. $this->query->add_where_expression(0, "$column IN($placeholder) OR $column IS NULL", array($placeholder => $language_fallback_candidates));
  229. }
  230. }
  231. // The revision id inhibits grouping.
  232. // So, stop here if we're using grouping, or if aren't adding all columns to
  233. // the query.
  234. if ($use_groupby || empty($this->definition['add fields to query'])) {
  235. return;
  236. }
  237. $this->add_additional_fields(array('revision_id'));
  238. }
  239. /**
  240. * Determine if the field table should be added to the query.
  241. */
  242. function add_field_table($use_groupby) {
  243. // Grouping is enabled, or we are explicitly required to do this.
  244. if ($use_groupby || !empty($this->definition['add fields to query'])) {
  245. return TRUE;
  246. }
  247. // This a multiple value field, but "group multiple values" is not checked.
  248. if ($this->multiple && !$this->options['group_rows']) {
  249. return TRUE;
  250. }
  251. return FALSE;
  252. }
  253. /**
  254. * Determine if this field is click sortable.
  255. */
  256. function click_sortable() {
  257. // Not click sortable in any case.
  258. if (empty($this->definition['click sortable'])) {
  259. return FALSE;
  260. }
  261. // A field is not click sortable if it's a multiple field with
  262. // "group multiple values" checked, since a click sort in that case would
  263. // add a join to the field table, which would produce unwanted duplicates.
  264. if ($this->multiple && $this->options['group_rows']) {
  265. return FALSE;
  266. }
  267. return TRUE;
  268. }
  269. /**
  270. * Called to determine what to tell the clicksorter.
  271. */
  272. function click_sort($order) {
  273. // No column selected, can't continue.
  274. if (empty($this->options['click_sort_column'])) {
  275. return;
  276. }
  277. $this->ensure_my_table();
  278. $column = _field_sql_storage_columnname($this->definition['field_name'], $this->options['click_sort_column']);
  279. if (!isset($this->aliases[$column])) {
  280. // Column is not in query; add a sort on it (without adding the column).
  281. $this->aliases[$column] = $this->table_alias . '.' . $column;
  282. }
  283. $this->query->add_orderby(NULL, NULL, $order, $this->aliases[$column]);
  284. }
  285. function option_definition() {
  286. $options = parent::option_definition();
  287. // option_definition runs before init/construct, so no $this->field_info
  288. $field = field_info_field($this->definition['field_name']);
  289. $field_type = field_info_field_types($field['type']);
  290. $column_names = array_keys($field['columns']);
  291. $default_column = '';
  292. // Try to determine a sensible default.
  293. if (count($column_names) == 1) {
  294. $default_column = $column_names[0];
  295. }
  296. elseif (in_array('value', $column_names)) {
  297. $default_column = 'value';
  298. }
  299. // If the field has a "value" column, we probably need that one.
  300. $options['click_sort_column'] = array(
  301. 'default' => $default_column,
  302. );
  303. $options['type'] = array(
  304. 'default' => $field_type['default_formatter'],
  305. );
  306. $options['settings'] = array(
  307. 'default' => array(),
  308. );
  309. $options['group_column'] = array(
  310. 'default' => $default_column,
  311. );
  312. $options['group_columns'] = array(
  313. 'default' => array(),
  314. );
  315. // Options used for multiple value fields.
  316. $options['group_rows'] = array(
  317. 'default' => TRUE,
  318. 'bool' => TRUE,
  319. );
  320. // If we know the exact number of allowed values, then that can be
  321. // the default. Otherwise, default to 'all'.
  322. $options['delta_limit'] = array(
  323. 'default' => ($field['cardinality'] > 1) ? $field['cardinality'] : 'all',
  324. );
  325. $options['delta_offset'] = array(
  326. 'default' => 0,
  327. );
  328. $options['delta_reversed'] = array(
  329. 'default' => FALSE,
  330. 'bool' => TRUE,
  331. );
  332. $options['delta_first_last'] = array(
  333. 'default' => FALSE,
  334. 'bool' => TRUE,
  335. );
  336. $options['multi_type'] = array(
  337. 'default' => 'separator'
  338. );
  339. $options['separator'] = array(
  340. 'default' => ', '
  341. );
  342. $options['field_api_classes'] = array(
  343. 'default' => FALSE,
  344. 'bool' => TRUE,
  345. );
  346. return $options;
  347. }
  348. function options_form(&$form, &$form_state) {
  349. parent::options_form($form, $form_state);
  350. $field = $this->field_info;
  351. $formatters = _field_view_formatter_options($field['type']);
  352. $column_names = array_keys($field['columns']);
  353. // If this is a multiple value field, add its options.
  354. if ($this->multiple) {
  355. $this->multiple_options_form($form, $form_state);
  356. }
  357. // No need to ask the user anything if the field has only one column.
  358. if (count($field['columns']) == 1) {
  359. $form['click_sort_column'] = array(
  360. '#type' => 'value',
  361. '#value' => isset($column_names[0]) ? $column_names[0] : '',
  362. );
  363. }
  364. else {
  365. $form['click_sort_column'] = array(
  366. '#type' => 'select',
  367. '#title' => t('Column used for click sorting'),
  368. '#options' => drupal_map_assoc($column_names),
  369. '#default_value' => $this->options['click_sort_column'],
  370. '#description' => t('Used by Style: Table to determine the actual column to click sort the field on. The default is usually fine.'),
  371. '#fieldset' => 'more',
  372. );
  373. }
  374. $form['type'] = array(
  375. '#type' => 'select',
  376. '#title' => t('Formatter'),
  377. '#options' => $formatters,
  378. '#default_value' => $this->options['type'],
  379. '#ajax' => array(
  380. 'path' => views_ui_build_form_url($form_state),
  381. ),
  382. '#submit' => array('views_ui_config_item_form_submit_temporary'),
  383. '#executes_submit_callback' => TRUE,
  384. );
  385. $form['field_api_classes'] = array(
  386. '#title' => t('Use field template'),
  387. '#type' => 'checkbox',
  388. '#default_value' => $this->options['field_api_classes'],
  389. '#description' => t('If checked, field api classes will be added using field.tpl.php (or equivalent). This is not recommended unless your CSS depends upon these classes. If not checked, template will not be used.'),
  390. '#fieldset' => 'style_settings',
  391. '#weight' => 20,
  392. );
  393. if ($this->multiple) {
  394. $form['field_api_classes']['#description'] .= ' ' . t('Checking this option will cause the group Display Type and Separator values to be ignored.');
  395. }
  396. // Get the currently selected formatter.
  397. $format = $this->options['type'];
  398. $formatter = field_info_formatter_types($format);
  399. $settings = $this->options['settings'] + field_info_formatter_settings($format);
  400. // Provide an instance array for hook_field_formatter_settings_form().
  401. ctools_include('fields');
  402. $this->instance = ctools_fields_fake_field_instance($this->definition['field_name'], '_custom', $formatter, $settings);
  403. // Store the settings in a '_custom' view mode.
  404. $this->instance['display']['_custom'] = array(
  405. 'type' => $format,
  406. 'settings' => $settings,
  407. );
  408. // Get the settings form.
  409. $settings_form = array('#value' => array());
  410. $function = $formatter['module'] . '_field_formatter_settings_form';
  411. if (function_exists($function)) {
  412. $settings_form = $function($field, $this->instance, '_custom', $form, $form_state);
  413. }
  414. $form['settings'] = $settings_form;
  415. }
  416. /**
  417. * Provide options for multiple value fields.
  418. */
  419. function multiple_options_form(&$form, &$form_state) {
  420. $field = $this->field_info;
  421. $form['multiple_field_settings'] = array(
  422. '#type' => 'fieldset',
  423. '#title' => t('Multiple field settings'),
  424. '#collapsible' => TRUE,
  425. '#collapsed' => TRUE,
  426. '#weight' => 5,
  427. );
  428. $form['group_rows'] = array(
  429. '#title' => t('Display all values in the same row'),
  430. '#type' => 'checkbox',
  431. '#default_value' => $this->options['group_rows'],
  432. '#description' => t('If checked, multiple values for this field will be shown in the same row. If not checked, each value in this field will create a new row. If using group by, please make sure to group by "Entity ID" for this setting to have any effect.'),
  433. '#fieldset' => 'multiple_field_settings',
  434. );
  435. // Make the string translatable by keeping it as a whole rather than
  436. // translating prefix and suffix separately.
  437. list($prefix, $suffix) = explode('@count', t('Display @count value(s)'));
  438. if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
  439. $type = 'textfield';
  440. $options = NULL;
  441. $size = 5;
  442. }
  443. else {
  444. $type = 'select';
  445. $options = drupal_map_assoc(range(1, $field['cardinality']));
  446. $size = 1;
  447. }
  448. $form['multi_type'] = array(
  449. '#type' => 'radios',
  450. '#title' => t('Display type'),
  451. '#options' => array(
  452. 'ul' => t('Unordered list'),
  453. 'ol' => t('Ordered list'),
  454. 'separator' => t('Simple separator'),
  455. ),
  456. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  457. '#default_value' => $this->options['multi_type'],
  458. '#fieldset' => 'multiple_field_settings',
  459. );
  460. $form['separator'] = array(
  461. '#type' => 'textfield',
  462. '#title' => t('Separator'),
  463. '#default_value' => $this->options['separator'],
  464. '#dependency' => array(
  465. 'radio:options[multi_type]' => array('separator'),
  466. 'edit-options-group-rows' => array(TRUE),
  467. ),
  468. '#dependency_count' => 2,
  469. '#fieldset' => 'multiple_field_settings',
  470. );
  471. $form['delta_limit'] = array(
  472. '#type' => $type,
  473. '#size' => $size,
  474. '#field_prefix' => $prefix,
  475. '#field_suffix' => $suffix,
  476. '#options' => $options,
  477. '#default_value' => $this->options['delta_limit'],
  478. '#prefix' => '<div class="container-inline">',
  479. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  480. '#fieldset' => 'multiple_field_settings',
  481. );
  482. list($prefix, $suffix) = explode('@count', t('starting from @count'));
  483. $form['delta_offset'] = array(
  484. '#type' => 'textfield',
  485. '#size' => 5,
  486. '#field_prefix' => $prefix,
  487. '#field_suffix' => $suffix,
  488. '#default_value' => $this->options['delta_offset'],
  489. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  490. '#description' => t('(first item is 0)'),
  491. '#fieldset' => 'multiple_field_settings',
  492. );
  493. $form['delta_reversed'] = array(
  494. '#title' => t('Reversed'),
  495. '#type' => 'checkbox',
  496. '#default_value' => $this->options['delta_reversed'],
  497. '#suffix' => $suffix,
  498. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  499. '#description' => t('(start from last values)'),
  500. '#fieldset' => 'multiple_field_settings',
  501. );
  502. $form['delta_first_last'] = array(
  503. '#title' => t('First and last only'),
  504. '#type' => 'checkbox',
  505. '#default_value' => $this->options['delta_first_last'],
  506. '#suffix' => '</div>',
  507. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  508. '#fieldset' => 'multiple_field_settings',
  509. );
  510. }
  511. /**
  512. * Extend the groupby form with group columns.
  513. */
  514. function groupby_form(&$form, &$form_state) {
  515. parent::groupby_form($form, $form_state);
  516. // With "field API" fields, the column target of the grouping function
  517. // and any additional grouping columns must be specified.
  518. $group_columns = array(
  519. 'entity_id' => t('Entity ID'),
  520. ) + drupal_map_assoc(array_keys($this->field_info['columns']), 'ucfirst');
  521. $form['group_column'] = array(
  522. '#type' => 'select',
  523. '#title' => t('Group column'),
  524. '#default_value' => $this->options['group_column'],
  525. '#description' => t('Select the column of this field to apply the grouping function selected above.'),
  526. '#options' => $group_columns,
  527. );
  528. $options = drupal_map_assoc(array('bundle', 'language', 'entity_type'), 'ucfirst');
  529. // Add on defined fields, noting that they're prefixed with the field name.
  530. $form['group_columns'] = array(
  531. '#type' => 'checkboxes',
  532. '#title' => t('Group columns (additional)'),
  533. '#default_value' => $this->options['group_columns'],
  534. '#description' => t('Select any additional columns of this field to include in the query and to group on.'),
  535. '#options' => $options + $group_columns,
  536. );
  537. }
  538. function groupby_form_submit(&$form, &$form_state) {
  539. parent::groupby_form_submit($form, $form_state);
  540. $item =& $form_state['handler']->options;
  541. // Add settings for "field API" fields.
  542. $item['group_column'] = $form_state['values']['options']['group_column'];
  543. $item['group_columns'] = array_filter($form_state['values']['options']['group_columns']);
  544. }
  545. /**
  546. * Load the entities for all fields that are about to be displayed.
  547. */
  548. function post_execute(&$values) {
  549. if (!empty($values)) {
  550. // Divide the entity ids by entity type, so they can be loaded in bulk.
  551. $entities_by_type = array();
  552. $revisions_by_type = array();
  553. foreach ($values as $key => $object) {
  554. if (isset($this->aliases['entity_type']) && isset($object->{$this->aliases['entity_type']}) && isset($object->{$this->field_alias}) && !isset($values[$key]->_field_data[$this->field_alias])) {
  555. $entity_type = $object->{$this->aliases['entity_type']};
  556. if (empty($this->definition['is revision'])) {
  557. $entity_id = $object->{$this->field_alias};
  558. $entities_by_type[$entity_type][$key] = $entity_id;
  559. }
  560. else {
  561. $revision_id = $object->{$this->field_alias};
  562. $entity_id = $object->{$this->aliases['entity_id']};
  563. $entities_by_type[$entity_type][$key] = array($entity_id, $revision_id);
  564. }
  565. }
  566. }
  567. // Load the entities.
  568. foreach ($entities_by_type as $entity_type => $entity_ids) {
  569. $entity_info = entity_get_info($entity_type);
  570. if (empty($this->definition['is revision'])) {
  571. $entities = entity_load($entity_type, $entity_ids);
  572. $keys = $entity_ids;
  573. }
  574. else {
  575. // Revisions can't be loaded multiple, so we have to load them
  576. // one by one.
  577. $entities = array();
  578. $keys = array();
  579. foreach ($entity_ids as $key => $combined) {
  580. list($entity_id, $revision_id) = $combined;
  581. $entity = entity_load($entity_type, array($entity_id), array($entity_info['entity keys']['revision'] => $revision_id));
  582. if ($entity) {
  583. $entities[$revision_id] = array_shift($entity);
  584. $keys[$key] = $revision_id;
  585. }
  586. }
  587. }
  588. foreach ($keys as $key => $entity_id) {
  589. // If this is a revision, load the revision instead.
  590. if (isset($entities[$entity_id])) {
  591. $values[$key]->_field_data[$this->field_alias] = array(
  592. 'entity_type' => $entity_type,
  593. 'entity' => $entities[$entity_id],
  594. );
  595. }
  596. }
  597. }
  598. // Now, transfer the data back into the resultset so it can be easily used.
  599. foreach ($values as $row_id => &$value) {
  600. $value->{'field_' . $this->options['id']} = $this->set_items($value, $row_id);
  601. }
  602. }
  603. }
  604. /**
  605. * Render all items in this field together.
  606. *
  607. * When using advanced render, each possible item in the list is rendered
  608. * individually. Then the items are all pasted together.
  609. */
  610. function render_items($items) {
  611. if (!empty($items)) {
  612. if (!$this->options['group_rows']) {
  613. return implode('', $items);
  614. }
  615. if ($this->options['multi_type'] == 'separator') {
  616. return implode(filter_xss_admin($this->options['separator']), $items);
  617. }
  618. else {
  619. return theme('item_list',
  620. array(
  621. 'items' => $items,
  622. 'title' => NULL,
  623. 'type' => $this->options['multi_type']
  624. ));
  625. }
  626. }
  627. }
  628. function get_items($values) {
  629. return $values->{'field_' . $this->options['id']};
  630. }
  631. function get_value($values, $field = NULL) {
  632. // Go ahead and render and store in $this->items.
  633. $entity = clone $values->_field_data[$this->field_alias]['entity'];
  634. $entity_type = $values->_field_data[$this->field_alias]['entity_type'];
  635. $langcode = $this->field_language($entity_type, $entity);
  636. // If we are grouping, copy our group fields into the cloned entity.
  637. // It's possible this will cause some weirdness, but there's only
  638. // so much we can hope to do.
  639. if (!empty($this->group_fields)) {
  640. // first, test to see if we have a base value.
  641. $base_value = array();
  642. // Note: We would copy original values here, but it can cause problems.
  643. // For example, text fields store cached filtered values as
  644. // 'safe_value' which doesn't appear anywhere in the field definition
  645. // so we can't affect it. Other side effects could happen similarly.
  646. $data = FALSE;
  647. foreach ($this->group_fields as $field_name => $column) {
  648. if (property_exists($values, $this->aliases[$column])) {
  649. $base_value[$field_name] = $values->{$this->aliases[$column]};
  650. if (isset($base_value[$field_name])) {
  651. $data = TRUE;
  652. }
  653. }
  654. }
  655. // If any of our aggregated fields have data, fake it:
  656. if ($data) {
  657. // Now, overwrite the original value with our aggregated value.
  658. // This overwrites it so there is always just one entry.
  659. $entity->{$this->definition['field_name']}[$langcode] = array($base_value);
  660. }
  661. else {
  662. $entity->{$this->definition['field_name']}[$langcode] = array();
  663. }
  664. }
  665. // The field we are trying to display doesn't exist on this entity.
  666. if (!isset($entity->{$this->definition['field_name']})) {
  667. return array();
  668. }
  669. // We are supposed to show only certain deltas.
  670. if ($this->limit_values && !empty($entity->{$this->definition['field_name']})) {
  671. $all_values = !empty($entity->{$this->definition['field_name']}[$langcode]) ? $entity->{$this->definition['field_name']}[$langcode] : array();
  672. if ($this->options['delta_reversed']) {
  673. $all_values = array_reverse($all_values);
  674. }
  675. // Offset is calculated differently when row grouping for a field is
  676. // not enabled. Since there are multiple rows, the delta needs to be
  677. // taken into account, so that different values are shown per row.
  678. if (!$this->options['group_rows'] && isset($this->aliases['delta']) && isset($values->{$this->aliases['delta']})) {
  679. $delta_limit = 1;
  680. $offset = $values->{$this->aliases['delta']};
  681. }
  682. // Single fields don't have a delta available so choose 0.
  683. elseif (!$this->options['group_rows'] && !$this->multiple) {
  684. $delta_limit = 1;
  685. $offset = 0;
  686. }
  687. else {
  688. $delta_limit = $this->options['delta_limit'];
  689. $offset = intval($this->options['delta_offset']);
  690. // We should only get here in this case if there's an offset, and
  691. // in that case we're limiting to all values after the offset.
  692. if ($delta_limit == 'all') {
  693. $delta_limit = count($all_values) - $offset;
  694. }
  695. }
  696. // Determine if only the first and last values should be shown
  697. $delta_first_last = $this->options['delta_first_last'];
  698. $new_values = array();
  699. for ($i = 0; $i < $delta_limit; $i++) {
  700. $new_delta = $offset + $i;
  701. if (isset($all_values[$new_delta])) {
  702. // If first-last option was selected, only use the first and last values
  703. if (!$delta_first_last
  704. // Use the first value.
  705. || $new_delta == $offset
  706. // Use the last value.
  707. || $new_delta == ($delta_limit + $offset - 1)) {
  708. $new_values[] = $all_values[$new_delta];
  709. }
  710. }
  711. }
  712. $entity->{$this->definition['field_name']}[$langcode] = $new_values;
  713. }
  714. if ($field == 'entity') {
  715. return $entity;
  716. }
  717. else {
  718. return !empty($entity->{$this->definition['field_name']}[$langcode]) ? $entity->{$this->definition['field_name']}[$langcode] : array();
  719. }
  720. }
  721. /**
  722. * Return an array of items for the field.
  723. */
  724. function set_items($values, $row_id) {
  725. if (empty($values->_field_data[$this->field_alias]) || empty($values->_field_data[$this->field_alias]['entity'])) {
  726. return array();
  727. }
  728. $display = array(
  729. 'type' => $this->options['type'],
  730. 'settings' => $this->options['settings'],
  731. 'label' => 'hidden',
  732. // Pass the View object in the display so that fields can act on it.
  733. 'views_view' => $this->view,
  734. 'views_field' => $this,
  735. 'views_row_id' => $row_id,
  736. );
  737. $entity_type = $values->_field_data[$this->field_alias]['entity_type'];
  738. $entity = $this->get_value($values, 'entity');
  739. if (!$entity) {
  740. return array();
  741. }
  742. $langcode = $this->field_language($entity_type, $entity);
  743. $render_array = field_view_field($entity_type, $entity, $this->definition['field_name'], $display, $langcode);
  744. $items = array();
  745. if ($this->options['field_api_classes']) {
  746. // Make a copy.
  747. $array = $render_array;
  748. return array(array('rendered' => drupal_render($render_array)));
  749. }
  750. foreach (element_children($render_array) as $count) {
  751. $items[$count]['rendered'] = $render_array[$count];
  752. // field_view_field() adds an #access property to the render array that
  753. // determines whether or not the current user is allowed to view the
  754. // field in the context of the current entity. We need to respect this
  755. // parameter when we pull out the children of the field array for
  756. // rendering.
  757. if (isset($render_array['#access'])) {
  758. $items[$count]['rendered']['#access'] = $render_array['#access'];
  759. }
  760. // Only add the raw field items (for use in tokens) if the current user
  761. // has access to view the field content.
  762. if ((!isset($items[$count]['rendered']['#access']) || $items[$count]['rendered']['#access']) && !empty($render_array['#items'][$count])) {
  763. $items[$count]['raw'] = $render_array['#items'][$count];
  764. }
  765. }
  766. return $items;
  767. }
  768. function render_item($count, $item) {
  769. return render($item['rendered']);
  770. }
  771. function document_self_tokens(&$tokens) {
  772. $field = $this->field_info;
  773. foreach ($field['columns'] as $id => $column) {
  774. $tokens['[' . $this->options['id'] . '-' . $id . ']'] = t('Raw @column', array('@column' => $id));
  775. }
  776. }
  777. function add_self_tokens(&$tokens, $item) {
  778. $field = $this->field_info;
  779. foreach ($field['columns'] as $id => $column) {
  780. // Use filter_xss_admin because it's user data and we can't be sure it is safe.
  781. // We know nothing about the data, though, so we can't really do much else.
  782. if (isset($item['raw'])) {
  783. // If $item['raw'] is an array then we can use as is, if it's an object
  784. // we cast it to an array, if it's neither, we can't use it.
  785. $raw = is_array($item['raw']) ? $item['raw'] :
  786. (is_object($item['raw']) ? (array)$item['raw'] : NULL);
  787. }
  788. if (isset($raw) && isset($raw[$id]) && is_scalar($raw[$id])) {
  789. $tokens['[' . $this->options['id'] . '-' . $id . ']'] = filter_xss_admin($raw[$id]);
  790. }
  791. else {
  792. // Take sure that empty values are replaced as well.
  793. $tokens['[' . $this->options['id'] . '-' . $id . ']'] = '';
  794. }
  795. }
  796. }
  797. /**
  798. * Return the language code of the language the field should be displayed in,
  799. * according to the settings.
  800. */
  801. function field_language($entity_type, $entity) {
  802. global $language_content;
  803. if (field_is_translatable($entity_type, $this->field_info)) {
  804. $default_language = language_default('language');
  805. $language = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
  806. array($language_content->language, $default_language),
  807. $this->view->display_handler->options['field_language']);
  808. // Give the Field Language API a chance to fallback to a different language
  809. // (or LANGUAGE_NONE), in case the field has no data for the selected language.
  810. // field_view_field() does this as well, but since the returned language code
  811. // is used before calling it, the fallback needs to happen explicitly.
  812. $language = field_language($entity_type, $entity, $this->field_info['field_name'], $language);
  813. return $language;
  814. }
  815. else {
  816. return LANGUAGE_NONE;
  817. }
  818. }
  819. }