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

return post media as one field #5011

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 11 additions & 6 deletions src/Ushahidi/Modules/V5/Actions/Post/HandlePostOnlyParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,17 @@ public function addHydrateRelationships(Post $post, array $hydrates)
$relations['enabled_languages'] = true;
break;
case 'post_media':
$post->post_media = $post->valuesPostMedia;
$post->post_media = $post->post_media->map(function ($media) {
$media = $media->toArray();
unset($media['post']); // Remove the 'post' property
return $media;
});
$post->post_media = $post->valuesPostMedia->first();
if ($post->post_media) {
$media = $post->post_media->toArray();
unset($media['post']);
// convert it to field structure
$media_field = $media['attribute'];
unset($media['attribute']);
unset($media['translations']);
$media_field['value'] = $media;
$post->post_media = $media_field;
}
break;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/Ushahidi/Modules/V5/Models/Post/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,10 @@ public function valuesPostsMedia()
public function valuesPostMedia()
{
return $this->hasMany('Ushahidi\Modules\V5\Models\PostValues\PostMedia', 'post_id', 'id')
->select('post_media.*');
->select('post_media.*')->with('attribute')->whereHas('attribute', function ($query) {
$query->where('input', 'image')
->orWhere('input', 'upload'); // for old uploaded images
});
}
public function valuesPostsSet()
{
Expand Down
Loading