Skip to content

Instantly share code, notes, and snippets.

@Unifex
Last active June 18, 2018 22:39
Show Gist options
  • Save Unifex/0c6080c5e9b3d942d9f5e0d2a1a4ad8f to your computer and use it in GitHub Desktop.
Save Unifex/0c6080c5e9b3d942d9f5e0d2a1a4ad8f to your computer and use it in GitHub Desktop.
For... reasons I was working with bad data and a migration into Drupal 8 had me in a situation where I couldn't trust that the source file URL was actually present. The following is a migration process plugin that will check the URL and skip the row if it is a 404.
<?php
namespace Drupal\migrate_custom\Plugin\migrate\process;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateSkipRowException;
use Drupal\migrate\Row;
use Drupal\Component\Utility\UrlHelper;
/**
* Provides a process plugin that checks if a URL is a 404 and skips the row if so.
*
* @MigrateProcessPlugin(
* id = "skip_on_404"
* )
*/
class SkipOn404 extends ProcessPluginBase {
/**
* Stops processing the current property when value is not set.
*
* @param mixed $value
* The input value. If an array the first item is assumed to be the URL we are checking.
* @param \Drupal\migrate\MigrateExecutableInterface $migrate_executable
* The migration in which this process is being executed.
* @param \Drupal\migrate\Row $row
* The row from the source to process.
* @param string $destination_property
* The destination property currently worked on. This is only used together
* with the $row above.
*
* @return mixed
* The input value, $value, if the test was not a 404.
*
* @throws \Drupal\migrate\MigrateSkipRowException
* Thrown if the source property is not set and the row should be skipped,
* records with STATUS_IGNORED status in the map.
*/
public function row($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (is_array($value)) {
$url = $value[0];
}
else {
$url = $value;
}
if (!UrlHelper::isValid($url, TRUE)) {
$message = !empty($this->configuration['message']) ? $this->configuration['message'] : 'SkipOn404: invalid URL';
throw new MigrateSkipRowException($message);
}
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
$response = curl_exec($handle);
$http_code = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if ($http_code == 404) {
$message = !empty($this->configuration['message']) ? $this->configuration['message'] : 'SkipOn404: ' . $url;
throw new MigrateSkipRowException($message);
}
return $value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment