View source
<?php
namespace Drupal\eck\Plugin\migrate\source\d7;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\migrate\Exception\RequirementsException;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;
class EckEntity extends FieldableEntity {
protected $entityType;
protected $bundle;
protected $eckTable;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, StateInterface $state, EntityTypeManagerInterface $entity_manager) {
$this->entityType = $configuration['entity_type'] ?? '';
if (!\is_string($this->entityType)) {
throw new MigrateException('The entity_type must be a string');
}
if (isset($configuration['bundle'])) {
$this->bundle = $configuration['bundle'];
if (!\is_string($this->bundle) && !\is_array($this->bundle)) {
throw new MigrateException('The bundle must be a string or an array');
}
}
$this->eckTable = 'eck_' . $this->entityType;
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $state, $entity_manager);
}
public function query() {
$query = $this
->select($this->eckTable, 'eck')
->fields('eck');
if (isset($this->bundle)) {
$query
->condition('eck.type', (array) $this->bundle, 'IN');
}
return $query;
}
public function prepareRow(Row $row) {
$language = $row
->getSourceProperty('language');
$id = $row
->getSourceProperty('id');
$vid = $row
->hasSourceProperty('revision_id') ? $row
->getSourceProperty('revision_id') : $id;
foreach ($this
->getFields($this->entityType, $this->bundle) as $field_name => $field) {
$field_language = $field['translatable'] ? $language : NULL;
$row
->setSourceProperty($field_name, $this
->getFieldValues($this->entityType, $field_name, $id, $vid, $field_language));
}
return parent::prepareRow($row);
}
public function fields() {
return [
'id' => $this
->t('The primary identifier for the entity'),
'type' => $this
->t('The bundle of the entity'),
'title' => $this
->t('Title'),
'uid' => $this
->t('Author'),
'created' => $this
->t('Created'),
'changed' => $this
->t('Changed'),
'language' => $this
->t('Entity language code'),
];
}
public function getIds() {
return [
'id' => [
'type' => 'integer',
'alias' => 'eck',
],
];
}
public function checkRequirements() {
$table_name = 'eck_' . $this->entityType;
if (!$this
->getDatabase()
->schema()
->tableExists($table_name)) {
throw new RequirementsException("ECK table for '{$this->entityType}' does not exist");
}
parent::checkRequirements();
}
}