You are here

public function D7FlaggingCollections::prepareRow in Flag Lists 8

Same name and namespace in other branches
  1. 4.0.x src/Plugin/migrate/source/D7FlaggingCollections.php \Drupal\flag_lists\Plugin\migrate\source\D7FlaggingCollections::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/D7FlaggingCollections.php, line 71

Class

D7FlaggingCollections
Minimalistic example for a SqlBase source plugin.

Namespace

Drupal\flag_lists\Plugin\migrate\source

Code

public function prepareRow(Row $row) {

  // Check if the user exists.
  $uid = $row
    ->getSourceProperty('uid');
  $user = User::load($uid);
  if (!empty($user)) {
    $owner = $uid;
  }
  else {

    // Make the Administrator the owner.
    $owner = 1;
  }
  $row
    ->setSourceProperty('uid', $owner);

  // Check if the template flag exist.
  $found = FALSE;
  $flagService = \Drupal::service('flag');
  $templateFlags = $flagService
    ->getAllFlags($row
    ->getSourceProperty('entity_type'));
  foreach ($templateFlags as $flag) {
    if ($found = $flag
      ->get('id') == $row
      ->getSourceProperty('name')) {
      break;
    }
  }
  if (!$found) {
    $message = $this
      ->t('The template flag "@flag" wasn\'t found. Using fallback.', [
      '@flag' => $row
        ->getSourceProperty('name'),
    ]);
    $messenger = \Drupal::messenger();
    $logger = \Drupal::logger('flag_lists');
    $messenger
      ->addWarning($message);
    $logger
      ->warning($message);

    // Fall back to known existing flag.
    $row
      ->setSourceProperty('name', 'flag_list_template_1');
  }
  return parent::prepareRow($row);
}