Element_Type::filter_json( array $data, awsmug\Torro_Forms\DB_Objects\Elements\Element $element, awsmug\Torro_Forms\DB_Objects\Submissions\Submission|null $submission = null )

Filters the array representation of a given element of this type.

Description

See also

Parameters

$data

(array) (Required) Element data to filter.

$element

(awsmug\Torro_Forms\DB_Objects\Elements\Element) (Required) The element object to get the data for.

$submission

(awsmug\Torro_Forms\DB_Objects\Submissions\Submission|null) (Optional) Submission to get the values from, if available.

Default value: null

Return

(array) Array including all information for the element type.

Source

File: src/db-objects/elements/element-types/element-type.php

	public function filter_json( $data, $element, $submission = null ) {
		$data['template_suffix'] = $this->slug;

		$settings = $this->get_settings( $element );
		$values   = $this->get_values( $element, $submission );

		$data['value'] = ! empty( $values['_main'] ) ? $values['_main'] : '';

		$placeholder = ! empty( $settings['placeholder'] ) ? $settings['placeholder'] : '';

		/**
		 * Filters the placeholder for an element field.
		 *
		 * @since 1.0.0
		 *
		 * @param string $placeholder Original placeholder.
		 * @param int    $element_id  Element ID.
		 */
		$placeholder = apply_filters( "{$this->manager->get_prefix()}input_placeholder", $placeholder, $element->id );

		if ( ! empty( $placeholder ) ) {
			$data['input_attrs']['placeholder'] = $placeholder;
		}

		if ( ! empty( $settings['description'] ) ) {
			$data['description'] = $settings['description'];

			$data['input_attrs']['aria-describedby'] = $data['description_attrs']['id'];
		}

		if ( ! empty( $settings['required'] ) && 'no' !== $settings['required'] ) {
			$required_indicator = '<span class="screen-reader-text">' . __( '(required)', 'torro-forms' ) . '</span><span class="torro-required-indicator" aria-hidden="true">*</span>';

			/**
			 * Filters the required indicator for an element that must be filled.
			 *
			 * @since 1.0.0
			 *
			 * @param string $required_indicator Indicator HTML string. Default is a screen-reader-only
			 *                                   '(required)' text and an asterisk for visual appearance.
			 */
			$data['label_required'] = apply_filters( "{$this->manager->get_prefix()}required_indicator", $required_indicator );

			$data['input_attrs']['aria-required'] = 'true';
			$data['input_attrs']['required'] = true;
		}

		if ( ! empty( $settings['css_classes'] ) ) {
			if ( ! empty( $data['input_attrs']['class'] ) ) {
				$data['input_attrs']['class'] .= ' ';
			} else {
				$data['input_attrs']['class'] = '';
			}

			$data['input_attrs']['class'] .= $settings['css_classes'];
		}

		if ( $submission && $submission->has_errors( $element->id ) ) {
			$data['errors'] = $submission->get_errors( $element->id );

			$data['input_attrs']['aria-invalid'] = 'true';
		}

		$choices = array();
		if ( is_a( $this, Choice_Element_Type_Interface::class ) ) {
			$choices = $this->get_choices( $element );

			$data['choices'] = ! empty( $choices['_main'] ) ? $choices['_main'] : array();
		}

		if ( is_a( $this, Multi_Field_Element_Type_Interface::class ) ) {
			$data['additional_fields'] = $this->additional_fields_to_json( $element, $submission, $choices, $settings, $values );
		}

		return $data;
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.