You are here

WishlistPurchaseDefaultFormatter.php in Commerce Wishlist 8.3

File

src/Plugin/Field/FieldFormatter/WishlistPurchaseDefaultFormatter.php
View source
<?php

namespace Drupal\commerce_wishlist\Plugin\Field\FieldFormatter;

use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin implementation of the 'commerce_wishlist_purchase_default' formatter.
 *
 * @FieldFormatter(
 *   id = "commerce_wishlist_purchase_default",
 *   label = @Translation("Default"),
 *   field_types = {
 *     "commerce_wishlist_purchase"
 *   }
 * )
 */
class WishlistPurchaseDefaultFormatter extends FormatterBase implements ContainerFactoryPluginInterface {

  /**
   * The date formatter.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Constructs a new WishlistPurchaseDefaultFormatter object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param array $third_party_settings
   *   Any third party settings settings.
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   The date formatter.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, DateFormatterInterface $date_formatter) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
    $this->dateFormatter = $date_formatter;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($plugin_id, $plugin_definition, $configuration['field_definition'], $configuration['settings'], $configuration['label'], $configuration['view_mode'], $configuration['third_party_settings'], $container
      ->get('date.formatter'));
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $elements = [
      '#type' => 'table',
      '#caption' => $this
        ->t('Purchases'),
      '#header' => [
        $this
          ->t('Order ID'),
        $this
          ->t('Quantity'),
        $this
          ->t('Purchased'),
      ],
      '#cache' => [
        'contexts' => [
          'languages:' . LanguageInterface::TYPE_INTERFACE,
        ],
      ],
    ];

    /** @var \Drupal\commerce_wishlist\Plugin\Field\FieldType\WishlistPurchaseItem $item */
    foreach ($items as $item) {
      $purchase = $item
        ->toPurchase();
      $elements['#rows'][] = [
        $purchase
          ->getOrderId(),
        $purchase
          ->getQuantity(),
        $this->dateFormatter
          ->format($purchase
          ->getPurchasedTime()),
      ];
    }
    return $elements;
  }

}

Classes

Namesort descending Description
WishlistPurchaseDefaultFormatter Plugin implementation of the 'commerce_wishlist_purchase_default' formatter.