CreateQuantity.php in farmOS 2.x
File
modules/core/quantity/src/Plugin/migrate/process/CreateQuantity.php
View source
<?php
namespace Drupal\quantity\Plugin\migrate\process;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CreateQuantity extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $quantityStorage;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = new static($configuration, $plugin_id, $plugin_definition);
$instance->quantityStorage = $container
->get('entity_type.manager')
->getStorage('quantity');
return $instance;
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$entity_values = [];
if (isset($this->configuration['default_values']) && is_array($this->configuration['default_values'])) {
foreach ($this->configuration['default_values'] as $key => $value) {
$entity_values[$key] = $value;
}
}
if (isset($this->configuration['values']) && is_array($this->configuration['values'])) {
foreach ($this->configuration['values'] as $key => $property) {
$source_value = $row
->get($property);
NestedArray::setValue($entity_values, explode(Row::PROPERTY_SEPARATOR, $key), $source_value, TRUE);
}
}
$entity = $this->quantityStorage
->create($entity_values);
$entity
->save();
return $entity;
}
}