Closed rungta closed 4 years ago
getLink()
is defined by ElementInterface
so adding an $attr
argument would be a breaking change for element types, so we’ll have to wait until Craft 4 to implement this, unfortunately.
Ah okay. Now we wait.
@rungta - you could always create a behavior to handle that functionality.
use yii\base\Behavior;
use craft\helpers\Template as TemplateHelper;
use craft\helpers\Html;
class BaseEntryBehavior extends Behavior
{
public function getLinkEl($attrs = [])
{
$entry = $this->owner;
$attrs['href'] = $entry->getUrl();
return TemplateHelper::raw(Html::tag('a', $entry->title, $attrs));
}
}
In twig:
{{ entry.getLinkEl({ class: 'button' }) }}
Great suggestion @fvaldes33. Thanks!
@brandonkelly
Maybe just create a new method and call it linkTag
or something like that?
Actually this isn’t really needed anymore as of Craft 3.3, thanks to the |attr Twig filter.
Description
It would be quite handy to pass in attributes (especially classes) to the
getLink()
function oncraft\base\Element
, similar to the newly introducedattr()
Twig function.