From 1ac458aa7695e28f61fcde94616490b4dee7a792 Mon Sep 17 00:00:00 2001 From: Kristof Jochmans Date: Wed, 4 Jun 2014 16:54:04 +0200 Subject: [PATCH 1/2] added an icon font form field type --- Controller/IconFontController.php | 40 ++++++++ .../Compiler/MediaHandlerCompilerPass.php | 16 +++- Form/Type/IconFontType.php | 96 +++++++++++++++++++ Helper/IconFont/AbstractIconFontLoader.php | 24 +++++ Helper/IconFont/DefaultIconFontLoader.php | 56 +++++++++++ Helper/IconFont/IconFontLoaderInterface.php | 10 ++ Helper/IconFont/IconFontManager.php | 61 ++++++++++++ Resources/config/routing.yml | 7 +- Resources/config/services.yml | 21 +++- Resources/views/Form/formWidgets.html.twig | 66 +++++++++++++ .../views/IconFont/iconFontChooser.html.twig | 43 +++++++++ 11 files changed, 433 insertions(+), 7 deletions(-) create mode 100644 Controller/IconFontController.php create mode 100644 Form/Type/IconFontType.php create mode 100644 Helper/IconFont/AbstractIconFontLoader.php create mode 100644 Helper/IconFont/DefaultIconFontLoader.php create mode 100644 Helper/IconFont/IconFontLoaderInterface.php create mode 100644 Helper/IconFont/IconFontManager.php create mode 100644 Resources/views/IconFont/iconFontChooser.html.twig diff --git a/Controller/IconFontController.php b/Controller/IconFontController.php new file mode 100644 index 00000000..e411a949 --- /dev/null +++ b/Controller/IconFontController.php @@ -0,0 +1,40 @@ +query->get('loader'); + $loaderData = unserialize($request->query->get('loader_data')); + + $iconFontManager = $this->get('kunstmaan_media.icon_font_manager'); + if (empty($loader)) { + $loader = $iconFontManager->getDefaultLoader(); + } else { + $loader = $iconFontManager->getLoader($loader); + } + $loader->setData($loaderData); + + return array( + 'loader' => $loader + ); + } +} diff --git a/DependencyInjection/Compiler/MediaHandlerCompilerPass.php b/DependencyInjection/Compiler/MediaHandlerCompilerPass.php index fc7b21fb..d120aa80 100644 --- a/DependencyInjection/Compiler/MediaHandlerCompilerPass.php +++ b/DependencyInjection/Compiler/MediaHandlerCompilerPass.php @@ -18,14 +18,20 @@ class MediaHandlerCompilerPass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - if (false === $container->hasDefinition('kunstmaan_media.media_manager')) { - return; + if ($container->hasDefinition('kunstmaan_media.media_manager')) { + $definition = $container->getDefinition('kunstmaan_media.media_manager'); + + foreach ($container->findTaggedServiceIds('kunstmaan_media.media_handler') as $id => $attributes) { + $definition->addMethodCall('addHandler', array(new Reference($id))); + } } - $definition = $container->getDefinition('kunstmaan_media.media_manager'); + if ($container->hasDefinition('kunstmaan_media.icon_font_manager')) { + $definition = $container->getDefinition('kunstmaan_media.icon_font_manager'); - foreach ($container->findTaggedServiceIds('kunstmaan_media.media_handler') as $id => $attributes) { - $definition->addMethodCall('addHandler', array(new Reference($id))); + foreach ($container->findTaggedServiceIds('kunstmaan_media.icon_font.loader') as $id => $attributes) { + $definition->addMethodCall('addLoader', array(new Reference($id), $id)); + } } } } diff --git a/Form/Type/IconFontType.php b/Form/Type/IconFontType.php new file mode 100644 index 00000000..76eb16de --- /dev/null +++ b/Form/Type/IconFontType.php @@ -0,0 +1,96 @@ +iconFontManager = $iconFontManager; + } + + /** + * @return string + */ + public function getParent() + { + return 'text'; + } + + /** + * @return string + */ + public function getName() + { + return 'iconfont'; + } + + /** + * Sets the default options for this type. + * + * @param OptionsResolverInterface $resolver The resolver for the options. + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + parent::setDefaultOptions($resolver); + + $resolver->setDefaults(array( + 'loader' => null, + 'loader_data' => null + )); + } + + /** + * Builds the form. + * + * This method is called for each type in the hierarchy starting form the + * top most type. Type extensions can further modify the form. + * + * @param FormBuilderInterface $builder The form builder + * @param array $options The options + * + * @see FormTypeExtensionInterface::buildForm() + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + if (!$options['loader']) { + $loader = $this->iconFontManager->getDefaultLoader(); + } else { + $loader = $this->iconFontManager->getLoader($options['loader']); + } + $loader->setData($options['loader_data']); + + $builder->setAttribute('loader', $options['loader']); + $builder->setAttribute('loader_object', $loader); + $builder->setAttribute('loader_data', $options['loader_data']); + } + + /** + * {@inheritdoc} + */ + public function buildView(FormView $view, FormInterface $form, array $options) + { + $view->vars['loader'] = $form->getConfig()->getAttribute('loader'); + $view->vars['loader_object'] = $form->getConfig()->getAttribute('loader_object'); + $view->vars['loader_data'] = serialize($form->getConfig()->getAttribute('loader_data')); + } + +} diff --git a/Helper/IconFont/AbstractIconFontLoader.php b/Helper/IconFont/AbstractIconFontLoader.php new file mode 100644 index 00000000..b56cf414 --- /dev/null +++ b/Helper/IconFont/AbstractIconFontLoader.php @@ -0,0 +1,24 @@ +rootPath = dirname($kernel->getRootDir()); + } +} \ No newline at end of file diff --git a/Helper/IconFont/DefaultIconFontLoader.php b/Helper/IconFont/DefaultIconFontLoader.php new file mode 100644 index 00000000..7979b85d --- /dev/null +++ b/Helper/IconFont/DefaultIconFontLoader.php @@ -0,0 +1,56 @@ +cssPath = trim($data['css'], '/'); + + $cssPath = $this->rootPath . '/web/' . $this->cssPath; + if (!file_exists($cssPath)) { + throw new InvalidOptionsException(sprintf('Could not find the css file with this path "%s"', $cssPath)); + } + } + + /** + * @return string + */ + public function getCssLink() + { + return '/' . $this->cssPath; + } + + /** + * @return array + */ + public function getCssClasses() + { + $contents = file_get_contents($this->rootPath . '/web/' . $this->cssPath); + + preg_match_all('/\.([a-zA-Z0-9-_]+):before[ ]*\{[ \n]*content:/', $contents, $matches); + + return $matches[1]; + } +} diff --git a/Helper/IconFont/IconFontLoaderInterface.php b/Helper/IconFont/IconFontLoaderInterface.php new file mode 100644 index 00000000..8447b66e --- /dev/null +++ b/Helper/IconFont/IconFontLoaderInterface.php @@ -0,0 +1,10 @@ +loaders[$serviceId] = $loader; + } + + /** + * @param IconFontLoaderInterface $loader + */ + public function setDefaultLoader(IconFontLoaderInterface $loader) + { + $this->defaultLoader = $loader; + } + + /** + * @param string $serviceId + * @return IconFontLoaderInterface + */ + public function getLoader($serviceId) + { + return $this->loaders[$serviceId]; + } + + /** + * @return IconFontLoaderInterface[] + */ + public function getLoaders() + { + return $this->loaders; + } + + /** + * @return IconFontLoaderInterface|null + */ + public function getDefaultLoader() + { + return $this->defaultLoader; + } +} diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index c2784ba6..a6797eed 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -16,4 +16,9 @@ KunstmaanMediaBundle_media_chooser: KunstmaanMediaBundle_media_folder: resource: "@KunstmaanMediaBundle/Controller/FolderController.php" type: annotation - prefix: /admin/media/folder + prefix: /admin/media/folder + +KunstmaanMediaBundle_icon_font: + resource: "@KunstmaanMediaBundle/Controller/IconFontController.php" + type: annotation + prefix: /admin/media/icon-font diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 3e89e200..168c0069 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -3,9 +3,11 @@ parameters: kunstmaan_media.menu.adaptor.class: 'Kunstmaan\MediaBundle\Helper\Menu\MediaMenuAdaptor' kunstmaan_media.listener.doctrine.class: 'Kunstmaan\MediaBundle\EventListener\DoctrineMediaListener' kunstmaan_media.form.type.media.class: 'Kunstmaan\MediaBundle\Form\Type\MediaType' + kunstmaan_media.form.type.iconfont.class: 'Kunstmaan\MediaBundle\Form\Type\IconFontType' + kunstmaan_media.icon_font_manager.class: 'Kunstmaan\MediaBundle\Helper\IconFont\IconFontManager' + kunstmaan_media.icon_font.default_loader.class: 'Kunstmaan\MediaBundle\Helper\IconFont\DefaultIconFontLoader' kunstmaan_media.media_creator_service.class: 'Kunstmaan\MediaBundle\Helper\Services\MediaCreatorService' - services: liip_imagine.data.loader.stream.profile_photos: class: "%liip_imagine.data.loader.stream.class%" @@ -44,6 +46,23 @@ services: tags: - { name: form.type, alias: media } + form.type.iconfont: + class: "%kunstmaan_media.form.type.iconfont.class%" + arguments: ["@kunstmaan_media.icon_font_manager"] + tags: + - { name: form.type, alias: iconfont } + + kunstmaan_media.icon_font_manager: + class: "%kunstmaan_media.icon_font_manager.class%" + calls: + - [ setDefaultLoader, [ "@kunstmaan_media.icon_font.default_loader" ] ] + + kunstmaan_media.icon_font.default_loader: + class: "%kunstmaan_media.icon_font.default_loader.class%" + arguments: ["@kernel"] + tags: + - { name: 'kunstmaan_media.icon_font.loader' } + kunstmaan_media.media_creator_service: class: "%kunstmaan_media.media_creator_service.class%" arguments: ["@service_container"] diff --git a/Resources/views/Form/formWidgets.html.twig b/Resources/views/Form/formWidgets.html.twig index a3d4aa71..14c58c1e 100644 --- a/Resources/views/Form/formWidgets.html.twig +++ b/Resources/views/Form/formWidgets.html.twig @@ -1,3 +1,69 @@ +{% block iconfont_widget %} + {% spaceless %} +
+ + +
+
+ +
+ +
+
+
+
+ +

