Skip to content
Snippets Groups Projects
Select Git revision
  • 7d176b393c75945acf9b1aa1797001fdd36965cc
  • 11.x default protected
  • 10.5.x protected
  • 10.6.x protected
  • 11.2.x protected
  • 11.1.x protected
  • 10.4.x protected
  • 11.0.x protected
  • 10.3.x protected
  • 7.x protected
  • 10.2.x protected
  • 10.1.x protected
  • 9.5.x protected
  • 10.0.x protected
  • 9.4.x protected
  • 9.3.x protected
  • 9.2.x protected
  • 9.1.x protected
  • 8.9.x protected
  • 9.0.x protected
  • 8.8.x protected
  • 10.5.1 protected
  • 11.2.2 protected
  • 11.2.1 protected
  • 11.2.0 protected
  • 10.5.0 protected
  • 11.2.0-rc2 protected
  • 10.5.0-rc1 protected
  • 11.2.0-rc1 protected
  • 10.4.8 protected
  • 11.1.8 protected
  • 10.5.0-beta1 protected
  • 11.2.0-beta1 protected
  • 11.2.0-alpha1 protected
  • 10.4.7 protected
  • 11.1.7 protected
  • 10.4.6 protected
  • 11.1.6 protected
  • 10.3.14 protected
  • 10.4.5 protected
  • 11.0.13 protected
41 results

aggregator.module

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    NumericFormatterBase.php 3.06 KiB
    <?php
    
    /**
     * @file
     * Contains \Drupal\Core\Field\Plugin\Field\FieldFormatter\NumericFormatterBase.
     */
    
    namespace Drupal\Core\Field\Plugin\Field\FieldFormatter;
    
    use Drupal\Core\Field\AllowedTagsXssTrait;
    use Drupal\Core\Field\FormatterBase;
    use Drupal\Core\Field\FieldItemListInterface;
    use Drupal\Core\Form\FormStateInterface;
    
    /**
     * Parent plugin for decimal and integer formatters.
     */
    abstract class NumericFormatterBase extends FormatterBase {
    
      use AllowedTagsXssTrait;
    
      /**
       * {@inheritdoc}
       */
      public function settingsForm(array $form, FormStateInterface $form_state) {
        $options = array(
          ''  => t('- None -'),
          '.' => t('Decimal point'),
          ',' => t('Comma'),
          ' ' => t('Space'),
          chr(8201) => t('Thin space'),
          "'" => t('Apostrophe'),
        );
        $elements['thousand_separator'] = array(
          '#type' => 'select',
          '#title' => t('Thousand marker'),
          '#options' => $options,
          '#default_value' => $this->getSetting('thousand_separator'),
          '#weight' => 0,
        );
    
        $elements['prefix_suffix'] = array(
          '#type' => 'checkbox',
          '#title' => t('Display prefix and suffix'),
          '#default_value' => $this->getSetting('prefix_suffix'),
          '#weight' => 10,
        );
    
        return $elements;
      }
    
      /**
       * {@inheritdoc}
       */
      public function settingsSummary() {
        $summary = array();
    
        $summary[] = $this->numberFormat(1234.1234567890);
        if ($this->getSetting('prefix_suffix')) {
          $summary[] = t('Display with prefix and suffix.');
        }
    
        return $summary;
      }
    
      /**
       * {@inheritdoc}
       */
      public function viewElements(FieldItemListInterface $items, $langcode) {
        $elements = array();
        $settings = $this->getFieldSettings();
    
        foreach ($items as $delta => $item) {
          $output = $this->numberFormat($item->value);
    
          // Account for prefix and suffix.
          if ($this->getSetting('prefix_suffix')) {
            $prefixes = isset($settings['prefix']) ? array_map(array('Drupal\Core\Field\FieldFilteredString', 'create'), explode('|', $settings['prefix'])) : array('');
            $suffixes = isset($settings['suffix']) ? array_map(array('Drupal\Core\Field\FieldFilteredString', 'create'), explode('|', $settings['suffix'])) : array('');
            $prefix = (count($prefixes) > 1) ? $this->formatPlural($item->value, $prefixes[0], $prefixes[1]) : $prefixes[0];
            $suffix = (count($suffixes) > 1) ? $this->formatPlural($item->value, $suffixes[0], $suffixes[1]) : $suffixes[0];
            $output = $prefix . $output . $suffix;
          }
          // Output the raw value in a content attribute if the text of the HTML
          // element differs from the raw value (for example when a prefix is used).
          if (isset($item->_attributes) && $item->value != $output) {
            $item->_attributes += array('content' => $item->value);
          }
    
          $elements[$delta] = array('#markup' => $output);
        }
    
        return $elements;
      }
    
      /**
       * Formats a number.
       *
       * @param mixed $number
       *   The numeric value.
       *
       * @return string
       *   The formatted number.
       */
      abstract protected function numberFormat($number);
    
    }