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

[5.x] Ability to have a custom Entry class per collection #11203

Open
wants to merge 16 commits into
base: 5.x
Choose a base branch
from

Conversation

edalzell
Copy link
Contributor

@edalzell edalzell commented Dec 2, 2024

(This is from @jasonvarga below)

Create a custom entry class:

class BlogPost extends Entry
{
    public function shareToSocials() {
      //
    }
}

Use it in the collection:

title: Blog
+class: App\BlogPost

Now you can use your custom methods.

$entry = Entry::find('blog-post-1'); // App\BlogPost
$entry->shareToSocials();

But only for that collection.

$entry = Entry::find('page-1'); // Statamic\Entries\Entry
$entry->shareToSocials(); // MethodNotFoundException

@edalzell edalzell changed the base branch from 5.x to master December 2, 2024 01:02
@edalzell edalzell changed the title Ability to have a customer per collection class [6.x] Ability to have a customer per collection class Dec 2, 2024
@andjsch
Copy link
Contributor

andjsch commented Dec 2, 2024

This looks awesome!

@jasonvarga
Copy link
Member

Requiring a collection in Entry::make() feels a little awkward to me. I get why you did it though. But most other places where we have a make(), you would pass in the handle of the thing you're making - not a related thing.

I wonder if $entry->collection($collection) could return an instance of the entry in the new class instead?

So like...

$entry = Entry::make();     // Statamic\Entries\Entry
$entry->locale('en');       // Statamic\Entries\Entry
$entry->collection('blog'); // App\Entries\BlogPost
$entry->slug('foo');        // App\Entries\BlogPost
class Entry
{
    public function collection($collection = null)
    {
        // ...

+       $custom = $collection->class();
+
+       if ($custom && ! $this instanceof $class) {
+           return app($custom)
+               ->slug($this->slug())
+               ->date($this->date())
+               ->etc();
+       }

        return $this;
    }
}

@edalzell
Copy link
Contributor Author

edalzell commented Dec 2, 2024

I wonder if $entry->collection($collection) could return an instance of the entry in the new class instead?

Ya I like this, would mean I could revert all the test changes too, which I like.

I'll keep hacking on this, thanks for your feedback

@edalzell edalzell changed the base branch from master to 5.x December 2, 2024 22:44
@edalzell edalzell changed the title [6.x] Ability to have a customer per collection class [5.x] Ability to have a customer per collection class Dec 2, 2024
@edalzell edalzell marked this pull request as ready for review December 2, 2024 23:14
@duncanmcclean
Copy link
Member

Can you provide some more details about how this works in the PR description?

@jasonvarga
Copy link
Member

I believe the gist is...

Create a custom entry class:

class BlogPost extends Entry
{
    public function shareToSocials() {
      //
    }
}

Use it in the collection:

title: Blog
+class: App\BlogPost

Now you can use your custom methods.

$entry = Entry::find('blog-post-1'); // App\BlogPost
$entry->shareToSocials();

But only for that collection.

$entry = Entry::find('page-1'); // Statamic\Entries\Entry
$entry->shareToSocials(); // MethodNotFoundException

@ryanmitchell
Copy link
Contributor

This is probably as close as I'll ever get to you letting me have a model per collection...

@duncanmcclean duncanmcclean changed the title [5.x] Ability to have a customer per collection class [5.x] Ability to have a custom Entry class per collection Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants