Skip to content

Commit

Permalink
Don't auto namespace fields on displayField
Browse files Browse the repository at this point in the history
  • Loading branch information
Hubert Prein committed Apr 26, 2016
1 parent a32f97b commit ec4e792
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 6 deletions.
2 changes: 1 addition & 1 deletion AmFormsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getReleaseFeedUrl()
*/
public function getVersion()
{
return '1.4.0';
return '1.4.1';
}

/**
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ This will only display basic fields!
{{ form.displayField('fieldHandle') }}
```

### Simple field tag (using a namespace)

```
{% set form = craft.amForms.getForm('formHandle') %}
<input type="hidden" name="namespace" value="{{ form.getNamespace() }}">
{{ form.displayField('fieldHandle') }}
```

### Custom HTML

```
Expand Down Expand Up @@ -110,6 +119,9 @@ This will only display basic fields!
{# Insert your form's handle. #}
<input type="hidden" name="handle" value="{{ form.handle }}">
{# This will namespace your inputs (for IDs and such), but it's not required though #}
<input type="hidden" name="namespace" value="{{ form.getNamespace() }}">
{# Optional: Anti-spam protection. #}
{{ craft.amForms.displayAntispam() }}
Expand Down
10 changes: 10 additions & 0 deletions models/AmForms_FormModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,16 @@ public function getRedirectUrl()
return null;
}

/**
* Get a namespace for this form.
*
* @return string
*/
public function getNamespace()
{
return craft()->amForms_forms->getNamespaceForForm($this);
}

/**
* Display a field.
*
Expand Down
10 changes: 10 additions & 0 deletions releases.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
[
{
"version": "1.4.1",
"downloadUrl": "https://github.com/am-impact/amforms/archive/master.zip",
"date": "2016-04-26T10:00:00+01:00",
"notes": [
"[Added] Get a namespace for your form, using `form.getNamespace()`.",
"[Improved] Updated README with examples for getting a namespace when using displayField.",
"[Fixed] Don't namespace the form fields automatically when using displayField."
]
},
{
"version": "1.4.0",
"downloadUrl": "https://github.com/am-impact/amforms/archive/master.zip",
Expand Down
34 changes: 29 additions & 5 deletions services/AmForms_FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
class AmForms_FormsService extends BaseApplicationComponent
{
private $_fields = array();
private $_namespaces = array();

/**
* Returns a criteria model for AmForms_Form elements.
Expand Down Expand Up @@ -200,6 +201,23 @@ public function deleteForm(AmForms_FormModel $form)
return false;
}

/**
* Get a namespace for a form.
*
* @param AmForms_FormModel $form
* @param bool $createNewOnEmpty
*
* @return false|string
*/
public function getNamespaceForForm(AmForms_FormModel $form, $createNewOnEmpty = true)
{
if (! isset($this->_namespaces[ $form->id ]) && $createNewOnEmpty) {
$this->_namespaces[ $form->id ] = 'form_'.StringHelper::randomString(10);
}

return isset($this->_namespaces[ $form->id ]) ? $this->_namespaces[ $form->id ] : false;
}

/**
* Display a field.
*
Expand All @@ -213,9 +231,11 @@ public function displayField(AmForms_FormModel $form, $handle)
// Get submission model
$submission = craft()->amForms_submissions->getActiveSubmission($form);

// Set namespace
$namespace = 'form_'.StringHelper::randomString(10);
craft()->templates->setNamespace($namespace);
// Set namespace, if one was set
$namespace = $this->getNamespaceForForm($form, false);
if ($namespace) {
craft()->templates->setNamespace($namespace);
}

// Get template path
$fieldTemplateInfo = craft()->amForms->getDisplayTemplateInfo('field', $form->fieldTemplate);
Expand Down Expand Up @@ -260,7 +280,9 @@ public function displayField(AmForms_FormModel $form, $handle)
'element' => $submission,
'namespace' => $namespace
));
$fieldHtml = craft()->templates->namespaceInputs($fieldHtml);
if ($namespace) {
$fieldHtml = craft()->templates->namespaceInputs($fieldHtml);
}

// Add to fields
$this->_fields[$form->id][$field->handle] = $fieldHtml;
Expand All @@ -272,7 +294,9 @@ public function displayField(AmForms_FormModel $form, $handle)
method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($siteTemplatesPath) : craft()->path->setTemplatesPath($siteTemplatesPath);

// Reset namespace
craft()->templates->setNamespace(null);
if ($namespace) {
craft()->templates->setNamespace(null);
}

// Return field!
if (isset($this->_fields[$form->id][$handle])) {
Expand Down
12 changes: 12 additions & 0 deletions variables/AmFormsVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ public function getAllForms()
return craft()->amForms_forms->getAllForms();
}

/**
* Get a namespace for a form.
*
* @param AmForms_FormModel $form
*
* @return string
*/
public function getNamespaceForForm(AmForms_FormModel $form)
{
return craft()->amForms_forms->getNamespaceForForm($form);
}

/**
* Get a form by its handle.
*
Expand Down

0 comments on commit ec4e792

Please sign in to comment.