{{ value|default('') }}

+
+
+ + + {% endspaceless %} +{% endblock iconfont_widget %} + {% block media_widget %} {% spaceless %}
diff --git a/Resources/views/IconFont/iconFontChooser.html.twig b/Resources/views/IconFont/iconFontChooser.html.twig new file mode 100644 index 00000000..82ac7d69 --- /dev/null +++ b/Resources/views/IconFont/iconFontChooser.html.twig @@ -0,0 +1,43 @@ +{% extends 'KunstmaanAdminBundle:Default:layout.html.twig' %} + +{% block extracss %} + +{% endblock %} + +{% block extraparamsinbody %} + id="smallWindow" +{% endblock %} + +{% block body %} +
+ +
+ + + +
+ +
+ {% for class in loader.cssClasses %} +
+ {% endfor %} +
+
+{% endblock %} \ No newline at end of file From c55cc81d4f7ca1125fd4c8463b92b6a925cdde0d Mon Sep 17 00:00:00 2001 From: Kristof Jochmans Date: Thu, 5 Jun 2014 10:34:02 +0200 Subject: [PATCH 2/2] fixed javascript error --- Resources/views/Form/formWidgets.html.twig | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/Resources/views/Form/formWidgets.html.twig b/Resources/views/Form/formWidgets.html.twig index 14c58c1e..c7816e6c 100644 --- a/Resources/views/Form/formWidgets.html.twig +++ b/Resources/views/Form/formWidgets.html.twig @@ -18,11 +18,10 @@