PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/core/modules/views/src/Plugin/views/field/FieldPluginBase.php

http://github.com/drupal/drupal
PHP | 1862 lines | 1265 code | 188 blank | 409 comment | 197 complexity | 6ebe6d5adebf0269b1ae2e361c04e809 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. namespace Drupal\views\Plugin\views\field;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Component\Render\MarkupInterface;
  5. use Drupal\Component\Utility\UrlHelper;
  6. use Drupal\Component\Utility\Xss;
  7. use Drupal\Core\Form\FormStateInterface;
  8. use Drupal\Core\Url as CoreUrl;
  9. use Drupal\views\Plugin\views\HandlerBase;
  10. use Drupal\views\Plugin\views\display\DisplayPluginBase;
  11. use Drupal\views\Render\ViewsRenderPipelineMarkup;
  12. use Drupal\views\ResultRow;
  13. use Drupal\views\ViewExecutable;
  14. /**
  15. * @defgroup views_field_handlers Views field handler plugins
  16. * @{
  17. * Handler plugins for Views fields.
  18. *
  19. * Field handlers handle both querying and display of fields in views.
  20. *
  21. * Field handler plugins extend
  22. * \Drupal\views\Plugin\views\field\FieldPluginBase. They must be
  23. * annotated with \Drupal\views\Annotation\ViewsField annotation, and they
  24. * must be in namespace directory Plugin\views\field.
  25. *
  26. * The following items can go into a hook_views_data() implementation in a
  27. * field section to affect how the field handler will behave:
  28. * - additional fields: An array of fields that should be added to the query.
  29. * The array is in one of these forms:
  30. * @code
  31. * // Simple form, for fields within the same table.
  32. * array('identifier' => fieldname)
  33. * // Form for fields in a different table.
  34. * array('identifier' => array('table' => tablename, 'field' => fieldname))
  35. * @endcode
  36. * As many fields as are necessary may be in this array.
  37. * - click sortable: If TRUE (default), this field may be click sorted.
  38. *
  39. * @ingroup views_plugins
  40. * @see plugin_api
  41. */
  42. /**
  43. * Base class for views fields.
  44. *
  45. * @ingroup views_field_handlers
  46. */
  47. abstract class FieldPluginBase extends HandlerBase implements FieldHandlerInterface {
  48. /**
  49. * Indicator of the renderText() method for rendering a single item.
  50. * (If no render_item() is present).
  51. */
  52. const RENDER_TEXT_PHASE_SINGLE_ITEM = 0;
  53. /**
  54. * Indicator of the renderText() method for rendering the whole element.
  55. * (if no render_item() method is available).
  56. */
  57. const RENDER_TEXT_PHASE_COMPLETELY = 1;
  58. /**
  59. * Indicator of the renderText() method for rendering the empty text.
  60. */
  61. const RENDER_TEXT_PHASE_EMPTY = 2;
  62. /**
  63. * @var string
  64. */
  65. public $field_alias = 'unknown';
  66. public $aliases = [];
  67. /**
  68. * The field value prior to any rewriting.
  69. *
  70. * @var mixed
  71. */
  72. public $original_value = NULL;
  73. /**
  74. * Stores additional fields which get added to the query.
  75. *
  76. * The generated aliases are stored in $aliases.
  77. *
  78. * @var array
  79. */
  80. public $additional_fields = [];
  81. /**
  82. * The link generator.
  83. *
  84. * @var \Drupal\Core\Utility\LinkGeneratorInterface
  85. */
  86. protected $linkGenerator;
  87. /**
  88. * Stores the render API renderer.
  89. *
  90. * @var \Drupal\Core\Render\RendererInterface
  91. */
  92. protected $renderer;
  93. /**
  94. * Keeps track of the last render index.
  95. *
  96. * @var int|null
  97. */
  98. protected $lastRenderIndex;
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
  103. parent::init($view, $display, $options);
  104. $this->additional_fields = [];
  105. if (!empty($this->definition['additional fields'])) {
  106. $this->additional_fields = $this->definition['additional fields'];
  107. }
  108. if (!isset($this->options['exclude'])) {
  109. $this->options['exclude'] = '';
  110. }
  111. }
  112. /**
  113. * Determine if this field can allow advanced rendering.
  114. *
  115. * Fields can set this to FALSE if they do not wish to allow
  116. * token based rewriting or link-making.
  117. */
  118. protected function allowAdvancedRender() {
  119. return TRUE;
  120. }
  121. /**
  122. * Called to add the field to a query.
  123. */
  124. public function query() {
  125. $this->ensureMyTable();
  126. // Add the field.
  127. $params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : [];
  128. $this->field_alias = $this->query->addField($this->tableAlias, $this->realField, NULL, $params);
  129. $this->addAdditionalFields();
  130. }
  131. /**
  132. * Add 'additional' fields to the query.
  133. *
  134. * @param $fields
  135. * An array of fields. The key is an identifier used to later find the
  136. * field alias used. The value is either a string in which case it's
  137. * assumed to be a field on this handler's table; or it's an array in the
  138. * form of
  139. * @code array('table' => $tablename, 'field' => $fieldname) @endcode
  140. */
  141. protected function addAdditionalFields($fields = NULL) {
  142. if (!isset($fields)) {
  143. // notice check
  144. if (empty($this->additional_fields)) {
  145. return;
  146. }
  147. $fields = $this->additional_fields;
  148. }
  149. $group_params = [];
  150. if ($this->options['group_type'] != 'group') {
  151. $group_params = [
  152. 'function' => $this->options['group_type'],
  153. ];
  154. }
  155. if (!empty($fields) && is_array($fields)) {
  156. foreach ($fields as $identifier => $info) {
  157. if (is_array($info)) {
  158. if (isset($info['table'])) {
  159. $table_alias = $this->query->ensureTable($info['table'], $this->relationship);
  160. }
  161. else {
  162. $table_alias = $this->tableAlias;
  163. }
  164. if (empty($table_alias)) {
  165. trigger_error(sprintf(
  166. "Handler % tried to add additional_field %s but % could not be added!",
  167. $this->definition['id'],
  168. $identifier,
  169. $info['table']
  170. ), E_USER_WARNING);
  171. $this->aliases[$identifier] = 'broken';
  172. continue;
  173. }
  174. $params = [];
  175. if (!empty($info['params'])) {
  176. $params = $info['params'];
  177. }
  178. $params += $group_params;
  179. $this->aliases[$identifier] = $this->query->addField($table_alias, $info['field'], NULL, $params);
  180. }
  181. else {
  182. $this->aliases[$info] = $this->query->addField($this->tableAlias, $info, NULL, $group_params);
  183. }
  184. }
  185. }
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function clickSort($order) {
  191. if (isset($this->field_alias)) {
  192. // Since fields should always have themselves already added, just
  193. // add a sort on the field.
  194. $params = $this->options['group_type'] != 'group' ? ['function' => $this->options['group_type']] : [];
  195. $this->query->addOrderBy(NULL, NULL, $order, $this->field_alias, $params);
  196. }
  197. }
  198. /**
  199. * {@inheritdoc}
  200. */
  201. public function clickSortable() {
  202. return isset($this->definition['click sortable']) ? $this->definition['click sortable'] : TRUE;
  203. }
  204. /**
  205. * {@inheritdoc}
  206. */
  207. public function label() {
  208. if (!isset($this->options['label'])) {
  209. return '';
  210. }
  211. return $this->options['label'];
  212. }
  213. /**
  214. * {@inheritdoc}
  215. */
  216. public function elementType($none_supported = FALSE, $default_empty = FALSE, $inline = FALSE) {
  217. if ($none_supported) {
  218. if ($this->options['element_type'] === '0') {
  219. return '';
  220. }
  221. }
  222. if ($this->options['element_type']) {
  223. return $this->options['element_type'];
  224. }
  225. if ($default_empty) {
  226. return '';
  227. }
  228. if ($inline) {
  229. return 'span';
  230. }
  231. if (isset($this->definition['element type'])) {
  232. return $this->definition['element type'];
  233. }
  234. return 'span';
  235. }
  236. /**
  237. * {@inheritdoc}
  238. */
  239. public function elementLabelType($none_supported = FALSE, $default_empty = FALSE) {
  240. if ($none_supported) {
  241. if ($this->options['element_label_type'] === '0') {
  242. return '';
  243. }
  244. }
  245. if ($this->options['element_label_type']) {
  246. return $this->options['element_label_type'];
  247. }
  248. if ($default_empty) {
  249. return '';
  250. }
  251. return 'span';
  252. }
  253. /**
  254. * {@inheritdoc}
  255. */
  256. public function elementWrapperType($none_supported = FALSE, $default_empty = FALSE) {
  257. if ($none_supported) {
  258. if ($this->options['element_wrapper_type'] === '0') {
  259. return 0;
  260. }
  261. }
  262. if ($this->options['element_wrapper_type']) {
  263. return $this->options['element_wrapper_type'];
  264. }
  265. if ($default_empty) {
  266. return '';
  267. }
  268. return 'div';
  269. }
  270. /**
  271. * {@inheritdoc}
  272. */
  273. public function getElements() {
  274. static $elements = NULL;
  275. if (!isset($elements)) {
  276. // @todo Add possible html5 elements.
  277. $elements = [
  278. '' => $this->t('- Use default -'),
  279. '0' => $this->t('- None -'),
  280. ];
  281. $elements += \Drupal::config('views.settings')->get('field_rewrite_elements');
  282. }
  283. return $elements;
  284. }
  285. /**
  286. * {@inheritdoc}
  287. */
  288. public function elementClasses($row_index = NULL) {
  289. $classes = $this->tokenizeValue($this->options['element_class'], $row_index);
  290. $classes = explode(' ', $classes);
  291. foreach ($classes as &$class) {
  292. $class = Html::cleanCssIdentifier($class);
  293. }
  294. return implode(' ', $classes);
  295. }
  296. /**
  297. * {@inheritdoc}
  298. */
  299. public function tokenizeValue($value, $row_index = NULL) {
  300. if (strpos($value, '{{') !== FALSE) {
  301. $fake_item = [
  302. 'alter_text' => TRUE,
  303. 'text' => $value,
  304. ];
  305. // Use isset() because empty() will trigger on 0 and 0 is
  306. // the first row.
  307. if (isset($row_index) && isset($this->view->style_plugin->render_tokens[$row_index])) {
  308. $tokens = $this->view->style_plugin->render_tokens[$row_index];
  309. }
  310. else {
  311. // Get tokens from the last field.
  312. $last_field = end($this->view->field);
  313. if (isset($last_field->last_tokens)) {
  314. $tokens = $last_field->last_tokens;
  315. }
  316. else {
  317. $tokens = $last_field->getRenderTokens($fake_item);
  318. }
  319. }
  320. $value = strip_tags($this->renderAltered($fake_item, $tokens));
  321. if (!empty($this->options['alter']['trim_whitespace'])) {
  322. $value = trim($value);
  323. }
  324. }
  325. return $value;
  326. }
  327. /**
  328. * {@inheritdoc}
  329. */
  330. public function elementLabelClasses($row_index = NULL) {
  331. $classes = $this->tokenizeValue($this->options['element_label_class'], $row_index);
  332. $classes = explode(' ', $classes);
  333. foreach ($classes as &$class) {
  334. $class = Html::cleanCssIdentifier($class);
  335. }
  336. return implode(' ', $classes);
  337. }
  338. /**
  339. * {@inheritdoc}
  340. */
  341. public function elementWrapperClasses($row_index = NULL) {
  342. $classes = $this->tokenizeValue($this->options['element_wrapper_class'], $row_index);
  343. $classes = explode(' ', $classes);
  344. foreach ($classes as &$class) {
  345. $class = Html::cleanCssIdentifier($class);
  346. }
  347. return implode(' ', $classes);
  348. }
  349. /**
  350. * {@inheritdoc}
  351. */
  352. public function getEntity(ResultRow $values) {
  353. $relationship_id = $this->options['relationship'];
  354. if ($relationship_id == 'none') {
  355. return $values->_entity;
  356. }
  357. elseif (isset($values->_relationship_entities[$relationship_id])) {
  358. return $values->_relationship_entities[$relationship_id];
  359. }
  360. }
  361. /**
  362. * {@inheritdoc}
  363. */
  364. public function getValue(ResultRow $values, $field = NULL) {
  365. $alias = isset($field) ? $this->aliases[$field] : $this->field_alias;
  366. if (isset($values->{$alias})) {
  367. return $values->{$alias};
  368. }
  369. }
  370. /**
  371. * {@inheritdoc}
  372. */
  373. public function useStringGroupBy() {
  374. return TRUE;
  375. }
  376. protected function defineOptions() {
  377. $options = parent::defineOptions();
  378. $options['label'] = ['default' => ''];
  379. // Some styles (for example table) should have labels enabled by default.
  380. $style = $this->view->getStyle();
  381. if (isset($style) && $style->defaultFieldLabels()) {
  382. $options['label']['default'] = $this->definition['title'];
  383. }
  384. $options['exclude'] = ['default' => FALSE];
  385. $options['alter'] = [
  386. 'contains' => [
  387. 'alter_text' => ['default' => FALSE],
  388. 'text' => ['default' => ''],
  389. 'make_link' => ['default' => FALSE],
  390. 'path' => ['default' => ''],
  391. 'absolute' => ['default' => FALSE],
  392. 'external' => ['default' => FALSE],
  393. 'replace_spaces' => ['default' => FALSE],
  394. 'path_case' => ['default' => 'none'],
  395. 'trim_whitespace' => ['default' => FALSE],
  396. 'alt' => ['default' => ''],
  397. 'rel' => ['default' => ''],
  398. 'link_class' => ['default' => ''],
  399. 'prefix' => ['default' => ''],
  400. 'suffix' => ['default' => ''],
  401. 'target' => ['default' => ''],
  402. 'nl2br' => ['default' => FALSE],
  403. 'max_length' => ['default' => 0],
  404. 'word_boundary' => ['default' => TRUE],
  405. 'ellipsis' => ['default' => TRUE],
  406. 'more_link' => ['default' => FALSE],
  407. 'more_link_text' => ['default' => ''],
  408. 'more_link_path' => ['default' => ''],
  409. 'strip_tags' => ['default' => FALSE],
  410. 'trim' => ['default' => FALSE],
  411. 'preserve_tags' => ['default' => ''],
  412. 'html' => ['default' => FALSE],
  413. ],
  414. ];
  415. $options['element_type'] = ['default' => ''];
  416. $options['element_class'] = ['default' => ''];
  417. $options['element_label_type'] = ['default' => ''];
  418. $options['element_label_class'] = ['default' => ''];
  419. $options['element_label_colon'] = ['default' => TRUE];
  420. $options['element_wrapper_type'] = ['default' => ''];
  421. $options['element_wrapper_class'] = ['default' => ''];
  422. $options['element_default_classes'] = ['default' => TRUE];
  423. $options['empty'] = ['default' => ''];
  424. $options['hide_empty'] = ['default' => FALSE];
  425. $options['empty_zero'] = ['default' => FALSE];
  426. $options['hide_alter_empty'] = ['default' => TRUE];
  427. return $options;
  428. }
  429. /**
  430. * Performs some cleanup tasks on the options array before saving it.
  431. */
  432. public function submitOptionsForm(&$form, FormStateInterface $form_state) {
  433. $options = &$form_state->getValue('options');
  434. $types = ['element_type', 'element_label_type', 'element_wrapper_type'];
  435. $classes = array_combine(['element_class', 'element_label_class', 'element_wrapper_class'], $types);
  436. foreach ($types as $type) {
  437. if (!$options[$type . '_enable']) {
  438. $options[$type] = '';
  439. }
  440. }
  441. foreach ($classes as $class => $type) {
  442. if (!$options[$class . '_enable'] || !$options[$type . '_enable']) {
  443. $options[$class] = '';
  444. }
  445. }
  446. if (empty($options['custom_label'])) {
  447. $options['label'] = '';
  448. $options['element_label_colon'] = FALSE;
  449. }
  450. }
  451. /**
  452. * Default options form that provides the label widget that all fields
  453. * should have.
  454. */
  455. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  456. parent::buildOptionsForm($form, $form_state);
  457. $label = $this->label();
  458. $form['custom_label'] = [
  459. '#type' => 'checkbox',
  460. '#title' => $this->t('Create a label'),
  461. '#default_value' => $label !== '',
  462. '#weight' => -103,
  463. ];
  464. $form['label'] = [
  465. '#type' => 'textfield',
  466. '#title' => $this->t('Label'),
  467. '#default_value' => $label,
  468. '#states' => [
  469. 'visible' => [
  470. ':input[name="options[custom_label]"]' => ['checked' => TRUE],
  471. ],
  472. ],
  473. '#weight' => -102,
  474. ];
  475. $form['element_label_colon'] = [
  476. '#type' => 'checkbox',
  477. '#title' => $this->t('Place a colon after the label'),
  478. '#default_value' => $this->options['element_label_colon'],
  479. '#states' => [
  480. 'visible' => [
  481. ':input[name="options[custom_label]"]' => ['checked' => TRUE],
  482. ],
  483. ],
  484. '#weight' => -101,
  485. ];
  486. $form['exclude'] = [
  487. '#type' => 'checkbox',
  488. '#title' => $this->t('Exclude from display'),
  489. '#default_value' => $this->options['exclude'],
  490. '#description' => $this->t('Enable to load this field as hidden. Often used to group fields, or to use as token in another field.'),
  491. '#weight' => -100,
  492. ];
  493. $form['style_settings'] = [
  494. '#type' => 'details',
  495. '#title' => $this->t('Style settings'),
  496. '#weight' => 99,
  497. ];
  498. $form['element_type_enable'] = [
  499. '#type' => 'checkbox',
  500. '#title' => $this->t('Customize field HTML'),
  501. '#default_value' => !empty($this->options['element_type']) || (string) $this->options['element_type'] == '0' || !empty($this->options['element_class']) || (string) $this->options['element_class'] == '0',
  502. '#fieldset' => 'style_settings',
  503. ];
  504. $form['element_type'] = [
  505. '#title' => $this->t('HTML element'),
  506. '#options' => $this->getElements(),
  507. '#type' => 'select',
  508. '#default_value' => $this->options['element_type'],
  509. '#description' => $this->t('Choose the HTML element to wrap around this field, e.g. H1, H2, etc.'),
  510. '#states' => [
  511. 'visible' => [
  512. ':input[name="options[element_type_enable]"]' => ['checked' => TRUE],
  513. ],
  514. ],
  515. '#fieldset' => 'style_settings',
  516. ];
  517. $form['element_class_enable'] = [
  518. '#type' => 'checkbox',
  519. '#title' => $this->t('Create a CSS class'),
  520. '#states' => [
  521. 'visible' => [
  522. ':input[name="options[element_type_enable]"]' => ['checked' => TRUE],
  523. ],
  524. ],
  525. '#default_value' => !empty($this->options['element_class']) || (string) $this->options['element_class'] == '0',
  526. '#fieldset' => 'style_settings',
  527. ];
  528. $form['element_class'] = [
  529. '#title' => $this->t('CSS class'),
  530. '#description' => $this->t('You may use token substitutions from the rewriting section in this class.'),
  531. '#type' => 'textfield',
  532. '#default_value' => $this->options['element_class'],
  533. '#states' => [
  534. 'visible' => [
  535. ':input[name="options[element_type_enable]"]' => ['checked' => TRUE],
  536. ':input[name="options[element_class_enable]"]' => ['checked' => TRUE],
  537. ],
  538. ],
  539. '#fieldset' => 'style_settings',
  540. ];
  541. $form['element_label_type_enable'] = [
  542. '#type' => 'checkbox',
  543. '#title' => $this->t('Customize label HTML'),
  544. '#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',
  545. '#fieldset' => 'style_settings',
  546. ];
  547. $form['element_label_type'] = [
  548. '#title' => $this->t('Label HTML element'),
  549. '#options' => $this->getElements(FALSE),
  550. '#type' => 'select',
  551. '#default_value' => $this->options['element_label_type'],
  552. '#description' => $this->t('Choose the HTML element to wrap around this label, e.g. H1, H2, etc.'),
  553. '#states' => [
  554. 'visible' => [
  555. ':input[name="options[element_label_type_enable]"]' => ['checked' => TRUE],
  556. ],
  557. ],
  558. '#fieldset' => 'style_settings',
  559. ];
  560. $form['element_label_class_enable'] = [
  561. '#type' => 'checkbox',
  562. '#title' => $this->t('Create a CSS class'),
  563. '#states' => [
  564. 'visible' => [
  565. ':input[name="options[element_label_type_enable]"]' => ['checked' => TRUE],
  566. ],
  567. ],
  568. '#default_value' => !empty($this->options['element_label_class']) || (string) $this->options['element_label_class'] == '0',
  569. '#fieldset' => 'style_settings',
  570. ];
  571. $form['element_label_class'] = [
  572. '#title' => $this->t('CSS class'),
  573. '#description' => $this->t('You may use token substitutions from the rewriting section in this class.'),
  574. '#type' => 'textfield',
  575. '#default_value' => $this->options['element_label_class'],
  576. '#states' => [
  577. 'visible' => [
  578. ':input[name="options[element_label_type_enable]"]' => ['checked' => TRUE],
  579. ':input[name="options[element_label_class_enable]"]' => ['checked' => TRUE],
  580. ],
  581. ],
  582. '#fieldset' => 'style_settings',
  583. ];
  584. $form['element_wrapper_type_enable'] = [
  585. '#type' => 'checkbox',
  586. '#title' => $this->t('Customize field and label wrapper HTML'),
  587. '#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',
  588. '#fieldset' => 'style_settings',
  589. ];
  590. $form['element_wrapper_type'] = [
  591. '#title' => $this->t('Wrapper HTML element'),
  592. '#options' => $this->getElements(FALSE),
  593. '#type' => 'select',
  594. '#default_value' => $this->options['element_wrapper_type'],
  595. '#description' => $this->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.'),
  596. '#states' => [
  597. 'visible' => [
  598. ':input[name="options[element_wrapper_type_enable]"]' => ['checked' => TRUE],
  599. ],
  600. ],
  601. '#fieldset' => 'style_settings',
  602. ];
  603. $form['element_wrapper_class_enable'] = [
  604. '#type' => 'checkbox',
  605. '#title' => $this->t('Create a CSS class'),
  606. '#states' => [
  607. 'visible' => [
  608. ':input[name="options[element_wrapper_type_enable]"]' => ['checked' => TRUE],
  609. ],
  610. ],
  611. '#default_value' => !empty($this->options['element_wrapper_class']) || (string) $this->options['element_wrapper_class'] == '0',
  612. '#fieldset' => 'style_settings',
  613. ];
  614. $form['element_wrapper_class'] = [
  615. '#title' => $this->t('CSS class'),
  616. '#description' => $this->t('You may use token substitutions from the rewriting section in this class.'),
  617. '#type' => 'textfield',
  618. '#default_value' => $this->options['element_wrapper_class'],
  619. '#states' => [
  620. 'visible' => [
  621. ':input[name="options[element_wrapper_class_enable]"]' => ['checked' => TRUE],
  622. ':input[name="options[element_wrapper_type_enable]"]' => ['checked' => TRUE],
  623. ],
  624. ],
  625. '#fieldset' => 'style_settings',
  626. ];
  627. $form['element_default_classes'] = [
  628. '#type' => 'checkbox',
  629. '#title' => $this->t('Add default classes'),
  630. '#default_value' => $this->options['element_default_classes'],
  631. '#description' => $this->t('Use default Views classes to identify the field, field label and field content.'),
  632. '#fieldset' => 'style_settings',
  633. ];
  634. $form['alter'] = [
  635. '#title' => $this->t('Rewrite results'),
  636. '#type' => 'details',
  637. '#weight' => 100,
  638. ];
  639. if ($this->allowAdvancedRender()) {
  640. $form['alter']['#tree'] = TRUE;
  641. $form['alter']['alter_text'] = [
  642. '#type' => 'checkbox',
  643. '#title' => $this->t('Override the output of this field with custom text'),
  644. '#default_value' => $this->options['alter']['alter_text'],
  645. ];
  646. $form['alter']['text'] = [
  647. '#title' => $this->t('Text'),
  648. '#type' => 'textarea',
  649. '#default_value' => $this->options['alter']['text'],
  650. // The tag list will be escaped.
  651. '#description' => $this->t('The text to display for this field. You may enter data from this view as per the "Replacement patterns" below. You may include <a href="@twig_docs">Twig</a> or the following allowed HTML tags: <code>@tags</code>', [
  652. '@twig_docs' => 'https://twig.symfony.com/doc/' . \Twig_Environment::MAJOR_VERSION . '.x',
  653. '@tags' => '<' . implode('> <', Xss::getAdminTagList()) . '>',
  654. ]),
  655. '#states' => [
  656. 'visible' => [
  657. ':input[name="options[alter][alter_text]"]' => ['checked' => TRUE],
  658. ],
  659. ],
  660. ];
  661. $form['alter']['make_link'] = [
  662. '#type' => 'checkbox',
  663. '#title' => $this->t('Output this field as a custom link'),
  664. '#default_value' => $this->options['alter']['make_link'],
  665. ];
  666. $form['alter']['path'] = [
  667. '#title' => $this->t('Link path'),
  668. '#type' => 'textfield',
  669. '#default_value' => $this->options['alter']['path'],
  670. '#description' => $this->t('The Drupal path or absolute URL for this link. You may enter data from this view as per the "Replacement patterns" below.'),
  671. '#states' => [
  672. 'visible' => [
  673. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  674. ],
  675. ],
  676. '#maxlength' => 255,
  677. ];
  678. $form['alter']['absolute'] = [
  679. '#type' => 'checkbox',
  680. '#title' => $this->t('Use absolute path'),
  681. '#default_value' => $this->options['alter']['absolute'],
  682. '#states' => [
  683. 'visible' => [
  684. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  685. ],
  686. ],
  687. ];
  688. $form['alter']['replace_spaces'] = [
  689. '#type' => 'checkbox',
  690. '#title' => $this->t('Replace spaces with dashes'),
  691. '#default_value' => $this->options['alter']['replace_spaces'],
  692. '#states' => [
  693. 'visible' => [
  694. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  695. ],
  696. ],
  697. ];
  698. $form['alter']['external'] = [
  699. '#type' => 'checkbox',
  700. '#title' => $this->t('External server URL'),
  701. '#default_value' => $this->options['alter']['external'],
  702. '#description' => $this->t("Links to an external server using a full URL: e.g. 'http://www.example.com' or 'www.example.com'."),
  703. '#states' => [
  704. 'visible' => [
  705. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  706. ],
  707. ],
  708. ];
  709. $form['alter']['path_case'] = [
  710. '#type' => 'select',
  711. '#title' => $this->t('Transform the case'),
  712. '#description' => $this->t('When printing URL paths, how to transform the case of the filter value.'),
  713. '#states' => [
  714. 'visible' => [
  715. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  716. ],
  717. ],
  718. '#options' => [
  719. 'none' => $this->t('No transform'),
  720. 'upper' => $this->t('Upper case'),
  721. 'lower' => $this->t('Lower case'),
  722. 'ucfirst' => $this->t('Capitalize first letter'),
  723. 'ucwords' => $this->t('Capitalize each word'),
  724. ],
  725. '#default_value' => $this->options['alter']['path_case'],
  726. ];
  727. $form['alter']['link_class'] = [
  728. '#title' => $this->t('Link class'),
  729. '#type' => 'textfield',
  730. '#default_value' => $this->options['alter']['link_class'],
  731. '#description' => $this->t('The CSS class to apply to the link.'),
  732. '#states' => [
  733. 'visible' => [
  734. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  735. ],
  736. ],
  737. ];
  738. $form['alter']['alt'] = [
  739. '#title' => $this->t('Title text'),
  740. '#type' => 'textfield',
  741. '#default_value' => $this->options['alter']['alt'],
  742. '#description' => $this->t('Text to place as "title" text which most browsers display as a tooltip when hovering over the link.'),
  743. '#states' => [
  744. 'visible' => [
  745. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  746. ],
  747. ],
  748. ];
  749. $form['alter']['rel'] = [
  750. '#title' => $this->t('Rel Text'),
  751. '#type' => 'textfield',
  752. '#default_value' => $this->options['alter']['rel'],
  753. '#description' => $this->t('Include Rel attribute for use in lightbox2 or other javascript utility.'),
  754. '#states' => [
  755. 'visible' => [
  756. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  757. ],
  758. ],
  759. ];
  760. $form['alter']['prefix'] = [
  761. '#title' => $this->t('Prefix text'),
  762. '#type' => 'textfield',
  763. '#default_value' => $this->options['alter']['prefix'],
  764. '#description' => $this->t('Any text to display before this link. You may include HTML.'),
  765. '#states' => [
  766. 'visible' => [
  767. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  768. ],
  769. ],
  770. ];
  771. $form['alter']['suffix'] = [
  772. '#title' => $this->t('Suffix text'),
  773. '#type' => 'textfield',
  774. '#default_value' => $this->options['alter']['suffix'],
  775. '#description' => $this->t('Any text to display after this link. You may include HTML.'),
  776. '#states' => [
  777. 'visible' => [
  778. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  779. ],
  780. ],
  781. ];
  782. $form['alter']['target'] = [
  783. '#title' => $this->t('Target'),
  784. '#type' => 'textfield',
  785. '#default_value' => $this->options['alter']['target'],
  786. '#description' => $this->t("Target of the link, such as _blank, _parent or an iframe's name. This field is rarely used."),
  787. '#states' => [
  788. 'visible' => [
  789. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  790. ],
  791. ],
  792. ];
  793. // Get a list of the available fields and arguments for token replacement.
  794. // Setup the tokens for fields.
  795. $previous = $this->getPreviousFieldLabels();
  796. $optgroup_arguments = (string) t('Arguments');
  797. $optgroup_fields = (string) t('Fields');
  798. foreach ($previous as $id => $label) {
  799. $options[$optgroup_fields]["{{ $id }}"] = substr(strrchr($label, ":"), 2);
  800. }
  801. // Add the field to the list of options.
  802. $options[$optgroup_fields]["{{ {$this->options['id']} }}"] = substr(strrchr($this->adminLabel(), ":"), 2);
  803. foreach ($this->view->display_handler->getHandlers('argument') as $arg => $handler) {
  804. $options[$optgroup_arguments]["{{ arguments.$arg }}"] = $this->t('@argument title', ['@argument' => $handler->adminLabel()]);
  805. $options[$optgroup_arguments]["{{ raw_arguments.$arg }}"] = $this->t('@argument input', ['@argument' => $handler->adminLabel()]);
  806. }
  807. $this->documentSelfTokens($options[$optgroup_fields]);
  808. // Default text.
  809. $output = [];
  810. $output[] = [
  811. '#markup' => '<p>' . $this->t('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>',
  812. ];
  813. // We have some options, so make a list.
  814. if (!empty($options)) {
  815. $output[] = [
  816. '#markup' => '<p>' . $this->t("The following replacement 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.") . '</p>',
  817. ];
  818. foreach (array_keys($options) as $type) {
  819. if (!empty($options[$type])) {
  820. $items = [];
  821. foreach ($options[$type] as $key => $value) {
  822. $items[] = $key . ' == ' . $value;
  823. }
  824. $item_list = [
  825. '#theme' => 'item_list',
  826. '#items' => $items,
  827. ];
  828. $output[] = $item_list;
  829. }
  830. }
  831. }
  832. // This construct uses 'hidden' and not markup because process doesn't
  833. // run. It also has an extra div because the dependency wants to hide
  834. // the parent in situations like this, so we need a second div to
  835. // make this work.
  836. $form['alter']['help'] = [
  837. '#type' => 'details',
  838. '#title' => $this->t('Replacement patterns'),
  839. '#value' => $output,
  840. '#states' => [
  841. 'visible' => [
  842. [
  843. ':input[name="options[alter][make_link]"]' => ['checked' => TRUE],
  844. ],
  845. [
  846. ':input[name="options[alter][alter_text]"]' => ['checked' => TRUE],
  847. ],
  848. [
  849. ':input[name="options[alter][more_link]"]' => ['checked' => TRUE],
  850. ],
  851. ],
  852. ],
  853. ];
  854. $form['alter']['trim'] = [
  855. '#type' => 'checkbox',
  856. '#title' => $this->t('Trim this field to a maximum number of characters'),
  857. '#default_value' => $this->options['alter']['trim'],
  858. ];
  859. $form['alter']['max_length'] = [
  860. '#title' => $this->t('Maximum number of characters'),
  861. '#type' => 'textfield',
  862. '#default_value' => $this->options['alter']['max_length'],
  863. '#states' => [
  864. 'visible' => [
  865. ':input[name="options[alter][trim]"]' => ['checked' => TRUE],
  866. ],
  867. ],
  868. ];
  869. $form['alter']['word_boundary'] = [
  870. '#type' => 'checkbox',
  871. '#title' => $this->t('Trim only on a word boundary'),
  872. '#description' => $this->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.'),
  873. '#default_value' => $this->options['alter']['word_boundary'],
  874. '#states' => [
  875. 'visible' => [
  876. ':input[name="options[alter][trim]"]' => ['checked' => TRUE],
  877. ],
  878. ],
  879. ];
  880. $form['alter']['ellipsis'] = [
  881. '#type' => 'checkbox',
  882. '#title' => $this->t('Add "…" at the end of trimmed text'),
  883. '#default_value' => $this->options['alter']['ellipsis'],
  884. '#states' => [
  885. 'visible' => [
  886. ':input[name="options[alter][trim]"]' => ['checked' => TRUE],
  887. ],
  888. ],
  889. ];
  890. $form['alter']['more_link'] = [
  891. '#type' => 'checkbox',
  892. '#title' => $this->t('Add a read-more link if output is trimmed'),
  893. '#default_value' => $this->options['alter']['more_link'],
  894. '#states' => [
  895. 'visible' => [
  896. ':input[name="options[alter][trim]"]' => ['checked' => TRUE],
  897. ],
  898. ],
  899. ];
  900. $form['alter']['more_link_text'] = [
  901. '#type' => 'textfield',
  902. '#title' => $this->t('More link label'),
  903. '#default_value' => $this->options['alter']['more_link_text'],
  904. '#description' => $this->t('You may use the "Replacement patterns" above.'),
  905. '#states' => [
  906. 'visible' => [
  907. ':input[name="options[alter][trim]"]' => ['checked' => TRUE],
  908. ':input[name="options[alter][more_link]"]' => ['checked' => TRUE],
  909. ],
  910. ],
  911. ];
  912. $form['alter']['more_link_path'] = [
  913. '#type' => 'textfield',
  914. '#title' => $this->t('More link path'),
  915. '#default_value' => $this->options['alter']['more_link_path'],
  916. '#description' => $this->t('This can be an internal Drupal path such as node/add or an external URL such as "https://www.drupal.org". You may use the "Replacement patterns" above.'),
  917. '#states' => [
  918. 'visible' => [
  919. ':input[name="options[alter][trim]"]' => ['checked' => TRUE],
  920. ':input[name="options[alter][more_link]"]' => ['checked' => TRUE],
  921. ],
  922. ],
  923. ];
  924. $form['alter']['html'] = [
  925. '#type' => 'checkbox',
  926. '#title' => $this->t('Field can contain HTML'),
  927. '#description' => $this->t('An HTML corrector will be run to ensure HTML tags are properly closed after trimming.'),
  928. '#default_value' => $this->options['alter']['html'],
  929. '#states' => [
  930. 'visible' => [
  931. ':input[name="options[alter][trim]"]' => ['checked' => TRUE],
  932. ],
  933. ],
  934. ];
  935. $form['alter']['strip_tags'] = [
  936. '#type' => 'checkbox',
  937. '#title' => $this->t('Strip HTML tags'),
  938. '#default_value' => $this->options['alter']['strip_tags'],
  939. ];
  940. $form['alter']['preserve_tags'] = [
  941. '#type' => 'textfield',
  942. '#title' => $this->t('Preserve certain tags'),
  943. '#description' => $this->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'),
  944. '#default_value' => $this->options['alter']['preserve_tags'],
  945. '#states' => [
  946. 'visible' => [
  947. ':input[name="options[alter][strip_tags]"]' => ['checked' => TRUE],
  948. ],
  949. ],
  950. ];
  951. $form['alter']['trim_whitespace'] = [
  952. '#type' => 'checkbox',
  953. '#title' => $this->t('Remove whitespace'),
  954. '#default_value' => $this->options['alter']['trim_whitespace'],
  955. ];
  956. $form['alter']['nl2br'] = [
  957. '#type' => 'checkbox',
  958. '#title' => $this->t('Convert newlines to HTML &lt;br&gt; tags'),
  959. '#default_value' => $this->options['alter']['nl2br'],
  960. ];
  961. }
  962. $form['empty_field_behavior'] = [
  963. '#type' => 'details',
  964. '#title' => $this->t('No results behavior'),
  965. '#weight' => 100,
  966. ];
  967. $form['empty'] = [
  968. '#type' => 'textarea',
  969. '#title' => $this->t('No results text'),
  970. '#default_value' => $this->options['empty'],
  971. '#description' => $this->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 above.'),
  972. '#fieldset' => 'empty_field_behavior',
  973. ];
  974. $form['empty_zero'] = [
  975. '#type' => 'checkbox',
  976. '#title' => $this->t('Count the number 0 as empty'),
  977. '#default_value' => $this->options['empty_zero'],
  978. '#description' => $this->t('Enable to display the "no results text" if the field contains the number 0.'),
  979. '#fieldset' => 'empty_field_behavior',
  980. ];
  981. $form['hide_empty'] = [
  982. '#type' => 'checkbox',
  983. '#title' => $this->t('Hide if empty'),
  984. '#default_value' => $this->options['hide_empty'],
  985. '#description' => $this->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.'),
  986. '#fieldset' => 'empty_field_behavior',
  987. ];
  988. $form['hide_alter_empty'] = [
  989. '#type' => 'checkbox',
  990. '#title' => $this->t('Hide rewriting if empty'),
  991. '#default_value' => $this->options['hide_alter_empty'],
  992. '#description' => $this->t('Do not display rewritten content if this field is empty.'),
  993. '#fieldset' => 'empty_field_behavior',
  994. ];
  995. }
  996. /**
  997. * Returns all field labels of fields before this field.
  998. *
  999. * @return array
  1000. * An array of field labels keyed by their field IDs.
  1001. */
  1002. protected function getPreviousFieldLabels() {
  1003. $all_fields = $this->view->display_handler->getFieldLabels();
  1004. $field_options = array_slice($all_fields, 0, array_search($this->options['id'], array_keys($all_fields)));
  1005. return $field_options;
  1006. }
  1007. /**
  1008. * Provide extra data to the administration form
  1009. */
  1010. public function adminSummary() {
  1011. return $this->label();
  1012. }
  1013. /**
  1014. * {@inheritdoc}
  1015. */
  1016. public function preRender(&$values) {}
  1017. /**
  1018. * {@inheritdoc}
  1019. */
  1020. public function render(ResultRow $values) {
  1021. $value = $this->getValue($values);
  1022. return $this->sanitizeValue($value);
  1023. }
  1024. /**
  1025. * {@inheritdoc}
  1026. */
  1027. public function postRender(ResultRow $row, $output) {
  1028. // Make sure the last rendered value is available also when this is
  1029. // retrieved from cache.
  1030. $this->last_render = $output;
  1031. return [];
  1032. }
  1033. /**
  1034. * {@inheritdoc}
  1035. */
  1036. public function advancedRender(ResultRow $values) {
  1037. // Clean up values from previous render calls.
  1038. if ($this->lastRenderIndex != $values->index) {
  1039. $this->last_render_text = '';
  1040. }
  1041. if ($this->allowAdvancedRender() && $this instanceof MultiItemsFieldHandlerInterface) {
  1042. $raw_items = $this->getItems($values);
  1043. // If there are no items, set the original value to NULL.
  1044. if (empty($raw_items)) {
  1045. $this->original_value = NULL;
  1046. }
  1047. }
  1048. else {
  1049. $value = $this->render($values);
  1050. if (is_array($value)) {
  1051. $value = $this->getRenderer()->render($value);
  1052. }
  1053. $this->last_render = $value;
  1054. $this->original_value = $value;
  1055. }
  1056. if ($this->allowAdvancedRender()) {
  1057. $tokens = NULL;
  1058. if ($this instanceof MultiItemsFieldHandlerInterface) {
  1059. $items = [];
  1060. foreach ($raw_items as $count => $item) {
  1061. $value = $this->render_item($count, $item);
  1062. if (is_array($value)) {
  1063. $value = (string) $this->getRenderer()->render($value);
  1064. }
  1065. $this->last_render = $value;
  1066. $this->original_value = $this->last_render;
  1067. $alter = $item + $this->options['alter'];
  1068. $alter['phase'] = static::RENDER_TEXT_PHASE_SINGLE_ITEM;
  1069. $items[] = $this->renderText($alter);
  1070. }
  1071. $value = $this->renderItems($items);
  1072. }
  1073. else {
  1074. $alter = ['phase' => static::RENDER_TEXT_PHASE_COMPLETELY] + $this->options['alter'];
  1075. $value = $this->renderText($alter);
  1076. }
  1077. if (is_array($value)) {
  1078. $value = $this->getRenderer()->render($value);
  1079. }
  1080. // This happens here so that renderAsLink can get the unaltered value of
  1081. // this field as a token rather than the altered value.
  1082. $this->last_render = $value;
  1083. }
  1084. // String cast is necessary to test emptiness of MarkupInterface
  1085. // objects.
  1086. if (empty((string) $this->last_render)) {
  1087. if ($this->isValueEmpty($this->last_render, $this->options['empty_zero'], FALSE)) {
  1088. $alter = $this->options['alter'];
  1089. $alter['alter_text'] = 1;
  1090. $alter['text'] = $this->options['empty'];
  1091. $alter['phase'] = static::RENDER_TEXT_PHASE_EMPTY;
  1092. $this->last_render = $this->renderText($alter);
  1093. }
  1094. }
  1095. // If we rendered something, update the last render index.
  1096. if ((string) $this->last_render !== '') {
  1097. $this->lastRenderIndex = $values->index;
  1098. }
  1099. return $this->last_render;
  1100. }
  1101. /**
  1102. * {@inheritdoc}
  1103. */
  1104. public function isValueEmpty($value, $empty_zero, $no_skip_empty = TRUE) {
  1105. // Convert MarkupInterface to a string for checking.
  1106. if ($value instanceof MarkupInterface) {
  1107. $value = (string) $value;
  1108. }
  1109. if (!isset($value)) {
  1110. $empty = TRUE;
  1111. }
  1112. else {
  1113. $empty = ($empty_zero || ($value !== 0 && $value !== '0'));
  1114. }
  1115. if ($no_skip_empty) {
  1116. $empty = empty($value) && $empty;
  1117. }
  1118. return $empty;
  1119. }
  1120. /**
  1121. * {@inheritdoc}
  1122. */
  1123. public function renderText($alter) {
  1124. // We need to preserve the safeness of the value regardless of the
  1125. // alterations made by this method. Any alterations or replacements made
  1126. // within this method need to ensure that at the minimum the result is
  1127. // XSS admin filtered. See self::renderAltered() as an example that does.
  1128. $value_is_safe = $this->last_render instanceof MarkupInterface;
  1129. // Cast to a string so that empty checks and string functions work as
  1130. // expected.
  1131. $value = (string) $this->last_render;
  1132. if (!empty($alter['alter_text']) && $alter['text'] !== '') {
  1133. $tokens = $this->getRenderTokens($alter);
  1134. $value = $this->renderAltered($alter, $tokens);
  1135. // $alter['text'] is entered through the views admin UI and will be safe
  1136. // because the output of $this->renderAltered() is run through
  1137. // Xss::filterAdmin().
  1138. // @see \Drupal\views\Plugin\views\PluginBase::viewsTokenReplace()
  1139. // @see \Drupal\Component\Utility\Xss::filterAdmin()
  1140. $value_is_safe = TRUE;
  1141. }
  1142. if (!empty($this->options['alter']['trim_whitespace'])) {
  1143. $value = trim($value);
  1144. }
  1145. // Check if there should be no further rewrite for empty values.
  1146. $no_rewrite_for_empty = $this->options['hide_alter_empty'] && $this->isValueEmpty($this->original_value, $this->options['empty_zero']);
  1147. // Check whether the value is empty and return nothing, so the field isn't rendered.
  1148. // First check whether the field should be hidden if the value(hide_alter_empty = TRUE) /the rewrite is empty (hide_alter_empty = FALSE).
  1149. // For numeric values you can specify whether "0"/0 should be empty.
  1150. if ((($this->options['hide_empty'] && empty($value))
  1151. || ($alter['phase'] != static::RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty))
  1152. && $this->isValueEmpty($value, $this->options['empty_zero'], FALSE)) {
  1153. return '';
  1154. }
  1155. // Only in empty phase.
  1156. if ($alter['phase'] == static::RENDER_TEXT_PHASE_EMPTY && $no_rewrite_for_empty) {
  1157. // If we got here then $alter contains the value of "No results text"
  1158. // and so there is nothing left to do.
  1159. return ViewsRenderPipelineMarkup::create($value);
  1160. }
  1161. if (!empty($alter['strip_tags'])) {
  1162. $value = strip_tags($value, $alter['preserve_tags']);
  1163. }
  1164. $more_link = '';
  1165. if (!empty($alter['trim']) && !empty($alter['max_length'])) {
  1166. $length = strlen($value);
  1167. $value = $this->renderTrimText($alter, $value);
  1168. if ($this->options['alter']['more_link'] && strlen($value) < $length) {
  1169. $tokens = $this->getRenderTokens($alter);
  1170. $more_link_text = $this->options['alter']['more_link_text'] ? $this->options['alter']['more_link_text'] : $this->t('more');
  1171. $more_link_text = strtr(Xss::filterAdmin($more_link_text), $tokens);
  1172. $more_link_path = $this->options['alter']['more_link_path'];
  1173. $more_link_path = strip_tags(Html::decodeEntities($this->viewsTokenReplace($more_link_path, $tokens)));
  1174. // Make sure that paths which were run through URL generation work as
  1175. // well.
  1176. $base_path = base_path();
  1177. // Checks whether the path starts with the base_path.
  1178. if (strpos($more_link_path, $base_path) === 0) {
  1179. $more_link_path = mb_substr($more_link_path, mb_strlen($base_path));
  1180. }
  1181. // @todo Views should expect and store a leading /. See
  1182. // https://www.drupal.org/node/2423913.
  1183. $options = [
  1184. 'attributes' => [
  1185. 'class' => [
  1186. 'views-more-link',
  1187. ],
  1188. ],
  1189. ];
  1190. if (UrlHelper::isExternal($more_link_path)) {
  1191. $more_link_url = CoreUrl::fromUri($more_link_path, $options);
  1192. }
  1193. else {
  1194. $more_link_url = CoreUrl::fromUserInput('/' . $more_link_path, $options);
  1195. }
  1196. $more_link = ' ' . $this->linkGenerator()->generate($more_link_text, $more_link_url);
  1197. }
  1198. }
  1199. if (!empty($alter['nl2br'])) {
  1200. $value = nl2br($value);
  1201. }
  1202. if ($value_is_safe) {
  1203. $value = ViewsRenderPipelineMarkup::create($value);
  1204. }
  1205. $this->last_render_text = $value;
  1206. if (!empty($alter['make_link']) && (!empty($alter['path']) || !empty($alter['url']))) {
  1207. if (!isset($tokens)) {
  1208. $tokens = $this->getRenderTokens($alter);
  1209. }
  1210. $value = $this->renderAsLink($alter, $value, $tokens);
  1211. }
  1212. // Preserve whether or not the string is safe. Since $more_link comes from
  1213. // \Drupal::l(), it is safe to append. Check if the value is an instance of
  1214. // \Drupal\Component\Render\MarkupInterface here because renderAsLink()
  1215. // can return both safe and unsafe values.
  1216. if ($value instanceof MarkupInterface) {
  1217. return ViewsRenderPipelineMarkup::create($value . $more_link);
  1218. }
  1219. else {
  1220. // If the string is not already marked safe, it is still OK to return it
  1221. // because it will be sanitized by Twig.
  1222. return $value . $more_link;
  1223. }
  1224. }
  1225. /**
  1226. * Render this field as user-defined altered text.
  1227. */
  1228. protected function renderAltered($alter, $tokens) {
  1229. return $this->viewsTokenReplace($alter['text'], $tokens);
  1230. }
  1231. /**
  1232. * Trims the field down to the specified length.
  1233. *
  1234. * @param array $alter
  1235. * The alter array of options to use.
  1236. * - max_length: Maximum length of the string, the rest gets truncated.
  1237. * - word_boundary: Trim only on a word boundary.
  1238. * - ellipsis: Show an ellipsis (…) at the end of the trimmed string.
  1239. * - html: Make sure that the html is correct.
  1240. *
  1241. * @param string $value
  1242. * The string which should be trimmed.
  1243. *
  1244. * @return string
  1245. * The rendered trimmed string.
  1246. */
  1247. protected function renderTrimText($alter, $value) {
  1248. if (!empty($alter['strip_tags'])) {
  1249. // NOTE: It's possible that some external fields might override the
  1250. // element type.
  1251. $this->definition['element type'] = 'span';
  1252. }
  1253. return static::trimText($alter, $value);
  1254. }
  1255. /**
  1256. * Render this field as a link, with the info from a fieldset set by
  1257. * the user.
  1258. */
  1259. protected function renderAsLink($alter, $text, $tokens) {
  1260. $options = [
  1261. 'absolute' => !empty($alter['absolute']) ? TRUE : FALSE,
  1262. 'alias' => FALSE,
  1263. 'entity' => NULL,
  1264. 'entity_type' => NULL,
  1265. 'fragment' => NULL,
  1266. 'language' => NULL,
  1267. 'query' => [],
  1268. ];
  1269. $alter += [
  1270. 'path' => NULL,
  1271. ];
  1272. $path = $alter['path'];
  1273. // strip_tags() and viewsTokenReplace remove <front>, so check whether it's
  1274. // different to front.
  1275. if ($path != '<front>') {
  1276. // Use strip_tags as there should never be HTML in the path.
  1277. // However, we need to preserve special characters like " that were
  1278. // removed by Html::escape().
  1279. $path = Html::decodeEntities($this->viewsTokenReplace($alter['path'], $tokens));
  1280. // Tokens might contain <front>, so check for <front> again.
  1281. if ($path != '<front>') {
  1282. $path = strip_tags($path);
  1283. }
  1284. // Tokens might have resolved URL's, as is the case for tokens provided by
  1285. // Link fields, so all internal paths will be prefixed by base_path(). For
  1286. // proper further handling reset this to internal:/.
  1287. if (strpos($path, base_path()) === 0) {
  1288. $path = 'internal:/' . substr($path, strlen(base_path()));
  1289. }
  1290. // If we have no $path and no $alter['url'], we have nothing to work with,
  1291. // so we just return the text.
  1292. if (empty($path) && empty($alter['url'])) {
  1293. return $text;
  1294. }
  1295. // If no scheme is provided in the $path, assign the default 'http://'.
  1296. // This allows a url of 'www.example.com' to be converted to
  1297. // 'http://www.example.com'.
  1298. // Only do this when flag for external has been set, $…

Large files files are truncated, but you can click here to view the full file