public function ParagraphsItem::prepareRow

File

paragraphs/src/Plugin/migrate/source/d7/ParagraphsItem.php, line 71

Class

ParagraphsItem
Paragraphs Item source plugin.

Namespace

Drupal\paragraphs\Plugin\migrate\source\d7

Code

public function prepareRow(Row $row) {
  [
    'item_id' => $paragraph_id,
    'revision_id' => $paragraph_revision_id,
    'field_name' => $paragraph_parent_field_name,
    'bundle' => $bundle,
  ] = $row
    ->getSource();
  if (!$paragraph_parent_field_name || !is_string($paragraph_parent_field_name)) {
    return FALSE;
  }

  // Get Field API field values.
  foreach (array_keys($this
    ->getFields('paragraphs_item', $bundle)) as $field_name) {
    $row
      ->setSourceProperty($field_name, $this
      ->getFieldValues('paragraphs_item', $field_name, $paragraph_id, $paragraph_revision_id));
  }

  // We have to find the corresponding parent entity (which might be an
  // another paragraph). Active revision only.
  try {
    $parent_data_query = $this
      ->getDatabase()
      ->select(static::PARENT_FIELD_TABLE_PREFIX . $paragraph_parent_field_name, 'fd');
    $parent_data_query
      ->addField('fd', 'entity_type', 'parent_type');
    $parent_data_query
      ->addField('fd', 'entity_id', 'parent_id');
    $parent_data = $parent_data_query
      ->condition("fd.{$paragraph_parent_field_name}_value", $paragraph_id)
      ->condition("fd.{$paragraph_parent_field_name}_revision_id", $paragraph_revision_id)
      ->execute()
      ->fetchAssoc();
  } catch (DatabaseExceptionWrapper $e) {

    // The paragraphs field data|revision table is missing, we cannot get
    // the parent entity identifiers. This is a corrupted database.
    // @todo Shouldn't we have to throw an exception instead?
    return FALSE;
  }
  if (!is_iterable($parent_data)) {

    // We cannot get the parent entity identifiers.
    return FALSE;
  }
  foreach ($parent_data as $property_name => $property_value) {
    $row
      ->setSourceProperty($property_name, $property_value);
  }
  return parent::prepareRow($row);
}