View source
<?php
namespace Drupal\migrate_external_translated_test\Plugin\migrate\source;
use Drupal\migrate\Plugin\migrate\source\SourcePluginBase;
class MigrateExternalTranslatedTestSource extends SourcePluginBase {
protected $import = [
[
'name' => 'cat',
'title' => 'Cat',
'lang' => 'English',
],
[
'name' => 'cat',
'title' => 'Chat',
'lang' => 'French',
],
[
'name' => 'cat',
'title' => 'Gato',
'lang' => 'Spanish',
],
[
'name' => 'dog',
'title' => 'Dog',
'lang' => 'English',
],
[
'name' => 'dog',
'title' => 'Chien',
'lang' => 'French',
],
[
'name' => 'monkey',
'title' => 'Monkey',
'lang' => 'English',
],
];
public function fields() {
return [
'name' => $this
->t('Unique name'),
'title' => $this
->t('Title'),
'lang' => $this
->t('Language'),
];
}
public function __toString() {
return '';
}
public function getIds() {
$ids['name']['type'] = 'string';
if (!$this->configuration['default_lang']) {
$ids['lang']['type'] = 'string';
}
return $ids;
}
protected function initializeIterator() {
$data = [];
$want_default = $this->configuration['default_lang'];
foreach ($this->import as $row) {
$is_english = $row['lang'] == 'English';
if ($want_default == $is_english) {
$data[] = $row;
}
}
return new \ArrayIterator($data);
}
}