Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add acf fields from relationships #95

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions lib/endpoints/class-acf-to-rest-api-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected function format_id( $object ) {
$this->id = 'options';
break;
}

$this->id = apply_filters( 'acf/rest_api/id', $this->id );

return $this->id;
Expand All @@ -196,7 +196,7 @@ protected function get_fields( $request, $response = null, $object = null ) {
if ( $request instanceof WP_REST_Request ) {
$field = $request->get_param( 'field' );
}

if ( $swap ) {
$data = $response->get_data();
}
Expand All @@ -211,16 +211,23 @@ protected function get_fields( $request, $response = null, $object = null ) {

$this->format_id( $object );

$acf_data = array();
if ( $this->id ) {
if ( $field ) {
$data = array( $field => get_field( $field, $this->id ) );
$acf_data = get_field( $field, $this->id );
} else {
$data['acf'] = get_fields( $this->id );
$acf_data = get_fields( $this->id );
}
}

$this->add_relation_fields( $acf_data, $field );

if ( $field ) {
$data[ $field ] = $acf_data;
} else {
$data['acf'] = array();
$data['acf'] = $acf_data;
}

if ( $swap ) {
$response->data = $data;
$data = $response;
Expand All @@ -229,6 +236,24 @@ protected function get_fields( $request, $response = null, $object = null ) {
return apply_filters( 'acf/rest_api/' . $this->type . '/get_fields', $data, $request, $response, $object );
}

protected function add_relation_fields( &$acf_data, $field ) {
foreach ( $acf_data as $k => $rel_array ) {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@airesvsg, do you mean I have to take care here? Taking care = checking $acf_data for being an object?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope. You must have check the $rel_array variable.
When I said: "you need to pass the values by reference" in this case also I refer the $rel_array. #95 (comment)
I prefer that the method add_relation_fields returns the data.
Thanks

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understood the comment with the reference but wanted to avoid having a foreach outside and inside the function but I can change that of course.

if ( is_array( $rel_array ) ) {
foreach ( $rel_array as $index => $rel_content ) {
$sub_acf_data = get_fields( $rel_content->ID );

$this->add_relation_fields( $sub_acf_data, $field );

if ( $field ) {
$rel_content->$field = $sub_acf_data;
} else {
$rel_content->acf = $sub_acf_data;
}
}
}
}
}

protected function get_field_objects( $id ) {
if ( empty( $id ) ) {
return false;
Expand Down Expand Up @@ -283,4 +308,4 @@ protected function get_field_objects( $id ) {
}

}
}
}