Habari\FormUI::get PHP Method

get() public method

Produce a form with the contained fields.
public get ( Theme $theme = null ) : string
$theme Theme The theme to render the controls into
return string HTML form generated from all controls assigned to this form
    public function get(Theme $theme = null)
    {
        // Allow plugins to modify the form
        Plugins::act('modify_form_' . Utils::slugify($this->formtype, '_'), $this);
        Plugins::act('modify_form', $this);
        // Get the theme used to render the form
        if (is_null($theme)) {
            $theme = $this->get_theme();
        }
        $theme->start_buffer();
        $theme->success = false;
        $this->success = false;
        $this->submitted = false;
        $this->success_render = false;
        // Set the ID of the control explicitly if it's not already set
        $this->get_id();
        // If the form template wasn't explicitly set, set it, because this class' name can't be used to determine its template
        if (!isset($this->settings['template'])) {
            $this->set_template('control.form');
        }
        // If the action of this form wasn't explicitly set, unset it to avoid validation errors in output
        if (empty($this->properties['action'])) {
            unset($this->properties['action']);
        }
        // Add the control ID to the template output for the form
        $this->vars['_control_id'] = $this->control_id();
        // Load all of the initial values of controls from their storage locations
        $this->load();
        // If there was an error submitting this form before, set the values of the controls to the old ones to retry
        $this->set_from_error_values();
        // Is this form not a same-page duplicate?
        if (!$this->get_setting('is_dupe', false)) {
            // Was the form submitted?
            if (isset($_POST['_form_id']) && $_POST['_form_id'] == $this->control_id()) {
                $this->submitted = true;
                // Process all of the submitted values into the controls
                $this->process();
                // Do any of the controls fail validation?  This call alters the wrap
                $validation_errors = $this->validate();
                if (count($validation_errors) == 0) {
                    // All of the controls validate
                    $this->success = true;
                    // If do_success() returns anything, it should be output instead of the form.
                    $this->success_render = $this->do_success($this);
                } else {
                    if (isset($this->settings['use_session_errors']) && $this->settings['use_session_errors']) {
                        $this->each(function ($control) {
                            $control->errors = array();
                        });
                        foreach ($validation_errors as $error) {
                            Session::error($error);
                        }
                    }
                }
            } else {
                // Store the location at which this form was loaded, so we can potentially redirect to it later
                if (!$this->has_session_data() || !isset($_SESSION['forms'][$this->control_id()]['url'])) {
                    $_SESSION['forms'][$this->control_id()]['url'] = Site::get_url('habari', true) . Controller::get_stub() . '#' . $this->get_id(false);
                }
            }
            $output = $this->pre_out_controls();
            if ($this->success_render) {
                $output .= $this->success_render;
            } else {
                $output .= parent::get($theme);
            }
            if ($this->success && isset($this->settings['success_message'])) {
                $output .= $this->settings['success_message'];
            }
        } else {
            $output = parent::get($theme);
        }
        if (class_exists('\\tidy')) {
            $t = new \tidy();
            $t->parseString($output, array('indent' => true, 'wrap' => 80, 'show-body-only' => true));
            //$output = (string) $t;
        }
        return $output;
    }

Usage Example

 /**
  * Implement the simple plugin configuration.
  * @return FormUI The configuration form
  */
 public function configure()
 {
     $ui = new FormUI('defensio');
     // Add a text control for the address you want the email sent to
     $api_key = $ui->append('text', 'api_key', 'option:' . self::OPTION_API_KEY, _t('Defensio API Key: ', 'defensio'));
     $api_key->add_validator('validate_required');
     $api_key->add_validator(array($this, 'validate_api_key'));
     // min spamines flag
     $spaminess = $ui->append('select', 'min_spaminess', 'option:' . self::OPTION_FLAG_SPAMINESS, _t('Minimum Spaminess to Flag as Spam (%): ', 'defensio'));
     $spaminess->options = array_combine(range(0, 100, 5), range(0, 100, 5));
     $spaminess->add_validator('validate_required');
     // using yes/no is not ideal but it's what we got :(
     $announce_posts = $ui->append('select', 'announce_posts', 'option:' . self::OPTION_ANNOUNCE_POSTS, _t('Announce New Posts To Defensio: ', 'defensio'));
     $announce_posts->options = array('yes' => _t('Yes', 'defensio'), 'no' => _t('No', 'defensio'));
     $announce_posts->add_validator('validate_required');
     $auto_approve = $ui->append('select', 'auto_approve', 'option:' . self::OPTION_AUTO_APPROVE, _t('Automatically Approve Non-Spam Comments: ', 'defensio'));
     $auto_approve->options = array('no' => _t('No', 'defensio'), 'yes' => _t('Yes', 'defensio'));
     $auto_approve->add_validator('validate_required');
     $register = $ui->append('static', 'register', '<a href="http://defensio.com/signup">' . _t('Get A New Defensio API Key.', 'defensio') . '</a>');
     $ui->append('submit', 'save', _t('Save', 'defensio'));
     $ui->on_success(array($this, 'formui_submit'));
     return $ui->get();
 }