Skip to content

Commit

Permalink
Merge pull request sonata-project#2400 from PHDA/help_form
Browse files Browse the repository at this point in the history
Add help option named sonata_admin_help to sub-fields that are non admin fields. Example given with sonata_type_immutable_array.
  • Loading branch information
rande committed Sep 9, 2014
2 parents 06a3aa9 + dbb580b commit fe3061a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
8 changes: 6 additions & 2 deletions Form/Extension/Field/Type/FormTypeFieldExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
);

$builder->setAttribute('sonata_admin_enabled', false);
$builder->setAttribute('sonata_help', false);

if ($options['sonata_field_description'] instanceof FieldDescriptionInterface) {
$fieldDescription = $options['sonata_field_description'];
Expand Down Expand Up @@ -107,7 +108,8 @@ protected function getTypes(FormBuilderInterface $formBuilder)
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$sonataAdmin = $form->getConfig()->getAttribute('sonata_admin');
$sonataAdmin = $form->getConfig()->getAttribute('sonata_admin');
$sonataAdminHelp = isset($options['sonata_help']) ? $options['sonata_help'] : null;

// avoid to add extra information not required by non admin field
if ($sonataAdmin && $form->getConfig()->getAttribute('sonata_admin_enabled', true)) {
Expand Down Expand Up @@ -142,7 +144,8 @@ public function buildView(FormView $view, FormInterface $form, array $options)
$view->vars['sonata_admin_enabled'] = false;
}

$view->vars['sonata_admin'] = $sonataAdmin;
$view->vars['sonata_help'] = $sonataAdminHelp;
$view->vars['sonata_admin'] = $sonataAdmin;
}

/**
Expand All @@ -168,6 +171,7 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)

// be compatible with mopa if not installed, avoid generating an exception for invalid option
'label_render' => true,
'sonata_help' => null
));
}

Expand Down
24 changes: 24 additions & 0 deletions Resources/doc/reference/form_help_message.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,29 @@ Alternative Way To Define Help Messages
}
}
Help messages in a sub-field
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. code-block:: php
<?php
class ExampleAdmin.php
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('enabled')
->add('settings', 'sonata_type_immutable_array', array(
'keys' => array(
array('content', 'textarea', array(
'sonata_help' => 'Set the content'
)),
array('public', 'checkbox', array()),
)
);
}
}
Advanced usage
^^^^^^^^^^^^^^
Expand Down Expand Up @@ -78,3 +101,4 @@ Example
}
}
7 changes: 7 additions & 0 deletions Resources/views/Form/form_admin_fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ file that was distributed with this source code.

{% extends 'form_div_layout.html.twig' %}

{% block form_widget -%}
{{ parent() }}
{% if sonata_help is defined and sonata_help %}
<span class="help-block sonata-ba-field-widget-help">{{ sonata_help|raw }}</span>
{% endif %}
{%- endblock form_widget %}

{% block form_widget_simple %}
{% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
{{ parent() }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ public function testDefaultOptions()

$this->assertArrayHasKey('sonata_admin', $options);
$this->assertArrayHasKey('sonata_field_description', $options);
$this->assertArrayHasKey('sonata_help', $options);

$this->assertNull($options['sonata_admin']);
$this->assertNull($options['sonata_field_description']);
$this->assertNull($options['sonata_help']);
}

public function testbuildViewWithNoSonataAdminArray()
Expand Down Expand Up @@ -96,7 +98,9 @@ public function testbuildViewWithWithSonataAdmin()
$formView->vars['block_prefixes'] = array('form', 'field', 'text', '_s50b26aa76cb96_username');

$extension = new FormTypeFieldExtension();
$extension->buildView($formView, $form, array());
$extension->buildView($formView, $form, array(
'sonata_help' => 'help text'
));

$this->assertArrayHasKey('block_prefixes', $formView->vars);
$this->assertArrayHasKey('sonata_admin_enabled', $formView->vars);
Expand All @@ -113,5 +117,6 @@ public function testbuildViewWithWithSonataAdmin()

$this->assertEquals($expected, $formView->vars['block_prefixes']);
$this->assertTrue($formView->vars['sonata_admin_enabled']);
$this->assertEquals('help text', $formView->vars['sonata_help']);
}
}

0 comments on commit fe3061a

Please sign in to comment.