Files
pcm-badge-bundle/README.md
2025-02-26 11:41:21 +00:00

96 lines
2.6 KiB
Markdown

# PCM Badge Bundle
Create badges from objects or as standalone elements.
IMAGE HERE
```twig
<twig:Pcm:Badge :obj="job.kind">{{ job.kind.label }}</twig:Pcm:Badge>
<twig:Pcm:Badge :obj="job.kind" outline>{{ job.kind.label }}</twig:Pcm:Badge>
<twig:Pcm:Badge colour="green">Sale 20% off</twig:Pcm:Badge>
<twig:Pcm:Badge class="text-xl p-8" colour="maroon" outline>Danger!</twig:Pcm:Badge>
```
<br>
# Creating badges with `BadgeableInterface`
Any object that you would like to be able to be turned into a badge must implement `BadgeableInterface`.
This interface specifies a single method `getBadgeColour()` which expects an instance of the enum `BadgeColour` to be returned and is used to determine the colour of the rendered badge.
This method can contain as much logic in it as you'd like to return different colours of badge under different circumstances. EG:
```php
// Job.php
public function getBadgeColour(): BadgeColour
{
return match ($this->getKind()) {
JobKind::Investigation => BadgeColour::BLUE,
JobKind::Interrogation => BadgeColour::RED,
JobKind::Shootout => BadgeColour::BLACK,
JobKind::SipWhiskey => BadgeColour::FOREST,
default => BadgeColour::GREY
};
}
```
You then specify the object using the `:obj` prop when rendering the badge:
```twig
{# job.html.twig #}
<twig:Pcm:Badge :obj="job">{{ job.kind.value }}</twig:Pcm:Badge>
```
<br>
# Standalone badges
If you just want a badge of a certain colour instead of associating it with an object, you can specify a `colour` prop instead.
```twig
<twig:Pcm:Badge colour="red">Danger</twig:Pcm:Badge>
```
<br>
# Props overview
A badge must contain either an `obj` or a `colour`, but not both.
`obj` - An instance of an object that implements `BadgeableInterface`. You can use either of the follow syntaxes:
```twig
:obj="job.kind"
obj="{{ job.kind }}"
```
`colour` - One of the available colours specified by the `Badge` enum. For a full list of acceptable values type in some junk and read the exception message.
`outline` - A boolean attribute that changes the style of the badge to an outline.
`class` - Extra classes you want to add to the badge element. These are merged with the badge base classes taking priority in case of conflicts.
`label` - Badge label text. Content inside the content block will be prioritised over the label attribute if present.
```php
{# Both of these render the same markup. #}
<twig:Pcm:Badge colour="red" label="Warning!" />
<twig:Pcm:Badge colour="red">Warning!<twig:Pcm:Badge>
```
<br>
# Config
```yaml
pcm_badge:
base_classes: "base classes here"
```