Skip to content

Instantly share code, notes, and snippets.

@sorenmalling
Created June 3, 2015 14:13
Show Gist options
  • Select an option

  • Save sorenmalling/b38bd88b15baf266532d to your computer and use it in GitHub Desktop.

Select an option

Save sorenmalling/b38bd88b15baf266532d to your computer and use it in GitHub Desktop.
A link viewhelper that can add a "active" class to a link
<?php
namespace Vendor\Namespace\ViewHelpers\Navigation;
use TYPO3\Fluid\ViewHelpers\Link\ActionViewHelper;
use TYPO3\Flow\Mvc\ActionRequest;
class ItemViewHelper extends ActionViewHelper {
public function render($action, $arguments = array(), $controller = NULL, $package = NULL, $subpackage = NULL, $section = '', $format = '', array $additionalParams = array(), $addQueryString = FALSE, array $argumentsToBeExcludedFromQueryString = array(), $useParentRequest = FALSE, $absolute = TRUE) {
if ($action === NULL || $controller === NULL || $package === NULL) {
throw new \Exception('I need action, controller and package arguments');
}
/** @var ActionRequest $request */
$request = $this->controllerContext->getRequest();
if (/*$action === $request->getControllerActionName() &&*/ $controller === $request->getControllerName() && $package === $request->getControllerPackageKey()) {
if ($this->tag->hasAttribute('class') === TRUE) {
$classAttribute = 'active ' . $this->tag->getAttribute('class');
$this->tag->removeAttribute('class');
} else {
$classAttribute = 'active';
}
$this->tag->addAttribute('class', $classAttribute);
}
return parent::render($action, $arguments, $controller, $package, $subpackage, $section, $format, $additionalParams, $addQueryString, $argumentsToBeExcludedFromQueryString, $useParentRequest, $absolute); // TODO: Change the autogenerated stub
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment