Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit b75b612

Browse files
ElectricMaxxxdbu
authored andcommitted
Fix ckeditor (#100)
add CkEditor support for static content admin
1 parent 524dfcd commit b75b612

File tree

14 files changed

+117
-7
lines changed

14 files changed

+117
-7
lines changed

composer.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,17 @@
3232
"symfony/phpunit-bridge": "^3.2",
3333
"matthiasnoback/symfony-dependency-injection-test": "~0.6",
3434
"matthiasnoback/symfony-config-test": "^1.3.1",
35-
"burgov/key-value-form-bundle": "^1.0"
35+
"burgov/key-value-form-bundle": "^1.0",
36+
"egeloen/ckeditor-bundle": "^4.0"
3637
},
3738
"minimum-stability": "dev",
3839
"prefer-stable": true,
3940
"suggest": {
4041
"symfony-cmf/routing-bundle": "To make use of the alternate locale provider.",
4142
"doctrine/phpcr-bundle": "To persist the metadata in PHPCR ODM documents",
4243
"doctrine/doctrine-bundle": "To persist the metadata in ORM entities",
43-
"doctrine/orm": "To persist the metadata in ORM entities, ~2.4"
44+
"doctrine/orm": "To persist the metadata in ORM entities, ~2.4",
45+
"egeloen/ckeditor-bundle": "To have CkEditor support on textareas."
4446
},
4547
"autoload": {
4648
"psr-4": {

src/Admin/Content/StaticContentAdmin.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,42 @@
1111

1212
namespace Symfony\Cmf\Bundle\SonataPhpcrAdminIntegrationBundle\Admin\Content;
1313

14+
use Ivory\CKEditorBundle\Form\Type\CKEditorType;
1415
use Sonata\AdminBundle\Datagrid\DatagridMapper;
1516
use Sonata\AdminBundle\Datagrid\ListMapper;
1617
use Sonata\AdminBundle\Form\FormMapper;
1718
use Symfony\Cmf\Bundle\ContentBundle\Model\StaticContentBase;
1819
use Symfony\Cmf\Bundle\SonataPhpcrAdminIntegrationBundle\Admin\AbstractAdmin;
1920
use Symfony\Cmf\Bundle\TreeBrowserBundle\Form\Type\TreeSelectType;
20-
use Symfony\Component\Form\Extension\Core\Type\TextType;
2121
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
22+
use Symfony\Component\Form\Extension\Core\Type\TextType;
2223

2324
class StaticContentAdmin extends AbstractAdmin
2425
{
2526
protected $translationDomain = 'CmfSonataPhpcrAdminIntegrationBundle';
2627

28+
/**
29+
* Configuration, that can be passed to CKEditorType.
30+
*
31+
* @var array
32+
*/
33+
private $ckEditorConfig;
34+
2735
public function getExportFormats()
2836
{
29-
return array();
37+
return [];
38+
}
39+
40+
/**
41+
* Set configuration for CKEditorType.
42+
*
43+
* Documentation: http://symfony.com/doc/master/bundles/IvoryCKEditorBundle/usage/config.html
44+
*
45+
* @param array $config configuration for CKEditorType
46+
*/
47+
public function setCkEditorConfig(array $config)
48+
{
49+
$this->ckEditorConfig = $config;
3050
}
3151

3252
protected function configureListFields(ListMapper $listMapper)
@@ -44,7 +64,11 @@ protected function configureFormFields(FormMapper $formMapper)
4464
->tab('form.tab_general')
4565
->with('form.group_content', ['class' => 'col-md-9'])
4666
->add('title', TextType::class)
47-
->add('body', TextareaType::class)
67+
->add(
68+
'body',
69+
$this->ckEditorConfig ? CKEditorType::class : TextareaType::class,
70+
$this->ckEditorConfig
71+
)
4872
->end()
4973

5074
->with('form.group_location', ['class' => 'col-md-3'])

src/DependencyInjection/CmfSonataPhpcrAdminIntegrationExtension.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Cmf\Bundle\SonataPhpcrAdminIntegrationBundle\DependencyInjection;
1313

14+
use Ivory\CKEditorBundle\IvoryCKEditorBundle;
15+
use Symfony\Cmf\Bundle\SonataPhpcrAdminIntegrationBundle\DependencyInjection\Factory\IsConfigEnabledTrait;
1416
use Symfony\Component\Config\FileLocator;
1517
use Symfony\Component\DependencyInjection\ContainerBuilder;
1618
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
@@ -24,6 +26,8 @@
2426
*/
2527
class CmfSonataPhpcrAdminIntegrationExtension extends Extension implements CompilerPassInterface
2628
{
29+
use IsConfigEnabledTrait;
30+
2731
/**
2832
* @var AdminFactoryInterface[]
2933
*/
@@ -61,6 +65,36 @@ public function load(array $configs, ContainerBuilder $container)
6165

6266
$loader->load('main.xml');
6367
$loader->load('enhancer.xml');
68+
69+
$this->loadIvoryCKEditor($config['ivory_ckeditor'], $container);
70+
}
71+
72+
/**
73+
* Adds the ckEditor configuration to the parameters list when configuration is enabled (auto, true). For both
74+
* settings the "egeloen/ckeditor-bundle" has to be installed and enabled.
75+
*
76+
* @param array $config
77+
* @param ContainerBuilder $container
78+
*
79+
* @throws \LogicException when configuration is enabled by true and "IvoryCKEditorBundle" is not enabled
80+
*/
81+
protected function loadIvoryCKEditor(array $config, ContainerBuilder $container)
82+
{
83+
$configParameter = [];
84+
if ($this->isConfigEnabled($container, $config)) {
85+
$bundles = $container->getParameter('kernel.bundles');
86+
if (true === $config['enabled'] && !isset($bundles['IvoryCKEditorBundle'])) {
87+
$message = 'IvoryCKEditorBundle integration was explicitely enabled, but the bundle is not available';
88+
if (class_exists(IvoryCKEditorBundle::class)) {
89+
$message .= ' (did you forget to register the bundle in the AppKernel?)';
90+
}
91+
throw new \LogicException($message.'.');
92+
} elseif (isset($bundles['IvoryCKEditorBundle'])) {
93+
$configParameter = ['config_name' => $config['config_name']];
94+
}
95+
}
96+
97+
$container->setParameter('cmf_sonata_phpcr_admin_integration.ivory_ckeditor.config', $configParameter);
6498
}
6599

66100
/**

src/DependencyInjection/Configuration.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,20 @@ public function getConfigTreeBuilder()
4141

4242
$this->addBundlesSection($root);
4343

44+
$root
45+
->children()
46+
->arrayNode('ivory_ckeditor')
47+
->addDefaultsIfNotSet()
48+
->children()
49+
->enumNode('enabled')
50+
->values([true, false, 'auto'])
51+
->defaultValue('auto')
52+
->end()
53+
->scalarNode('config_name')->isRequired()->end()
54+
->end()
55+
->end()
56+
->end();
57+
4458
return $treeBuilder;
4559
}
4660

src/DependencyInjection/Factory/IsConfigEnabledTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Cmf\Bundle\SonataPhpcrAdminIntegrationBundle\DependencyInjection\Factory;
1313

1414
use Symfony\Component\DependencyInjection\ContainerBuilder;
15+
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1516

1617
trait IsConfigEnabledTrait
1718
{

src/Resources/config/content.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@
2626
<call method="setRootPath">
2727
<argument>%cmf_content.persistence.phpcr.content_basepath%</argument>
2828
</call>
29+
30+
<call method="setCkEditorConfig">
31+
<argument>%cmf_sonata_phpcr_admin_integration.ivory_ckeditor.config%</argument>
32+
</call>
2933
</service>
3034
</services>
3135
</container>

src/Resources/config/schema/sonata-phpcr-admin-integration.xsd

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<xsd:complexType name="config">
1010
<xsd:sequence>
1111
<xsd:element name="bundles" type="bundles" minOccurs="0" maxOccurs="1" />
12+
<xsd:element name="ivory_ckeditor" type="ivory_ckeditor" minOccurs="0" maxOccurs="1" />
1213
</xsd:sequence>
1314
</xsd:complexType>
1415

@@ -18,4 +19,17 @@
1819
</xsd:sequence>
1920
</xsd:complexType>
2021

22+
<xsd:complexType name="ivory_ckeditor">
23+
<xsd:attribute name="enabled" type="enabled" default="auto" />
24+
<xsd:attribute name="config_name" type="xsd:string"/>
25+
</xsd:complexType>
26+
27+
<xsd:simpleType name="enabled">
28+
<xsd:restriction base="xsd:string">
29+
<xsd:enumeration value="true"/>
30+
<xsd:enumeration value="false"/>
31+
<xsd:enumeration value="auto"/>
32+
</xsd:restriction>
33+
</xsd:simpleType>
34+
2135
</xsd:schema>

tests/Resources/Fixtures/config/config.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
'enabled' => true,
1919
],
2020
],
21+
'ivory_ckeditor' => [
22+
'config_name' => 'cmf_sonata_phpcr_admin_integration',
23+
],
2124
]);

tests/Resources/Fixtures/config/config.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
<bundles>
88
<seo enabled="true" />
99
</bundles>
10+
<ivory_ckeditor config_name="cmf_sonata_phpcr_admin_integration" />
1011
</config>
1112
</container>
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
cmf_sonata_phpcr_admin_integration:
22
bundles:
3-
seo:
4-
enabled: true
3+
seo: ~
4+
ivory_ckeditor:
5+
config_name: "cmf_sonata_phpcr_admin_integration"

0 commit comments

Comments
 (0)