Search code examples
arrayscakephphtml-helper

Create links to tagged list from an array of tags


I just followed a tag tutorial (https://book.cakephp.org/3.0/en/tutorials-and-examples/cms/tags-and-users.html) on the Red Velvet Cookbook. I was successful in adding tagging to my website. Though I feel it needs a couple more things. The comma separated list of tags is just that, a list. I would prefer a list of links. The links would obviously link to a list of posts that are tagged with the same tag. This list already exists thanks to the tutorial. The url looks like http://localhost:8765/story/tagged/cats/

I have fiddled with the html helper to try and create links but I am unsure how to do this with an array. Any leads or help would be greatly appreciated.

This is the line in my template that shows the list of tags

<p><b>Tags:</b> <?= h($story->tag_string) ?></p>

This is the function that gets the tag_string

class Story extends Entity
{

/**
 * Fields that can be mass assigned using newEntity() or patchEntity().
 *
 * Note that when '*' is set to true, this allows all unspecified fields to
 * be mass assigned. For security purposes, it is advised to set '*' to false
 * (or remove it), and explicitly make individual fields accessible as needed.
 *
 * @var array
 */
protected $_accessible = [
    '*' => false,
    'tag_string' => true
];

protected function _getTagString()
{
    if (isset($this->_properties['tag_string'])) {
        return $this->_properties['tag_string'];
    }
    if (empty($this->tags)) {
        return '';
    }
    $tags = new Collection($this->tags);
    $str = $tags->reduce(function ($string, $tag) {
        return $string . $tag->name . ', ';
    }, '');
    return trim($str, ', ');
}

}

Solution

  • In your $story entity you have tags property, which you already used in computed field tag_string. To get separated tags, you need to iterate over it in a template, eg:

    <ul>
    <?php foreach($story->tags as $tag): ?>
        <li><?= $tag->title ?></li>
    <?php endforeach; ?>
    </ul>
    

    Assuming you have a method in your StoriesController named tagged, which accepts one parameter: tag title, you can construct link using HtmlHelper for each tag:

    <ul>
    <?php foreach($story->tags as $tag): ?>
        <li>
            <?= $this->Html->link($tag->title, [
                "controller" => "Stories",
                "action" => "tagged",
                $tag->title
            ], [
                "class" => "your-tag-link-class" //second array is for properties, in case you would like to add eg class for these links to style them using css.
            ]) ?>
        </li>
    <?php endforeach; ?>
    </ul>