cms icon indicating copy to clipboard operation
cms copied to clipboard

Entries fieldtype: status filter to include unpublished entries

Open mikemartin opened this issue 4 years ago • 9 comments

Since 3.1.15, it looks like we're now filtering unpublished entries out when outputting entries from the Entries fieldtype. Unfortunately this has been a nasty breaking change for me as I'd expect that any Entries selected (published or unpublished) would be outputted and there seems to be no way to include unpublished entries at the moment.

Can we implement Jason's solution:

{{ features status:in="draft|published" }}
...
{{ /features }}
Screen Shot 2021-06-01 at 2 31 02 pm

mikemartin avatar Jun 29 '21 07:06 mikemartin

In the mean time, you can use a custom fieldtype.

Drop this into app/Fieldtypes/EntriesWithDrafts.php

<?php

namespace App\Fieldtypes;

use Statamic\Contracts\Data\Localization;
use Statamic\Facades\Entry;
use Statamic\Facades\Site;
use Statamic\Fieldtypes\Entries;

class EntriesWithDrafts extends Entries
{
    public function augmentValue($value)
    {
        if (! is_object($value)) {
            $value = Entry::find($value);
        }

        if ($value != null && $parent = $this->field()->parent()) {
            $site = $parent instanceof Localization ? $parent->locale() : Site::current()->handle();
            $value = $value->in($site);
        }

        return $value;
    }
}

In your blueprint where you want the entries field to output drafts, change type: entries to type: entries_with_drafts.

All I've done is make a custom fieldtype that overrides this single line:

https://github.com/statamic/cms/blob/dc341772fe1657279d79581d6ed860f2a46fdc66/src/Fieldtypes/Entries.php#L222

jasonvarga avatar Jun 29 '21 13:06 jasonvarga

@jasonvarga I've added the custom field entries_with_drafts and it works locally. However, on production it's throwing a entries_with_drafts fieldtype is missing error. Any idea why this could be?

mikemartin avatar Jul 06 '21 07:07 mikemartin

Did you commit it? 😄

Is it named EntriesWithDrafts.php with that casing? e.g. not Entrieswithdrafts.php

jasonvarga avatar Jul 06 '21 13:07 jasonvarga

Folder was named FieldTypes and not "Fieldtypes". 🤦‍♂️

mikemartin avatar Jul 07 '21 12:07 mikemartin

Hey @jasonvarga I've been using the "EntriesWithDrafts" fieldtype workaround for a while now. Is there anything we can do to support showing unpublished entries on the frontend? Here's an example of a use case that I use quite often.

We are using unpublished "Feature" entries for our feature lists because not all features need marketing pages.

Screen Shot 2022-03-18 at 3 58 32 pm

mikemartin avatar Mar 18 '22 05:03 mikemartin

Not at the moment, but with changes in 3.3, it should be much easier to support this. Thanks for the nudge.

You should be able to do {{ features status:in="draft|published" }} however it looks like we are adding the where('status', 'published') for you - if you added that parameter it would just add another where clause. We need to add a way to override the where.

jasonvarga avatar Mar 18 '22 15:03 jasonvarga

Was chatting w/ @mikemartin about this. What about a config field show_drafts or something, then in the augment method we use a when and check it?

edalzell avatar Mar 24 '22 00:03 edalzell

I'm cool with that.

jasonvarga avatar Mar 24 '22 13:03 jasonvarga

Was there ever any resolution on showing unpublished entries on the frontend using a custom fieldtype like the 'EntriesWithDrafts.php' shown here?

Looking to do a similar thing. First instinct was to do status:in="draft|published" but no luck. Have manually set a toggle on the entry to manage it instead, but it's not ideal (other knock-on effects with content queries elsewhere keeping the entry published).

Would love clean way of showing unpublished entries on the frontend with the entries field for posts/items that are 'Coming Soon', or shouldn't generate a route.

andrew-ireland avatar Aug 02 '22 02:08 andrew-ireland