Pods::form PHP Method

form() public method

Embed a form to add / edit a pod item from within your theme. Provide an array of $fields to include and override options where needed. For WP object based Pods, you can pass through the WP object field names too, such as "post_title" or "post_content" for example.
Since: 2.0
public form ( array $params = null, string $label = null, string $thank_you = null ) : boolean | mixed
$params array (optional) Fields to show on the form, defaults to all fields
$label string (optional) Save button label, defaults to "Save Changes"
$thank_you string (optional) Thank you URL to send to upon success
return boolean | mixed
    public function form($params = null, $label = null, $thank_you = null)
    {
        $defaults = array('fields' => $params, 'label' => $label, 'thank_you' => $thank_you, 'fields_only' => false);
        if (is_array($params)) {
            $params = array_merge($defaults, $params);
        } else {
            $params = $defaults;
        }
        $pod =& $this;
        $params = $this->do_hook('form_params', $params);
        $fields = $params['fields'];
        if (null !== $fields && !is_array($fields) && 0 < strlen($fields)) {
            $fields = explode(',', $fields);
        }
        $object_fields = (array) pods_var_raw('object_fields', $this->pod_data, array(), null, true);
        if (empty($fields)) {
            // Add core object fields if $fields is empty
            $fields = array_merge($object_fields, $this->fields);
        }
        $form_fields = $fields;
        // Temporary
        $fields = array();
        foreach ($form_fields as $k => $field) {
            $name = $k;
            $defaults = array('name' => $name);
            if (!is_array($field)) {
                $name = $field;
                $field = array('name' => $name);
            }
            $field = array_merge($defaults, $field);
            $field['name'] = trim($field['name']);
            $default_value = pods_var_raw('default', $field);
            $value = pods_var_raw('value', $field);
            if (empty($field['name'])) {
                $field['name'] = trim($name);
            }
            if (isset($object_fields[$field['name']])) {
                $field = array_merge($object_fields[$field['name']], $field);
            } elseif (isset($this->fields[$field['name']])) {
                $field = array_merge($this->fields[$field['name']], $field);
            }
            if (pods_var_raw('hidden', $field, false, null, true)) {
                $field['type'] = 'hidden';
            }
            $fields[$field['name']] = $field;
            if (empty($this->id) && null !== $default_value) {
                $this->row_override[$field['name']] = $default_value;
            } elseif (!empty($this->id) && null !== $value) {
                $this->row[$field['name']] = $value;
            }
        }
        unset($form_fields);
        // Cleanup
        $fields = $this->do_hook('form_fields', $fields, $params);
        $label = $params['label'];
        if (empty($label)) {
            $label = __('Save Changes', 'pods');
        }
        $thank_you = $params['thank_you'];
        $fields_only = $params['fields_only'];
        PodsForm::$form_counter++;
        ob_start();
        if (empty($thank_you)) {
            $success = 'success';
            if (1 < PodsForm::$form_counter) {
                $success .= PodsForm::$form_counter;
            }
            $thank_you = pods_query_arg(array('success*' => null, $success => 1));
            if (1 == pods_v($success, 'get', 0)) {
                $message = __('Form submitted successfully', 'pods');
                /**
                 * Change the text of the message that appears on succesful form submission.
                 *
                 * @param string $message
                 *
                 * @returns string the message
                 *
                 * @since 3.0.0
                 */
                $message = apply_filters('pods_pod_form_success_message', $message);
                echo '<div id="message" class="pods-form-front-success">' . $message . '</div>';
            }
        }
        pods_view(PODS_DIR . 'ui/front/form.php', compact(array_keys(get_defined_vars())));
        $output = ob_get_clean();
        if (empty($this->id)) {
            $this->row_override = array();
        }
        return $this->do_hook('form', $output, $fields, $label, $thank_you, $this, $this->id());
    }