Created
March 31, 2017 12:43
-
-
Save bwaidelich/9818d7ee2e40c3185aad0cd431ed7166 to your computer and use it in GitHub Desktop.
Simple ViewHelper to create facet filter links (without filling up the Routing Cache) Raw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Wwwision\Test\ViewHelpers\Link; | |
use Neos\FluidAdaptor\Core\ViewHelper\AbstractTagBasedViewHelper; | |
use Neos\FluidAdaptor\Core\ViewHelper; | |
/** | |
* Simple ViewHelper to create facet filter links | |
* | |
* Usage: | |
* <x:link.filter facetName="someFacet" facetValue="someValue" /> | |
*/ | |
class FacetViewHelper extends AbstractTagBasedViewHelper | |
{ | |
/** | |
* @var string | |
*/ | |
protected $tagName = 'a'; | |
/** | |
* @return void | |
*/ | |
public function initializeArguments() | |
{ | |
$this->registerUniversalTagAttributes(); | |
$this->registerTagAttribute('name', 'string', 'Specifies the name of an anchor'); | |
$this->registerTagAttribute('rel', 'string', 'Specifies the relationship between the current document and the linked document'); | |
$this->registerTagAttribute('rev', 'string', 'Specifies the relationship between the linked document and the current document'); | |
$this->registerTagAttribute('target', 'string', 'Specifies where to open the linked document'); | |
} | |
/** | |
* @param string $facetName | |
* @param string $facetValue | |
* @return string | |
* @throws ViewHelper\Exception if the URI could not be resolved | |
*/ | |
public function render(string $facetName, string $facetValue) | |
{ | |
$uriBuilder = $this->controllerContext->getUriBuilder(); | |
$uriBuilder->reset(); | |
try { | |
// Adjust those values | |
$uri = $uriBuilder->uriFor('facetAction', [], 'controllerName', 'packageKey'); | |
} catch (\Exception $exception) { | |
throw new ViewHelper\Exception($exception->getMessage(), $exception->getCode(), $exception); | |
} | |
$uri .= '?' . htmlspecialchars($facetName) . '=' . htmlspecialchars($facetValue); | |
$this->tag->addAttribute('href', $uri); | |
$content = $this->renderChildren(); | |
if ($content === null) { | |
$content = $facetValue; | |
} | |
$this->tag->setContent($content); | |
$this->tag->forceClosingTag(true); | |
return $this->tag->render(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment