Craft\AmForms_FormsService::displayField PHP Method

displayField() public method

Display a field.
public displayField ( AmForms_FormModel $form, string $handle ) : string
$form AmForms_FormModel
$handle string
return string
    public function displayField(AmForms_FormModel $form, $handle)
    {
        // Get submission model
        $submission = craft()->amForms_submissions->getActiveSubmission($form);
        // 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);
        $templatePath = $fieldTemplateInfo['path'];
        // Get the current templates path so we can restore it at the end of this function
        $siteTemplatesPath = method_exists(craft()->templates, 'getTemplatesPath') ? craft()->templates->getTemplatesPath() : craft()->path->getTemplatesPath();
        $pluginTemplatePath = craft()->path->getPluginsPath() . 'amforms/templates/_display/templates/';
        // Do we have the current form fields?
        if (!isset($this->_fields[$form->id])) {
            $this->_fields[$form->id] = array();
            $supportedFields = craft()->amForms_fields->getSupportedFieldTypes();
            // Get tabs
            foreach ($form->getFieldLayout()->getTabs() as $tab) {
                // Get tab's fields
                foreach ($tab->getFields() as $layoutField) {
                    // Get actual field
                    $field = $layoutField->getField();
                    if (!in_array($field->type, $supportedFields)) {
                        // We don't display unsupported fields
                        continue;
                    }
                    // Reset templates path for input and get field input
                    method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($pluginTemplatePath) : craft()->path->setTemplatesPath($pluginTemplatePath);
                    $fieldInfo = craft()->fields->populateFieldType($field, $submission);
                    craft()->templates->getTwig(null, array('safe_mode' => false))->addGlobal('required', $layoutField->required);
                    $input = $fieldInfo->getInputHtml($field->handle, $submission->getFieldValue($field->handle));
                    craft()->templates->getTwig(null, array('safe_mode' => false))->addGlobal('required', null);
                    // Get field HTML
                    method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($fieldTemplateInfo['path']) : craft()->path->setTemplatesPath($fieldTemplateInfo['path']);
                    $fieldHtml = craft()->templates->render($fieldTemplateInfo['template'], array('form' => $form, 'field' => $field, 'input' => $input, 'required' => $layoutField->required, 'element' => $submission, 'namespace' => $namespace));
                    if ($namespace) {
                        $fieldHtml = craft()->templates->namespaceInputs($fieldHtml);
                    }
                    // Add to fields
                    $this->_fields[$form->id][$field->handle] = $fieldHtml;
                }
            }
        }
        // Restore the templates path variable to it's original value
        method_exists(craft()->templates, 'setTemplatesPath') ? craft()->templates->setTemplatesPath($siteTemplatesPath) : craft()->path->setTemplatesPath($siteTemplatesPath);
        // Reset namespace
        if ($namespace) {
            craft()->templates->setNamespace(null);
        }
        // Return field!
        if (isset($this->_fields[$form->id][$handle])) {
            return new \Twig_Markup($this->_fields[$form->id][$handle], craft()->templates->getTwig()->getCharset());
        } else {
            return null;
        }
    }