WP_REST_Block_Pattern_Categories_Controller::prepare_item_for_response()publicWP 6.0.0

Prepare a raw block pattern category before it gets output in a REST API response.

Method of the class: WP_REST_Block_Pattern_Categories_Controller{}

No Hooks.

Return

WP_REST_Response|WP_Error. Response object on success, or WP_Error object on failure.

Usage

$WP_REST_Block_Pattern_Categories_Controller = new WP_REST_Block_Pattern_Categories_Controller();
$WP_REST_Block_Pattern_Categories_Controller->prepare_item_for_response( $item, $request );
$item(array) (required)
Raw category as registered, before any changes.
$request(WP_REST_Request) (required)
Request object.

Changelog

Since 6.0.0 Introduced.

WP_REST_Block_Pattern_Categories_Controller::prepare_item_for_response() code WP 6.1.1

public function prepare_item_for_response( $item, $request ) {
	$fields = $this->get_fields_for_response( $request );
	$keys   = array( 'name', 'label' );
	$data   = array();
	foreach ( $keys as $key ) {
		if ( rest_is_field_included( $key, $fields ) ) {
			$data[ $key ] = $item[ $key ];
		}
	}

	$context = ! empty( $request['context'] ) ? $request['context'] : 'view';
	$data    = $this->add_additional_fields_to_object( $data, $request );
	$data    = $this->filter_response_by_context( $data, $context );

	return rest_ensure_response( $data );
}