Files
cdesai-migration/src/Plugin/migrate/process/ExtractFileContents.php
T

55 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace Drupal\cdesai_migrate\Plugin\migrate\process;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Row;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drush\Drush;
/**
* Provides a 'Extract File Contents' migrate process plugin.
*
* Extracts contents of a file.
*
* @MigrateProcessPlugin(
* id = "extract_file_contents"
* )
*/
class ExtractFileContents extends ProcessPluginBase {
/**
* {@inheritdoc}
*
* @throws \Drupal\migrate\MigrateSkipRowException
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
$error = FALSE;
$page = $row->get('page');
if (is_null($value) || trim($value) === '') {
$error = "Skipping because content filepath is empty for {$page}.";
}
elseif (!is_file($value)) {
$error = "Skipping because content file is missing for {$page}.";
}
else {
$contents = file_get_contents($value);
if ($contents !== FALSE) {
return $contents;
}
$error = "Skipping because content file is unreadable for {$page}.";
}
if ($error) {
Drush::output()->writeln($error);
throw new MigrateSkipRowException($error);
}
return '';
}
}