Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Plugin / DMU / Converter / Links.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Plugin\DMU\Converter;
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\Core\StringTranslation\TranslationInterface;
7 use Drupal\drupalmoduleupgrader\ConverterBase;
8 use Drupal\drupalmoduleupgrader\Routing\HookMenu;
9 use Drupal\drupalmoduleupgrader\Routing\LinkBinding\LinkBinding;
10 use Drupal\drupalmoduleupgrader\Routing\LinkBinding\LinkBindingFactory;
11 use Drupal\drupalmoduleupgrader\Routing\LinkBinding\LocalActionLinkBinding;
12 use Drupal\drupalmoduleupgrader\Routing\LinkBinding\LocalTaskLinkBinding;
13 use Drupal\drupalmoduleupgrader\Routing\LinkBinding\MenuLinkBinding;
14 use Drupal\drupalmoduleupgrader\Routing\LinkIndex;
15 use Drupal\drupalmoduleupgrader\TargetInterface;
16 use Drupal\drupalmoduleupgrader\Utility\Filter\ContainsLogicFilter;
17 use Pharborist\DocCommentNode;
18 use Psr\Log\LoggerInterface;
19
20 /**
21  * @Converter(
22  *  id = "links",
23  *  description = @Translation("Converts Drupal 7's hook_menu() links to plugin definitions."),
24  *  hook = "hook_menu",
25  *  fixme = @Translation("@FIXME
26 This implementation of hook_menu() cannot be automatically converted because
27 it contains logic (i.e., branching statements, function calls, object
28 instantiation, etc.) You will need to convert it manually. Sorry!
29
30 For more information on how to convert hook_menu() to Drupal 8's new routing
31 and linking systems, see https://api.drupal.org/api/drupal/core%21includes%21menu.inc/group/menu/8"),
32  *  dependencies = { "plugin.manager.drupalmoduleupgrader.route", "drupalmoduleupgrader.link_binding" }
33  * )
34  */
35 class Links extends ConverterBase {
36
37   /**
38    * @var PluginManagerInterface
39    */
40   protected $routeConverters;
41
42   /**
43    * @var LinkBindingFactory
44    */
45   protected $linkBinding;
46
47   /**
48    * Constructs a Links object.
49    *
50    * @param array $configuration
51    *   Additional configuration for the plugin.
52    * @param string $plugin_id
53    *   The plugin ID, will be "Links".
54    * @param string $plugin_definition
55    *   The plugin definition as derived from the annotations.
56    * @param \Drupal\Component\Plugin\PluginManagerInterface $route_converters
57    *  The plugin manager for route converters, used by HookMenu.
58    */
59   public function __construct(array $configuration, $plugin_id, $plugin_definition, TranslationInterface $translator, LoggerInterface $log, PluginManagerInterface $route_converters, LinkBindingFactory $link_binding) {
60     parent::__construct($configuration, $plugin_id, $plugin_definition, $translator, $log);
61     $this->routeConverters = $route_converters;
62     $this->linkBinding = $link_binding;
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function convert(TargetInterface $target) {
69     // If the hook implementation contains logic, we cannot convert it and
70     // that's that. So we'll leave a FIXME and bail out.
71     /** @var \Pharborist\Functions\FunctionDeclarationNode $hook */
72     $hook = $target->getIndexer('function')->get('hook_menu');
73     if ($hook->is(new ContainsLogicFilter)) {
74       $hook->setDocComment(DocCommentNode::create($this->pluginDefinition['fixme']));
75       $target->save($hook);
76       return;
77     }
78
79     // Links are split out by group because there are separate config files
80     // for each link type.
81     $links = [
82       'menu' => new LinkIndex(),
83       'task' => new LinkIndex(),
84       'action' => new LinkIndex(),
85       'contextual' => new LinkIndex(),
86     ];
87
88     $hook_menu = new HookMenu($target, $this->routeConverters);
89     foreach ($hook_menu->getSourceRoutes()->getAllLinks() as $path => $source) {
90       /** @var LinkBinding $binding */
91       $binding = $this->linkBinding->create($source, $hook_menu->getDestinationRoute($path));
92
93       // Skip if the converter wasn't able to find a destination.
94       $destination = $binding->getDestination();
95       if (empty($destination)) {
96         continue;
97       }
98
99       if ($binding instanceof MenuLinkBinding) {
100         $links['menu']->addBinding($binding);
101       }
102       elseif ($binding instanceof LocalTaskLinkBinding) {
103         $links['task']->addBinding($binding);
104       }
105       elseif ($binding instanceof LocalActionLinkBinding) {
106         $links['action']->addBinding($binding);
107       }
108       elseif ($source->isContextualLink()) {
109         $links['contextual']->addBinding($binding);
110       }
111     }
112
113     $links = array_map(function(LinkIndex $index) {
114       return $index->build();
115     }, $links);
116
117     foreach ($links['contextual'] as $link) {
118       $link['group'] = $target->id();
119     }
120
121     foreach ($links as $group => $data) {
122       if ($data) {
123         $this->writeInfo($target, 'links.' . $group, $data);
124       }
125     }
126   }
127
128 }