Skip to content

Instantly share code, notes, and snippets.

@duncanmcclean
Created February 4, 2023 22:45
Show Gist options
  • Save duncanmcclean/f68c25b58db5141f9c7ca9d09cf4a25f to your computer and use it in GitHub Desktop.
Save duncanmcclean/f68c25b58db5141f9c7ca9d09cf4a25f to your computer and use it in GitHub Desktop.

Customising the fields returned when an entry is 'shallow augmented'

  1. Create a new app/Statamic folder.
  2. Inside it, create two files:
    • EntryRepository.php
    • Entry.php
  3. For both of the two files, copy the contents from the relevant files in this Gist.
  4. Then, in your app/Providers/AppServiceProvider.php file, inside it's boot method, add the Statamic::repository bit.
  5. Run composer dump-autoload
  6. You should see the
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Statamic\Statamic;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// Statamic::script('app', 'cp');
// Statamic::style('app', 'cp');
// Add this bit... start
Statamic::repository(
\Statamic\Contracts\Entries\EntryRepository::class,
\App\Statamic\EntryRepository::class
);
// End
}
}
<?php
namespace App\Statamic;
class Entry extends \Statamic\Entries\Entry
{
public function shallowAugmentedArrayKeys()
{
// Note: we've added `price` here. Feel free to add any other fields you like.
return ['id', 'title', 'url', 'permalink', 'api_url', 'price'];
}
}
<?php
namespace App\Statamic;
use Statamic\Contracts\Entries\Entry;
use Statamic\Contracts\Entries\QueryBuilder;
use Statamic\Stache\Query\EntryQueryBuilder;
class EntryRepository extends \Statamic\Stache\Repositories\EntryRepository
{
public static function bindings(): array
{
return [
Entry::class => \App\Statamic\Entry::class,
QueryBuilder::class => EntryQueryBuilder::class,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment