views_handler_field_field.inc 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  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['delta_random'] = array(
  337. 'default' => FALSE,
  338. 'bool' => TRUE,
  339. );
  340. $options['multi_type'] = array(
  341. 'default' => 'separator'
  342. );
  343. $options['separator'] = array(
  344. 'default' => ', '
  345. );
  346. $options['field_api_classes'] = array(
  347. 'default' => FALSE,
  348. 'bool' => TRUE,
  349. );
  350. return $options;
  351. }
  352. function options_form(&$form, &$form_state) {
  353. parent::options_form($form, $form_state);
  354. $field = $this->field_info;
  355. $formatters = _field_view_formatter_options($field['type']);
  356. $column_names = array_keys($field['columns']);
  357. // If this is a multiple value field, add its options.
  358. if ($this->multiple) {
  359. $this->multiple_options_form($form, $form_state);
  360. }
  361. // No need to ask the user anything if the field has only one column.
  362. if (count($field['columns']) == 1) {
  363. $form['click_sort_column'] = array(
  364. '#type' => 'value',
  365. '#value' => isset($column_names[0]) ? $column_names[0] : '',
  366. );
  367. }
  368. else {
  369. $form['click_sort_column'] = array(
  370. '#type' => 'select',
  371. '#title' => t('Column used for click sorting'),
  372. '#options' => drupal_map_assoc($column_names),
  373. '#default_value' => $this->options['click_sort_column'],
  374. '#description' => t('Used by Style: Table to determine the actual column to click sort the field on. The default is usually fine.'),
  375. '#fieldset' => 'more',
  376. );
  377. }
  378. $form['type'] = array(
  379. '#type' => 'select',
  380. '#title' => t('Formatter'),
  381. '#options' => $formatters,
  382. '#default_value' => $this->options['type'],
  383. '#ajax' => array(
  384. 'path' => views_ui_build_form_url($form_state),
  385. ),
  386. '#submit' => array('views_ui_config_item_form_submit_temporary'),
  387. '#executes_submit_callback' => TRUE,
  388. );
  389. $form['field_api_classes'] = array(
  390. '#title' => t('Use field template'),
  391. '#type' => 'checkbox',
  392. '#default_value' => $this->options['field_api_classes'],
  393. '#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.'),
  394. '#fieldset' => 'style_settings',
  395. '#weight' => 20,
  396. );
  397. if ($this->multiple) {
  398. $form['field_api_classes']['#description'] .= ' ' . t('Checking this option will cause the group Display Type and Separator values to be ignored.');
  399. }
  400. // Get the currently selected formatter.
  401. $format = $this->options['type'];
  402. $formatter = field_info_formatter_types($format);
  403. $settings = $this->options['settings'] + field_info_formatter_settings($format);
  404. // Provide an instance array for hook_field_formatter_settings_form().
  405. ctools_include('fields');
  406. $this->instance = ctools_fields_fake_field_instance($this->definition['field_name'], '_custom', $formatter, $settings);
  407. // Store the settings in a '_custom' view mode.
  408. $this->instance['display']['_custom'] = array(
  409. 'type' => $format,
  410. 'settings' => $settings,
  411. );
  412. // Get the settings form.
  413. $settings_form = array('#value' => array());
  414. $function = $formatter['module'] . '_field_formatter_settings_form';
  415. if (function_exists($function)) {
  416. $settings_form = $function($field, $this->instance, '_custom', $form, $form_state);
  417. }
  418. $form['settings'] = $settings_form;
  419. }
  420. /**
  421. * Provide options for multiple value fields.
  422. */
  423. function multiple_options_form(&$form, &$form_state) {
  424. $field = $this->field_info;
  425. $form['multiple_field_settings'] = array(
  426. '#type' => 'fieldset',
  427. '#title' => t('Multiple field settings'),
  428. '#collapsible' => TRUE,
  429. '#collapsed' => TRUE,
  430. '#weight' => 5,
  431. );
  432. $form['group_rows'] = array(
  433. '#title' => t('Display all values in the same row'),
  434. '#type' => 'checkbox',
  435. '#default_value' => $this->options['group_rows'],
  436. '#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.'),
  437. '#fieldset' => 'multiple_field_settings',
  438. );
  439. // Make the string translatable by keeping it as a whole rather than
  440. // translating prefix and suffix separately.
  441. list($prefix, $suffix) = explode('@count', t('Display @count value(s)'));
  442. if ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) {
  443. $type = 'textfield';
  444. $options = NULL;
  445. $size = 5;
  446. }
  447. else {
  448. $type = 'select';
  449. $options = drupal_map_assoc(range(1, $field['cardinality']));
  450. $size = 1;
  451. }
  452. $form['multi_type'] = array(
  453. '#type' => 'radios',
  454. '#title' => t('Display type'),
  455. '#options' => array(
  456. 'ul' => t('Unordered list'),
  457. 'ol' => t('Ordered list'),
  458. 'separator' => t('Simple separator'),
  459. ),
  460. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  461. '#default_value' => $this->options['multi_type'],
  462. '#fieldset' => 'multiple_field_settings',
  463. );
  464. $form['separator'] = array(
  465. '#type' => 'textfield',
  466. '#title' => t('Separator'),
  467. '#default_value' => $this->options['separator'],
  468. '#dependency' => array(
  469. 'radio:options[multi_type]' => array('separator'),
  470. 'edit-options-group-rows' => array(TRUE),
  471. ),
  472. '#dependency_count' => 2,
  473. '#fieldset' => 'multiple_field_settings',
  474. );
  475. $form['delta_limit'] = array(
  476. '#type' => $type,
  477. '#size' => $size,
  478. '#field_prefix' => $prefix,
  479. '#field_suffix' => $suffix,
  480. '#options' => $options,
  481. '#default_value' => $this->options['delta_limit'],
  482. '#prefix' => '<div class="container-inline">',
  483. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  484. '#fieldset' => 'multiple_field_settings',
  485. );
  486. list($prefix, $suffix) = explode('@count', t('starting from @count'));
  487. $form['delta_offset'] = array(
  488. '#type' => 'textfield',
  489. '#size' => 5,
  490. '#field_prefix' => $prefix,
  491. '#field_suffix' => $suffix,
  492. '#default_value' => $this->options['delta_offset'],
  493. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  494. '#description' => t('(first item is 0)'),
  495. '#fieldset' => 'multiple_field_settings',
  496. );
  497. $form['delta_reversed'] = array(
  498. '#title' => t('Reversed'),
  499. '#type' => 'checkbox',
  500. '#default_value' => $this->options['delta_reversed'],
  501. '#suffix' => $suffix,
  502. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  503. '#description' => t('(start from last values)'),
  504. '#fieldset' => 'multiple_field_settings',
  505. );
  506. $form['delta_first_last'] = array(
  507. '#title' => t('First and last only'),
  508. '#type' => 'checkbox',
  509. '#default_value' => $this->options['delta_first_last'],
  510. '#suffix' => $suffix,
  511. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  512. '#fieldset' => 'multiple_field_settings',
  513. );
  514. $form['delta_random'] = array(
  515. '#title' => t('Random order'),
  516. '#type' => 'checkbox',
  517. '#default_value' => $this->options['delta_random'],
  518. '#suffix' => '</div>',
  519. '#dependency' => array('edit-options-group-rows' => array(TRUE)),
  520. '#fieldset' => 'multiple_field_settings',
  521. );
  522. }
  523. /**
  524. * Extend the groupby form with group columns.
  525. */
  526. function groupby_form(&$form, &$form_state) {
  527. parent::groupby_form($form, $form_state);
  528. // With "field API" fields, the column target of the grouping function
  529. // and any additional grouping columns must be specified.
  530. $group_columns = array(
  531. 'entity_id' => t('Entity ID'),
  532. ) + drupal_map_assoc(array_keys($this->field_info['columns']), 'ucfirst');
  533. $form['group_column'] = array(
  534. '#type' => 'select',
  535. '#title' => t('Group column'),
  536. '#default_value' => $this->options['group_column'],
  537. '#description' => t('Select the column of this field to apply the grouping function selected above.'),
  538. '#options' => $group_columns,
  539. );
  540. $options = drupal_map_assoc(array('bundle', 'language', 'entity_type'), 'ucfirst');
  541. // Add on defined fields, noting that they're prefixed with the field name.
  542. $form['group_columns'] = array(
  543. '#type' => 'checkboxes',
  544. '#title' => t('Group columns (additional)'),
  545. '#default_value' => $this->options['group_columns'],
  546. '#description' => t('Select any additional columns of this field to include in the query and to group on.'),
  547. '#options' => $options + $group_columns,
  548. );
  549. }
  550. function groupby_form_submit(&$form, &$form_state) {
  551. parent::groupby_form_submit($form, $form_state);
  552. $item =& $form_state['handler']->options;
  553. // Add settings for "field API" fields.
  554. $item['group_column'] = $form_state['values']['options']['group_column'];
  555. $item['group_columns'] = array_filter($form_state['values']['options']['group_columns']);
  556. }
  557. /**
  558. * Load the entities for all fields that are about to be displayed.
  559. */
  560. function post_execute(&$values) {
  561. if (!empty($values)) {
  562. // Divide the entity ids by entity type, so they can be loaded in bulk.
  563. $entities_by_type = array();
  564. $revisions_by_type = array();
  565. foreach ($values as $key => $object) {
  566. 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])) {
  567. $entity_type = $object->{$this->aliases['entity_type']};
  568. if (empty($this->definition['is revision'])) {
  569. $entity_id = $object->{$this->field_alias};
  570. $entities_by_type[$entity_type][$key] = $entity_id;
  571. }
  572. else {
  573. $revision_id = $object->{$this->field_alias};
  574. $entity_id = $object->{$this->aliases['entity_id']};
  575. $entities_by_type[$entity_type][$key] = array($entity_id, $revision_id);
  576. }
  577. }
  578. }
  579. // Load the entities.
  580. foreach ($entities_by_type as $entity_type => $entity_ids) {
  581. $entity_info = entity_get_info($entity_type);
  582. if (empty($this->definition['is revision'])) {
  583. $entities = entity_load($entity_type, $entity_ids);
  584. $keys = $entity_ids;
  585. }
  586. else {
  587. // Revisions can't be loaded multiple, so we have to load them
  588. // one by one.
  589. $entities = array();
  590. $keys = array();
  591. foreach ($entity_ids as $key => $combined) {
  592. list($entity_id, $revision_id) = $combined;
  593. $entity = entity_load($entity_type, array($entity_id), array($entity_info['entity keys']['revision'] => $revision_id));
  594. if ($entity) {
  595. $entities[$revision_id] = array_shift($entity);
  596. $keys[$key] = $revision_id;
  597. }
  598. }
  599. }
  600. foreach ($keys as $key => $entity_id) {
  601. // If this is a revision, load the revision instead.
  602. if (isset($entities[$entity_id])) {
  603. $values[$key]->_field_data[$this->field_alias] = array(
  604. 'entity_type' => $entity_type,
  605. 'entity' => $entities[$entity_id],
  606. );
  607. }
  608. }
  609. }
  610. // Now, transfer the data back into the resultset so it can be easily used.
  611. foreach ($values as $row_id => &$value) {
  612. $value->{'field_' . $this->options['id']} = $this->set_items($value, $row_id);
  613. }
  614. }
  615. }
  616. /**
  617. * Render all items in this field together.
  618. *
  619. * When using advanced render, each possible item in the list is rendered
  620. * individually. Then the items are all pasted together.
  621. */
  622. function render_items($items) {
  623. if (!empty($items)) {
  624. if (!$this->options['group_rows']) {
  625. return implode('', $items);
  626. }
  627. if ($this->options['multi_type'] == 'separator') {
  628. return implode(filter_xss_admin($this->options['separator']), $items);
  629. }
  630. else {
  631. return theme('item_list',
  632. array(
  633. 'items' => $items,
  634. 'title' => NULL,
  635. 'type' => $this->options['multi_type']
  636. ));
  637. }
  638. }
  639. }
  640. function get_items($values) {
  641. return $values->{'field_' . $this->options['id']};
  642. }
  643. function get_value($values, $field = NULL) {
  644. if (!isset($values->_field_data[$this->field_alias]['entity']) || !is_object($values->_field_data[$this->field_alias]['entity'])) {
  645. return array();
  646. }
  647. // Go ahead and render and store in $this->items.
  648. $entity = clone $values->_field_data[$this->field_alias]['entity'];
  649. $entity_type = $values->_field_data[$this->field_alias]['entity_type'];
  650. $langcode = $this->field_language($entity_type, $entity);
  651. // If we are grouping, copy our group fields into the cloned entity.
  652. // It's possible this will cause some weirdness, but there's only
  653. // so much we can hope to do.
  654. if (!empty($this->group_fields)) {
  655. // first, test to see if we have a base value.
  656. $base_value = array();
  657. // Note: We would copy original values here, but it can cause problems.
  658. // For example, text fields store cached filtered values as
  659. // 'safe_value' which doesn't appear anywhere in the field definition
  660. // so we can't affect it. Other side effects could happen similarly.
  661. $data = FALSE;
  662. foreach ($this->group_fields as $field_name => $column) {
  663. if (property_exists($values, $this->aliases[$column])) {
  664. $base_value[$field_name] = $values->{$this->aliases[$column]};
  665. if (isset($base_value[$field_name])) {
  666. $data = TRUE;
  667. }
  668. }
  669. }
  670. // If any of our aggregated fields have data, fake it:
  671. if ($data) {
  672. // Now, overwrite the original value with our aggregated value.
  673. // This overwrites it so there is always just one entry.
  674. $entity->{$this->definition['field_name']}[$langcode] = array($base_value);
  675. }
  676. else {
  677. $entity->{$this->definition['field_name']}[$langcode] = array();
  678. }
  679. }
  680. // The field we are trying to display doesn't exist on this entity.
  681. if (!isset($entity->{$this->definition['field_name']})) {
  682. return array();
  683. }
  684. // If requested, randomize the order of the deltas.
  685. if ($this->options['delta_random'] && !empty($entity->{$this->definition['field_name']})) {
  686. shuffle($entity->{$this->definition['field_name']}[$langcode]);
  687. }
  688. // We are supposed to show only certain deltas.
  689. if ($this->limit_values && !empty($entity->{$this->definition['field_name']})) {
  690. $all_values = !empty($entity->{$this->definition['field_name']}[$langcode]) ? $entity->{$this->definition['field_name']}[$langcode] : array();
  691. if ($this->options['delta_reversed']) {
  692. $all_values = array_reverse($all_values);
  693. }
  694. // Offset is calculated differently when row grouping for a field is
  695. // not enabled. Since there are multiple rows, the delta needs to be
  696. // taken into account, so that different values are shown per row.
  697. if (!$this->options['group_rows'] && isset($this->aliases['delta']) && isset($values->{$this->aliases['delta']})) {
  698. $delta_limit = 1;
  699. $offset = $values->{$this->aliases['delta']};
  700. }
  701. // Single fields don't have a delta available so choose 0.
  702. elseif (!$this->options['group_rows'] && !$this->multiple) {
  703. $delta_limit = 1;
  704. $offset = 0;
  705. }
  706. else {
  707. $delta_limit = $this->options['delta_limit'];
  708. $offset = intval($this->options['delta_offset']);
  709. // We should only get here in this case if there's an offset, and
  710. // in that case we're limiting to all values after the offset.
  711. if ($delta_limit == 'all') {
  712. $delta_limit = count($all_values) - $offset;
  713. }
  714. }
  715. // Determine if only the first and last values should be shown
  716. $delta_first_last = $this->options['delta_first_last'];
  717. $new_values = array();
  718. for ($i = 0; $i < $delta_limit; $i++) {
  719. $new_delta = $offset + $i;
  720. if (isset($all_values[$new_delta])) {
  721. // If first-last option was selected, only use the first and last values
  722. if (!$delta_first_last
  723. // Use the first value.
  724. || $new_delta == $offset
  725. // Use the last value.
  726. || $new_delta == ($delta_limit + $offset - 1)) {
  727. $new_values[] = $all_values[$new_delta];
  728. }
  729. }
  730. }
  731. $entity->{$this->definition['field_name']}[$langcode] = $new_values;
  732. }
  733. if ($field == 'entity') {
  734. return $entity;
  735. }
  736. else {
  737. return !empty($entity->{$this->definition['field_name']}[$langcode]) ? $entity->{$this->definition['field_name']}[$langcode] : array();
  738. }
  739. }
  740. /**
  741. * Return an array of items for the field.
  742. */
  743. function set_items($values, $row_id) {
  744. // In some cases the instance on the entity might be easy, see
  745. // https://drupal.org/node/1161708 and https://drupal.org/node/1461536 for
  746. // more information.
  747. if (empty($values->_field_data[$this->field_alias]) || empty($values->_field_data[$this->field_alias]['entity']) || !isset($values->_field_data[$this->field_alias]['entity']->{$this->definition['field_name']})) {
  748. return array();
  749. }
  750. $display = array(
  751. 'type' => $this->options['type'],
  752. 'settings' => $this->options['settings'],
  753. 'label' => 'hidden',
  754. // Pass the View object in the display so that fields can act on it.
  755. 'views_view' => $this->view,
  756. 'views_field' => $this,
  757. 'views_row_id' => $row_id,
  758. );
  759. $entity_type = $values->_field_data[$this->field_alias]['entity_type'];
  760. $entity = $this->get_value($values, 'entity');
  761. if (!$entity) {
  762. return array();
  763. }
  764. $langcode = $this->field_language($entity_type, $entity);
  765. $render_array = field_view_field($entity_type, $entity, $this->definition['field_name'], $display, $langcode);
  766. $items = array();
  767. if ($this->options['field_api_classes']) {
  768. // Make a copy.
  769. $array = $render_array;
  770. return array(array('rendered' => drupal_render($render_array)));
  771. }
  772. foreach (element_children($render_array) as $count) {
  773. $items[$count]['rendered'] = $render_array[$count];
  774. // field_view_field() adds an #access property to the render array that
  775. // determines whether or not the current user is allowed to view the
  776. // field in the context of the current entity. We need to respect this
  777. // parameter when we pull out the children of the field array for
  778. // rendering.
  779. if (isset($render_array['#access'])) {
  780. $items[$count]['rendered']['#access'] = $render_array['#access'];
  781. }
  782. // Only add the raw field items (for use in tokens) if the current user
  783. // has access to view the field content.
  784. if ((!isset($items[$count]['rendered']['#access']) || $items[$count]['rendered']['#access']) && !empty($render_array['#items'][$count])) {
  785. $items[$count]['raw'] = $render_array['#items'][$count];
  786. }
  787. }
  788. return $items;
  789. }
  790. function render_item($count, $item) {
  791. return render($item['rendered']);
  792. }
  793. function document_self_tokens(&$tokens) {
  794. $field = $this->field_info;
  795. foreach ($field['columns'] as $id => $column) {
  796. $tokens['[' . $this->options['id'] . '-' . $id . ']'] = t('Raw @column', array('@column' => $id));
  797. }
  798. }
  799. function add_self_tokens(&$tokens, $item) {
  800. $field = $this->field_info;
  801. foreach ($field['columns'] as $id => $column) {
  802. // Use filter_xss_admin because it's user data and we can't be sure it is safe.
  803. // We know nothing about the data, though, so we can't really do much else.
  804. if (isset($item['raw'])) {
  805. // If $item['raw'] is an array then we can use as is, if it's an object
  806. // we cast it to an array, if it's neither, we can't use it.
  807. $raw = is_array($item['raw']) ? $item['raw'] :
  808. (is_object($item['raw']) ? (array)$item['raw'] : NULL);
  809. }
  810. if (isset($raw) && isset($raw[$id]) && is_scalar($raw[$id])) {
  811. $tokens['[' . $this->options['id'] . '-' . $id . ']'] = filter_xss_admin($raw[$id]);
  812. }
  813. else {
  814. // Take sure that empty values are replaced as well.
  815. $tokens['[' . $this->options['id'] . '-' . $id . ']'] = '';
  816. }
  817. }
  818. }
  819. /**
  820. * Return the language code of the language the field should be displayed in,
  821. * according to the settings.
  822. */
  823. function field_language($entity_type, $entity) {
  824. global $language_content;
  825. if (field_is_translatable($entity_type, $this->field_info)) {
  826. $default_language = language_default('language');
  827. $language = str_replace(array('***CURRENT_LANGUAGE***', '***DEFAULT_LANGUAGE***'),
  828. array($language_content->language, $default_language),
  829. $this->view->display_handler->options['field_language']);
  830. // Give the Field Language API a chance to fallback to a different language
  831. // (or LANGUAGE_NONE), in case the field has no data for the selected language.
  832. // field_view_field() does this as well, but since the returned language code
  833. // is used before calling it, the fallback needs to happen explicitly.
  834. $language = field_language($entity_type, $entity, $this->field_info['field_name'], $language);
  835. return $language;
  836. }
  837. else {
  838. return LANGUAGE_NONE;
  839. }
  840. }
  841. }