Media::format_values_for_export( array $values, awsmug\Torro_Forms\DB_Objects\Elements\Element $element, string $export_format )

Formats values for an export.

Description

See also

Parameters

$values

(array) (Required) Associative array of $field => $value pairs, with the main element field having the key '_main'.

$element

(awsmug\Torro_Forms\DB_Objects\Elements\Element) (Required) Element the values belong to.

$export_format

(string) (Required) Export format identifier. May be 'xls', 'csv', 'json', 'xml' or 'html'.

Return

(array) Associative array of <code>$column_slug => $column_value</code> pairs. The number of items and the column slugs must match those returned from the get_export_columns() method.

Source

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

	public function format_values_for_export( $values, $element, $export_format ) {
		$value = isset( $values['_main'] ) ? (int) $values['_main'] : 0;

		$skip_escaping = false;

		if ( ! empty( $value ) ) {
			$attachment = get_post( $value );

			if ( $attachment ) {
				$output = wp_basename( get_attached_file( $attachment->ID ) );

				if ( 'html' === $export_format ) {
					$skip_escaping = true;

					if ( 'image' === substr( $attachment->post_mime_type, 0, 5 ) ) {
						$image_url = wp_get_attachment_image_url( $attachment->ID, 'full' );

						$output = '<img src="' . $image_url . '" style="max-width:300px;height:auto;" />';
					}

					$attachment_url = get_edit_post_link( $attachment->ID );

					/**
					 * Filters the URL to use when generating the export output for an attachment.
					 *
					 * @since 1.0.0
					 *
					 * @param string  $attachment_url URL to use for the attachment. Default is the edit URL.
					 * @param WP_Post $attachment     Attachment object the URL is for.
					 */
					$attachment_url = apply_filters( "{$this->manager->get_prefix()}export_attachment_url", $attachment_url, $attachment );

					$output = '<a href="' . esc_url( $attachment_url ) . '">' . $output . '</a>';
				}
			} else {
				$output = __( 'File deleted.', 'torro-forms' );
			}
		} else {
			$output = __( 'No file uploaded.', 'torro-forms' );
		}

		if ( ! $skip_escaping ) {
			$output = $this->escape_single_value_for_export( $output, $export_format );
		}

		return array(
			'element_' . $element->id . '__main' => $output,
		);
	}

Changelog

Changelog
Version Description
1.0.0 Introduced.