Skip to content

Commit

Permalink
Merge branch 'release/0.1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
w00fz committed Aug 4, 2015
2 parents 190c9c2 + 95920a3 commit f59a163
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 46 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# v0.1.0
## 08/04/2015

1. [](#new)
* ChangeLog started...
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
grav-plugin-form
================
# Grav Form Plugin

Grav Form Plugin
The **form plugin** for [Grav](http://github.com/getgrav/grav) adds the ability to create and use forms. This is currently used extensively by the **admin** and **login** plugins.

| IMPORTANT!!! This plugin is currently in development as is to be considered a **beta release**. As such, use this in a production environment **at your own risk!**. More features will be added in the future.

# Installation

The form plugin is easy to install with GPM.

```
$ bin/gpm install form
```

# Configuration

Simply copy the `user/plugins/forms/forms.yaml` into `user/config/plugins/forms.yaml` and make your modifications.

```
enabled: true
```
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

15 changes: 12 additions & 3 deletions blueprints.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
name: Form Handling Plugin
name: Form
version: 1.0.0
description: Enables forms.
validation: strict
description: Enables the forms handling
icon: check-square
author:
name: Team Grav
email: [email protected]
url: http://getgrav.org
keywords: plugin, form
homepage: https://github.com/getgrav/grav-plugin-form
bugs: https://github.com/getgrav/grav-plugin-form/issues
license: MIT

form:
validation: strict
fields:
enabled:
type: toggle
Expand Down
14 changes: 7 additions & 7 deletions classes/form.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php
namespace Grav\Plugin;

use \Grav\Common\Iterator;
use \Grav\Common\Grav;
use \Grav\Common\Registry;
use \Grav\Common\Page\Page;
use Grav\Common\Iterator;
use Grav\Common\Grav;
use Grav\Common\GravTrait;
use Grav\Common\Page\Page;

class Form extends Iterator
{
use GravTrait;

/**
* @var array
*/
Expand Down Expand Up @@ -81,14 +83,12 @@ public function post()

$process = isset($this->items['process']) ? $this->items['process'] : array();
if (is_array($process)) {
/** @var Grav $grav */
$grav = Registry::instance()->get('Grav');
foreach ($process as $action => $data) {
if (is_numeric($action)) {
$action = \key($data);
$data = $data[$action];
}
$grav->fireEvent('onProcessForm', $this, $action, $data);
self::getGrav()->fireEvent('onFormProcessed', $this, $action, $data);
}
} else {
// Default action.
Expand Down
62 changes: 30 additions & 32 deletions form.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<?php
namespace Grav\Plugin;

use \Grav\Common\Plugin;
use \Grav\Common\Registry;
use \Grav\Common\Page\Page;
use \Grav\Common\Page\Pages;
use \Grav\Common\Grav;
use \Grav\Common\Uri;
use \Grav\Common\Filesystem\File;
use \Grav\Common\Twig;
use \Grav\Plugin\Form;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;
use Grav\Common\Grav;
use Grav\Common\Uri;
use Grav\Common\Twig;
use Grav\Plugin\Form;
use RocketTheme\Toolbox\File\File;

class FormPlugin extends Plugin
{

/**
* @var bool
*/
Expand All @@ -24,19 +24,25 @@ class FormPlugin extends Plugin
protected $form;

/**
* @var Grav
* @return array
*/
protected $grav;
public static function getSubscribedEvents()
{
return [
'onPageInitialized' => ['onPageInitialized', 0],
'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0],
'onFormProcessed' => ['onFormProcessed', 0]
];
}

/**
* Initialize form if the page has one. Also catches form processing if user posts the form.
*/
public function onAfterGetPage()
public function onPageInitialized()
{
$this->grav = Registry::get('Grav');

/** @var Page $page */
$page = $this->grav->page;
$page = $this->grav['page'];
if (!$page) {
return;
}
Expand All @@ -53,35 +59,27 @@ public function onAfterGetPage()
if (!empty($_POST)) {
$this->form->post();
}

$registry = Registry::instance();
$registry->store('Form', $this->form);
}
}

/**
* Add current directory to twig lookup paths.
*/
public function onAfterTwigTemplatesPaths()
public function onTwigTemplatePaths()
{
Registry::get('Twig')->twig_paths[] = __DIR__ . '/templates';
$this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
}

/**
* Make form accessible from twig.
*/
public function onAfterSiteTwigVars()
public function onTwigSiteVariables()
{
if (!$this->active) {
return;
}

/** @var Twig $twig */
$twig = Registry::get('Twig');
/** @var Form $form */
$form = Registry::get('Form');

$twig->twig_vars['form'] = $form;
$this->grav['twig']->twig_vars['form'] = $this->form;
}

/**
Expand All @@ -91,7 +89,7 @@ public function onAfterSiteTwigVars()
* @param string $task
* @param string $params
*/
public function onProcessForm(Form $form, $task, $params)
public function onFormProcessed(Form $form, $task, $params)
{
if (!$this->active) {
return;
Expand All @@ -113,11 +111,11 @@ public function onProcessForm(Form $form, $task, $params)
$route = (string) $params;
if (!$route || $route[0] != '/') {
/** @var Uri $uri */
$uri = Registry::get('Uri');
$uri = $this->grav['uri'];
$route = $uri->route() . ($route ? '/' . $route : '');
}
/** @var Pages $pages */
$pages = Registry::get('Pages');
$pages = $this->grav['pages'];
$this->grav->page = $pages->dispatch($route, true);
break;
case 'save':
Expand All @@ -127,12 +125,12 @@ public function onProcessForm(Form $form, $task, $params)
$filename = $prefix . $this->udate($format) . $ext;

/** @var Twig $twig */
$twig = Registry::get('Twig');
$twig = $this->grav['twig'];
$vars = array(
'form' => $this->form
);

$file = File\General::instance(DATA_DIR . $this->form->name . '/' . $filename);
$file = File::instance(DATA_DIR . $this->form->name . '/' . $filename);
$body = $twig->processString(
!empty($params['body']) ? $params['body'] : '{% include "forms/data.txt.twig" %}',
$vars
Expand Down
1 change: 1 addition & 0 deletions templates/forms/fields/password/password.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
name="{{ (scope ~ field.name)|fieldName }}"
value="{{ value|join(', ') }}"
{% if field.placeholder %}placeholder="{{ field.placeholder }}"{% endif %}
{% if field.title %}title="{{ field.title }}"{% endif %}
{% if field.autofocus in ['on', 'true', 1] %}autofocus="autofocus"{% endif %}
{% if field.novalidate in ['on', 'true', 1] %}novalidate="novalidate"{% endif %}
{% if field.autocomplete in ['on', 'off'] %}autocomplete="{{ field.autocomplete }}"{% endif %}
Expand Down
2 changes: 2 additions & 0 deletions templates/forms/fields/text/text.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
name="{{ (scope ~ field.name)|fieldName }}"
value="{{ value|join(', ') }}"
{% if field.placeholder %}placeholder="{{ field.placeholder }}"{% endif %}
{% if field.title %}title="{{ field.title }}"{% endif %}
{% if field.autofocus in ['on', 'true', 1] %}autofocus="autofocus"{% endif %}
{% if field.novalidate in ['on', 'true', 1] %}novalidate="novalidate"{% endif %}
{% if field.autocomplete in ['on', 'off'] %}autocomplete="{{ field.autocomplete }}"{% endif %}
{% if field.validate.required in ['on', 'true', 1] %}required="required"{% endif %}
{% if field.validate.pattern %}pattern="{{ field.validate.pattern }}"{% endif %}

/>
</label>
</div>

0 comments on commit f59a163

Please sign in to comment.