PHP code example of hdevs / tag-bundle

1. Go to this page and download the library: Download hdevs/tag-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hdevs / tag-bundle example snippets



// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...
            new HDevs\TagBundle\HDevsTagBundle(),
        );

        // ...
    }

    // ...
}



namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use HDevs\TagBundle\Model\Tag as BaseTag

/**
 * Tag
 *
 * @ORM\Table(name="tag")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\TagRepository")
 */
class Tag extends BaseTag
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;


    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }
}



namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use HDevs\TagBundle\Behavior\Taggable;

/**
 * Event
 *
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\PostRepository")
 */
class Post implements Taggable
{
    // other fields

    /**
     * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Tag")
     */
    private $tags;

    /**
     * @var string
     */
    private $tagsText;


    public function __construct(){
        $this->tags = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add tag
     *
     * @param \HDevs\TagBundle\Model\Tag $tag
     *
     * @return Event
     */
    public function addTag(\HDevs\TagBundle\Model\Tag $tag)
    {
        $this->tags[] = $tag;

        return $this;
    }

    /**
     * Remove tag
     *
     * @param \HDevs\TagBundle\Model\Tag $tag
     */
    public function removeTag(\HDevs\TagBundle\Model\Tag $tag)
    {
        $this->tags->removeElement($tag);
    }

    /**
     * Get tags
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getTags()
    {
        return $this->tags;
    }

    /**
     * @return string
     */
    public function getTagsText()
    {
        return $this->tagsText;
    }

    /**
     * @param string $tagsText
     */
    public function setTagsText($tagsText)
    {
        $this->tagsText = $tagsText;
    }


}

namespace AppBundle\Behavior;


use HDevs\TagBundle\Model\Tag;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use HDevs\TagBundle\Behavior\TagBehavior as TagBehaviorInterface

class TagBehavior implements TagBehaviorInterface
{

    /**
     * @var TokenStorage
     */
    private $tokenStorage;

    public function __construct(TokenStorage $tokenStorage)
    {
        $this->tokenStorage = $tokenStorage;
    }

    public function run(Tag $tag)
    {
        $tag->setUser($this->tokenStorage->getToken()->getUser());
    }

    public function validate(Tag $tag)
    {
        if( $this->tokenStorage->getToken()->getUser() != $tag->getUser() ){
            $t = new \AppBundle\Entity\Tag();
            $t->setUser($this->tokenStorage->getToken()->getUser());
            $t->setValue($tag->getValue());
            return $t;
        }
        return $tag;
    }
}