Skip to content
Snippets Groups Projects

Added support for product attribute value entity

1 file
+ 109
0
Compare changes
  • Side-by-side
  • Inline
<?php
namespace Drupal\commerce_sync\Plugin\SingleContentSyncBaseFieldsProcessor;
use Drupal\commerce_product\Entity\ProductAttributeValueInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\FieldableEntityInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\single_content_sync\ContentExporterInterface;
use Drupal\single_content_sync\SingleContentSyncBaseFieldsProcessorPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Implementation for commerce product variation base fields processor plugin.
*
* @SingleContentSyncBaseFieldsProcessor(
* id = "commerce_product_attribute_value",
* label = @Translation("Commerce Product Attribute Value"),
* entity_type = "commerce_product_attribute_value",
* )
*/
class CommerceProductAttributeValue extends SingleContentSyncBaseFieldsProcessorPluginBase implements ContainerFactoryPluginInterface {
/**
* The content exporter.
*
* @var \Drupal\single_content_sync\ContentExporterInterface
*/
protected ContentExporterInterface $exporter;
/**
* The messenger service.
*
* @var \Drupal\Core\Messenger\MessengerInterface
*/
protected MessengerInterface $messenger;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, MessengerInterface $messenger, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->messenger = $messenger;
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('messenger'),
$container->get('entity_type.manager'),
);
}
/**
* {@inheritdoc}
*/
public function exportBaseValues(FieldableEntityInterface $entity): array {
assert($entity instanceof ProductAttributeValueInterface);
$base_fields = [
'attribute' => $entity->getAttribute()->toArray(),
'name' => $entity->getName(),
'weight' => $entity->getWeight(),
];
return $base_fields;
}
/**
* {@inheritdoc}
*/
public function mapBaseFieldsValues(array $values, FieldableEntityInterface $entity): array {
assert($entity instanceof ProductAttributeValueInterface);
$base_fields = [
'name' => $values['name'],
'weight' => $values['weight'],
];
if (!empty($values['attribute'])) {
$storage = $this->entityTypeManager->getStorage('commerce_product_attribute');
$attribute = $storage->load($values['attribute']['id']);
if ($attribute) {
$base_fields['attribute'] = $attribute->id();
}
else {
$this->messenger->addError(t('Attribute type @type does not exist.', ['@type' => $values['attribute']['id']]));
}
}
return $base_fields;
}
}
Loading