ParagraphsMigrateParagraphsFieldHandler.inc in Paragraphs 7
File
migrate/fields/ParagraphsMigrateParagraphsFieldHandler.inc
View source
<?php
class ParagraphsMigrateParagraphsFieldHandler extends MigrateSimpleFieldHandler {
public function __construct() {
parent::__construct(array(
'value_key' => 'value',
'skip_empty' => TRUE,
));
$this
->registerTypes(array(
'paragraphs',
));
}
public function fields($type, $instance, $migration = NULL) {
return array(
'revision_id' => t('Option: Provide optional revision id.'),
);
}
protected function notNull($value) {
return !is_null($value) && $value !== FALSE;
}
public function prepare($entity, array $field_info, array $instance, array $values) {
$arguments = array();
if (isset($values['arguments'])) {
$arguments = $values['arguments'];
unset($values['arguments']);
}
$language = $this
->getFieldLanguage($entity, $field_info, $arguments);
if ($this->skipEmpty) {
$values = array_filter($values, array(
$this,
'notNull',
));
}
if (empty($values)) {
return NULL;
}
$revision_ids = $this
->getRevisionIds($values, $arguments);
$delta = 0;
foreach ($values as $value) {
if (is_array($language)) {
$current_language = $language[$delta];
}
else {
$current_language = $language;
}
$return[$current_language][] = array(
$this->fieldValueKey => $value,
'revision_id' => $revision_ids[$delta],
);
$delta++;
}
return isset($return) ? $return : NULL;
}
protected function getRevisionIds(array $values, array $arguments) {
$return = array();
if (!isset($arguments['revision_id'])) {
$arguments['revision_id'] = array();
}
elseif (!is_array($arguments['revision_id'])) {
$arguments['revision_id'] = array(
$arguments['revision_id'],
);
}
$revision_ids = db_select('paragraphs_item', 'p')
->fields('p', array(
'item_id',
'revision_id',
))
->condition('item_id', $values)
->execute()
->fetchAllKeyed();
foreach ($values as $delta => $item_id) {
if (!empty($arguments['revision_id'][$delta])) {
$return[$delta] = $arguments['revision_id'][$delta];
}
else {
$return[$delta] = $revision_ids[$item_id];
}
}
return $return;
}
}