View source
<?php
namespace Drupal\customfilter\Plugin\migrate\source;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
class CustomFilterMigrationSource extends DrupalSqlBase {
public function fields() {
return [
'fid' => $this
->t('The custom filter internal ID'),
'type' => $this
->t('The custom filter machine name'),
'name' => $this
->t('The custom filter human name'),
'description' => $this
->t('Description'),
'shorttip' => $this
->t('Short tip'),
'longtip' => $this
->t('Long tip'),
'cache' => $this
->t('Cacheability'),
];
}
public function getIds() {
return [
'fid' => [
'type' => 'integer',
],
'type' => [
'type' => 'string',
'max_length' => 64,
'is_ascii' => TRUE,
],
];
}
public function query() {
$query = $this
->select('customfilter_filter', 'f');
$query
->fields('f', [
'fid',
'type',
'name',
'cache',
'description',
'shorttip',
'longtip',
]);
$query
->orderBy('f.fid');
return $query;
}
public function prepareRow(Row $row) {
$result = parent::prepareRow($row);
$query = $this
->select('customfilter_rule', 'r');
$query
->join('customfilter_filter', 'f', 'f.fid = r .fid');
$query
->fields('r', [
'rid',
'fid',
'prid',
'name',
'description',
'enabled',
'matches',
'pattern',
'replacement',
'code',
'weight',
]);
$query
->fields('f', [
'type',
]);
$query
->condition('r.fid', $row
->getSourceProperty('fid'));
$rules = $query
->execute()
->fetchAllAssoc('rid');
foreach ($rules as &$rule) {
$rule['rid'] = 'rule_' . $rule['rid'];
$rule['prid'] = $rule['prid'] ? 'rule_' . $rule['prid'] : '';
}
$row
->setSourceProperty('rules', $rules);
return $result;
}
}