1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\View\Layout\Reader;
use Magento\Framework\View\Layout;
class Move implements Layout\ReaderInterface
{
/**#@+
* Supported types
*/
const TYPE_MOVE = 'move';
/**#@-*/
/**
* {@inheritdoc}
*
* @return string[]
*/
public function getSupportedNodes()
{
return [self::TYPE_MOVE];
}
/**
* {@inheritdoc}
*
* @param Context $readerContext
* @param Layout\Element $currentElement
* @return $this
*/
public function interpret(Context $readerContext, Layout\Element $currentElement)
{
$this->scheduleMove($readerContext->getScheduledStructure(), $currentElement);
return $this;
}
/**
* Schedule structural changes for move directive
*
* @param \Magento\Framework\View\Layout\ScheduledStructure $scheduledStructure
* @param \Magento\Framework\View\Layout\Element $currentElement
* @throws \Magento\Framework\Exception\LocalizedException
* @return $this
*/
protected function scheduleMove(Layout\ScheduledStructure $scheduledStructure, Layout\Element $currentElement)
{
$elementName = (string)$currentElement->getAttribute('element');
$destination = (string)$currentElement->getAttribute('destination');
$alias = (string)$currentElement->getAttribute('as') ?: '';
if ($elementName && $destination) {
list($siblingName, $isAfter) = $this->beforeAfterToSibling($currentElement);
$scheduledStructure->setElementToMove(
$elementName,
[$destination, $siblingName, $isAfter, $alias]
);
} else {
throw new \Magento\Framework\Exception\LocalizedException(
new \Magento\Framework\Phrase('Element name and destination must be specified.')
);
}
return $this;
}
/**
* Analyze "before" and "after" information in the node and return sibling name and whether "after" or "before"
*
* @param \Magento\Framework\View\Layout\Element $node
* @return array
*/
protected function beforeAfterToSibling($node)
{
$result = [null, true];
if (isset($node['after'])) {
$result[0] = (string)$node['after'];
} elseif (isset($node['before'])) {
$result[0] = (string)$node['before'];
$result[1] = false;
}
return $result;
}
}