LinkUri.php in Drupal 8
File
core/modules/menu_link_content/src/Plugin/migrate/process/LinkUri.php
View source
<?php
namespace Drupal\menu_link_content\Plugin\migrate\process;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Url;
use Drupal\migrate\MigrateException;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LinkUri extends ProcessPluginBase implements ContainerFactoryPluginInterface {
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) {
$configuration += [
'validate_route' => TRUE,
];
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'));
}
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (is_array($value)) {
$value = reset($value);
@trigger_error('Passing an array as source value into the link_uri migrate process plugin is deprecated in drupal:8.8.0. The possibility to pass an array as source value to the plugin will be removed in drupal:9.0.0. Pass a string value instead. See https://www.drupal.org/node/3043694', E_USER_DEPRECATED);
}
$path = ltrim($value, '/');
if (parse_url($path, PHP_URL_SCHEME) === NULL) {
if ($path == '<front>') {
$path = '';
}
elseif ($path == '<nolink>') {
return 'route:<nolink>';
}
$path = 'internal:/' . $path;
$url = Url::fromUri($path);
if ($url
->isRouted()) {
$route_name = $url
->getRouteName();
foreach (array_keys($this->entityTypeManager
->getDefinitions()) as $entity_type_id) {
if ($route_name == "entity.{$entity_type_id}.canonical" && isset($url
->getRouteParameters()[$entity_type_id])) {
return "entity:{$entity_type_id}/" . $url
->getRouteParameters()[$entity_type_id];
}
}
}
else {
if (!$this->configuration['validate_route']) {
return $url
->getUri();
}
else {
throw new MigrateException(sprintf('The path "%s" failed validation.', $path));
}
}
}
return $path;
}
}
Classes
Name |
Description |
LinkUri |
Generates an internal URI from the source value. |