views_handler_field.inc 56 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630
  1. <?php
  2. /**
  3. * @file
  4. * @todo.
  5. */
  6. /**
  7. * @defgroup views_field_handlers Views field handlers
  8. * @{
  9. * Handlers to tell Views how to build and display fields.
  10. *
  11. */
  12. /**
  13. * Indicator of the render_text() method for rendering a single item.
  14. * (If no render_item() is present).
  15. */
  16. define('VIEWS_HANDLER_RENDER_TEXT_PHASE_SINGLE_ITEM', 0);
  17. /**
  18. * Indicator of the render_text() method for rendering the whole element.
  19. * (if no render_item() method is available).
  20. */
  21. define('VIEWS_HANDLER_RENDER_TEXT_PHASE_COMPLETELY', 1);
  22. /**
  23. * Indicator of the render_text() method for rendering the empty text.
  24. */
  25. define('VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY', 2);
  26. /**
  27. * Base field handler that has no options and renders an unformatted field.
  28. *
  29. * Definition terms:
  30. * - additional fields: An array of fields that should be added to the query
  31. * for some purpose. The array is in the form of:
  32. * array('identifier' => array('table' => tablename,
  33. * 'field' => fieldname); as many fields as are necessary
  34. * may be in this array.
  35. * - click sortable: If TRUE, this field may be click sorted.
  36. *
  37. * @ingroup views_field_handlers
  38. */
  39. class views_handler_field extends views_handler {
  40. var $field_alias = 'unknown';
  41. var $aliases = array();
  42. /**
  43. * The field value prior to any rewriting.
  44. *
  45. * @var mixed
  46. */
  47. public $original_value = NULL;
  48. /**
  49. * @var array
  50. * Stores additional fields which get's added to the query.
  51. * The generated aliases are stored in $aliases.
  52. */
  53. var $additional_fields = array();
  54. /**
  55. * Construct a new field handler.
  56. */
  57. function construct() {
  58. parent::construct();
  59. $this->additional_fields = array();
  60. if (!empty($this->definition['additional fields'])) {
  61. $this->additional_fields = $this->definition['additional fields'];
  62. }
  63. if (!isset($this->options['exclude'])) {
  64. $this->options['exclude'] = '';
  65. }
  66. }
  67. /**
  68. * Determine if this field can allow advanced rendering.
  69. *
  70. * Fields can set this to FALSE if they do not wish to allow
  71. * token based rewriting or link-making.
  72. */
  73. function allow_advanced_render() {
  74. return TRUE;
  75. }
  76. function init(&$view, &$options) {
  77. parent::init($view, $options);
  78. }
  79. /**
  80. * Called to add the field to a query.
  81. */
  82. function query() {
  83. $this->ensure_my_table();
  84. // Add the field.
  85. $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
  86. $this->field_alias = $this->query->add_field($this->table_alias, $this->real_field, NULL, $params);
  87. $this->add_additional_fields();
  88. }
  89. /**
  90. * Add 'additional' fields to the query.
  91. *
  92. * @param $fields
  93. * An array of fields. The key is an identifier used to later find the
  94. * field alias used. The value is either a string in which case it's
  95. * assumed to be a field on this handler's table; or it's an array in the
  96. * form of
  97. * @code array('table' => $tablename, 'field' => $fieldname) @endcode
  98. */
  99. function add_additional_fields($fields = NULL) {
  100. if (!isset($fields)) {
  101. // notice check
  102. if (empty($this->additional_fields)) {
  103. return;
  104. }
  105. $fields = $this->additional_fields;
  106. }
  107. $group_params = array();
  108. if ($this->options['group_type'] != 'group') {
  109. $group_params = array(
  110. 'function' => $this->options['group_type'],
  111. );
  112. }
  113. if (!empty($fields) && is_array($fields)) {
  114. foreach ($fields as $identifier => $info) {
  115. if (is_array($info)) {
  116. if (isset($info['table'])) {
  117. $table_alias = $this->query->ensure_table($info['table'], $this->relationship);
  118. }
  119. else {
  120. $table_alias = $this->table_alias;
  121. }
  122. if (empty($table_alias)) {
  123. debug(t('Handler @handler tried to add additional_field @identifier but @table could not be added!', array('@handler' => $this->definition['handler'], '@identifier' => $identifier, '@table' => $info['table'])));
  124. $this->aliases[$identifier] = 'broken';
  125. continue;
  126. }
  127. $params = array();
  128. if (!empty($info['params'])) {
  129. $params = $info['params'];
  130. }
  131. $params += $group_params;
  132. $this->aliases[$identifier] = $this->query->add_field($table_alias, $info['field'], NULL, $params);
  133. }
  134. else {
  135. $this->aliases[$info] = $this->query->add_field($this->table_alias, $info, NULL, $group_params);
  136. }
  137. }
  138. }
  139. }
  140. /**
  141. * Called to determine what to tell the clicksorter.
  142. */
  143. function click_sort($order) {
  144. if (isset($this->field_alias)) {
  145. // Since fields should always have themselves already added, just
  146. // add a sort on the field.
  147. $params = $this->options['group_type'] != 'group' ? array('function' => $this->options['group_type']) : array();
  148. $this->query->add_orderby(NULL, NULL, $order, $this->field_alias, $params);
  149. }
  150. }
  151. /**
  152. * Determine if this field is click sortable.
  153. */
  154. function click_sortable() {
  155. return !empty($this->definition['click sortable']);
  156. }
  157. /**
  158. * Get this field's label.
  159. */
  160. function label() {
  161. if (!isset($this->options['label'])) {
  162. return '';
  163. }
  164. return $this->options['label'];
  165. }
  166. /**
  167. * Return an HTML element based upon the field's element type.
  168. */
  169. function element_type($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
  170. if ($none_supported) {
  171. if ($this->options['element_type'] === '0') {
  172. return '';
  173. }
  174. }
  175. if ($this->options['element_type']) {
  176. return check_plain($this->options['element_type']);
  177. }
  178. if ($default_empty) {
  179. return '';
  180. }
  181. if ($inline) {
  182. return 'span';
  183. }
  184. if (isset($this->definition['element type'])) {
  185. return $this->definition['element type'];
  186. }
  187. return 'span';
  188. }
  189. /**
  190. * Return an HTML element for the label based upon the field's element type.
  191. */
  192. function element_label_type($none_supported = FALSE, $default_empty = FALSE) {
  193. if ($none_supported) {
  194. if ($this->options['element_label_type'] === '0') {
  195. return '';
  196. }
  197. }
  198. if ($this->options['element_label_type']) {
  199. return check_plain($this->options['element_label_type']);
  200. }
  201. if ($default_empty) {
  202. return '';
  203. }
  204. return 'span';
  205. }
  206. /**
  207. * Return an HTML element for the wrapper based upon the field's element type.
  208. */
  209. function element_wrapper_type($none_supported = FALSE, $default_empty = FALSE) {
  210. if ($none_supported) {
  211. if ($this->options['element_wrapper_type'] === '0') {
  212. return 0;
  213. }
  214. }
  215. if ($this->options['element_wrapper_type']) {
  216. return check_plain($this->options['element_wrapper_type']);
  217. }
  218. if ($default_empty) {
  219. return '';
  220. }
  221. return 'div';
  222. }
  223. /**
  224. * Provide a list of elements valid for field HTML.
  225. *
  226. * This function can be overridden by fields that want more or fewer
  227. * elements available, though this seems like it would be an incredibly
  228. * rare occurence.
  229. */
  230. function get_elements() {
  231. static $elements = NULL;
  232. if (!isset($elements)) {
  233. $elements = variable_get('views_field_rewrite_elements', array(
  234. '' => t('- Use default -'),
  235. '0' => t('- None -'),
  236. 'div' => 'DIV',
  237. 'span' => 'SPAN',
  238. 'h1' => 'H1',
  239. 'h2' => 'H2',
  240. 'h3' => 'H3',
  241. 'h4' => 'H4',
  242. 'h5' => 'H5',
  243. 'h6' => 'H6',
  244. 'p' => 'P',
  245. 'strong' => 'STRONG',
  246. 'em' => 'EM',
  247. ));
  248. }
  249. return $elements;
  250. }
  251. /**
  252. * Return the class of the field.
  253. */
  254. function element_classes($row_index = NULL) {
  255. $classes = explode(' ', $this->options['element_class']);
  256. foreach ($classes as &$class) {
  257. $class = $this->tokenize_value($class, $row_index);
  258. $class = views_clean_css_identifier($class);
  259. }
  260. return implode(' ', $classes);
  261. }
  262. /**
  263. * Replace a value with tokens from the last field.
  264. *
  265. * This function actually figures out which field was last and uses its
  266. * tokens so they will all be available.
  267. */
  268. function tokenize_value($value, $row_index = NULL) {
  269. if (strpos($value, '[') !== FALSE || strpos($value, '!') !== FALSE || strpos($value, '%') !== FALSE) {
  270. $fake_item = array(
  271. 'alter_text' => TRUE,
  272. 'text' => $value,
  273. );
  274. // Use isset() because empty() will trigger on 0 and 0 is
  275. // the first row.
  276. if (isset($row_index) && isset($this->view->style_plugin->render_tokens[$row_index])) {
  277. $tokens = $this->view->style_plugin->render_tokens[$row_index];
  278. }
  279. else {
  280. // Get tokens from the last field.
  281. $last_field = end($this->view->field);
  282. if (isset($last_field->last_tokens)) {
  283. $tokens = $last_field->last_tokens;
  284. }
  285. else {
  286. $tokens = $last_field->get_render_tokens($fake_item);
  287. }
  288. }
  289. $value = strip_tags($this->render_altered($fake_item, $tokens));
  290. if (!empty($this->options['alter']['trim_whitespace'])) {
  291. $value = trim($value);
  292. }
  293. }
  294. return $value;
  295. }
  296. /**
  297. * Return the class of the field's label.
  298. */
  299. function element_label_classes($row_index = NULL) {
  300. $classes = explode(' ', $this->options['element_label_class']);
  301. foreach ($classes as &$class) {
  302. $class = $this->tokenize_value($class, $row_index);
  303. $class = views_clean_css_identifier($class);
  304. }
  305. return implode(' ', $classes);
  306. }
  307. /**
  308. * Return the class of the field's wrapper.
  309. */
  310. function element_wrapper_classes($row_index = NULL) {
  311. $classes = explode(' ', $this->options['element_wrapper_class']);
  312. foreach ($classes as &$class) {
  313. $class = $this->tokenize_value($class, $row_index);
  314. $class = views_clean_css_identifier($class);
  315. }
  316. return implode(' ', $classes);
  317. }
  318. /**
  319. * Get the value that's supposed to be rendered.
  320. *
  321. * This api exists so that other modules can easy set the values of the field
  322. * without having the need to change the render method as well.
  323. *
  324. * @param $values
  325. * An object containing all retrieved values.
  326. * @param $field
  327. * Optional name of the field where the value is stored.
  328. */
  329. function get_value($values, $field = NULL) {
  330. $alias = isset($field) && isset($this->aliases[$field]) ? $this->aliases[$field] : $this->field_alias;
  331. if (isset($values->{$alias})) {
  332. return $values->{$alias};
  333. }
  334. }
  335. /**
  336. * Determines if this field will be available as an option to group the result
  337. * by in the style settings.
  338. *
  339. * @return bool
  340. * TRUE if this field handler is groupable, otherwise FALSE.
  341. */
  342. function use_string_group_by() {
  343. return TRUE;
  344. }
  345. function option_definition() {
  346. $options = parent::option_definition();
  347. $options['label'] = array('default' => $this->definition['title'], 'translatable' => TRUE);
  348. $options['exclude'] = array('default' => FALSE, 'bool' => TRUE);
  349. $options['alter'] = array(
  350. 'contains' => array(
  351. 'alter_text' => array('default' => FALSE, 'bool' => TRUE),
  352. 'text' => array('default' => '', 'translatable' => TRUE),
  353. 'make_link' => array('default' => FALSE, 'bool' => TRUE),
  354. 'path' => array('default' => ''),
  355. 'absolute' => array('default' => FALSE, 'bool' => TRUE),
  356. 'external' => array('default' => FALSE, 'bool' => TRUE),
  357. 'replace_spaces' => array('default' => FALSE, 'bool' => TRUE),
  358. 'path_case' => array('default' => 'none', 'translatable' => FALSE),
  359. 'trim_whitespace' => array('default' => FALSE, 'bool' => TRUE),
  360. 'alt' => array('default' => '', 'translatable' => TRUE),
  361. 'rel' => array('default' => ''),
  362. 'link_class' => array('default' => ''),
  363. 'prefix' => array('default' => '', 'translatable' => TRUE),
  364. 'suffix' => array('default' => '', 'translatable' => TRUE),
  365. 'target' => array('default' => ''),
  366. 'nl2br' => array('default' => FALSE, 'bool' => TRUE),
  367. 'max_length' => array('default' => ''),
  368. 'word_boundary' => array('default' => TRUE, 'bool' => TRUE),
  369. 'ellipsis' => array('default' => TRUE, 'bool' => TRUE),
  370. 'more_link' => array('default' => FALSE, 'bool' => TRUE),
  371. 'more_link_text' => array('default' => '', 'translatable' => TRUE),
  372. 'more_link_path' => array('default' => ''),
  373. 'strip_tags' => array('default' => FALSE, 'bool' => TRUE),
  374. 'trim' => array('default' => FALSE, 'bool' => TRUE),
  375. 'preserve_tags' => array('default' => ''),
  376. 'html' => array('default' => FALSE, 'bool' => TRUE),
  377. ),
  378. );
  379. $options['element_type'] = array('default' => '');
  380. $options['element_class'] = array('default' => '');
  381. $options['element_label_type'] = array('default' => '');
  382. $options['element_label_class'] = array('default' => '');
  383. $options['element_label_colon'] = array('default' => TRUE, 'bool' => TRUE);
  384. $options['element_wrapper_type'] = array('default' => '');
  385. $options['element_wrapper_class'] = array('default' => '');
  386. $options['element_default_classes'] = array('default' => TRUE, 'bool' => TRUE);
  387. $options['empty'] = array('default' => '', 'translatable' => TRUE);
  388. $options['hide_empty'] = array('default' => FALSE, 'bool' => TRUE);
  389. $options['empty_zero'] = array('default' => FALSE, 'bool' => TRUE);
  390. $options['hide_alter_empty'] = array('default' => TRUE, 'bool' => TRUE);
  391. return $options;
  392. }
  393. /**
  394. * Performs some cleanup tasks on the options array before saving it.
  395. */
  396. function options_submit(&$form, &$form_state) {
  397. $options = &$form_state['values']['options'];
  398. $types = array('element_type', 'element_label_type', 'element_wrapper_type');
  399. $classes = array_combine(array('element_class', 'element_label_class', 'element_wrapper_class'), $types);
  400. foreach ($types as $type) {
  401. if (!$options[$type . '_enable']) {
  402. $options[$type] = '';
  403. }
  404. }
  405. foreach ($classes as $class => $type) {
  406. if (!$options[$class . '_enable'] || !$options[$type . '_enable']) {
  407. $options[$class] = '';
  408. }
  409. }
  410. if (empty($options['custom_label'])) {
  411. $options['label'] = '';
  412. $options['element_label_colon'] = FALSE;
  413. }
  414. }
  415. /**
  416. * Default options form that provides the label widget that all fields
  417. * should have.
  418. */
  419. function options_form(&$form, &$form_state) {
  420. parent::options_form($form, $form_state);
  421. $label = $this->label();
  422. $form['custom_label'] = array(
  423. '#type' => 'checkbox',
  424. '#title' => t('Create a label'),
  425. '#description' => t('Enable to create a label for this field.'),
  426. '#default_value' => $label !== '',
  427. '#weight' => -103,
  428. );
  429. $form['label'] = array(
  430. '#type' => 'textfield',
  431. '#title' => t('Label'),
  432. '#default_value' => $label,
  433. '#dependency' => array(
  434. 'edit-options-custom-label' => array(1),
  435. ),
  436. '#weight' => -102,
  437. );
  438. $form['element_label_colon'] = array(
  439. '#type' => 'checkbox',
  440. '#title' => t('Place a colon after the label'),
  441. '#default_value' => $this->options['element_label_colon'],
  442. '#dependency' => array(
  443. 'edit-options-custom-label' => array(1),
  444. ),
  445. '#weight' => -101,
  446. );
  447. $form['exclude'] = array(
  448. '#type' => 'checkbox',
  449. '#title' => t('Exclude from display'),
  450. '#default_value' => $this->options['exclude'],
  451. '#description' => t('Enable to load this field as hidden. Often used to group fields, or to use as token in another field.'),
  452. '#weight' => -100,
  453. );
  454. $form['style_settings'] = array(
  455. '#type' => 'fieldset',
  456. '#title' => t('Style settings'),
  457. '#collapsible' => TRUE,
  458. '#collapsed' => TRUE,
  459. '#weight' => 99,
  460. );
  461. $form['element_type_enable'] = array(
  462. '#type' => 'checkbox',
  463. '#title' => t('Customize field HTML'),
  464. '#default_value' => !empty($this->options['element_type']) || (string) $this->options['element_type'] == '0' || !empty($this->options['element_class']) || (string) $this->options['element_class'] == '0',
  465. '#fieldset' => 'style_settings',
  466. );
  467. $form['element_type'] = array(
  468. '#title' => t('HTML element'),
  469. '#options' => $this->get_elements(),
  470. '#type' => 'select',
  471. '#default_value' => $this->options['element_type'],
  472. '#description' => t('Choose the HTML element to wrap around this field, e.g. H1, H2, etc.'),
  473. '#dependency' => array(
  474. 'edit-options-element-type-enable' => array(1),
  475. ),
  476. '#fieldset' => 'style_settings',
  477. );
  478. $form['element_class_enable'] = array(
  479. '#type' => 'checkbox',
  480. '#title' => t('Create a CSS class'),
  481. '#dependency' => array(
  482. 'edit-options-element-type-enable' => array(1),
  483. ),
  484. '#default_value' => !empty($this->options['element_class']) || (string) $this->options['element_class'] == '0',
  485. '#fieldset' => 'style_settings',
  486. );
  487. $form['element_class'] = array(
  488. '#title' => t('CSS class'),
  489. '#description' => t('You may use token substitutions from the rewriting section in this class.'),
  490. '#type' => 'textfield',
  491. '#default_value' => $this->options['element_class'],
  492. '#dependency' => array(
  493. 'edit-options-element-class-enable' => array(1),
  494. 'edit-options-element-type-enable' => array(1),
  495. ),
  496. '#dependency_count' => 2,
  497. '#fieldset' => 'style_settings',
  498. );
  499. $form['element_label_type_enable'] = array(
  500. '#type' => 'checkbox',
  501. '#title' => t('Customize label HTML'),
  502. '#default_value' => !empty($this->options['element_label_type']) || (string) $this->options['element_label_type'] == '0' || !empty($this->options['element_label_class']) || (string) $this->options['element_label_class'] == '0',
  503. '#fieldset' => 'style_settings',
  504. );
  505. $form['element_label_type'] = array(
  506. '#title' => t('Label HTML element'),
  507. '#options' => $this->get_elements(FALSE),
  508. '#type' => 'select',
  509. '#default_value' => $this->options['element_label_type'],
  510. '#description' => t('Choose the HTML element to wrap around this label, e.g. H1, H2, etc.'),
  511. '#dependency' => array(
  512. 'edit-options-element-label-type-enable' => array(1),
  513. ),
  514. '#fieldset' => 'style_settings',
  515. );
  516. $form['element_label_class_enable'] = array(
  517. '#type' => 'checkbox',
  518. '#title' => t('Create a CSS class'),
  519. '#dependency' => array(
  520. 'edit-options-element-label-type-enable' => array(1)
  521. ),
  522. '#default_value' => !empty($this->options['element_label_class']) || (string) $this->options['element_label_class'] == '0',
  523. '#fieldset' => 'style_settings',
  524. );
  525. $form['element_label_class'] = array(
  526. '#title' => t('CSS class'),
  527. '#description' => t('You may use token substitutions from the rewriting section in this class.'),
  528. '#type' => 'textfield',
  529. '#default_value' => $this->options['element_label_class'],
  530. '#dependency' => array(
  531. 'edit-options-element-label-class-enable' => array(1),
  532. 'edit-options-element-label-type-enable' => array(1),
  533. ),
  534. '#dependency_count' => 2,
  535. '#fieldset' => 'style_settings',
  536. );
  537. $form['element_wrapper_type_enable'] = array(
  538. '#type' => 'checkbox',
  539. '#title' => t('Customize field and label wrapper HTML'),
  540. '#default_value' => !empty($this->options['element_wrapper_type']) || (string) $this->options['element_wrapper_type'] == '0' || !empty($this->options['element_wrapper_class']) || (string) $this->options['element_wrapper_class'] == '0',
  541. '#fieldset' => 'style_settings',
  542. );
  543. $form['element_wrapper_type'] = array(
  544. '#title' => t('Wrapper HTML element'),
  545. '#options' => $this->get_elements(FALSE),
  546. '#type' => 'select',
  547. '#default_value' => $this->options['element_wrapper_type'],
  548. '#description' => t('Choose the HTML element to wrap around this field and label, e.g. H1, H2, etc. This may not be used if the field and label are not rendered together, such as with a table.'),
  549. '#dependency' => array(
  550. 'edit-options-element-wrapper-type-enable' => array(1),
  551. ),
  552. '#fieldset' => 'style_settings',
  553. );
  554. $form['element_wrapper_class_enable'] = array(
  555. '#type' => 'checkbox',
  556. '#title' => t('Create a CSS class'),
  557. '#dependency' => array(
  558. 'edit-options-element-wrapper-type-enable' => array(1),
  559. ),
  560. '#default_value' => !empty($this->options['element_wrapper_class']) || (string) $this->options['element_wrapper_class'] == '0',
  561. '#fieldset' => 'style_settings',
  562. );
  563. $form['element_wrapper_class'] = array(
  564. '#title' => t('CSS class'),
  565. '#description' => t('You may use token substitutions from the rewriting section in this class.'),
  566. '#type' => 'textfield',
  567. '#default_value' => $this->options['element_wrapper_class'],
  568. '#dependency' => array(
  569. 'edit-options-element-wrapper-class-enable' => array(1),
  570. 'edit-options-element-wrapper-type-enable' => array(1),
  571. ),
  572. '#dependency_count' => 2,
  573. '#fieldset' => 'style_settings',
  574. );
  575. $form['element_default_classes'] = array(
  576. '#type' => 'checkbox',
  577. '#title' => t('Add default classes'),
  578. '#default_value' => $this->options['element_default_classes'],
  579. '#description' => t('Use default Views classes to identify the field, field label and field content.'),
  580. '#fieldset' => 'style_settings',
  581. );
  582. $form['alter'] = array(
  583. '#title' => t('Rewrite results'),
  584. '#type' => 'fieldset',
  585. '#collapsible' => TRUE,
  586. '#collapsed' => TRUE,
  587. '#weight' => 100,
  588. );
  589. if ($this->allow_advanced_render()) {
  590. $form['alter']['#tree'] = TRUE;
  591. $form['alter']['alter_text'] = array(
  592. '#type' => 'checkbox',
  593. '#title' => t('Rewrite the output of this field'),
  594. '#description' => t('Enable to override the output of this field with custom text or replacement tokens.'),
  595. '#default_value' => $this->options['alter']['alter_text'],
  596. );
  597. $form['alter']['text'] = array(
  598. '#title' => t('Text'),
  599. '#type' => 'textarea',
  600. '#default_value' => $this->options['alter']['text'],
  601. '#description' => t('The text to display for this field. You may include HTML. You may enter data from this view as per the "Replacement patterns" below.'),
  602. '#dependency' => array(
  603. 'edit-options-alter-alter-text' => array(1),
  604. ),
  605. );
  606. $form['alter']['make_link'] = array(
  607. '#type' => 'checkbox',
  608. '#title' => t('Output this field as a link'),
  609. '#description' => t('If checked, this field will be made into a link. The destination must be given below.'),
  610. '#default_value' => $this->options['alter']['make_link'],
  611. );
  612. $form['alter']['path'] = array(
  613. '#title' => t('Link path'),
  614. '#type' => 'textfield',
  615. '#default_value' => $this->options['alter']['path'],
  616. '#description' => t('The Drupal path or absolute URL for this link. You may enter data from this view as per the "Replacement patterns" below.'),
  617. '#dependency' => array(
  618. 'edit-options-alter-make-link' => array(1),
  619. ),
  620. '#maxlength' => 255,
  621. );
  622. $form['alter']['absolute'] = array(
  623. '#type' => 'checkbox',
  624. '#title' => t('Use absolute path'),
  625. '#default_value' => $this->options['alter']['absolute'],
  626. '#dependency' => array(
  627. 'edit-options-alter-make-link' => array(1),
  628. ),
  629. );
  630. $form['alter']['replace_spaces'] = array(
  631. '#type' => 'checkbox',
  632. '#title' => t('Replace spaces with dashes'),
  633. '#default_value' => $this->options['alter']['replace_spaces'],
  634. '#dependency' => array(
  635. 'edit-options-alter-make-link' => array(1)
  636. ),
  637. );
  638. $form['alter']['external'] = array(
  639. '#type' => 'checkbox',
  640. '#title' => t('External server URL'),
  641. '#default_value' => $this->options['alter']['external'],
  642. '#description' => t("Links to an external server using a full URL: e.g. 'http://www.example.com' or 'www.example.com'."),
  643. '#dependency' => array(
  644. 'edit-options-alter-make-link' => array(1),
  645. ),
  646. );
  647. $form['alter']['path_case'] = array(
  648. '#type' => 'select',
  649. '#title' => t('Transform the case'),
  650. '#description' => t('When printing url paths, how to transform the case of the filter value.'),
  651. '#dependency' => array(
  652. 'edit-options-alter-make-link' => array(1),
  653. ),
  654. '#options' => array(
  655. 'none' => t('No transform'),
  656. 'upper' => t('Upper case'),
  657. 'lower' => t('Lower case'),
  658. 'ucfirst' => t('Capitalize first letter'),
  659. 'ucwords' => t('Capitalize each word'),
  660. ),
  661. '#default_value' => $this->options['alter']['path_case'],
  662. );
  663. $form['alter']['link_class'] = array(
  664. '#title' => t('Link class'),
  665. '#type' => 'textfield',
  666. '#default_value' => $this->options['alter']['link_class'],
  667. '#description' => t('The CSS class to apply to the link.'),
  668. '#dependency' => array(
  669. 'edit-options-alter-make-link' => array(1),
  670. ),
  671. );
  672. $form['alter']['alt'] = array(
  673. '#title' => t('Title text'),
  674. '#type' => 'textfield',
  675. '#default_value' => $this->options['alter']['alt'],
  676. '#description' => t('Text to place as "title" text which most browsers display as a tooltip when hovering over the link.'),
  677. '#dependency' => array(
  678. 'edit-options-alter-make-link' => array(1),
  679. ),
  680. );
  681. $form['alter']['rel'] = array(
  682. '#title' => t('Rel Text'),
  683. '#type' => 'textfield',
  684. '#default_value' => $this->options['alter']['rel'],
  685. '#description' => t('Include Rel attribute for use in lightbox2 or other javascript utility.'),
  686. '#dependency' => array(
  687. 'edit-options-alter-make-link' => array(1),
  688. ),
  689. );
  690. $form['alter']['prefix'] = array(
  691. '#title' => t('Prefix text'),
  692. '#type' => 'textfield',
  693. '#default_value' => $this->options['alter']['prefix'],
  694. '#description' => t('Any text to display before this link. You may include HTML.'),
  695. '#dependency' => array(
  696. 'edit-options-alter-make-link' => array(1),
  697. ),
  698. );
  699. $form['alter']['suffix'] = array(
  700. '#title' => t('Suffix text'),
  701. '#type' => 'textfield',
  702. '#default_value' => $this->options['alter']['suffix'],
  703. '#description' => t('Any text to display after this link. You may include HTML.'),
  704. '#dependency' => array(
  705. 'edit-options-alter-make-link' => array(1),
  706. ),
  707. );
  708. $form['alter']['target'] = array(
  709. '#title' => t('Target'),
  710. '#type' => 'textfield',
  711. '#default_value' => $this->options['alter']['target'],
  712. '#description' => t("Target of the link, such as _blank, _parent or an iframe's name. This field is rarely used."),
  713. '#dependency' => array(
  714. 'edit-options-alter-make-link' => array(1),
  715. ),
  716. );
  717. // Get a list of the available fields and arguments for token replacement.
  718. $options = array();
  719. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  720. $options[t('Fields')]["[$field]"] = $handler->ui_name();
  721. // We only use fields up to (and including) this one.
  722. if ($field == $this->options['id']) {
  723. break;
  724. }
  725. }
  726. $count = 0; // This lets us prepare the key as we want it printed.
  727. foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
  728. $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
  729. $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
  730. }
  731. $this->document_self_tokens($options[t('Fields')]);
  732. // Default text.
  733. $output = t('<p>You must add some additional fields to this display before using this field. These fields may be marked as <em>Exclude from display</em> if you prefer. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.</p>');
  734. // We have some options, so make a list.
  735. if (!empty($options)) {
  736. $output = t('<p>The following tokens are available for this field. Note that due to rendering order, you cannot use fields that come after this field; if you need a field not listed here, rearrange your fields.
  737. If you would like to have the characters \'[\' and \']\' please use the html entity codes \'%5B\' or \'%5D\' or they will get replaced with empty space.</p>');
  738. foreach (array_keys($options) as $type) {
  739. if (!empty($options[$type])) {
  740. $items = array();
  741. foreach ($options[$type] as $key => $value) {
  742. $items[] = $key . ' == ' . check_plain($value);
  743. }
  744. $output .= theme('item_list',
  745. array(
  746. 'items' => $items,
  747. 'type' => $type
  748. ));
  749. }
  750. }
  751. }
  752. // This construct uses 'hidden' and not markup because process doesn't
  753. // run. It also has an extra div because the dependency wants to hide
  754. // the parent in situations like this, so we need a second div to
  755. // make this work.
  756. $form['alter']['help'] = array(
  757. '#type' => 'fieldset',
  758. '#title' => t('Replacement patterns'),
  759. '#collapsible' => TRUE,
  760. '#collapsed' => TRUE,
  761. '#value' => $output,
  762. '#dependency' => array(
  763. 'edit-options-alter-make-link' => array(1),
  764. 'edit-options-alter-alter-text' => array(1),
  765. 'edit-options-alter-more-link' => array(1),
  766. ),
  767. );
  768. $form['alter']['trim'] = array(
  769. '#type' => 'checkbox',
  770. '#title' => t('Trim this field to a maximum length'),
  771. '#description' => t('Enable to trim the field to a maximum length of characters'),
  772. '#default_value' => $this->options['alter']['trim'],
  773. );
  774. $form['alter']['max_length'] = array(
  775. '#title' => t('Maximum length'),
  776. '#type' => 'textfield',
  777. '#default_value' => $this->options['alter']['max_length'],
  778. '#description' => t('The maximum number of characters this field can be.'),
  779. '#dependency' => array(
  780. 'edit-options-alter-trim' => array(1),
  781. ),
  782. );
  783. $form['alter']['word_boundary'] = array(
  784. '#type' => 'checkbox',
  785. '#title' => t('Trim only on a word boundary'),
  786. '#description' => t('If checked, this field be trimmed only on a word boundary. This is guaranteed to be the maximum characters stated or less. If there are no word boundaries this could trim a field to nothing.'),
  787. '#default_value' => $this->options['alter']['word_boundary'],
  788. '#dependency' => array(
  789. 'edit-options-alter-trim' => array(1),
  790. ),
  791. );
  792. $form['alter']['ellipsis'] = array(
  793. '#type' => 'checkbox',
  794. '#title' => t('Add an ellipsis'),
  795. '#description' => t('If checked, a "..." will be added if a field was trimmed.'),
  796. '#default_value' => $this->options['alter']['ellipsis'],
  797. '#dependency' => array(
  798. 'edit-options-alter-trim' => array(1),
  799. ),
  800. );
  801. $form['alter']['more_link'] = array(
  802. '#type' => 'checkbox',
  803. '#title' => t('Add a read-more link if output is trimmed.'),
  804. '#description' => t('If checked, a read-more link will be added at the end of the trimmed output'),
  805. '#default_value' => $this->options['alter']['more_link'],
  806. '#dependency' => array(
  807. 'edit-options-alter-trim' => array(1),
  808. ),
  809. );
  810. $form['alter']['more_link_text'] = array(
  811. '#type' => 'textfield',
  812. '#title' => t('More link text'),
  813. '#default_value' => $this->options['alter']['more_link_text'],
  814. '#description' => t('The text which will be displayed on the more link. You may enter data from this view as per the "Replacement patterns" above.'),
  815. '#dependency_count' => 2,
  816. '#dependency' => array(
  817. 'edit-options-alter-trim' => array(1),
  818. 'edit-options-alter-more-link' => array(1),
  819. ),
  820. );
  821. $form['alter']['more_link_path'] = array(
  822. '#type' => 'textfield',
  823. '#title' => t('More link path'),
  824. '#default_value' => $this->options['alter']['more_link_path'],
  825. '#description' => t('The path which is used for the more link. You may enter data from this view as per the "Replacement patterns" above.'),
  826. '#dependency_count' => 2,
  827. '#dependency' => array(
  828. 'edit-options-alter-trim' => array(1),
  829. 'edit-options-alter-more-link' => array(1),
  830. ),
  831. );
  832. $form['alter']['html'] = array(
  833. '#type' => 'checkbox',
  834. '#title' => t('Field can contain HTML'),
  835. '#description' => t('If checked, HTML corrector will be run to ensure tags are properly closed after trimming.'),
  836. '#default_value' => $this->options['alter']['html'],
  837. '#dependency' => array(
  838. 'edit-options-alter-trim' => array(1),
  839. ),
  840. );
  841. $form['alter']['strip_tags'] = array(
  842. '#type' => 'checkbox',
  843. '#title' => t('Strip HTML tags'),
  844. '#description' => t('If checked, all HTML tags will be stripped.'),
  845. '#default_value' => $this->options['alter']['strip_tags'],
  846. );
  847. $form['alter']['preserve_tags'] = array(
  848. '#type' => 'textfield',
  849. '#title' => t('Preserve certain tags'),
  850. '#description' => t('List the tags that need to be preserved during the stripping process. example &quot;&lt;p&gt; &lt;br&gt;&quot; which will preserve all p and br elements'),
  851. '#default_value' => $this->options['alter']['preserve_tags'],
  852. '#dependency' => array(
  853. 'edit-options-alter-strip-tags' => array(1),
  854. ),
  855. );
  856. $form['alter']['trim_whitespace'] = array(
  857. '#type' => 'checkbox',
  858. '#title' => t('Remove whitespace'),
  859. '#description' => t('If checked, all whitespaces at the beginning and the end of the output will be removed.'),
  860. '#default_value' => $this->options['alter']['trim_whitespace'],
  861. );
  862. $form['alter']['nl2br'] = array(
  863. '#type' => 'checkbox',
  864. '#title' => t('Convert newlines to HTML &lt;br&gt; tags'),
  865. '#description' => t('If checked, all newlines chars (e.g. \n) are converted into HTML &lt;br&gt; tags.'),
  866. '#default_value' => $this->options['alter']['nl2br'],
  867. );
  868. }
  869. $form['empty_field_behavior'] = array(
  870. '#type' => 'fieldset',
  871. '#title' => t('No results behavior'),
  872. '#collapsible' => TRUE,
  873. '#collapsed' => TRUE,
  874. '#weight' => 100,
  875. );
  876. $form['empty'] = array(
  877. '#type' => 'textarea',
  878. '#title' => t('No results text'),
  879. '#default_value' => $this->options['empty'],
  880. '#description' => t('Provide text to display if this field contains an empty result. You may include HTML. You may enter data from this view as per the "Replacement patterns" in the "Rewrite Results" section below.'),
  881. '#fieldset' => 'empty_field_behavior',
  882. );
  883. $form['empty_zero'] = array(
  884. '#type' => 'checkbox',
  885. '#title' => t('Count the number 0 as empty'),
  886. '#default_value' => $this->options['empty_zero'],
  887. '#description' => t('Enable to display the "no results text" if the field contains the number 0.'),
  888. '#fieldset' => 'empty_field_behavior',
  889. );
  890. $form['hide_empty'] = array(
  891. '#type' => 'checkbox',
  892. '#title' => t('Hide if empty'),
  893. '#default_value' => $this->options['hide_empty'],
  894. '#description' => t('Enable to hide this field if it is empty. Note that the field label or rewritten output may still be displayed. To hide labels, check the style or row style settings for empty fields. To hide rewritten content, check the "Hide rewriting if empty" checkbox.'),
  895. '#fieldset' => 'empty_field_behavior',
  896. );
  897. $form['hide_alter_empty'] = array(
  898. '#type' => 'checkbox',
  899. '#title' => t('Hide rewriting if empty'),
  900. '#default_value' => $this->options['hide_alter_empty'],
  901. '#description' => t('Do not display rewritten content if this field is empty.'),
  902. '#fieldset' => 'empty_field_behavior',
  903. );
  904. }
  905. /**
  906. * Provide extra data to the administration form
  907. */
  908. function admin_summary() {
  909. return $this->label();
  910. }
  911. /**
  912. * Run before any fields are rendered.
  913. *
  914. * This gives the handlers some time to set up before any handler has
  915. * been rendered.
  916. *
  917. * @param $values
  918. * An array of all objects returned from the query.
  919. */
  920. function pre_render(&$values) { }
  921. /**
  922. * Render the field.
  923. *
  924. * @param $values
  925. * The values retrieved from the database.
  926. */
  927. function render($values) {
  928. $value = $this->get_value($values);
  929. return $this->sanitize_value($value);
  930. }
  931. /**
  932. * Render a field using advanced settings.
  933. *
  934. * This renders a field normally, then decides if render-as-link and
  935. * text-replacement rendering is necessary.
  936. */
  937. function advanced_render($values) {
  938. if ($this->allow_advanced_render() && method_exists($this, 'render_item')) {
  939. $raw_items = $this->get_items($values);
  940. // If there are no items, set the original value to NULL.
  941. if (empty($raw_items)) {
  942. $this->original_value = NULL;
  943. }
  944. }
  945. else {
  946. $value = $this->render($values);
  947. if (is_array($value)) {
  948. $value = drupal_render($value);
  949. }
  950. $this->last_render = $value;
  951. $this->original_value = $value;
  952. }
  953. if ($this->allow_advanced_render()) {
  954. $tokens = NULL;
  955. if (method_exists($this, 'render_item')) {
  956. $items = array();
  957. foreach ($raw_items as $count => $item) {
  958. $value = $this->render_item($count, $item);
  959. if (is_array($value)) {
  960. $value = drupal_render($value);
  961. }
  962. $this->last_render = $value;
  963. $this->original_value = $this->last_render;
  964. $alter = $item + $this->options['alter'];
  965. $alter['phase'] = VIEWS_HANDLER_RENDER_TEXT_PHASE_SINGLE_ITEM;
  966. $items[] = $this->render_text($alter);
  967. }
  968. $value = $this->render_items($items);
  969. }
  970. else {
  971. $alter = array('phase' => VIEWS_HANDLER_RENDER_TEXT_PHASE_COMPLETELY) + $this->options['alter'];
  972. $value = $this->render_text($alter);
  973. }
  974. if (is_array($value)) {
  975. $value = drupal_render($value);
  976. }
  977. // This happens here so that render_as_link can get the unaltered value of
  978. // this field as a token rather than the altered value.
  979. $this->last_render = $value;
  980. }
  981. if (empty($this->last_render)) {
  982. if ($this->is_value_empty($this->last_render, $this->options['empty_zero'], FALSE)) {
  983. $alter = $this->options['alter'];
  984. $alter['alter_text'] = 1;
  985. $alter['text'] = $this->options['empty'];
  986. $alter['phase'] = VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY;
  987. $this->last_render = $this->render_text($alter);
  988. }
  989. }
  990. return $this->last_render;
  991. }
  992. /**
  993. * Checks if a field value is empty.
  994. *
  995. * @param $value
  996. * The field value.
  997. * @param bool $empty_zero
  998. * Whether or not this field is configured to consider 0 as empty.
  999. * @param bool $no_skip_empty
  1000. * Whether or not to use empty() to check the value.
  1001. *
  1002. * @return bool
  1003. * TRUE if the value is considered empty, FALSE otherwise.
  1004. */
  1005. function is_value_empty($value, $empty_zero, $no_skip_empty = TRUE) {
  1006. if (!isset($value)) {
  1007. $empty = TRUE;
  1008. }
  1009. else {
  1010. $empty = ($empty_zero || ($value !== 0 && $value !== '0'));
  1011. }
  1012. if ($no_skip_empty) {
  1013. $empty = empty($value) && $empty;
  1014. }
  1015. return $empty;
  1016. }
  1017. /**
  1018. * Perform an advanced text render for the item.
  1019. *
  1020. * This is separated out as some fields may render lists, and this allows
  1021. * each item to be handled individually.
  1022. */
  1023. function render_text($alter) {
  1024. $value = $this->last_render;
  1025. if (!empty($alter['alter_text']) && $alter['text'] !== '') {
  1026. $tokens = $this->get_render_tokens($alter);
  1027. $value = $this->render_altered($alter, $tokens);
  1028. }
  1029. if (!empty($this->options['alter']['trim_whitespace'])) {
  1030. $value = trim($value);
  1031. }
  1032. // Check if there should be no further rewrite for empty values.
  1033. $no_rewrite_for_empty = $this->options['hide_alter_empty'] && $this->is_value_empty($this->original_value, $this->options['empty_zero']);
  1034. // Check whether the value is empty and return nothing, so the field isn't rendered.
  1035. // First check whether the field should be hidden if the value(hide_alter_empty = TRUE) /the rewrite is empty (hide_alter_empty = FALSE).
  1036. // For numeric values you can specify whether "0"/0 should be empty.
  1037. if ((($this->options['hide_empty'] && empty($value))
  1038. || ($alter['phase'] != VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty))
  1039. && $this->is_value_empty($value, $this->options['empty_zero'], FALSE)) {
  1040. return '';
  1041. }
  1042. // Only in empty phase.
  1043. if ($alter['phase'] == VIEWS_HANDLER_RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty) {
  1044. // If we got here then $alter contains the value of "No results text"
  1045. // and so there is nothing left to do.
  1046. return $value;
  1047. }
  1048. if (!empty($alter['strip_tags'])) {
  1049. $value = strip_tags($value, $alter['preserve_tags']);
  1050. }
  1051. $suffix = '';
  1052. if (!empty($alter['trim']) && !empty($alter['max_length'])) {
  1053. $length = strlen($value);
  1054. $value = $this->render_trim_text($alter, $value);
  1055. if ($this->options['alter']['more_link'] && strlen($value) < $length) {
  1056. $tokens = $this->get_render_tokens($alter);
  1057. $more_link_text = $this->options['alter']['more_link_text'] ? $this->options['alter']['more_link_text'] : t('more');
  1058. $more_link_text = strtr(filter_xss_admin($more_link_text), $tokens);
  1059. $more_link_path = $this->options['alter']['more_link_path'];
  1060. $more_link_path = strip_tags(decode_entities(strtr($more_link_path, $tokens)));
  1061. // Take sure that paths which was runned through url() does work as well.
  1062. $base_path = base_path();
  1063. // Checks whether the path starts with the base_path.
  1064. if (strpos($more_link_path, $base_path) === 0) {
  1065. $more_link_path = drupal_substr($more_link_path, drupal_strlen($base_path));
  1066. }
  1067. $more_link = l($more_link_text, $more_link_path, array('attributes' => array('class' => array('views-more-link'))));
  1068. $suffix .= " " . $more_link;
  1069. }
  1070. }
  1071. if (!empty($alter['nl2br'])) {
  1072. $value = nl2br($value);
  1073. }
  1074. $this->last_render_text = $value;
  1075. if (!empty($alter['make_link']) && !empty($alter['path'])) {
  1076. if (!isset($tokens)) {
  1077. $tokens = $this->get_render_tokens($alter);
  1078. }
  1079. $value = $this->render_as_link($alter, $value, $tokens);
  1080. }
  1081. return $value . $suffix;
  1082. }
  1083. /**
  1084. * Render this field as altered text, from a fieldset set by the user.
  1085. */
  1086. function render_altered($alter, $tokens) {
  1087. // Filter this right away as our substitutions are already sanitized.
  1088. $value = filter_xss_admin($alter['text']);
  1089. $value = strtr($value, $tokens);
  1090. return $value;
  1091. }
  1092. /**
  1093. * Trim the field down to the specified length.
  1094. */
  1095. function render_trim_text($alter, $value) {
  1096. if (!empty($alter['strip_tags'])) {
  1097. // NOTE: It's possible that some external fields might override the
  1098. // element type so if someone from, say, CCK runs into a bug here,
  1099. // this may be why =)
  1100. $this->definition['element type'] = 'span';
  1101. }
  1102. return views_trim_text($alter, $value);
  1103. }
  1104. /**
  1105. * Render this field as a link, with the info from a fieldset set by
  1106. * the user.
  1107. */
  1108. function render_as_link($alter, $text, $tokens) {
  1109. $value = '';
  1110. if (!empty($alter['prefix'])) {
  1111. $value .= filter_xss_admin(strtr($alter['prefix'], $tokens));
  1112. }
  1113. $options = array(
  1114. 'html' => TRUE,
  1115. 'absolute' => !empty($alter['absolute']) ? TRUE : FALSE,
  1116. );
  1117. // $path will be run through check_url() by l() so we do not need to
  1118. // sanitize it ourselves.
  1119. $path = $alter['path'];
  1120. // strip_tags() removes <front>, so check whether its different to front.
  1121. if ($path != '<front>') {
  1122. // Use strip tags as there should never be HTML in the path.
  1123. // However, we need to preserve special characters like " that
  1124. // were removed by check_plain().
  1125. $path = strip_tags(decode_entities(strtr($path, $tokens)));
  1126. if (!empty($alter['path_case']) && $alter['path_case'] != 'none') {
  1127. $path = $this->case_transform($path, $this->options['alter']['path_case']);
  1128. }
  1129. if (!empty($alter['replace_spaces'])) {
  1130. $path = str_replace(' ', '-', $path);
  1131. }
  1132. }
  1133. // Parse the URL and move any query and fragment parameters out of the path.
  1134. $url = parse_url($path);
  1135. // Seriously malformed URLs may return FALSE or empty arrays.
  1136. if (empty($url)) {
  1137. return $text;
  1138. }
  1139. // If the path is empty do not build a link around the given text and return
  1140. // it as is.
  1141. // http://www.example.com URLs will not have a $url['path'], so check host as well.
  1142. if (empty($url['path']) && empty($url['host']) && empty($url['fragment'])) {
  1143. return $text;
  1144. }
  1145. // If no scheme is provided in the $path, assign the default 'http://'.
  1146. // This allows a url of 'www.example.com' to be converted to 'http://www.example.com'.
  1147. // Only do this on for external URLs.
  1148. if ($alter['external']){
  1149. if (!isset($url['scheme'])) {
  1150. // There is no scheme, add the default 'http://' to the $path.
  1151. $path = "http://$path";
  1152. // Reset the $url array to include the new scheme.
  1153. $url = parse_url($path);
  1154. }
  1155. }
  1156. if (isset($url['query'])) {
  1157. $path = strtr($path, array('?' . $url['query'] => ''));
  1158. $query = drupal_get_query_array($url['query']);
  1159. // Remove query parameters that were assigned a query string replacement
  1160. // token for which there is no value available.
  1161. foreach ($query as $param => $val) {
  1162. if ($val == '%' . $param) {
  1163. unset($query[$param]);
  1164. }
  1165. }
  1166. $options['query'] = $query;
  1167. }
  1168. if (isset($url['fragment'])) {
  1169. $path = strtr($path, array('#' . $url['fragment'] => ''));
  1170. // If the path is empty we want to have a fragment for the current site.
  1171. if ($path == '') {
  1172. $options['external'] = TRUE;
  1173. }
  1174. $options['fragment'] = $url['fragment'];
  1175. }
  1176. $alt = strtr($alter['alt'], $tokens);
  1177. // Set the title attribute of the link only if it improves accessibility
  1178. if ($alt && $alt != $text) {
  1179. $options['attributes']['title'] = decode_entities($alt);
  1180. }
  1181. $class = strtr($alter['link_class'], $tokens);
  1182. if ($class) {
  1183. $options['attributes']['class'] = array($class);
  1184. }
  1185. if (!empty($alter['rel']) && $rel = strtr($alter['rel'], $tokens)) {
  1186. $options['attributes']['rel'] = $rel;
  1187. }
  1188. $target = check_plain(trim(strtr($alter['target'],$tokens)));
  1189. if (!empty($target)) {
  1190. $options['attributes']['target'] = $target;
  1191. }
  1192. // Allow the addition of arbitrary attributes to links. Additional attributes
  1193. // currently can only be altered in preprocessors and not within the UI.
  1194. if (isset($alter['link_attributes']) && is_array($alter['link_attributes'])) {
  1195. foreach ($alter['link_attributes'] as $key => $attribute) {
  1196. if (!isset($options['attributes'][$key])) {
  1197. $options['attributes'][$key] = strtr($attribute, $tokens);
  1198. }
  1199. }
  1200. }
  1201. // If the query and fragment were programatically assigned overwrite any
  1202. // parsed values.
  1203. if (isset($alter['query'])) {
  1204. // Convert the query to a string, perform token replacement, and then
  1205. // convert back to an array form for l().
  1206. $options['query'] = drupal_http_build_query($alter['query']);
  1207. $options['query'] = strtr($options['query'], $tokens);
  1208. $options['query'] = drupal_get_query_array($options['query']);
  1209. }
  1210. if (isset($alter['alias'])) {
  1211. // Alias is a boolean field, so no token.
  1212. $options['alias'] = $alter['alias'];
  1213. }
  1214. if (isset($alter['fragment'])) {
  1215. $options['fragment'] = strtr($alter['fragment'], $tokens);
  1216. }
  1217. if (isset($alter['language'])) {
  1218. $options['language'] = $alter['language'];
  1219. }
  1220. // If the url came from entity_uri(), pass along the required options.
  1221. if (isset($alter['entity'])) {
  1222. $options['entity'] = $alter['entity'];
  1223. }
  1224. if (isset($alter['entity_type'])) {
  1225. $options['entity_type'] = $alter['entity_type'];
  1226. }
  1227. $value .= l($text, $path, $options);
  1228. if (!empty($alter['suffix'])) {
  1229. $value .= filter_xss_admin(strtr($alter['suffix'], $tokens));
  1230. }
  1231. return $value;
  1232. }
  1233. /**
  1234. * Get the 'render' tokens to use for advanced rendering.
  1235. *
  1236. * This runs through all of the fields and arguments that
  1237. * are available and gets their values. This will then be
  1238. * used in one giant str_replace().
  1239. */
  1240. function get_render_tokens($item) {
  1241. $tokens = array();
  1242. if (!empty($this->view->build_info['substitutions'])) {
  1243. $tokens = $this->view->build_info['substitutions'];
  1244. }
  1245. $count = 0;
  1246. foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
  1247. $token = '%' . ++$count;
  1248. if (!isset($tokens[$token])) {
  1249. $tokens[$token] = '';
  1250. }
  1251. // Use strip tags as there should never be HTML in the path.
  1252. // However, we need to preserve special characters like " that
  1253. // were removed by check_plain().
  1254. $tokens['!' . $count] = isset($this->view->args[$count - 1]) ? strip_tags(decode_entities($this->view->args[$count - 1])) : '';
  1255. }
  1256. // Get flattened set of tokens for any array depth in $_GET parameters.
  1257. $tokens += $this->get_token_values_recursive($_GET);
  1258. // Now add replacements for our fields.
  1259. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  1260. if (isset($handler->last_render)) {
  1261. $tokens["[$field]"] = $handler->last_render;
  1262. }
  1263. else {
  1264. $tokens["[$field]"] = '';
  1265. }
  1266. if (!empty($item)) {
  1267. $this->add_self_tokens($tokens, $item);
  1268. }
  1269. // We only use fields up to (and including) this one.
  1270. if ($field == $this->options['id']) {
  1271. break;
  1272. }
  1273. }
  1274. // Store the tokens for the row so we can reference them later if necessary.
  1275. $this->view->style_plugin->render_tokens[$this->view->row_index] = $tokens;
  1276. $this->last_tokens = $tokens;
  1277. return $tokens;
  1278. }
  1279. /**
  1280. * Recursive function to add replacements for nested query string parameters.
  1281. *
  1282. * E.g. if you pass in the following array:
  1283. * array(
  1284. * 'foo' => array(
  1285. * 'a' => 'value',
  1286. * 'b' => 'value',
  1287. * ),
  1288. * 'bar' => array(
  1289. * 'a' => 'value',
  1290. * 'b' => array(
  1291. * 'c' => value,
  1292. * ),
  1293. * ),
  1294. * );
  1295. *
  1296. * Would yield the following array of tokens:
  1297. * array(
  1298. * '%foo_a' => 'value'
  1299. * '%foo_b' => 'value'
  1300. * '%bar_a' => 'value'
  1301. * '%bar_b_c' => 'value'
  1302. * );
  1303. *
  1304. * @param $array
  1305. * An array of values.
  1306. *
  1307. * @param $parent_keys
  1308. * An array of parent keys. This will represent the array depth.
  1309. *
  1310. * @return
  1311. * An array of available tokens, with nested keys representative of the array structure.
  1312. */
  1313. function get_token_values_recursive(array $array, array $parent_keys = array()) {
  1314. $tokens = array();
  1315. foreach ($array as $param => $val) {
  1316. if (is_array($val)) {
  1317. // Copy parent_keys array, so we don't afect other elements of this iteration.
  1318. $child_parent_keys = $parent_keys;
  1319. $child_parent_keys[] = $param;
  1320. // Get the child tokens.
  1321. $child_tokens = $this->get_token_values_recursive($val, $child_parent_keys);
  1322. // Add them to the current tokens array.
  1323. $tokens += $child_tokens;
  1324. }
  1325. else {
  1326. // Create a token key based on array element structure.
  1327. $token_string = !empty($parent_keys) ? implode('_', $parent_keys) . '_' . $param : $param;
  1328. $tokens['%' . $token_string] = strip_tags(decode_entities($val));
  1329. }
  1330. }
  1331. return $tokens;
  1332. }
  1333. /**
  1334. * Add any special tokens this field might use for itself.
  1335. *
  1336. * This method is intended to be overridden by items that generate
  1337. * fields as a list. For example, the field that displays all terms
  1338. * on a node might have tokens for the tid and the term.
  1339. *
  1340. * By convention, tokens should follow the format of [token-subtoken]
  1341. * where token is the field ID and subtoken is the field. If the
  1342. * field ID is terms, then the tokens might be [terms-tid] and [terms-name].
  1343. */
  1344. function add_self_tokens(&$tokens, $item) { }
  1345. /**
  1346. * Document any special tokens this field might use for itself.
  1347. *
  1348. * @see add_self_tokens()
  1349. */
  1350. function document_self_tokens(&$tokens) { }
  1351. /**
  1352. * Call out to the theme() function, which probably just calls render() but
  1353. * allows sites to override output fairly easily.
  1354. */
  1355. function theme($values) {
  1356. return theme($this->theme_functions(),
  1357. array(
  1358. 'view' => $this->view,
  1359. 'field' => $this,
  1360. 'row' => $values
  1361. ));
  1362. }
  1363. function theme_functions() {
  1364. $themes = array();
  1365. $hook = 'views_view_field';
  1366. $display = $this->view->display[$this->view->current_display];
  1367. if (!empty($display)) {
  1368. $themes[] = $hook . '__' . $this->view->name . '__' . $display->id . '__' . $this->options['id'];
  1369. $themes[] = $hook . '__' . $this->view->name . '__' . $display->id;
  1370. $themes[] = $hook . '__' . $display->id . '__' . $this->options['id'];
  1371. $themes[] = $hook . '__' . $display->id;
  1372. if ($display->id != $display->display_plugin) {
  1373. $themes[] = $hook . '__' . $this->view->name . '__' . $display->display_plugin . '__' . $this->options['id'];
  1374. $themes[] = $hook . '__' . $this->view->name . '__' . $display->display_plugin;
  1375. $themes[] = $hook . '__' . $display->display_plugin . '__' . $this->options['id'];
  1376. $themes[] = $hook . '__' . $display->display_plugin;
  1377. }
  1378. }
  1379. $themes[] = $hook . '__' . $this->view->name . '__' . $this->options['id'];
  1380. $themes[] = $hook . '__' . $this->view->name;
  1381. $themes[] = $hook . '__' . $this->options['id'];
  1382. $themes[] = $hook;
  1383. return $themes;
  1384. }
  1385. function ui_name($short = FALSE) {
  1386. return $this->get_field(parent::ui_name($short));
  1387. }
  1388. }
  1389. /**
  1390. * A special handler to take the place of missing or broken handlers.
  1391. *
  1392. * @ingroup views_field_handlers
  1393. */
  1394. class views_handler_field_broken extends views_handler_field {
  1395. function ui_name($short = FALSE) {
  1396. return t('Broken/missing handler');
  1397. }
  1398. function ensure_my_table() { /* No table to ensure! */ }
  1399. function query($group_by = FALSE) { /* No query to run */ }
  1400. function options_form(&$form, &$form_state) {
  1401. $form['markup'] = array(
  1402. '#markup' => '<div class="form-item description">' . t('The handler for this item is broken or missing and cannot be used. If a module provided the handler and was disabled, re-enabling the module may restore it. Otherwise, you should probably delete this item.') . '</div>',
  1403. );
  1404. }
  1405. /**
  1406. * Determine if the handler is considered 'broken'
  1407. */
  1408. function broken() { return TRUE; }
  1409. }
  1410. /**
  1411. * Render a numeric value as a size.
  1412. *
  1413. * @ingroup views_field_handlers
  1414. */
  1415. class views_handler_field_file_size extends views_handler_field {
  1416. function option_definition() {
  1417. $options = parent::option_definition();
  1418. $options['file_size_display'] = array('default' => 'formatted');
  1419. return $options;
  1420. }
  1421. function options_form(&$form, &$form_state) {
  1422. parent::options_form($form, $form_state);
  1423. $form['file_size_display'] = array(
  1424. '#title' => t('File size display'),
  1425. '#type' => 'select',
  1426. '#options' => array(
  1427. 'formatted' => t('Formatted (in KB or MB)'),
  1428. 'bytes' => t('Raw bytes'),
  1429. ),
  1430. );
  1431. }
  1432. function render($values) {
  1433. $value = $this->get_value($values);
  1434. if ($value) {
  1435. switch ($this->options['file_size_display']) {
  1436. case 'bytes':
  1437. return $value;
  1438. case 'formatted':
  1439. default:
  1440. return format_size($value);
  1441. }
  1442. }
  1443. else {
  1444. return '';
  1445. }
  1446. }
  1447. }
  1448. /**
  1449. * A handler to run a field through simple XSS filtering.
  1450. *
  1451. * @ingroup views_field_handlers
  1452. */
  1453. class views_handler_field_xss extends views_handler_field {
  1454. function render($values) {
  1455. $value = $this->get_value($values);
  1456. return $this->sanitize_value($value, 'xss');
  1457. }
  1458. }
  1459. /**
  1460. * @}
  1461. */