From 186b338f48b8971465befa6111b52929e2ea84be Mon Sep 17 00:00:00 2001 From: touffies Date: Thu, 28 Nov 2013 12:03:32 +0100 Subject: [PATCH] Add new SEO form --- core/lib/Thelia/Config/Resources/form.xml | 2 +- core/lib/Thelia/Form/SeoFieldsTrait.php | 89 +++++++++++++++++++++++ core/lib/Thelia/Form/SeoForm.php | 81 +++++++++++++++++++++ 3 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 core/lib/Thelia/Form/SeoFieldsTrait.php create mode 100644 core/lib/Thelia/Form/SeoForm.php diff --git a/core/lib/Thelia/Config/Resources/form.xml b/core/lib/Thelia/Config/Resources/form.xml index cf9ccd489..ec281266e 100644 --- a/core/lib/Thelia/Config/Resources/form.xml +++ b/core/lib/Thelia/Config/Resources/form.xml @@ -17,6 +17,7 @@
+ @@ -39,7 +40,6 @@ - diff --git a/core/lib/Thelia/Form/SeoFieldsTrait.php b/core/lib/Thelia/Form/SeoFieldsTrait.php new file mode 100644 index 000000000..dd66324d8 --- /dev/null +++ b/core/lib/Thelia/Form/SeoFieldsTrait.php @@ -0,0 +1,89 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Form; + +use Symfony\Component\Validator\Constraints\NotBlank; +use Thelia\Core\Translation\Translator; + +/** + * A trait to add standard localized description fields to a form. + * + * @author Christophe Laffont + */ +trait SeoFieldsTrait +{ + /** + * Add seo meta title, meta description and meta keywords fields + * + * @param array $exclude name of the fields that should not be added to the form + */ + protected function addSeoFields($exclude = array()) + { + + if (! in_array('url', $exclude)) + $this->formBuilder + ->add('url', 'text', array( + 'label' => Translator::getInstance()->trans('Rewriten URL'), + 'label_attr' => array( + 'for' => 'rewriten_url_field' + ), + 'required' => false + ) + ); + + if (! in_array('meta_title', $exclude)) + $this->formBuilder + ->add('meta_title', 'text', array( + 'constraints' => array( + new NotBlank() + ), + 'label' => Translator::getInstance()->trans('Page Title'), + 'label_attr' => array( + 'for' => 'meta_title' + ) + ) + ); + + if (! in_array('meta_description', $exclude)) + $this->formBuilder + ->add('meta_description', 'text', array( + 'label' => Translator::getInstance()->trans('Meta Description'), + 'label_attr' => array( + 'for' => 'meta_description' + ), + 'required' => false + ) + ); + + if (! in_array('meta_keyword', $exclude)) + $this->formBuilder + ->add('meta_keyword', 'text', array( + 'label' => Translator::getInstance()->trans('Meta Keyword'), + 'label_attr' => array( + 'for' => 'meta_keyword' + ), + 'required' => false + ) + ); + } +} diff --git a/core/lib/Thelia/Form/SeoForm.php b/core/lib/Thelia/Form/SeoForm.php new file mode 100644 index 000000000..28c90c9ba --- /dev/null +++ b/core/lib/Thelia/Form/SeoForm.php @@ -0,0 +1,81 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Form; + +use Symfony\Component\Validator\Constraints\GreaterThan; +use Symfony\Component\Validator\Constraints\NotBlank; + +/** + * Class SeoForm + * @package Thelia\Form + * @author Christophe Laffont + */ +class SeoForm extends BaseForm +{ + use SeoFieldsTrait; + + /** + * + * in this function you add all the fields you need for your Form. + * Form this you have to call add method on $this->formBuilder attribute : + * + * $this->formBuilder->add("name", "text") + * ->add("email", "email", array( + * "attr" => array( + * "class" => "field" + * ), + * "label" => "email", + * "constraints" => array( + * new \Symfony\Component\Validator\Constraints\NotBlank() + * ) + * ) + * ) + * ->add('age', 'integer'); + * + * @return null + */ + protected function buildForm() + { + + $this->formBuilder + ->add("id", "hidden", array( + "constraints" => array( + new GreaterThan(array('value' => 0)) + ) + )) + ->add("locale", "hidden", array( + "constraints" => array( + new NotBlank() + ) + )) + ; + + // Add SEO Fields + $this->addSeoFields(); + } + + public function getName() + { + return "thelia_seo"; + } +}