diff --git a/AmFormsPlugin.php b/AmFormsPlugin.php index 1ca4d23..2c8217e 100644 --- a/AmFormsPlugin.php +++ b/AmFormsPlugin.php @@ -33,7 +33,7 @@ public function getReleaseFeedUrl() */ public function getVersion() { - return '1.4.0'; + return '1.4.1'; } /** diff --git a/README.md b/README.md index b357ce9..ebfc660 100644 --- a/README.md +++ b/README.md @@ -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') %} + + +{{ form.displayField('fieldHandle') }} +``` + ### Custom HTML ``` @@ -110,6 +119,9 @@ This will only display basic fields! {# Insert your form's handle. #} + {# This will namespace your inputs (for IDs and such), but it's not required though #} + + {# Optional: Anti-spam protection. #} {{ craft.amForms.displayAntispam() }} diff --git a/models/AmForms_FormModel.php b/models/AmForms_FormModel.php index d197097..c99785c 100644 --- a/models/AmForms_FormModel.php +++ b/models/AmForms_FormModel.php @@ -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. * diff --git a/releases.json b/releases.json index 42fd60c..e7a7e5e 100644 --- a/releases.json +++ b/releases.json @@ -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", diff --git a/services/AmForms_FormsService.php b/services/AmForms_FormsService.php index 11429b7..e9fc733 100644 --- a/services/AmForms_FormsService.php +++ b/services/AmForms_FormsService.php @@ -7,6 +7,7 @@ class AmForms_FormsService extends BaseApplicationComponent { private $_fields = array(); + private $_namespaces = array(); /** * Returns a criteria model for AmForms_Form elements. @@ -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. * @@ -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); @@ -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; @@ -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])) { diff --git a/variables/AmFormsVariable.php b/variables/AmFormsVariable.php index 66dc6d1..997683e 100644 --- a/variables/AmFormsVariable.php +++ b/variables/AmFormsVariable.php @@ -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. *