Skip to content

Instantly share code, notes, and snippets.

@mablae
Created July 21, 2012 15:49
Show Gist options
  • Save mablae/3156212 to your computer and use it in GitHub Desktop.
Save mablae/3156212 to your computer and use it in GitHub Desktop.
<?php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Mablae\WirGehenMitBundle\Entity\Category
*
* @ORM\Table(name="feedbackcategory")
* @ORM\Entity(repositoryClass="Mablae\WirGehenMitBundle\Entity\CategoryRepository")
*/
class Category
{
/**
*
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="name",type="string")
*/
protected $name;
/**
* @ORM\ManyToOne(targetEntity="Category")
*/
protected $parent;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set parent
*
* @param Category $parent
*/
public function setParent( Category $parent )
{
$this->parent = $parent;
}
/**
* Get parent
*
* @return Category
*/
public function getParent()
{
return $this->parent;
}
public function __toString()
{
return $this->name;
}
public function isTopLevel()
{
return $this->getParent() == null ? true : false;
}
public function getCategoryTypeTitle()
{
return array('isTopLevel' => $this->isTopLevel(), 'label' => $this->getName());
}
}
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Acme\DemoBundle\Form\Type;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\Form\AbstractType;
use Symfony\Bridge\Doctrine\Form\ChoiceList\ORMQueryBuilderLoader;
class CategoryType extends AbstractType
{
public function getName()
{
return 'category';
}
public function getParent()
{
return 'entity';
}
}
services:
form.type.category:
class: AcmeDemoBundle\Form\Type\CategoryType
tags:
- { name: form.type, alias: category }
{% block category_choice_widget_options %}
{% spaceless %}
{% for index, choice in options %}
{% if _form_is_choice_group(choice) %}
<optgroup label="{{ index|trans({}, translation_domain) }}">
{% for nested_choice in choice %}
<option value="{{ nested_choice.value }}"{% if _form_is_choice_selected(form, nested_choice) %} selected="selected"{% endif %}>{{ nested_choice.label|trans({}, translation_domain) }}</option>
{% endfor %}
</optgroup>
{% else %}
<option {% if choice.label.isTopLevel %}class="isTopLevel"{% endif %} value="{{ choice.value }}"{% if _form_is_choice_selected(form, choice) %} selected="selected"{% endif %}>{{ choice.label.label|trans({}, translation_domain) }}</option>
{% endif %}
{% endfor %}
{% endspaceless %}
{% endblock category_choice_widget_options %}
{% block category_choice_widget_collapsed %}
{% spaceless %}
<select {{ block('widget_attributes') }}{% if multiple %} multiple="multiple"{% endif %}>
{% if empty_value is not none %}
<option value="">{{ empty_value|trans({}, translation_domain) }}</option>
{% endif %}
{% if preferred_choices|length > 0 %}
{% set options = preferred_choices %}
{{ block('choice_widget_options') }}
{% if choices|length > 0 and separator is not none %}
<option disabled="disabled">{{ separator }}</option>
{% endif %}
{% endif %}
{% set options = choices %}
{{ block('category_choice_widget_options') }}
</select>
{% endspaceless %}
{% endblock category_choice_widget_collapsed %}
{% block category_widget %}
{% spaceless %}
{% if expanded %}
<ul {{ block('widget_container_attributes') }}>
{% for child in form %}
<li>
{{ form_widget(child) }}
{{ form_label(child) }}
</li>
{% endfor %}
</ul>
{% else %}
{# just let the choice widget render the select tag #}
{{ block('category_choice_widget_collapsed') }}
{% endif %}
{% endspaceless %}
{% endblock %}
$builder->add('category', 'category', array(
'class' => 'AcmeDemoBundle:Category',
'property' => 'CategoryTypeTitle'
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment