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

Access to dateDeleted? #4493

Closed
boxadesign opened this issue Jul 1, 2019 · 8 comments
Closed

Access to dateDeleted? #4493

boxadesign opened this issue Jul 1, 2019 · 8 comments
Labels
enhancement improvements to existing features site development 👩‍💻 features related to website/API development

Comments

@boxadesign
Copy link

I have an endpoint that sets a response header when an entry is saved (code below).

Is there a similar method I can use when an entry is deleted?

'api/entries.json' => function() {
    return [
        'elementType' => Entry::class,
        'criteria' => ['order' => 'dateUpdated asc', 'limit' => 1],
        'transformer' => function(Entry $entry) {
            \Craft::$app->response->headers->set('Last-Modified', date('D, j F Y H:i:s \G\M\T', $entry->dateUpdated->getTimestamp()));
            return [
                'updated' => $entry->dateUpdated,
            ];
        }
    ];
},
@brandonkelly
Copy link
Member

You can access deleted elements by setting the trashed param:

'criteria' => [
    'trashed' => true,
    // ...
],

Trashed entries will have their dateDeleted attribute set.

@boxadesign
Copy link
Author

Great thanks for that. So with the dateDeleted I should be able to return the following:

                    return [
                        'deleted' => $entry->dateDeleted
                    ];

I'm getting "Getting unknown property: craft\\elements\\Entry::dateDeleted" when trying that.

Here is my complete code:

        'api/entries-deleted.json' => function() {
            return [
                'elementType' => Entry::class,
                'criteria' => ['section' => 'contentPage', 'trashed' => true, 'limit' => 1],
                'transformer' => function(Entry $entry) {
                    \Craft::$app->response->headers->set('Last-Modified', date('D, j F Y H:i:s \G\M\T', $entry->dateDeleted->getTimestamp()));                    
                    return [
                        'deleted' => $entry->dateDeleted
                    ];
                }
            ];
        },        

@boxadesign
Copy link
Author

My current workaround was to fetch all disabled entries which worked ok but not ideal...

        'api/entries-deleted.json' => function() {
            return [
                'elementType' => Entry::class,
                'criteria' => ['section' => 'contentPage', 'enabledForSite' => false, 'status' => 'disabled','order' => 'dateUpdated asc', 'limit' => 1],
                'transformer' => function(Entry $entry) {
                    \Craft::$app->response->headers->set('Last-Modified', date('D, j F Y H:i:s \G\M\T', $entry->dateUpdated->getTimestamp()));                    
                    return [
                        'deleted' => $entry->dateUpdated
                    ];
                }
            ];
        },        

@brandonkelly
Copy link
Member

Are you running Craft 3.1?

@boxadesign
Copy link
Author

Yes, running Craft Pro 3.1.33

@brandonkelly brandonkelly reopened this Jul 7, 2019
@brandonkelly brandonkelly transferred this issue from craftcms/element-api Jul 7, 2019
@brandonkelly brandonkelly added site development 👩‍💻 features related to website/API development enhancement improvements to existing features labels Jul 7, 2019
@brandonkelly brandonkelly added this to the 3.2 milestone Jul 7, 2019
brandonkelly added a commit that referenced this issue Jul 7, 2019
@brandonkelly
Copy link
Member

Sorry, I didn’t realize that we were actually unsetting the dateDeleted value when retrieving trashed elements. I’ve just changed that for Craft 3.2.0, which should be released on Tuesday.

To update to 3.2 a little early, change your craftcms/cms requirement in composer.json to:

"require": {
  "craftcms/cms": "3.2.x-dev#636ad42cf2656ec7a5e635ee2a7f81888bd9c59c as 3.2.0-RC3",
  "...": "..."
}

Then run composer update.

@boxadesign
Copy link
Author

OK cool thanks.

@boxadesign
Copy link
Author

@brandonkelly Works great. Thanks for your help on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement improvements to existing features site development 👩‍💻 features related to website/API development
Projects
None yet
Development

No branches or pull requests

2 participants