Skip to content

Commit

Permalink
Add default default formatter configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark authored and OskarStark committed Oct 5, 2015
1 parent 1d0ec9f commit 9a7057e
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 80 deletions.
1 change: 1 addition & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->scalarNode('default_formatter')->end() // TODO: make it required when the major version is changed
->arrayNode('formatters')
->useAttributeAsKey('name')
->prototype('array')
Expand Down
12 changes: 12 additions & 0 deletions DependencyInjection/SonataFormatterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,19 @@ public function load(array $configs, ContainerBuilder $container)
$loader->load('ckeditor.xml');
}

// TODO: To be removed when major version is changed
if (!isset($config['default_formatter'])) {
reset($config['formatters']);
$config['default_formatter'] = key($config['formatters']);
}

if (!array_key_exists($config['default_formatter'], $config['formatters'])) {
throw new \InvalidArgumentException(sprintf('SonataFormatterBundle - Invalid default formatter : %s, available : %s', $config['default_formatter'], json_encode(array_keys($config['formatters']))));
}

$pool = $container->getDefinition('sonata.formatter.pool');
// TODO: This should become the first (zero-indexed) argument when the major version is changed
$pool->addArgument($config['default_formatter']);

foreach ($config['formatters'] as $code => $configuration) {
if (count($configuration['extensions']) == 0) {
Expand Down
7 changes: 3 additions & 4 deletions Form/Type/FormatterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$options['format_field_options']['property_path'] = $formatField;
}

$options['format_field_options']['data'] = $this->pool->getDefaultFormatter();

if (is_array($options['source_field'])) {
list($sourceField, $sourcePropertyPath) = $options['source_field'];
$options['source_field_options']['property_path'] = $sourcePropertyPath;
Expand All @@ -75,10 +77,6 @@ public function buildForm(FormBuilderInterface $builder, array $options)
$formatChoices = $builder->get($formatField)->getOption('choices');

if (count($formatChoices) === 1) {
// Retrieve format value
reset($formatChoices); // Ensure we're at the start
$options['format_field_options']['data'] = key($formatChoices);

// Remove the choice field
unset($options['format_field_options']['choices']);
$builder->remove($formatField);
Expand Down Expand Up @@ -127,6 +125,7 @@ public function buildView(FormView $view, FormInterface $form, array $options)
} else {
$view->vars['format_field'] = $options['format_field'];
}

$view->vars['format_field_options'] = $options['format_field_options'];

$defaultConfig = $this->configManager->getDefaultConfig();
Expand Down
22 changes: 21 additions & 1 deletion Formatter/Pool.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@ class Pool
{
protected $formatters = array();

protected $defaultFormatter;

protected $logger;

public function __construct(LoggerInterface $logger = null)
public function __construct(LoggerInterface $logger = null, $defaultFormatter = null)
{
$this->logger = $logger;

// TODO: This should become a required first parameter when the major version is changed
$this->defaultFormatter = $defaultFormatter;
}

/**
Expand Down Expand Up @@ -103,4 +108,19 @@ public function getFormatters()
{
return $this->formatters;
}

/**
* @return string
*/
public function getDefaultFormatter()
{
// TODO: This should be removed when the major version is changed
if (is_null($this->defaultFormatter)) {
reset($this->formatters);

$this->defaultFormatter = key($this->formatters);
}

return $this->defaultFormatter;
}
}
150 changes: 75 additions & 75 deletions Resources/doc/reference/installation.rst
Original file line number Diff line number Diff line change
@@ -1,75 +1,75 @@
.. index::
single: Installation; Bundle; Configuration

Installation
============

To begin, add the dependent bundles:

.. code-block:: bash
$ php composer.phar require sonata-project/formatter-bundle
Register the bundle in ``app/AppKernel.php``:

.. code-block:: php
<?php
// app/AppKernel.php
$bundles = array(
// ...
// SonataMarkItUpBundle is deprecated. All assets are now available in formatter bundle
// new Sonata\MarkItUpBundle\SonataMarkItUpBundle(),
new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle(),
new Ivory\CKEditorBundle\IvoryCKEditorBundle(),
new Sonata\FormatterBundle\SonataFormatterBundle(),
);
Configuration
=============

Edit the ``config.yml`` file and add these lines:

.. code-block:: yaml
sonata_formatter:
formatters:
markdown:
service: sonata.formatter.text.markdown
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig #keep this commented unless you are using media bundle.
text:
service: sonata.formatter.text.text
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig
rawhtml:
service: sonata.formatter.text.raw
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig
richhtml:
service: sonata.formatter.text.raw
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig
twig:
service: sonata.formatter.text.twigengine
extensions: [] # Twig formatter cannot have extensions
.. index::
single: Installation; Bundle; Configuration

Installation
============

To begin, add the dependent bundles:

.. code-block:: bash
$ php composer.phar require sonata-project/formatter-bundle
Register the bundle in ``app/AppKernel.php``:

.. code-block:: php
<?php
// app/AppKernel.php
$bundles = array(
// ...
// SonataMarkItUpBundle is deprecated. All assets are now available in formatter bundle
// new Sonata\MarkItUpBundle\SonataMarkItUpBundle(),
new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle(),
new Ivory\CKEditorBundle\IvoryCKEditorBundle(),
new Sonata\FormatterBundle\SonataFormatterBundle(),
);
Configuration
=============

Edit the ``config.yml`` file and add these lines:

.. code-block:: yaml
sonata_formatter:
formatters:
markdown:
service: sonata.formatter.text.markdown
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig #keep this commented unless you are using media bundle.
text:
service: sonata.formatter.text.text
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig
rawhtml:
service: sonata.formatter.text.raw
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig
richhtml:
service: sonata.formatter.text.raw
extensions:
- sonata.formatter.twig.control_flow
- sonata.formatter.twig.gist
# - sonata.media.formatter.twig
twig:
service: sonata.formatter.text.twigengine
extensions: [] # Twig formatter cannot have extensions
1 change: 1 addition & 0 deletions Tests/Form/Type/FormatterTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class FormatterTypeTest extends \PHPUnit_Framework_TestCase
public function testBuildFormOneChoice()
{
$pool = $this->getMockBuilder('Sonata\FormatterBundle\Formatter\Pool')->disableOriginalConstructor()->getMock();

$translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
$configManager = $this->getMock('Ivory\CKEditorBundle\Model\ConfigManagerInterface');

Expand Down
20 changes: 20 additions & 0 deletions Tests/Formatter/PoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,24 @@ public function testUnexpectedException()

$pool->transform('foo', 'Salut');
}

public function testDefaultFormatter()
{
$pool = new Pool(null, 'default');

$this->assertEquals('default', $pool->getDefaultFormatter());
}

// TODO: This should be removed when the major version is changed
public function testBcDefaultFormatter()
{
$formatter = new RawFormatter();
$env = $this->getMock('\Twig_Environment');

$pool = new Pool();

$pool->add('foo', $formatter, $env);

$this->assertEquals('foo', $pool->getDefaultFormatter());
}
}

12 comments on commit 9a7057e

@tophsic
Copy link

@tophsic tophsic commented on 9a7057e Oct 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have an Exception after updating:

InvalidArgumentException in SonataFormatterExtension.php line 66:
SonataFormatterBundle - Invalid default formatter : , available : []

Can you tell me what I have to do?

@OskarStark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tophsic can you please post the output of composer show -i | grep sonata ?

@sagikazarmark can you please help and if this is a bug, add a test case?

@tophsic
Copy link

@tophsic tophsic commented on 9a7057e Oct 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sonata-project/admin-bundle              dev-master 1feecf9 Symfony SonataAdminBundle
sonata-project/block-bundle              2.3.8              Symfony SonataBlockBundle
sonata-project/cache                     1.0.6              Cache library
sonata-project/classification-bundle     dev-master c2c3e63 Symfony SonataClassificationBundle
sonata-project/core-bundle               2.3.9              Symfony SonataCoreBundle
sonata-project/datagrid-bundle           dev-master cc42f28 Symfony SonataDatagridBundle
sonata-project/doctrine-extensions       1.0.2              Doctrine2 behavioral extensions
sonata-project/doctrine-orm-admin-bundle dev-master 02b1205 Symfony Sonata / Integrate Doctrine ORM into the SonataAdminBundle
sonata-project/easy-extends-bundle       2.1.9              Symfony SonataEasyExtendsBundle
sonata-project/exporter                  1.4.0              Lightweight Exporter library
sonata-project/formatter-bundle          dev-master ae7b408 Symfony SonataFormatterBundle
sonata-project/google-authenticator      1.0.2              Library to integrate Google Authenticator into a PHP project
sonata-project/intl-bundle               2.2.3              Symfony SonataIntlBundle
sonata-project/media-bundle              dev-master 60f7191 Symfony SonataMediaBundle
sonata-project/notification-bundle       2.3.2              Symfony SonataNotificationBundle
sonata-project/translation-bundle        dev-master a39e130 SonataTranslationBundle
sonata-project/user-bundle               dev-master 4ce8c13 Symfony SonataUserBundle

@OskarStark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and please post the config part from sonata_formatter

@tophsic
Copy link

@tophsic tophsic commented on 9a7057e Oct 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure I have time to help today. Will have a look later if you do not have time neither.

@OskarStark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 but the config would be helpful, afterwards we can try to reproduce the error, IMO it is weird, that no formatters are available...

@sagikazarmark
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed it is. Is it possible that the check for a valid formatter is executed prior to the actual config schema check?

@tophsic
Copy link

@tophsic tophsic commented on 9a7057e Oct 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Till now I did not have any sonata_formatter config part. Did I miss something ?

Here is a var_dump of $config variable from SonataFormatterExtension.php:59 file

array (size=2)
  'formatters' => 
    array (size=0)
      empty
  'ckeditor' => 
    array (size=1)
      'templates' => 
        array (size=2)
          'browser' => string 'SonataFormatterBundle:Ckeditor:browser.html.twig' (length=48)
          'upload' => string 'SonataFormatterBundle:Ckeditor:upload.html.twig' (length=47)

@OskarStark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but you already use the formatter/ckeditor in your project?

@tophsic
Copy link

@tophsic tophsic commented on 9a7057e Oct 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have to remember when I activate ckeditor 😉. I assume it is related to richhtml fields in my forms.

@tophsic
Copy link

@tophsic tophsic commented on 9a7057e Oct 6, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remember now and find what I did in my project. We use ckeditor because we have rich fields in our forms. But I wanted to have only one format available so I did this simple field configuration

->add('description', 'sonata_simple_formatter_type', [
    'format' => 'richhtml'
])

When I did this, the sonata_formatter configuration appeared to be uselees so I removed it.

I just get it back and described only the richhtml part and all went good. So I assume you do not have anything to do ;).

Thank you very much for your reactivity and your rubber duck advices 😉

@soullivaneuh
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please next time, open issues instead of commenting a commit when you got problems.

Please sign in to comment.