url_resolver = $url_resolver; $this->pageSelector = $pageSelector; $this->blockManager = $blockManager; } public function initRuntime(\Twig_Environment $environment) { $this->environment = $environment; } public function getName() { return 'page_extension'; } public function getGlobals() { return array( 'manager' => $this->pageSelector->retrieve(), 'currentNode' => $this->pageSelector->retrieve()->getCurrentNode(), ); } public function getFunctions() { return array( 'render_breadcrumb' => new \Twig_Function_Method($this, 'renderBreadcrumb', array('is_safe' => array('html'))), 'render_dictionary' => new \Twig_Function_Method($this, 'renderDictionary', array('is_safe' => array('html'))), ); } public function getFilters() { return array( 'transform_url' => new \Twig_Filter_Method($this, 'transformUrl', array('is_safe' => array('html'))), 'transform_link' => new \Twig_Filter_Method($this, 'transformLink', array('is_safe' => array('html'))) ); } public function transformUrl($value) { return preg_replace_callback( '`href=["\']{1}/\{.*\}`', array( $this->url_resolver, 'translate' ), $value ); } public function transformLink($value) { return $this->url_resolver->translate($value, true); } public function renderBreadcrumb(array $addNodes = null, array $params = array(), $template = 'TrinityContentManagerBundle:Utils:breadcrumb.html.twig') { $cms = $this->pageSelector->retrieve(); if (!$cms) { return null; } $currentNode = $cms->getCurrentNode(); $nodes = NodeQuery::create() ->ancestorsOf($currentNode) ->filterByStatus(NodePeer::IS_VISIBLE) ->_or() ->filterByStatus(NodePeer::IS_VISIBLE + NodePeer::IS_ACCESSIBLE) ->orderByLevel() ->find(); if ($currentNode->isVisible() && !$currentNode->layOnDataModels()) { $nodes->append($currentNode); } if ($addNodes) { foreach ($addNodes as $add) { if (!array_key_exists('node', $add)) { continue; } // use a copy of the node avoid conflict when node is re-used $node = $add['node']->copy(); if (array_key_exists('params', $add)) { $node->addDefaultParams($add['params']); } if (array_key_exists('title', $add)) { $node->setTitle($add['title']); } if ($nodes->contains($node)) { $aim = $nodes->search($node); if ($aim) { $nodes[$aim] = $node; } } else { $nodes->append($node); } unset($node); } } return $this->render($template, array('nodes' => $nodes, 'params' => $params)); } public function renderDictionary($page) { return $this->render( 'TrinityContentManagerBundle:Utils:dictionary.html.twig', array( 'dictionary' => $page->getMetaWords(), ) ); } public function render($template, array $parameters = array()) { if (!isset($this->resources[$template])) { $this->resources[$template] = $this->environment->loadTemplate($template); } return $this->resources[$template]->render($parameters); } }