Skip to content

SOLVED: Error when retrieving existing values from a media field #601

@tonnysantana2004

Description

@tonnysantana2004

The error:

Uncaught TypeError: Jet_Form_Builder\Blocks\Render\Media_Field_Render::get_name_from_file(): Argument #1 ($file_url) must be of type string, null given, called in /home/u910012032/domains/site.com.br/public_html/wp-content/plugins/jetformbuilder/includes/blocks/render/media-field-render.php on line 71

Solved adding the following operator ( ?? '' ):

protected function render_previews(): string {
		$files = $this->block_type->block_attrs['default'];

		if ( empty( $files ) ) {
			return '';
		}

		$preview = $this->get_preview_html();
		$html    = '';

		foreach ( $files as $file ) {

			if (
				empty( $file['url'] ) &&
				isset( $file['id'] ) &&
				is_array( $file['id'] )
			) {
				$file = $file['id'];
			}
			$file_url = isset( $file['url'] ) ? $file['url'] : wp_get_attachment_url( $file );
			$updated = str_replace( '%file_url%', $file_url, $preview );
			$updated = str_replace(
				'%file_name%',
                                // Error here
				$this->get_name_from_file( $file['url'] ?? '' ),
				$updated
			);

			// preset field
			$updated = str_replace( '<!-- field -->', $this->get_field_preset( $file ), $updated );

			$image_ext    = array( 'jpg', 'jpeg', 'jpe', 'gif', 'png', 'svg', 'webp' );
			$img_ext_preg = '!\.(' . join( '|', $image_ext ) . ')$!i';

			if ( preg_match( $img_ext_preg, $file['url'] ) ) {
				$replace = sprintf( '<img src="%s" alt="" width="100px" height="100px">', $file['url'] );

				$updated = str_replace( '<!-- preview -->', $replace, $updated );
			}

			$html .= $updated;
		}

		return $html;
	}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions