chaplean/social-buttons-bundle
Installation:
composer require chaplean/social-buttons-bundle
Add to config/bundles.php:
Chaplean\SocialButtonsBundle\ChapleanSocialButtonsBundle::class => ['all' => true],
Basic Configuration:
Update config/packages/chaplean_social_buttons.yaml with your desired buttons (e.g., facebook, twitter) and their configurations (e.g., url, locale). Example:
chaplean_social_buttons:
buttons:
twitter:
url: "https://example.com/share"
message: "Check this out!"
First Use Case: Render buttons in a Twig template:
{{ render(social_buttons('twitter')) }}
Ensure required assets are loaded in your layout (see README for CSS/JS includes).
Dynamic URL Generation: Use Twig to pass dynamic URLs to buttons:
{% set shareUrl = app.request.uri %}
{{ render(social_buttons('facebook', {'url': shareUrl})) }}
Conditional Rendering: Hide/show buttons based on logic:
{% if app.user.isPremium %}
{{ render(social_buttons(['twitter', 'linkedin'])) }}
{% endif %}
Custom Styling: Extend default classes via config:
twitter:
class_a: "btn-twitter-custom"
class_i: "fa-twitter-square"
Integration with CMS: Use Twig functions in a CMS (e.g., Sylius, Sonata) to embed buttons in product pages or blog posts.
API-Driven Config: Fetch button configs from a database and merge them with default settings:
// In a controller/service
$config = $this->getButtonConfigFromDB();
return $this->renderView('template.html.twig', [
'socialButtons' => array_merge($defaultConfig, $config)
]);
Asset Loading:
bootstrap-social.css or font-awesome will break button rendering.URL Validation:
url: null will render but may fail silently. Validate URLs in config or runtime:
{% if socialButton.url is not null %}
{{ render(social_buttons(socialButton)) }}
{% endif %}
Locale Quirks:
fr_FR vs. fr).Deprecated Dependencies:
Twig Function Scope:
social_buttons Twig function is bundle-specific. Avoid naming conflicts with other extensions.Inspect Rendered HTML: Use browser dev tools to verify button markup. Example output for Twitter:
<a href="https://twitter.com/intent/tweet?text=Check%20this%20out%21&url=https%3A%2F%2Fexample.com%2Fshare&via=Chaplean"
class="twitter btn btn-twitter" target="_blank">
<i class="fa fa-twitter"></i> Tweet
</a>
Check Config Overrides:
Use Symfony’s dump() to debug merged configs:
$this->container->getParameter('chaplean_social_buttons.buttons');
Custom Button Types: Extend the bundle by creating a new button service. Example:
# config/packages/chaplean_social_buttons.yaml
buttons:
custom:
type: "custom" # Requires custom implementation
url: "https://example.com"
Override Twig Functions:
Replace the default social_buttons function in your theme’s Twig environment:
$twig->addFunction(new \Twig\TwigFunction('social_buttons', [$this, 'customSocialButtons']));
Event Listeners: Listen for button rendering events (if the bundle exposes them) to modify behavior dynamically. Example:
// src/EventListener/SocialButtonsListener.php
public function onSocialButtonRender(SocialButtonEvent $event) {
$event->setOption('class_a', 'my-custom-class');
}
How can I help you explore Laravel packages today?