Measurement.php in Commerce Feeds 8
File
src/Feeds/Target/Measurement.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;
use Drupal\physical\MeasurementType;
class Measurement 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['unit'] = $this->configuration['unit'];
}
public function defaultConfiguration() {
return [
'unit' => MeasurementType::getUnitClass($this
->getMeasurementType())::getBaseUnit(),
];
}
protected function getMeasurementType() {
return $this->settings['measurement_type'];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['unit'] = [
'#type' => 'select',
'#title' => $this
->t('Unit'),
'#options' => MeasurementType::getUnitClass($this
->getMeasurementType())::getLabels(),
'#default_value' => $this->configuration['unit'],
];
return $form;
}
public function getSummary() {
return [
'measurement_type' => [
'#type' => 'item',
'#markup' => $this
->t('Measurement type: @type', [
'@type' => MeasurementType::getLabels()[$this->settings['measurement_type']],
]),
],
'unit' => [
'#type' => 'item',
'#markup' => $this
->t('Unit: %unit', [
'%unit' => $this->configuration['unit'],
]),
],
];
}
}