Returns the current values for the element fields, optionally for a specific submission.
Description
See also
Parameters
- $element
-
(awsmug\Torro_Forms\DB_Objects\Elements\Element) (Required) The element object to get values for.
- $submission
-
(awsmug\Torro_Forms\DB_Objects\Submissions\Submission|null) (Optional) Submission to get the values from, if available.
Default value: null
Return
(array) Associative array of <code>$field => $value</code> pairs, with the main element field having the key '_main'.
Source
File: src/db-objects/elements/element-types/element-type.php
public function get_values( $element, $submission = null ) { $values = array(); if ( $submission ) { $all_values = $submission->get_element_values_data(); if ( isset( $all_values[ $element->id ] ) ) { $values = $all_values[ $element->id ]; } } if ( has_filter( "{$this->manager->get_prefix()}allow_get_params" ) && isset( $_GET[ 'torro_input_value_' . $element->id ] ) && ( is_array( $_GET[ 'torro_input_value_' . $element->id ] ) || empty( $values['_main'] ) ) ) { $container = $element->get_container(); if ( $container ) { $form = $container->get_form(); if ( $form ) { /** * Filters whether to allow GET parameters to pre-populate form element values. * * @since 1.0.0 * * @param bool $allow_get_paramss Whether to allow GET parameters. Default false. * @param int $element_id Element ID for which GET parameters are being checked. * @param int $form_id Form ID the element is part of. */ $allow_get_params = apply_filters( "{$this->manager->get_prefix()}allow_get_params", false, $element->id, $form->id ); if ( $allow_get_params ) { $choices = is_a( $this, Choice_Element_Type_Interface::class ) ? $this->get_choices( $element ) : array(); $get_params = wp_unslash( $_GET[ 'torro_input_value_' . $element->id ] ); if ( is_array( $get_params ) ) { foreach ( $get_params as $field => $value ) { if ( empty( $values[ $field ] ) ) { if ( ! empty( $choices[ $field ] ) ) { if ( isset( $choices[ $field ][ $value ] ) ) { $values[ $field ] = $choices[ $field ][ $value ]; } elseif ( in_array( $value, $choices[ $field ], true ) ) { $values[ $field ] = $value; } continue; } $values[ $field ] = $value; } } } elseif ( empty( $values['_main'] ) ) { if ( ! empty( $choices['_main'] ) ) { if ( isset( $choices['_main'][ $get_params ] ) ) { $values['_main'] = $choices['_main'][ $get_params ]; } elseif ( in_array( $get_params, $choices['_main'], true ) ) { $values[ $field ] = $get_params; } } else { $values['_main'] = $get_params; } } } } } } return $values; }
Changelog
Version | Description |
---|---|
1.0.0 | Introduced. |