/
AppendHTMLModifier.php
63 lines (52 loc) · 1.84 KB
/
AppendHTMLModifier.php
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
<?php
namespace Concrete\Core\Navigation\Modifier;
use Concrete\Core\Html\Service\Navigation;
use Concrete\Core\Navigation\Item\PageItem;
use Concrete\Core\Navigation\Modifier\Traits\GetPageItemFromNavigationTrait;
use Concrete\Core\Navigation\NavigationInterface;
use Concrete\Core\Page\Page;
class AppendHTMLModifier implements ModifierInterface
{
use GetPageItemFromNavigationTrait;
/**
* @var Page
*/
protected $currentPage;
/**
* @var Navigation
*/
protected $navigationService;
public function __construct(Navigation $navigationService, Page $currentPage)
{
$this->navigationService = $navigationService;
//$this->currentPage = $currentPage;
$c = Page::getCurrentPage();
$this->currentPage = $c;
}
protected function removeUnrelatedSectionsFromNavigation(array $items, array $sectionIDs)
{
foreach($items as $item) {
/**
* @var $item PageItem
*/
if ($item->getPageID() == $this->currentPage->getCollectionID()) {
$item->setIsActive(true);
} else if (in_array($item->getPageID(), $sectionIDs)) {
$item->setIsActiveParent(true);
} else {
//$item->setChildren([]);
}
$this->removeUnrelatedSectionsFromNavigation($item->getChildren(), $sectionIDs);
}
}
protected function getSectionIDs(): array
{
$parents = array_reverse($this->navigationService->getTrailToCollection($this->currentPage));
$sectionIDs = array_map(function($page) { return $page->getCollectionID(); }, $parents);
return $sectionIDs;
}
public function modify(NavigationInterface $navigation)
{
$this->removeUnrelatedSectionsFromNavigation($navigation->getItems(), $this->getSectionIDs());
}
}