You are here

FormattedPrice.php in Price 2.0.x

Same filename and directory in other branches
  1. 2.x src/Plugin/DataType/FormattedPrice.php

File

src/Plugin/DataType/FormattedPrice.php
View source
<?php

namespace Drupal\price\Plugin\DataType;

use Drupal\price\Plugin\Field\FieldType\PriceItem;
use Drupal\Core\TypedData\Plugin\DataType\StringData;

/**
 * Defines a data type for formatted prices.
 *
 * @DataType(
 *   id = "formatted_price",
 *   label = @Translation("Formatted price")
 * )
 */
class FormattedPrice extends StringData {

  /**
   * {@inheritdoc}
   */
  public function getValue() {
    $parent = $this
      ->getParent();
    assert($parent instanceof PriceItem);
    $formatted_price = NULL;
    if (!$parent
      ->isEmpty()) {
      try {
        $price = $parent
          ->toPrice();
        $currency_formatter = \Drupal::service('price.currency_formatter');
        $formatted_price = $currency_formatter
          ->format($price
          ->getNumber(), $price
          ->getCurrencyCode());
      } catch (\InvalidArgumentException $exception) {
      }
    }
    return $formatted_price;
  }

}

Classes

Namesort descending Description
FormattedPrice Defines a data type for formatted prices.