View source
<?php
namespace Drupal\recipe\Plugin\migrate\source\recipe61;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
class Recipe61 extends DrupalSqlBase {
const CHANGED_UNITS = [
'pint' => 'us liquid pint',
'quart' => 'us liquid quart',
'gallon' => 'us gallon',
'tablespoon (metric)' => 'tablespoon',
'metric tablespoon' => 'tablespoon',
'teaspoon (metric)' => 'teaspoon',
'metric teaspoon' => 'teaspoon',
'millilitre' => 'milliliter',
'centilitre' => 'centiliter',
'decilitre' => 'decilitre',
'litre' => 'liter',
];
public function query() {
$query = $this
->select('recipe', 'r')
->fields('r')
->orderBy('r.nid');
$query
->join('node', 'n', 'r.nid = n.nid');
$query
->fields('n', [
'tnid',
'language',
]);
$this
->handleTranslations($query);
return $query;
}
public function prepareRow(Row $row) {
$query = $this
->select('recipe_node_ingredient', 'i');
$query
->leftJoin('recipe_unit', 'ru', 'i.unit_id = ru.id');
$query
->fields('i', [
'quantity',
'ingredient_id',
'weight',
'note',
])
->fields('ru', [
'name',
])
->condition('nid', $row
->getSourceProperty('nid'))
->orderBy('weight', 'ASC');
$results = $query
->execute();
$ingredients = [];
foreach ($results as $result) {
$result['name'] = strtolower($result['name']);
$result['unit_key'] = isset(self::CHANGED_UNITS[$result['name']]) ? self::CHANGED_UNITS[$result['name']] : $result['name'];
$ingredients[] = $result;
}
$row
->setSourceProperty('ingredients', $ingredients);
if ($row
->getSourceProperty('tnid') == 0) {
$row
->setSourceProperty('tnid', $row
->getSourceProperty('nid'));
}
return parent::prepareRow($row);
}
public function fields() {
return [
'nid' => $this
->t('Recipe node ID'),
'tnid' => $this
->t('The translation node ID'),
'source' => $this
->t('Recipe source'),
'yield' => $this
->t('Recipe yield amount'),
'yield_unit' => $this
->t('Units of the recipe yield'),
'description' => $this
->t('Recipe description'),
'instructions' => $this
->t('Recipe instructions'),
'notes' => $this
->t('Recipe notes'),
'preptime' => $this
->t('Recipe preparation time'),
'cooktime' => $this
->t('Recipe cook time'),
'ingredients' => $this
->t('Recipe ingredients, measures, and notes'),
'language' => $this
->t('Node language'),
];
}
public function getIds() {
return [
'nid' => [
'type' => 'integer',
'alias' => 'r',
],
];
}
protected function handleTranslations(SelectInterface $query) {
if (empty($this->configuration['translations'])) {
$query
->where('n.tnid = 0 OR n.tnid = n.nid');
}
else {
$query
->where('n.tnid <> 0 AND n.tnid <> n.nid');
}
}
}