Skip to content

Commit

Permalink
Merge pull request #9 from martywallace/master
Browse files Browse the repository at this point in the history
Ability to provide rendering options to the render function
  • Loading branch information
matt-west authored Apr 3, 2019
2 parents b1893cd + 3ebbcee commit 47f135f
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ Add the following tag to your form where you’d like the reCAPTCHA to be displa
{{ craft.recaptcha.render() }}
```

Render parameters [per the documentation](https://developers.google.com/recaptcha/docs/display#render_param) are injectable to the `render()` function, e.g.

```twig
{{ craft.recaptcha.render({
theme: 'dark',
size: 'compact'
}) }}
```

You can also create the reCAPTCHA element yourself using the `sitekey` template variable. This is especially useful for implementing invisible recaptcha.

```twig
Expand Down
8 changes: 6 additions & 2 deletions src/services/CraftRecaptchaService.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,19 @@ class CraftRecaptchaService extends Component
// Public Methods
// =========================================================================

public function render()
public function render(array $options = [])
{
$settings = CraftRecaptcha::$plugin->getSettings();

\Craft::$app->view->registerJsFile('https://www.google.com/recaptcha/api.js');

$defaultOptions = [
'sitekey' => $settings->attributes['siteKey']
];

$vars = array(
'id' => 'gRecaptchaContainer',
'siteKey' => $settings->attributes['siteKey']
'options' => array_merge($defaultOptions, $options)
);

$oldMode = \Craft::$app->view->getTemplateMode();
Expand Down
2 changes: 1 addition & 1 deletion src/templates/_recaptcha.twig
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<div class="g-recaptcha" data-sitekey="{{siteKey}}" id="{{id}}"></div>
<div class="g-recaptcha" id="{{id}}" {% for key, value in options %}data-{{ key }}="{{ value }}" {% endfor %}></div>
6 changes: 3 additions & 3 deletions src/variables/CraftRecaptchaVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ class CraftRecaptchaVariable
*
* {{ craft.recaptcha.render() }}
*
* @param null $params
* @param array $options
* @return string
*/
public function render()
public function render(array $options = [])
{
return CraftRecaptcha::$plugin->craftRecaptchaService->render();
return CraftRecaptcha::$plugin->craftRecaptchaService->render($options);
}

public function sitekey()
Expand Down

0 comments on commit 47f135f

Please sign in to comment.