You are here

public function D7FlagListItems::prepareRow in Flag Lists 8

Same name and namespace in other branches
  1. 4.0.x src/Plugin/migrate/source/D7FlagListItems.php \Drupal\flag_lists\Plugin\migrate\source\D7FlagListItems::prepareRow()

Adds additional data to the row.

Parameters

\Drupal\migrate\Row $row: The row object.

Return value

bool FALSE if this row needs to be skipped.

Overrides SourcePluginBase::prepareRow

File

src/Plugin/migrate/source/D7FlagListItems.php, line 73

Class

D7FlagListItems
Minimalistic example for a SqlBase source plugin.

Namespace

Drupal\flag_lists\Plugin\migrate\source

Code

public function prepareRow(Row $row) {
  $messenger = \Drupal::messenger();
  $logger = \Drupal::logger('flag_lists');

  // Check and get the user name.
  $uid = $row
    ->getSourceProperty('uid');
  $user = User::load($uid);
  if (!empty($user)) {
    $owner = $uid;
  }
  else {
    $owner = 1;
  }
  $row
    ->setSourceProperty('uid', $owner);

  // Check if the flagging collection exist.
  $found = FALSE;
  $flagListsService = \Drupal::service('flaglists');
  $baseFlags = $flagListsService
    ->getAllFlaggingCollections();
  foreach ($baseFlags as $flag) {
    if ($found = $flag
      ->get('id')->value == $row
      ->getSourceProperty('fid')) {
      $relatedFlag = $flag
        ->get('relatedflag')
        ->getValue();
      $relatedFlagList = $relatedFlag['0']['target_id'];
      $row
        ->setSourceProperty('relatedflag', $relatedFlagList);
      break;
    }
  }
  if (!$found) {
    $message = $this
      ->t('The flagging collection "@collection" wasn\'t found');
    $messenger
      ->addError($message, [
      '@collection' => $row
        ->getSourceProperty('relatedflag'),
    ]);
    $logger
      ->error($message, [
      '@collection' => $row
        ->getSourceProperty('relatedflag'),
    ]);
  }

  // Check if the entity exists.
  $entity_id = $row
    ->getSourceProperty('entity_id');
  $entity = \Drupal::entityTypeManager()
    ->getStorage($row
    ->getSourceProperty('entity_type'))
    ->load($entity_id);
  if (empty($entity)) {
    $message = $this
      ->t('The entity with ID "@entity_id" wasn\'t found', [
      '@entity_id' => $entity_id,
    ]);
    $messenger
      ->addError($message);
    $logger
      ->error($message);
  }
  return parent::prepareRow($row);
}