views_handler_field.inc 58 KB

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