WordPress at a glance
function is not described

WP_REST_Controller::add_additional_fields_to_object() protected WP 4.7.0

Adds the values from additional fields to a data object.

{} It's a method of the class: WP_REST_Controller{}

No Hooks.

Return

Array. Modified data object with additional fields.

Usage

// protected - for code of main (parent) or child class
$result = $this->add_additional_fields_to_object( $prepared, $request );
$prepared(array) (required)
Prepared response array.
$request(WP_REST_Request) (required)
Full details about the request.

Changelog

Since 4.7.0 Introduced.

Code of WP_REST_Controller::add_additional_fields_to_object() WP 5.5.1

<?php
protected function add_additional_fields_to_object( $prepared, $request ) {

	$additional_fields = $this->get_additional_fields();

	$requested_fields = $this->get_fields_for_response( $request );

	foreach ( $additional_fields as $field_name => $field_options ) {
		if ( ! $field_options['get_callback'] ) {
			continue;
		}

		if ( ! rest_is_field_included( $field_name, $requested_fields ) ) {
			continue;
		}

		$prepared[ $field_name ] = call_user_func( $field_options['get_callback'], $prepared, $field_name, $request, $this->get_object_type() );
	}

	return $prepared;
}