code quality improvements + add boolean in a ctive link helper to check if url is active without displaying link

This commit is contained in:
2026-03-17 02:01:07 -07:00
parent 4f996eb7d2
commit 924734b8a4
5 changed files with 22 additions and 15 deletions

View File

@@ -5,12 +5,15 @@ namespace CheeseCake\View\Helper;
use Cake\Routing\Router;
use Cake\View\Helper;
use Cake\View\Helper\HtmlHelper;
/**
* ActiveLink helper
*
* @property HtmlHelper $Html
*/
class ActiveLinkHelper extends Helper {
/**
* Default configuration.
*
@@ -42,12 +45,12 @@ class ActiveLinkHelper extends Helper {
if (!array_key_exists('target', $options) || !$currentUrl) {
return $this->Html->link($title, $url, $options);
}
$target = $options['target'];
$activeClass = $options['activeClass'] ?? $this->getConfig('activeClass');
unset($options['target']);
unset($options['activeClass']);
$activeClass = $options['activeClass'] ?? $this->getConfig('activeClass');
unset($options['target']);
unset($options['activeClass']);
if (is_string($target)) {
return $this->_linkFromStringTarget($currentUrl, $target, $title, $url, $activeClass, $options);
}
@@ -78,7 +81,7 @@ class ActiveLinkHelper extends Helper {
}
$options['class'] = $this->_addClass($options, $activeClass);
return $this->Html->link($title, $url, $options);
}
@@ -113,18 +116,20 @@ class ActiveLinkHelper extends Helper {
/**
* @param array $providedOptions
* @param string $toAdd
*
* @return string
*/
protected function _addClass(array $providedOptions, string $toAdd): string {
return array_key_exists('class', $providedOptions) ? $providedOptions['class'] . ' ' . $toAdd : $toAdd;
}
/**
/**
* @param array $current
* @param string $targetString
* @param string $title
* @param array|string|null $url
* @param string $activeClass
* @param array $options
*
* @return string
@@ -138,8 +143,8 @@ class ActiveLinkHelper extends Helper {
return $this->Html->link($title, $url, $options);
}
/**
/**
* @param array $current
* @param array $targetUrl
*