Further modules included.
[yaffs-website] / web / modules / contrib / drupalmoduleupgrader / src / Routing / LinkBinding / LocalTaskLinkBinding.php
1 <?php
2
3 namespace Drupal\drupalmoduleupgrader\Routing\LinkBinding;
4
5 use Drupal\Component\Plugin\PluginManagerInterface;
6 use Drupal\drupalmoduleupgrader\Routing\Drupal7\RouteWrapper as Drupal7Route;
7 use Drupal\drupalmoduleupgrader\Routing\Drupal8\RouteWrapper as Drupal8Route;
8
9 /**
10  * Represents a local task or default local task.
11  */
12 class LocalTaskLinkBinding extends LinkBinding {
13
14   /**
15    * @var PluginManagerInterface
16    */
17   private $linkManager;
18
19   /**
20    * Constructs a LinkBinding object.
21    */
22   public function __construct(Drupal7Route $source, Drupal8Route $destination, PluginManagerInterface $link_manager) {
23     parent::__construct($source, $destination);
24     $this->linkManager = $link_manager;
25   }
26
27   /**
28    * {@inheritdoc}
29    */
30   public function build() {
31     $link = parent::build();
32
33     $source = $this->getSource();
34
35     if ($source->isDefaultLocalTask()) {
36       $link['base_route'] = $link['route_name'];
37     }
38     elseif ($source->isLocalTask()) {
39       $default_task = $source->getDefaultTask();
40       if ($default_task) {
41         $path = $default_task->getPath()->__toString();
42
43         if ($this->index->containsKey($path)) {
44           $link['base_route'] = $this->index[$path]->getDestination()->getIdentifier();
45         }
46       }
47     }
48
49     if ($source->hasParent()) {
50       $parent = $source->getParent();
51
52       if ($parent->isLocalTask() || $parent->isDefaultLocalTask()) {
53         $parent_id = $this->getParentID();
54
55         if ($parent_id) {
56           unset($link['base_route']);
57           $link['parent_id'] = $parent_id;
58         }
59       }
60     }
61
62     return $link;
63   }
64
65   /**
66    * Gets the parent task's link ID, if any.
67    *
68    * @return string|NULL
69    */
70   public function getParentID() {
71     $path = $this->getSource()->getParent()->getPath()->__toString();
72
73     if ($this->index->containsKey($path)) {
74       return $this->index[$path]->getIdentifier();
75     }
76
77     $parent = $this->getDestination()->getParent()->getIdentifier();
78
79     foreach ($this->linkManager->getDefinitions() as $id => $link) {
80       if ($link['route_name'] == $parent) {
81         return $id;
82       }
83     }
84   }
85
86 }