View source
<?php
namespace Drupal\commerce_stock_field\Plugin\Field\FieldFormatter;
use Drupal\commerce\PurchasableEntityInterface;
use Drupal\commerce_stock\StockServiceManager;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class SimpleStockLevelFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $stockServiceManager;
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, StockServiceManager $simple_stock_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->stockServiceManager = $simple_stock_manager;
}
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('commerce_stock.service_manager'));
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$entity = $items
->getEntity();
if ($entity instanceof PurchasableEntityInterface) {
$stockServiceManager = $this->stockServiceManager;
$level = $stockServiceManager
->getStockLevel($entity);
}
else {
$level = 0;
}
$elements = [];
$elements[0] = [
'#type' => 'html_tag',
'#tag' => 'p',
'#value' => $level,
];
return $elements;
}
}