PageRenderTime 74ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/core/modules/migrate/src/Plugin/migrate/process/MenuLinkParent.php

http://github.com/drupal/drupal
PHP | 177 lines | 94 code | 15 blank | 68 comment | 14 complexity | 37779fb3acc664aca4aa407d86b1fcb7 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\migrate\Plugin\migrate\process;
  3. use Drupal\Component\Utility\UrlHelper;
  4. use Drupal\Core\Entity\EntityStorageInterface;
  5. use Drupal\Core\Menu\MenuLinkManagerInterface;
  6. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  7. use Drupal\Core\Url;
  8. use Drupal\migrate\MigrateLookupInterface;
  9. use Drupal\migrate\Plugin\MigrationInterface;
  10. use Drupal\migrate\MigrateExecutableInterface;
  11. use Drupal\migrate\MigrateSkipRowException;
  12. use Drupal\migrate\Plugin\MigrateProcessInterface;
  13. use Drupal\migrate\ProcessPluginBase;
  14. use Drupal\migrate\Row;
  15. use Symfony\Component\DependencyInjection\ContainerInterface;
  16. /**
  17. * This plugin figures out menu link parent plugin IDs.
  18. *
  19. * @MigrateProcessPlugin(
  20. * id = "menu_link_parent"
  21. * )
  22. */
  23. class MenuLinkParent extends ProcessPluginBase implements ContainerFactoryPluginInterface {
  24. /**
  25. * @var \Drupal\Core\Menu\MenuLinkManagerInterface
  26. */
  27. protected $menuLinkManager;
  28. /**
  29. * The Migration process plugin.
  30. *
  31. * @var \Drupal\migrate\Plugin\MigrateProcessInterface
  32. *
  33. * @deprecated in drupal:8.8.x and is removed from drupal:9.0.0. Use
  34. * the migrate.lookup service instead.
  35. *
  36. * @see https://www.drupal.org/node/3047268
  37. */
  38. protected $migrationPlugin;
  39. /**
  40. * The currently running migration.
  41. *
  42. * @var \Drupal\migrate\Plugin\MigrationInterface
  43. */
  44. protected $migration;
  45. /**
  46. * The migrate lookup service.
  47. *
  48. * @var \Drupal\migrate\MigrateLookupInterface
  49. */
  50. protected $migrateLookup;
  51. /**
  52. * @var \Drupal\Core\Entity\EntityStorageInterface
  53. */
  54. protected $menuLinkStorage;
  55. /**
  56. * Constructs a MenuLinkParent object.
  57. *
  58. * @param array $configuration
  59. * A configuration array containing information about the plugin instance.
  60. * @param string $plugin_id
  61. * The plugin_id for the plugin instance.
  62. * @param mixed $plugin_definition
  63. * The plugin implementation definition.
  64. * @param \Drupal\migrate\MigrateLookupInterface $migrate_lookup
  65. * The migrate lookup service.
  66. * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
  67. * The menu link manager.
  68. * @param \Drupal\Core\Entity\EntityStorageInterface $menu_link_storage
  69. * The menu link storage object.
  70. * @param \Drupal\migrate\Plugin\MigrationInterface $migration
  71. * The currently running migration.
  72. */
  73. // @codingStandardsIgnoreLine
  74. public function __construct(array $configuration, $plugin_id, $plugin_definition, $migrate_lookup, MenuLinkManagerInterface $menu_link_manager, EntityStorageInterface $menu_link_storage, MigrationInterface $migration = NULL) {
  75. parent::__construct($configuration, $plugin_id, $plugin_definition);
  76. if ($migrate_lookup instanceof MigrateProcessInterface) {
  77. @trigger_error('Passing a migration process plugin as the fourth argument to ' . __METHOD__ . ' is deprecated in drupal:8.8.0 and will throw an error in drupal:9.0.0. Pass the migrate.lookup service instead. See https://www.drupal.org/node/3047268', E_USER_DEPRECATED);
  78. $this->migrationPlugin = $migrate_lookup;
  79. $migrate_lookup = \Drupal::service('migrate.lookup');
  80. }
  81. elseif (!$migrate_lookup instanceof MigrateLookupInterface) {
  82. throw new \InvalidArgumentException("The fourth argument to " . __METHOD__ . " must be an instance of MigrateLookupInterface.");
  83. }
  84. elseif (!$migration instanceof MigrationInterface) {
  85. throw new \InvalidArgumentException("The seventh argument to " . __METHOD__ . " must be an instance of MigrationInterface.");
  86. }
  87. $this->migration = $migration;
  88. $this->migrateLookup = $migrate_lookup;
  89. $this->menuLinkManager = $menu_link_manager;
  90. $this->menuLinkStorage = $menu_link_storage;
  91. }
  92. /**
  93. * {@inheritdoc}
  94. */
  95. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
  96. $migration_configuration['migration'][] = $migration->id();
  97. return new static(
  98. $configuration,
  99. $plugin_id,
  100. $plugin_definition,
  101. $container->get('migrate.lookup'),
  102. $container->get('plugin.manager.menu.link'),
  103. $container->get('entity_type.manager')->getStorage('menu_link_content'),
  104. $migration
  105. );
  106. }
  107. /**
  108. * {@inheritdoc}
  109. *
  110. * Find the parent link GUID.
  111. */
  112. public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
  113. $parent_id = array_shift($value);
  114. if (!$parent_id) {
  115. // Top level item.
  116. return '';
  117. }
  118. // This BC layer is included because if the plugin constructor was called
  119. // in the legacy way with a migration_lookup process plugin, it may have
  120. // been preconfigured with a different migration to look up against. While
  121. // this is unlikely, for maximum BC we will continue to use the plugin to do
  122. // the lookup if it is provided, and support for this will be removed in
  123. // Drupal 9.
  124. if ($this->migrationPlugin) {
  125. try {
  126. $already_migrated_id = $this
  127. ->migrationPlugin
  128. ->transform($parent_id, $migrate_executable, $row, $destination_property);
  129. }
  130. catch (MigrateSkipRowException $e) {
  131. }
  132. }
  133. else {
  134. $lookup_result = $this->migrateLookup->lookup($this->migration->id(), [$parent_id]);
  135. if ($lookup_result) {
  136. $already_migrated_id = $lookup_result[0]['id'];
  137. }
  138. }
  139. if (!empty($already_migrated_id) && ($link = $this->menuLinkStorage->load($already_migrated_id))) {
  140. return $link->getPluginId();
  141. }
  142. if (isset($value[1])) {
  143. list($menu_name, $parent_link_path) = $value;
  144. $links = [];
  145. if (UrlHelper::isExternal($parent_link_path)) {
  146. $links = $this->menuLinkStorage->loadByProperties(['link__uri' => $parent_link_path]);
  147. }
  148. else {
  149. $url = Url::fromUserInput("/$parent_link_path");
  150. if ($url->isRouted()) {
  151. $links = $this->menuLinkManager->loadLinksByRoute($url->getRouteName(), $url->getRouteParameters(), $menu_name);
  152. }
  153. }
  154. if (count($links) == 1) {
  155. /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
  156. $link = reset($links);
  157. return $link->getPluginId();
  158. }
  159. }
  160. throw new MigrateSkipRowException(sprintf("No parent link found for plid '%d' in menu '%s'.", $parent_id, $value[0]));
  161. }
  162. }