PromotionOfferBase.php in Commerce Core 8.2
File
modules/promotion/src/Plugin/Commerce/PromotionOffer/PromotionOfferBase.php
View source
<?php
namespace Drupal\commerce_promotion\Plugin\Commerce\PromotionOffer;
use Drupal\commerce_price\RounderInterface;
use Drupal\commerce_promotion\Entity\PromotionInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class PromotionOfferBase extends PluginBase implements PromotionOfferInterface, ContainerFactoryPluginInterface {
protected $rounder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, RounderInterface $rounder) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this
->setConfiguration($configuration);
$this->rounder = $rounder;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('commerce_price.rounder'));
}
public function defaultConfiguration() {
return [];
}
public function getConfiguration() {
return $this->configuration;
}
public function setConfiguration(array $configuration) {
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
return $this;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['#type'] = 'fieldset';
$form['#title'] = $this
->t('Offer');
$form['#collapsible'] = FALSE;
return $form;
}
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
if (!$form_state
->getErrors()) {
$this->configuration = [];
}
}
public function getEntityTypeId() {
return $this->pluginDefinition['entity_type'];
}
public function clear(EntityInterface $entity, PromotionInterface $promotion) {
}
protected function assertEntity(EntityInterface $entity) {
$entity_type_id = $entity
->getEntityTypeId();
$offer_entity_type_id = $this
->getEntityTypeId();
if ($entity_type_id != $offer_entity_type_id) {
throw new \InvalidArgumentException(sprintf('The offer requires a "%s" entity, but a "%s" entity was given.', $offer_entity_type_id, $entity_type_id));
}
}
}