diff --git a/src/FieldHandler/FieldHandlerBase.php b/src/FieldHandler/FieldHandlerBase.php index 38249c5..6680771 100644 --- a/src/FieldHandler/FieldHandlerBase.php +++ b/src/FieldHandler/FieldHandlerBase.php @@ -3,6 +3,7 @@ namespace Drupal\commerce_sheets\FieldHandler; use Drupal\Component\Utility\NestedArray; +use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\PluginBase; @@ -12,7 +13,8 @@ use PhpOffice\PhpSpreadsheet\Style\Style; /** * Base class for all field handler plugins. */ -abstract class FieldHandlerBase extends PluginBase implements FieldHandlerInterface { +abstract class FieldHandlerBase extends PluginBase implements + FieldHandlerInterface { /** * {@inheritdoc} @@ -88,8 +90,35 @@ abstract class FieldHandlerBase extends PluginBase implements FieldHandlerInterf /** * {@inheritdoc} */ - public function toCellValue($field) { - return $field->value; + public function validate($value) {} + + /** + * {@inheritdoc} + */ + public function fromCellGetValue($cell) { + return (string) $cell->getValue(); + } + + /** + * {@inheritdoc} + */ + public function fromCellToField($cell, FieldItemListInterface $field) { + if ($this->getLocked()) { + return; + } + + $field->setValue($this->fromCellGetValue($cell)); + } + + /** + * {@inheritdoc} + */ + public function toCellValue($property_value) { + if ($property_value instanceof FieldItemListInterface) { + return $property_value->value; + } + + return $property_value; } /** @@ -107,4 +136,14 @@ abstract class FieldHandlerBase extends PluginBase implements FieldHandlerInterf } } + /** + * Returns the `locked` setting for the field handler. + * + * @return bool + * The value of the `locked` setting. + */ + protected function getLocked() { + return $this->getConfiguration()['locked']; + } + } diff --git a/src/FieldHandler/FieldHandlerInterface.php b/src/FieldHandler/FieldHandlerInterface.php index 528997d..ec28830 100644 --- a/src/FieldHandler/FieldHandlerInterface.php +++ b/src/FieldHandler/FieldHandlerInterface.php @@ -8,6 +8,7 @@ use Drupal\Component\Plugin\PluginInspectionInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Plugin\PluginFormInterface; +use PhpOffice\PhpSpreadsheet\Cell\Cell; use PhpOffice\PhpSpreadsheet\Style\Style; /** @@ -19,6 +20,8 @@ use PhpOffice\PhpSpreadsheet\Style\Style; * storing in fields. * - Convert field values into cell values for storing in the exported * spreadsheet. + * + * @I Rename to PropertyHandler */ interface FieldHandlerInterface extends ConfigurableInterface, @@ -26,8 +29,38 @@ interface FieldHandlerInterface extends PluginFormInterface, PluginInspectionInterface { + /** + * Validates that the given value is in the format expected by the field. + * + * @param mixed $value + * The value to validate. + */ public function validate($value); + /** + * Returns the value of the given cell. + * + * The value is returned in the most reasonable format for the data contained + * in the cell. + * + * @param \PhpOffice\PhpSpreadsheet\Cell\Cell $cell + * The cell to get the value from. + * + * @return mixed + * The value. + */ + public function fromCellGetValue(Cell $cell); + + /** + * Stores the value of the cell to the given field. + * + * @param \PhpOffice\PhpSpreadsheet\Cell\Cell $cell + * The cell to get the value from. + * @param \Drupal\Core\Field\FieldItemListInterface $field + * The field where to store the value to. + */ + public function fromCellToField(Cell $cell, FieldItemListInterface $field); + /** * Returns the cell value for the given field. * diff --git a/src/Plugin/CommerceSheets/FieldHandler/Boolean.php b/src/Plugin/CommerceSheets/FieldHandler/Boolean.php index 8805eb5..054b2ad 100644 --- a/src/Plugin/CommerceSheets/FieldHandler/Boolean.php +++ b/src/Plugin/CommerceSheets/FieldHandler/Boolean.php @@ -19,6 +19,28 @@ use Drupal\commerce_sheets\FieldHandler\FieldHandlerBase; */ class Boolean extends FieldHandlerBase { + /** + * {@inheritdoc} + */ + public function fromCellToField($cell, $field) { + if ($this->getLocked()) { + return; + } + + $value = NULL; + switch ($cell->getValue()) { + case 'Y': + $value = '1'; + break; + + case 'N': + $value = '0'; + break; + } + + $field->setValue($value); + } + /** * {@inheritdoc} */ diff --git a/src/Plugin/CommerceSheets/FieldHandler/Bundle.php b/src/Plugin/CommerceSheets/FieldHandler/Bundle.php index c046514..0d2f127 100644 --- a/src/Plugin/CommerceSheets/FieldHandler/Bundle.php +++ b/src/Plugin/CommerceSheets/FieldHandler/Bundle.php @@ -2,8 +2,6 @@ namespace Drupal\commerce_sheets\Plugin\CommerceSheets\FieldHandler; -use Drupal\commerce_sheets\FieldHandler\FieldHandlerBase; - /** * Provides a handler plugin for bundle fields. * @@ -17,7 +15,7 @@ use Drupal\commerce_sheets\FieldHandler\FieldHandlerBase; * } * ) */ -class Bundle extends FieldHandlerBase { +class Bundle extends EntityReference { /** * {@inheritdoc} @@ -29,12 +27,4 @@ class Bundle extends FieldHandlerBase { ] + parent::defaultConfiguration(); } - /** - * {@inheritdoc} - */ - public function toCellValue($field) { - $entity = $field->entity; - return $entity->label() . ' (' . $entity->id() . ')'; - } - } diff --git a/src/Plugin/CommerceSheets/FieldHandler/EntityReference.php b/src/Plugin/CommerceSheets/FieldHandler/EntityReference.php index 0185397..05ac8bb 100644 --- a/src/Plugin/CommerceSheets/FieldHandler/EntityReference.php +++ b/src/Plugin/CommerceSheets/FieldHandler/EntityReference.php @@ -17,6 +17,49 @@ use Drupal\commerce_sheets\FieldHandler\FieldHandlerBase; */ class EntityReference extends FieldHandlerBase { + /** + * {@inheritdoc} + */ + public function fromCellGetValue($cell) { + $values = $cell->getValue(); + if (!$values) { + return; + } + + $ids = array_map( + function ($value) { + $id = NULL; + + // The value should be in the format "label (entity id)'; match the ID + // from inside the parentheses. + if (preg_match("/.+\s\(([^\)]+)\)/", $value, $matches)) { + $id = $matches[1]; + } + + return $id; + }, + explode(',', $values) + ); + + return array_filter($ids); + } + + /** + * {@inheritdoc} + */ + public function fromCellToField($cell, $field) { + if ($this->getLocked()) { + return; + } + + $ids = $this->fromCellGetValue($cell); + if (!$ids) { + $field->setValue(NULL); + } + + $field->setValue($ids); + } + /** * {@inheritdoc} */ diff --git a/src/Plugin/CommerceSheets/FieldHandler/Integer.php b/src/Plugin/CommerceSheets/FieldHandler/Integer.php index b3bf625..0c01ae2 100644 --- a/src/Plugin/CommerceSheets/FieldHandler/Integer.php +++ b/src/Plugin/CommerceSheets/FieldHandler/Integer.php @@ -21,6 +21,13 @@ use PhpOffice\PhpSpreadsheet\Style\NumberFormat; */ class Integer extends FieldHandlerBase { + /** + * {@inheritdoc} + */ + public function fromCellGetValue($cell) { + return (int) $cell->getValue(); + } + /** * {@inheritdoc} */ diff --git a/src/Plugin/CommerceSheets/FieldHandler/Measurement.php b/src/Plugin/CommerceSheets/FieldHandler/Measurement.php index b99d5e3..d64b59c 100644 --- a/src/Plugin/CommerceSheets/FieldHandler/Measurement.php +++ b/src/Plugin/CommerceSheets/FieldHandler/Measurement.php @@ -17,6 +17,22 @@ use Drupal\commerce_sheets\FieldHandler\FieldHandlerBase; */ class Measurement extends FieldHandlerBase { + /** + * {@inheritdoc} + */ + public function fromCellGetValue($cell) { + $value = $cell->getValue(); + if (!$value) { + return; + } + + $value_parts = explode(' ', $value); + return [ + 'number' => $value_parts[0], + 'unit' => $value_parts[1], + ]; + } + /** * {@inheritdoc} */ diff --git a/src/Plugin/CommerceSheets/FieldHandler/Path.php b/src/Plugin/CommerceSheets/FieldHandler/Path.php index 98342de..4a44dcc 100644 --- a/src/Plugin/CommerceSheets/FieldHandler/Path.php +++ b/src/Plugin/CommerceSheets/FieldHandler/Path.php @@ -17,6 +17,40 @@ use Drupal\commerce_sheets\FieldHandler\FieldHandlerBase; */ class Path extends FieldHandlerBase { + /** + * {@inheritdoc} + */ + public function fromCellToField($cell, $field) { + if ($this->getLocked()) { + return; + } + + $is_pathauto = FALSE; + + // Detect if we have a Pathauto field first - and handle accordingly. + // If the cell does not have an alias defined, it will get its alias from + // the pathauto pattern. + $field_definition = $field->getFieldDefinition(); + $type_definition = \Drupal::service('plugin.manager.field.field_type') + ->getDefinition($field_definition->getType()); + $type_properties = $type_definition['class']::propertyDefinitions( + $field_definition->getFieldStorageDefinition() + ); + if (isset($type_properties['pathauto'])) { + $is_pathauto = TRUE; + } + + $value = $cell->getValue(); + if (!$value) { + $value = NULL; + } + $field->alias = $value; + + if ($is_pathauto) { + $field->pathauto = $value ? 0 : 1; + } + } + /** * {@inheritdoc} */ diff --git a/src/Plugin/CommerceSheets/FieldHandler/Price.php b/src/Plugin/CommerceSheets/FieldHandler/Price.php index c1dfaa9..5f27427 100644 --- a/src/Plugin/CommerceSheets/FieldHandler/Price.php +++ b/src/Plugin/CommerceSheets/FieldHandler/Price.php @@ -17,6 +17,22 @@ use Drupal\commerce_sheets\FieldHandler\FieldHandlerBase; */ class Price extends FieldHandlerBase { + /** + * {@inheritdoc} + */ + public function fromCellGetValue($cell) { + $value = $cell->getValue(); + if (!$value) { + return; + } + + $value_parts = explode(' ', $value); + return [ + 'number' => $value_parts[0], + 'currency_code' => $value_parts[1], + ]; + } + /** * {@inheritdoc} */