1212/**
1313 * A field for entering a recaptcha text.
1414 */
15- class EWZRecaptchaType extends AbstractType
15+ class EWZRecaptchaType extends AbstractEWZRecaptchaType
1616{
17- /**
18- * The reCAPTCHA server URL.
19- *
20- * @var string
21- */
22- protected $ recaptchaApiServer ;
23-
2417 /**
2518 * The reCAPTCHA JS server URL.
2619 *
2720 * @var string
2821 */
2922 protected $ recaptchaApiJsServer ;
3023
31- /**
32- * The public key.
33- *
34- * @var string
35- */
36- protected $ publicKey ;
37-
38- /**
39- * The API server host name.
40- *
41- * @var string
42- */
43- protected $ apiHost ;
44-
45- /**
46- * Enable recaptcha?
47- *
48- * @var bool
49- */
50- protected $ enabled ;
51-
5224 /**
5325 * Use AJAX api?
5426 *
@@ -69,43 +41,31 @@ class EWZRecaptchaType extends AbstractType
6941 */
7042 public function __construct ($ publicKey , $ enabled , $ ajax , LocaleResolver $ localeResolver , $ apiHost = 'www.google.com ' )
7143 {
72- $ this ->publicKey = $ publicKey ;
73- $ this ->enabled = $ enabled ;
44+ parent ::__construct ($ publicKey , $ enabled , $ apiHost );
7445 $ this ->ajax = $ ajax ;
75- $ this ->apiHost = $ apiHost ;
7646 $ this ->localeResolver = $ localeResolver ;
77- $ this ->recaptchaApiJsServer = sprintf ('//%s/recaptcha/api/js/recaptcha_ajax.js ' , $ apiHost );
78- $ this ->recaptchaApiServer = sprintf ('https://%s/recaptcha/api.js ' , $ apiHost );
7947 }
8048
8149 /**
8250 * {@inheritdoc}
8351 */
84- public function buildView (FormView $ view , FormInterface $ form , array $ options )
52+ protected function addCustomVars (FormView $ view , FormInterface $ form , array $ options )
8553 {
8654 $ view ->vars = array_replace ($ view ->vars , array (
87- 'ewz_recaptcha_enabled ' => $ this ->enabled ,
8855 'ewz_recaptcha_ajax ' => $ this ->ajax ,
89- 'ewz_recaptcha_apihost ' => $ this ->apiHost ,
9056 ));
9157
92- if (!$ this ->enabled ) {
93- return ;
94- }
95-
9658 if (!isset ($ options ['language ' ])) {
9759 $ options ['language ' ] = $ this ->localeResolver ->resolve ();
9860 }
9961
10062 if (!$ this ->ajax ) {
10163 $ view ->vars = array_replace ($ view ->vars , array (
102- 'url_challenge ' => sprintf ('%s?hl=%s ' , $ this ->recaptchaApiServer , $ options ['language ' ]),
103- 'public_key ' => $ this ->publicKey ,
64+ 'url_challenge ' => sprintf ('%s?hl=%s ' , $ this ->recaptchaApiServer , $ options ['language ' ])
10465 ));
10566 } else {
10667 $ view ->vars = array_replace ($ view ->vars , array (
107- 'url_api ' => $ this ->recaptchaApiJsServer ,
108- 'public_key ' => $ this ->publicKey ,
68+ 'url_api ' => sprintf ('//%s/recaptcha/api/js/recaptcha_ajax.js ' , $ this ->apiHost )
10969 ));
11070 }
11171 }
@@ -145,14 +105,6 @@ public function getParent()
145105 return TextType::class;
146106 }
147107
148- /**
149- * {@inheritdoc}
150- */
151- public function getBlockPrefix ()
152- {
153- return 'ewz_recaptcha ' ;
154- }
155-
156108 /**
157109 * Gets the Javascript source URLs.
158110 *
@@ -165,23 +117,4 @@ public function getScriptURL($key)
165117 return isset ($ this ->scripts [$ key ]) ? $ this ->scripts [$ key ] : null ;
166118 }
167119
168- /**
169- * Gets the public key.
170- *
171- * @return string The javascript source URL
172- */
173- public function getPublicKey ()
174- {
175- return $ this ->publicKey ;
176- }
177-
178- /**
179- * Gets the API host name.
180- *
181- * @return string The hostname for API
182- */
183- public function getApiHost ()
184- {
185- return $ this ->apiHost ;
186- }
187120}
0 commit comments