PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/reasonat/test8
PHP | 181 lines | 134 code | 16 blank | 31 comment | 13 complexity | e2b54aa279fc2ff5e5a820962928e05e MD5 | raw file
  1. <?php
  2. namespace Drupal\views\Plugin\views\field;
  3. use Drupal\Core\Form\FormStateInterface;
  4. use Drupal\Core\StringTranslation\PluralTranslatableMarkup;
  5. use Drupal\views\ResultRow;
  6. /**
  7. * Render a field as a numeric value
  8. *
  9. * Definition terms:
  10. * - float: If true this field contains a decimal value. If unset this field
  11. * will be assumed to be integer.
  12. *
  13. * @ingroup views_field_handlers
  14. *
  15. * @ViewsField("numeric")
  16. */
  17. class NumericField extends FieldPluginBase {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function defineOptions() {
  22. $options = parent::defineOptions();
  23. $options['set_precision'] = array('default' => FALSE);
  24. $options['precision'] = array('default' => 0);
  25. $options['decimal'] = array('default' => '.');
  26. $options['separator'] = array('default' => ',');
  27. $options['format_plural'] = array('default' => FALSE);
  28. $options['format_plural_string'] = array('default' => '1' . LOCALE_PLURAL_DELIMITER . '@count');
  29. $options['prefix'] = array('default' => '');
  30. $options['suffix'] = array('default' => '');
  31. return $options;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function buildOptionsForm(&$form, FormStateInterface $form_state) {
  37. if (!empty($this->definition['float'])) {
  38. $form['set_precision'] = array(
  39. '#type' => 'checkbox',
  40. '#title' => $this->t('Round'),
  41. '#description' => $this->t('If checked, the number will be rounded.'),
  42. '#default_value' => $this->options['set_precision'],
  43. );
  44. $form['precision'] = array(
  45. '#type' => 'textfield',
  46. '#title' => $this->t('Precision'),
  47. '#default_value' => $this->options['precision'],
  48. '#description' => $this->t('Specify how many digits to print after the decimal point.'),
  49. '#states' => array(
  50. 'visible' => array(
  51. ':input[name="options[set_precision]"]' => array('checked' => TRUE),
  52. ),
  53. ),
  54. '#size' => 2,
  55. );
  56. $form['decimal'] = array(
  57. '#type' => 'textfield',
  58. '#title' => $this->t('Decimal point'),
  59. '#default_value' => $this->options['decimal'],
  60. '#description' => $this->t('What single character to use as a decimal point.'),
  61. '#size' => 2,
  62. );
  63. }
  64. $form['separator'] = array(
  65. '#type' => 'select',
  66. '#title' => $this->t('Thousands marker'),
  67. '#options' => array(
  68. '' => $this->t('- None -'),
  69. ',' => $this->t('Comma'),
  70. ' ' => $this->t('Space'),
  71. '.' => $this->t('Decimal'),
  72. '\'' => $this->t('Apostrophe'),
  73. ),
  74. '#default_value' => $this->options['separator'],
  75. '#description' => $this->t('What single character to use as the thousands separator.'),
  76. '#size' => 2,
  77. );
  78. $form['format_plural'] = array(
  79. '#type' => 'checkbox',
  80. '#title' => $this->t('Format plural'),
  81. '#description' => $this->t('If checked, special handling will be used for plurality.'),
  82. '#default_value' => $this->options['format_plural'],
  83. );
  84. $form['format_plural_string'] = array(
  85. '#type' => 'value',
  86. '#default_value' => $this->options['format_plural_string'],
  87. );
  88. $plural_array = explode(LOCALE_PLURAL_DELIMITER, $this->options['format_plural_string']);
  89. $plurals = $this->getNumberOfPlurals($this->view->storage->get('langcode'));
  90. for ($i = 0; $i < $plurals; $i++) {
  91. $form['format_plural_values'][$i] = array(
  92. '#type' => 'textfield',
  93. // @todo Should use better labels https://www.drupal.org/node/2499639
  94. '#title' => ($i == 0 ? $this->t('Singular form') : $this->formatPlural($i, 'First plural form', '@count. plural form')),
  95. '#default_value' => isset($plural_array[$i]) ? $plural_array[$i] : '',
  96. '#description' => $this->t('Text to use for this variant, @count will be replaced with the value.'),
  97. '#states' => array(
  98. 'visible' => array(
  99. ':input[name="options[format_plural]"]' => array('checked' => TRUE),
  100. ),
  101. ),
  102. );
  103. }
  104. if ($plurals == 2) {
  105. // Simplify interface text for the most common case.
  106. $form['format_plural_values'][0]['#description'] = $this->t('Text to use for the singular form, @count will be replaced with the value.');
  107. $form['format_plural_values'][1]['#title'] = $this->t('Plural form');
  108. $form['format_plural_values'][1]['#description'] = $this->t('Text to use for the plural form, @count will be replaced with the value.');
  109. }
  110. $form['prefix'] = array(
  111. '#type' => 'textfield',
  112. '#title' => $this->t('Prefix'),
  113. '#default_value' => $this->options['prefix'],
  114. '#description' => $this->t('Text to put before the number, such as currency symbol.'),
  115. );
  116. $form['suffix'] = array(
  117. '#type' => 'textfield',
  118. '#title' => $this->t('Suffix'),
  119. '#default_value' => $this->options['suffix'],
  120. '#description' => $this->t('Text to put after the number, such as currency symbol.'),
  121. );
  122. parent::buildOptionsForm($form, $form_state);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function submitOptionsForm(&$form, FormStateInterface $form_state) {
  128. // Merge plural format options into one string and drop the individual
  129. // option values.
  130. $options = &$form_state->getValue('options');
  131. $options['format_plural_string'] = implode(LOCALE_PLURAL_DELIMITER, $options['format_plural_values']);
  132. unset($options['format_plural_values']);
  133. parent::submitOptionsForm($form, $form_state);
  134. }
  135. /**
  136. * {@inheritdoc}
  137. */
  138. public function render(ResultRow $values) {
  139. $value = $this->getValue($values);
  140. if (!empty($this->options['set_precision'])) {
  141. $value = number_format($value, $this->options['precision'], $this->options['decimal'], $this->options['separator']);
  142. }
  143. else {
  144. $remainder = abs($value) - intval(abs($value));
  145. $value = $value > 0 ? floor($value) : ceil($value);
  146. $value = number_format($value, 0, '', $this->options['separator']);
  147. if ($remainder) {
  148. // The substr may not be locale safe.
  149. $value .= $this->options['decimal'] . substr($remainder, 2);
  150. }
  151. }
  152. // Check to see if hiding should happen before adding prefix and suffix.
  153. if ($this->options['hide_empty'] && empty($value) && ($value !== 0 || $this->options['empty_zero'])) {
  154. return '';
  155. }
  156. // If we should format as plural, take the (possibly) translated plural
  157. // setting and format with the current language.
  158. if (!empty($this->options['format_plural'])) {
  159. $value = PluralTranslatableMarkup::createFromTranslatedString($value, $this->options['format_plural_string']);
  160. }
  161. return $this->sanitizeValue($this->options['prefix'], 'xss')
  162. . $this->sanitizeValue($value)
  163. . $this->sanitizeValue($this->options['suffix'], 'xss');
  164. }
  165. }