Skip to content

Instantly share code, notes, and snippets.

@mazenovi
Created April 10, 2013 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mazenovi/5354527 to your computer and use it in GitHub Desktop.
Save mazenovi/5354527 to your computer and use it in GitHub Desktop.
Simple Tree Management with Symfony2 / Propel / AdmigeneratorGeneratorBundle with forbidden path disabled
# Twig Configuration
twig:
form:
resources:
- 'MazenoviLightCmsBundle:Form:fields.html.twig'
<?php
namespace Mazenovi\LightCmsBundle\Form\Type\Folder;
use Admingenerated\MazenoviLightCmsBundle\Form\BaseFolderType\EditType as BaseEditType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\FormInterface;
use Mazenovi\LightCmsBundle\Model\FolderQuery;
use Mazenovi\LightCmsBundle\Form\EventListener\DisableNestedPathSubscriber;
class EditType extends BaseEditType
{
public function finishView(FormView $view, FormInterface $form, array $options)
{
//http://stackoverflow.com/questions/14344639/symfony-2form-how-to-disable-specific-item-in-form-choice-type
$nested_folder_ids = array();
$nested_folder = FolderQuery::create()->childrenOf($view->vars['value'])->find();
array_walk(
$nested_folder,
function($val, $key) use(&$nested_folder_ids){
$nested_folder_ids[] = $val->getId();
}
);
foreach ($view->children['parent_folder']->vars['choices'] as $parent) {
if(in_array($parent->data->getId(), $nested_folder_ids) || $parent->data->getId() == $view->vars['value']->getId())
{
$parent->disabled = 'disabled';
}
}
}
}
{% block choice_widget_options %}
{% spaceless %}
{% for group_label, choice in options %}
{% if choice is iterable %}
<optgroup label="{{ group_label|trans({}, translation_domain) }}">
{% set options = choice %}
{{ block('choice_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %} {% if choice.disabled is defined %} disabled="{{ choice.disabled }}"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
{% endif %}
{% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment