Installation:
composer require coosos/tag-bundle
Ensure your composer.json includes the bundle in require.
Enable the Bundle:
Add to config/bundles.php (Symfony 4+):
return [
// ...
Coosos\TagBundle\CoososTagBundle::class => ['all' => true],
];
Create Tag Entity:
Run the migration to generate the Tag table:
php bin/console doctrine:migrations:diff
php bin/console doctrine:migrations:migrate
First Use Case:
Use the TagType form field in a Symfony form:
use Coosos\TagBundle\Form\Type\TagType;
$builder->add('tags', TagType::class, [
'multiple' => true,
'label' => 'Tags',
]);
Basic Tagging in Forms:
Attach TagType to any entity form for tagging functionality:
$builder->add('tags', TagType::class, [
'multiple' => true,
'required' => false,
'label' => 'Related Tags',
]);
Customizing Tag Behavior:
Override the default Tag entity (extend Coosos\TagBundle\Entity\Tag):
namespace App\Entity;
use Coosos\TagBundle\Entity\Tag as BaseTag;
class Tag extends BaseTag
{
// Add custom fields/methods
}
Integration with Entities:
Use @ManyToMany in your entity to link tags:
/**
* @ORM\ManyToMany(targetEntity="Coosos\TagBundle\Entity\Tag")
*/
private $tags;
Frontend Integration:
jquery.tagEditor for rich tagging UI:
<link rel="stylesheet" href="path/to/jquery.tagEditor.css">
<script src="path/to/jquery.tagEditor.js"></script>
{{ form_widget(form.tags, {'attr': {'class': 'tag-editor'}}) }}
<script>
$('.tag-editor').tagEditor();
</script>
API/REST Usage: Expose tag endpoints via API Platform or custom controllers:
// Example: Fetch tags for autocomplete
$tags = $this->getDoctrine()->getRepository(Tag::class)->findByName($query);
Outdated Dependencies:
Missing Doctrine Configuration:
Coosos\TagBundle\Entity\Tag is mapped in config/packages/doctrine.yaml:
orm:
mappings:
coosos_tag:
type: attribute
dir: "%kernel.project_dir%/vendor/coosos/tag-bundle/Entity"
prefix: "Coosos\TagBundle\Entity"
alias: CoososTagBundle
Frontend Assets:
tagEditor, autocomplete) are optional but required for full functionality.Tag Uniqueness:
Tag entity if needed:
/**
* @ORM\Column(unique=true)
*/
private $name;
Check Database Schema:
Run php bin/console doctrine:schema:validate to ensure the Tag table is correctly created.
Form Submission Issues:
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entity->getTags()->clear(); // Clear existing tags if needed
$entity->addTag($form->get('tags')->getData());
}
Autocomplete Not Working:
jquery.tagEditor.Custom Tag Repository:
Extend Coosos\TagBundle\Repository\TagRepository to add query methods:
namespace App\Repository;
use Coosos\TagBundle\Repository\TagRepository as BaseTagRepository;
class TagRepository extends BaseTagRepository
{
public function findPopularTags(int $limit = 10)
{
return $this->createQueryBuilder('t')
->orderBy('t.usageCount', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult();
}
}
Event Listeners:
Attach listeners to Tag events (e.g., prePersist):
namespace App\EventListener;
use Coosos\TagBundle\Entity\Tag;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\LifecycleEventArgs;
class TagListener implements EventSubscriber
{
public function getSubscribedEvents()
{
return ['prePersist'];
}
public function prePersist(Tag $tag, LifecycleEventArgs $args)
{
$tag->setSlug(strtolower($tag->getName()));
}
}
Override Twig Templates:
Copy templates from vendor/coosos/tag-bundle/Resources/views/ to templates/coosos_tag/ to customize rendering.
Symfony 5+ Flex Integration:
If using Symfony Flex, manually register the bundle in config/bundles.php (no AppKernel.php in Flex).
How can I help you explore Laravel packages today?