UrlEncode.php in Drupal 10
File
core/modules/migrate/src/Plugin/migrate/process/UrlEncode.php
View source
<?php
namespace Drupal\migrate\Plugin\migrate\process;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\MigrateException;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use GuzzleHttp\Psr7\Uri;
class UrlEncode extends ProcessPluginBase {
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (is_string($value) && strpos($value, '://') > 0) {
$parsed_url = parse_url($value);
if ($parsed_url === FALSE) {
throw new MigrateException("Value '{$value}' is not a valid URL");
}
$url_parts_to_encode = [
'path',
'query',
'fragment',
];
foreach ($parsed_url as $parsed_url_key => $parsed_url_value) {
if (in_array($parsed_url_key, $url_parts_to_encode)) {
$urlencoded_parsed_url_value = rawurlencode($parsed_url_value);
switch ($parsed_url_key) {
case 'query':
$urlencoded_parsed_url_value = str_replace('%26', '&', $urlencoded_parsed_url_value);
break;
case 'path':
$urlencoded_parsed_url_value = str_replace('%2F', '/', $urlencoded_parsed_url_value);
break;
}
$parsed_url[$parsed_url_key] = $urlencoded_parsed_url_value;
}
}
$value = (string) Uri::fromParts($parsed_url);
}
return $value;
}
}