Skip to content

Commit

Permalink
Improve label display
Browse files Browse the repository at this point in the history
  • Loading branch information
Jibbarth committed Oct 28, 2018
1 parent 8da123f commit d1575de
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Service/FormConfigService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;
use Symfony\Component\Form\FormInterface;

Expand All @@ -16,7 +16,7 @@ class FormConfigService
/**
* @var FormFactoryInterface
*/
private $factory;
protected $factory;

public function __construct(
FormFactoryInterface $factory
Expand All @@ -36,9 +36,10 @@ public function getFormForConfig(array $config): FormInterface
return $formBuilder->getForm();
}

protected function addToForm(FormBuilder $formBuilder, string $key, $field)
protected function addToForm(FormBuilderInterface $formBuilder, string $key, $field, $parentKey = ''): void
{
$params = [
'label' => $this->humanize($key) . (($parentKey) ? sprintf(' (%s)', $this->humanize($parentKey)) : null),
'data' => $field,
'required' => false,
'translation_domain' => 'barth_simple_config',
Expand All @@ -47,7 +48,8 @@ protected function addToForm(FormBuilder $formBuilder, string $key, $field)
case \is_array($field):
foreach ($field as $subKey => $value) {
if (!\is_int($subKey)) {
$this->addToForm($formBuilder, $key . ':' . $subKey, $value);

$this->addToForm($formBuilder, $subKey, $value, ($parentKey) ? $parentKey . ':' . $key : $key);
}
}
return;
Expand All @@ -67,8 +69,15 @@ protected function addToForm(FormBuilder $formBuilder, string $key, $field)
default:
return;
}

if ('' !== $parentKey) {
$key = $parentKey . ':' . $key;
}
$key = \str_replace('.', '-', $key);
$formBuilder->add($key, $type, $params);
}

protected function humanize($text)
{
return ucfirst(strtolower(trim(preg_replace(array('/([A-Z])/', '/[_\s]+/'), array('_$1', ' '), $text))));
}
}

0 comments on commit d1575de

Please sign in to comment.