Skip to content

Commit

Permalink
Allow AdvancedModelType to handle option overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
allejo committed Feb 10, 2018
1 parent 27fd41d commit 52c2d1f
Showing 1 changed file with 54 additions and 15 deletions.
69 changes: 54 additions & 15 deletions src/Form/Type/AdvancedModelType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

namespace BZIon\Form\Type;

use __;
use BZIon\Form\Constraint\ValidModel;
use BZIon\Form\Transformer\MultipleAdvancedModelTransformer;
use BZIon\Form\Transformer\SingleAdvancedModelTransformer;
use Doctrine\Common\Inflector\Inflector;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
Expand All @@ -19,6 +22,8 @@ class AdvancedModelType extends AbstractType
*/
private $types = array();

private $options = array();

/**
* An object to always include
* @var \Model|null
Expand All @@ -33,11 +38,24 @@ class AdvancedModelType extends AbstractType

/**
* Create new ModelType
*
* @param string|string[] $type The types of the model
* @param array $options
*/
public function __construct($type)
public function __construct($type, $options = [])
{
$this->types = (is_array($type)) ? $type : array($type);
$this->types = (is_array($type)) ? $type : [$type];

if (!is_array($type)) {
$options = [
$type => $options,
];
}

foreach ($this->types as $t) {
$this->options[strtolower($t)] = __::get($options, $t, []);
}

$this->types = array_map('strtolower', $this->types);
}

Expand Down Expand Up @@ -72,11 +90,12 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}

// Model IDs that will be manipulated by javascript
$builder->add('ids', 'hidden', array(
$label = __::get(array_column(array_values($this->options), 'label'), 0, $builderName);
$builder->add('ids', HiddenType::class, array(
'attr' => array(
'class' => 'select2-compatible',
'data-exclude' => $exclude,
'data-label' => $builderName,
'data-label' => ($label === null) ? $builderName : $label,
'data-multiple' => $this->multiple,
'data-required' => $options['required']
),
Expand All @@ -88,17 +107,19 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$pluralType = ($this->multiple) ? Inflector::pluralize($type) : $type;
$label = (count($this->types) > 1) ? "$builderName $pluralType" : $builderName;

$builder->add(
$builder->create($type, 'text', array(
'attr' => array(
'class' => 'model-select',
'data-type' => $type,
'placeholder' => $placeholder,
),
'label' => $label,
'required' => false
))
);
$defaultOptions = [
'attr' => [
'class' => 'model-select',
'data-type' => $type,
'placeholder' => $placeholder,
],
'label' => $label,
'required' => false,
];
$manualOptions = __::get($this->options, $type, []);

// @todo Replace array_merge_recursive_distinct() with __::merge() when it's available
$builder->add($type, TextType::class, $this->array_merge_recursive_distinct($defaultOptions, $manualOptions));
}

if ($this->multiple) {
Expand Down Expand Up @@ -181,4 +202,22 @@ public function getName()
{
return 'advanced_model';
}

/**
* @deprecated Will be replaced by __::merge()
*/
private function array_merge_recursive_distinct(array &$array1, array &$array2)
{
$merged = $array1;

foreach ($array2 as $key => &$value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = $this->array_merge_recursive_distinct($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}

return $merged;
}
}

0 comments on commit 52c2d1f

Please sign in to comment.