Price.php in Commerce Feeds 8
File
src/Feeds/Target/Price.php
View source
<?php
namespace Drupal\commerce_feeds\Feeds\Target;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\feeds\FieldTargetDefinition;
use Drupal\feeds\Plugin\Type\Target\ConfigurableTargetInterface;
use Drupal\feeds\Plugin\Type\Target\FieldTargetBase;
class Price extends FieldTargetBase implements ConfigurableTargetInterface {
protected static function prepareTarget(FieldDefinitionInterface $field_definition) {
return FieldTargetDefinition::createFromFieldDefinition($field_definition)
->addProperty('number');
}
protected function prepareValue($delta, array &$values) {
parent::prepareValue($delta, $values);
$values['currency_code'] = $this->configuration['currency_code'];
}
public function defaultConfiguration() {
$currency_code = NULL;
$currencies = \Drupal::entityTypeManager()
->getStorage('commerce_currency')
->loadMultiple();
$currency_codes = array_keys($currencies);
if (count($currency_codes) == 1) {
$currency_code = reset($currency_codes);
}
return [
'currency_code' => $currency_code,
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$currencies = \Drupal::entityTypeManager()
->getStorage('commerce_currency')
->loadMultiple();
$currency_codes = array_keys($currencies);
$form['currency_code'] = [
'#type' => 'select',
'#title' => $this
->t('Currency'),
'#options' => array_combine($currency_codes, $currency_codes),
'#default_value' => $this->configuration['currency_code'],
];
return $form;
}
public function getSummary() {
return [
'unit' => [
'#type' => 'item',
'#markup' => $this
->t('Currency: %currency_code', [
'%currency_code' => $this->configuration['currency_code'],
]),
],
];
}
}
Classes
Name |
Description |
Price |
Defines a commerce_price field mapper. |