Skip to content

Commit

Permalink
Add nova_get_regions helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Tarpsvo committed Apr 25, 2019
1 parent 0e722f1 commit 334e47a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/helpers.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use OptimistDigital\NovaPageManager\Models\Page;
use OptimistDigital\NovaPageManager\Models\Region;
use Illuminate\Support\Collection;

if (!function_exists('nova_get_pages_structure')) {
Expand Down Expand Up @@ -33,3 +34,27 @@ function nova_get_pages_structure()
return $formatPages($parentPages);
}
}

if (!function_exists('nova_get_regions')) {
function nova_get_regions()
{
$formatRegions = function (Collection $regions) {
$data = [];
$regions->each(function ($region) use (&$data) {
$localeChildren = Region::where('locale_parent_id', $region->id)->get();
$_regions = collect([$region, $localeChildren])->flatten();
$data[] = [
'locales' => $_regions->pluck('locale'),
'id' => $_regions->pluck('id', 'locale'),
'name' => $_regions->pluck('name', 'locale'),
'template' => $region->template,
'data' => $_regions->pluck('data', 'locale'),
];
});
return $data;
};

$parentRegions = Region::whereNull('locale_parent_id')->get();
return $formatRegions($parentRegions);
}
}

0 comments on commit 334e47a

Please sign in to comment.