You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

36 lines
1.0 KiB

<?php
namespace frontend\widgets\post;
use core\entities\post\PostCategory;
use core\repositories\post\read\PostCategoryReadRepository;
use yii\base\Widget;
use yii\helpers\Html;
class CategoriesWidget extends Widget
{
/** @var PostCategory|null */
public $active;
private $categories;
public function __construct(PostCategoryReadRepository $categories, $config = [])
{
parent::__construct($config);
$this->categories = $categories;
}
public function run(): string
{
return Html::tag('div', implode(PHP_EOL, array_map(function (PostCategory $category) {
$active = $this->active && ($this->active->id == $category->id);
return Html::a(
Html::encode($category->name),
['/post/category', 'slug' => $category->slug],
['class' => $active ? 'list-group-item active' : 'list-group-item']
);
}, $this->categories->getAll())), [
'class' => 'list-group',
]);
}
}