View source
<?php
namespace Drupal\gathercontent\Plugin\migrate\source;
use Cheppers\GatherContent\DataTypes\Element;
use Cheppers\GatherContent\DataTypes\ElementSimpleChoice;
use Cheppers\GatherContent\DataTypes\ElementSimpleFile;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\gathercontent\DrupalGatherContentClient;
use Drupal\migrate\MigrateException;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
use Drupal\migrate\Plugin\MigrateIdMapInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class GatherContentMigrateSource extends SourcePluginBase implements ContainerFactoryPluginInterface {
protected $projectId;
protected $templateId;
protected $fields = [];
protected $metatagFields = [];
protected $client;
protected $trackChanges = TRUE;
public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, DrupalGatherContentClient $client) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $migration);
$configFields = [
'projectId',
'templateId',
'fields',
'metatagFields',
];
foreach ($configFields as $configField) {
if (isset($configuration[$configField])) {
$this->{$configField} = $configuration[$configField];
}
else {
throw new MigrateException("The source configuration must include '{$configField}'.");
}
}
$this->client = $client;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
return new static($configuration, $plugin_id, $plugin_definition, $migration, $container
->get('gathercontent.client'));
}
public function count($refresh = FALSE) {
return count($this
->getItems());
}
public function getIds() {
return [
'id' => [
'type' => 'string',
],
];
}
protected $items = NULL;
public function __toString() {
return 'Gathercontent migration';
}
public function fields() {
return $this->fields;
}
public function get(string $property) {
return $this->{$property};
}
protected function getItems() {
if ($this->items === NULL) {
$this->items = $this->client
->itemsGet($this->projectId, [
'template_id' => $this->templateId,
]);
if (!empty($this->items['data'])) {
$pagination = $this->items['pagination'];
$total_pages = $pagination->totalPages;
$current_page = $pagination->currentPage;
while ($current_page <= $total_pages) {
$query = [
'template_id' => $this->templateId,
'page' => $current_page + 1,
];
$next_items = $this->client
->itemsGet($this->projectId, $query);
if (!empty($next_items['data'])) {
$this->items['data'] = array_merge($this->items['data'], $next_items['data']);
}
$current_page++;
}
}
}
return $this
->convertItemsToArray($this->items['data']);
}
protected function convertItemsToArray(array $items) {
$converted = [];
if ($items !== NULL) {
foreach ($items as $key => $item) {
$converted[$key] = get_object_vars($item);
}
}
return $converted;
}
protected function getFiles(array $gcFiles, Element $field) {
$value = [];
foreach ($gcFiles as $file) {
if ($file->field == $field->id) {
$value[] = $file;
}
}
return $value;
}
protected function initializeIterator() {
return new \ArrayIterator($this
->getItems());
}
public function prepareRow(Row $row) {
$ret = parent::prepareRow($row);
if ($ret) {
$collectedMetaTags = [];
$gcId = $row
->getSourceProperty('id');
$gcItem = $this->client
->itemGet($gcId);
if (empty($gcItem)) {
return FALSE;
}
foreach ($gcItem->content as $fieldId => $field) {
$value = $this
->getFieldValue($field);
if (array_key_exists($fieldId, $this->metatagFields)) {
$collectedMetaTags[$this->metatagFields[$fieldId]] = $value;
continue;
}
if (is_array($field)) {
foreach ($field as $subObject) {
if ($subObject instanceof ElementSimpleFile) {
if (in_array($fieldId, $this->fields)) {
$idMap = $row
->getIdMap();
$idMap['source_row_status'] = MigrateIdMapInterface::STATUS_NEEDS_UPDATE;
$row
->setIdMap($idMap);
}
continue;
}
$value[] = [
'value' => $this
->getFieldValue($subObject),
];
}
}
$row
->setSourceProperty($fieldId, $value);
}
$row
->setSourceProperty('item_title', $gcItem->name);
if (!empty($collectedMetaTags)) {
$value = $this
->prepareMetatags($collectedMetaTags);
$row
->setSourceProperty('meta_tags', $value);
}
}
return $ret;
}
protected function prepareMetatags(array $collectedMetaTags) {
return serialize($collectedMetaTags);
}
protected function getFieldValue($field) {
if (!is_array($field)) {
return $field
->getValue();
}
$value = [];
foreach ($field as $item) {
if ($item instanceof ElementSimpleChoice) {
$value[] = [
'gc_id' => $item->id,
];
}
if ($item instanceof ElementSimpleFile) {
$value[] = $item;
}
}
return $value;
}
}