Skip to content

Instantly share code, notes, and snippets.

View tentacode's full-sized avatar
🐙
tentacoding

Gabriel Pillet tentacode

🐙
tentacoding
View GitHub Profile
@tentacode
tentacode / index.html.twig
Created October 27, 2014 13:51
Calling twig macro dynamically with twig "attribute" method
{% import 'macro.html.twig' as macro %}
{{ attribute(macro, 'type_1', 'Foobar') }} {# <p>Type One Foobar</p> #}
{{ attribute(macro, 'type_2', 'Barfoo') }} {# <p>Type Two Barfoo</p> #}
@tentacode
tentacode / PictureType.php
Created October 2, 2014 09:41
Using symfony type as a data transformer when it's its only purpose
<?php
namespace Tentacode\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\DataTransformerInterface;
class PictureType extends AbstractType implements DataTransformerInterface
{
@tentacode
tentacode / RegistrationContext.php
Last active August 29, 2015 14:06
Make good use of the turnip syntax in Behat 3
<?php
namespace Context;
class RegistrationContext extends BaseContext
{
/**
* @When I go to the registration page
*/
public function iGoToTheRegistrationPage()
@tentacode
tentacode / ConnectedUser.php
Created August 19, 2014 09:04
PHP DI + Trait + Symfony service === <3 ?
<?php
namespace Tentacode\App\Security;
trait ConnectedUser
{
/**
* @Inject
* @var Symfony\Component\Security\Core\SecurityContext
*/
@tentacode
tentacode / GroupConcat.php
Created June 11, 2014 12:10
Adding MySQL GROUP_CONCAT to doctrine in a Symfony project
<?php
// src/Tentacode/LolcatBundle/Doctrine/Mysql/GroupConcat.php
namespace Tentacode\LolcatBundle\Doctrine\Mysql;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\SqlWalker;
use Doctrine\ORM\Query\Parser;
@tentacode
tentacode / CrazyCatLadyType.php
Last active August 29, 2015 14:01
PrototypeCollectionType is a new type I made to easily, generically, manage collection form with prototypes (add + remove), in a nicer way (IMO) than in the Symfony cookbook.
<?php
// src/Tentacode/LolcatBundle/Form/CrazyCatLadyType.php
namespace Tentacode\LolcatBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class CrazyCatLadyType extends AbstractType
@tentacode
tentacode / form.html
Created May 22, 2014 08:21
Small jQuery script that notices the user when leaving the page without saving after updating a form
<form action="..." data-unsaved-warning>
<!-- Something something the form -->
</form>
@tentacode
tentacode / validation.php
Created April 16, 2014 08:39
Know if a property is required for current validation groups using Validator metadatas
<?php
function isRequired($object, $property, $validationGroups = ['Default'])
{
$classMetadata = $this->validator->getMetadataFor($object);
$propertyMetadatas = $classMetadata->getPropertyMetadata($property);
foreach ($propertyMetadatas as $propertyMetadata) {
$constraintsByGroups = $propertyMetadata->constraintsByGroup;
@tentacode
tentacode / form_theme.twig
Created April 10, 2014 16:16
Displaying buttons at the end of a form in a Twig form theme for Symfony
{% block form_rows %}
{% spaceless %}
{% for child in form if child.vars.block_prefixes.1 != 'submit' %}
{{ form_row(child) }}
{% endfor %}
{% for child in form if child.vars.block_prefixes.1 == 'submit' %}
{{ form_row(child) }}
{% endfor %}
{% endspaceless %}
{% endblock form_rows %}
@tentacode
tentacode / form.html.twig
Created August 24, 2013 09:02
Separating regular fields and buttons rendering in a Symfony/Twig form. (from Symfony 2.3)
{% macro form_fields(form) %}
{% for field in form %}
{% if field.vars.block_prefixes[0] != 'button' %}
{{ form_row(field) }}
{% endif %}
{% endfor %}
{% endmacro %}
{% import _self as macros %}