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

No draft was identified by the request error on Approve and publish if the Entry is set Disabled by default #180

Closed
romainpoirier opened this issue Jan 3, 2023 · 21 comments

Comments

@romainpoirier
Copy link

romainpoirier commented Jan 3, 2023

Describe the bug

I have a Channel where the Default Status is set to Disabled.

I have a front-end form setup following the explanations in the doc:

{{ actionInput('entries/save-entry') }}
{{ hiddenInput('sectionId', getenv('SECTION_ID')) }}
{{ hiddenInput('typeId', getenv('TYPE_ID')) }}
{{ hiddenInput('enabled', '0') }}
{{ hiddenInput('workflow-action', 'save-submission') }}

The newly submitted Entries are appearing disabled in the Channel. When reviewing them, and clicking on the Approve and publish button to approve them and set them live, I get this error:

[HTTP 400 – Bad Request](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#400) – [yii\web\BadRequestHttpException](https://www.yiiframework.com/doc-2.0/yii-web-badrequesthttpexception.html)

No draft was identified by the request.

if (!$element || !$element->getIsDraft()) {
    throw new BadRequestHttpException('No draft was identified by the request.');
}

The Entry doesn't appear as draft in the CP, but as a Disabled Entry. I'm getting an unexpected error about draft, while the entries/save-entry was triggered and enabled set to false.

However, if I enable the new Entry using the Enabled for all sites Lightswitch just before clicking the Approve and publish button, I don't get this error.

Is there any way to prevent the error, and make the Approve and publish button setting the Entry as Enabled simultaneously?

Steps to reproduce

  1. Setup a Channel where Entries are not Enabled by default
  2. Setup a front-end form
  3. Submit a new Entry on front-end
  4. Review the submission in the CP and click the Approve and publish button

Craft CMS version

4.3.5

Plugin version

2.0.3

Multi-site?

Yes

Additional context

No response

@romainpoirier romainpoirier changed the title Unexpected No draft was identified by the request error on Approve and publish No draft was identified by the request error on Approve and publish Jan 3, 2023
@romainpoirier romainpoirier changed the title No draft was identified by the request error on Approve and publish No draft was identified by the request error on Approve and publish Jan 3, 2023
@romainpoirier romainpoirier changed the title No draft was identified by the request error on Approve and publish No draft was identified by the request error on Approve and publish if the Entry is Disabled by default Jan 3, 2023
@romainpoirier romainpoirier changed the title No draft was identified by the request error on Approve and publish if the Entry is Disabled by default No draft was identified by the request error on Approve and publish if the Entry is Disabled by default Jan 3, 2023
@romainpoirier romainpoirier changed the title No draft was identified by the request error on Approve and publish if the Entry is Disabled by default No draft was identified by the request error on Approve and publish if the Entry is set Disabled by default Jan 3, 2023
@engram-design
Copy link
Member

Right, so this is one of those tricky edge cases where you're not working with a draft, like Workflow assumes, and how Craft otherwise works. The reason that as soon as you interact with the entry, is that a draft is created when you do that, and once a draft exist, and you hit "Approve", it applies that draft. Otherwise, when creating an entry from the front-end a draft isn't created so we hit this issue.

I've added some handling here for the next release. To get the fix early, change your verbb/workflow requirement in composer.json to:

"require": {
  "verbb/workflow": "dev-craft-4 as 2.0.3",
  "...": "..."
}

Then run composer update.

@romainpoirier
Copy link
Author

romainpoirier commented Jan 6, 2023

Thank you. I have updated to dev-craft-4 as 2.0.3, but unfortunately that makes no difference.
I still get the HTTP 400 – Bad Request error on clicking Approve and publish.

@engram-design
Copy link
Member

Hmm, can't seem to replicate that I'm afraid, after creating new submissions from the front-end. The changes should switch from using elements/save-draft or elements/apply-draft if entry.getIsDraft() is not true.

Are you able to check your Network requests to see if the action is being passed to elements/apply-draft or elements/save?

@romainpoirier
Copy link
Author

Thank you, it's now submitting to elements/save after clearing my composer caches.

However, I'm still facing the following challenges:

  1. When submitting a new disabled Entry on front-end, it's creating an unpublished Entry and the link received in the notification email is like /admin/entries/{channel}/10089-title?site=fr&revisionId=5592 (link to the single revision, instead of the current single version);
  2. On this link target, clicking on Approve and publish (or Approve only) just do nothing: it's not submitting anything, and the page is not reloading, no activity in the Network of my browser also → why and how to fix this?
  3. To fix this, I have to switch to the Current version, then click again Approve and publish: now it works, and the Entry is set Enabled (in the current site version only);
  4. However, in a multi-sites setup, it's setting Enabled in the current site only → as it's a brand new Entry, is there a way to Enabled for all sites automatically on the first Approve and publish?

@romainpoirier
Copy link
Author

Sorry, I was a bit optimistic and am still getting HTTP 400 – Bad Request error in this scenario:

  1. Submitting a new disabled Entry ({{ hiddenInput('enabled', '0') }}) from front end;
  2. In the CP, review this Entry by going to the Current version;
  3. Click on Enabled for all sites (Craft), then on Approve and publish (Workflow) which is submitting to elements/save;
  4. Get the error;

@engram-design
Copy link
Member

That's strange about the revision. Are you sure you're creating a brand new entry from the front end? On my end, there's no such revision information (also testing on a multi-site)

Hey Reviewer,

Editor has submitted the entry "Some Title" for approval on Default Site.

To review it please log into your control panel.

https://craft4.test/admin/entries/workflow/20587-some-title-2?site=default

As for it not doing anything, I'm not surprised with it being a revision - it's probably targeting the wrong entry. Is there a Save button at the top of the page? This should determine whether you (the current user) can actually save the entry, which is what Workflow leverages (it just submits the entry form with some extra params). If it's not doing anything, then it sounds like the entry form isn't available, or that current user isn't allowed to save the entry.

As for the Enabled for all sites that's both tricky and potentially risky. I'll have a think about that. There's nothing stopping you from setting the enabled state of the entry for all sites (using the expanded lightswitch thing) before approving. But this does require your publishers to do this upon approval.

Are you able to send through the full stack trace of the error? Is it still the same error complaining about no draft existing?

@romainpoirier
Copy link
Author

Are you sure you're creating a brand new entry from the front end?

Yes, as you can see with this setup (no entryId is passed):

{{ actionInput('entries/save-entry') }}
{{ hiddenInput('sectionId', getenv('SECTION_ID')) }}
{{ hiddenInput('typeId', getenv('TYPE_ID')) }}
{{ hiddenInput('enabled', '0') }}
{{ hiddenInput('workflow-action', 'save-submission') }}

But please note that in this multi-site config, Entries in this Channel are not enabled by default (Default Status is unchecked at /admin/settings/sections/{id}?site={handle}). Also, on front-end the Entry is submitted from a basic public User, which hasn't the Workflow or Admin permission.

Is there a Save button at the top of the page?

When clicking on the link in the notification email (/admin/entries/{channel}/{id}-title?site={handle}&revisionId={id}): no, there is only the Revert content from this version and it's redirected by default to Revision 1. After that, switching to the Current version show me the Save button (I am logged in as Admin). I think everything is fine here, that's the Craft expected behavior.

If it's not doing anything, then it sounds like the entry form isn't available, or that current user isn't allowed to save the entry.

The Craft's entry form is available, but is in Revision mode, which means that the Save button is not available at this step of an unpublished Entry. The current user is allowed: it is as Admin, and I have checked for this user every permission available.

There's nothing stopping you from setting the enabled state of the entry for all sites (using the expanded lightswitch thing) before approving. But this does require your publishers to do this upon approval.

I'm looking for a way to not have to do this manually. As in my case, every approved Entry should be published on every site (Entries in this Channel are not translated, and are the same on every site version).

Also, if I enable the new Entry using the Enabled for all sites Lightswitch just before clicking the Approve and publish button, I get this HTTP 400 – Bad Request error as explained in my previous scenario.

Are you able to send through the full stack trace of the error?

I have sent it to you by email. This full stack trace is the one gotten from my previous scenario.

@engram-design
Copy link
Member

Thanks for all the info. I'm still pretty dumfounded by how it's setting the revision, as that's one thing I can't replicate. There shouldn't even be a revision for a brand new entry where you haven't touched anything, which is what I don't understand.

Also, on front-end the Entry is submitted from a basic public User, which hasn't the Workflow or Admin permission.

I thought that might be the differentiating factor, but I've tested that as well. I'll keep at it.

I'm looking for a way to not have to do this manually. As in my case, every approved Entry should be published on every site (Entries in this Channel are not translated, and are the same on every site version).

I'll see what I can do, but we just need to be careful about setting the status across all sites automatically.

As for the actual issue of the latest 400 error (please let me know if this is also correct):

[HTTP 400 – Bad Request] - No element was identified by the request.

I can see why this happens. This is because as soon as you touch anything on the entry, it'll create a provisional draft. However, Workflow won't know about that and think you're still trying to modify an existing entry, not a draft.

I'm going to have to revisit creating submissions from the front end. It used to be the case where you could create a draft from the front-end, but that's no longer possible in Craft 4. It's pretty critical that front-end-created entries are created as unpublished drafts, just like the control panel for consistency.

I'll let you know how things go.

@engram-design
Copy link
Member

Okay, so I've created a custom endpoint to create and save a draft submission for, used for the front-end.

{{ actionInput('workflow/elements/save-entry') }}

You should only need to change the action input. Let me know how that goes!

@romainpoirier
Copy link
Author

I thought that might be the differentiating factor, but I've tested that as well. I'll keep at it.

FYI the multi-site is set up with this propagation method: Save entries to all sites enabled for this section.

You should only need to change the action input. Let me know how that goes!

Regarding the 400 error:

  • I'm still using dev-craft-4 as 2.0.3, which had been upgraded from 70b1ce7 to 7be7a66 after a composer update today;
  • I've tested while keeping the {{ hiddenInput('enabled', '0') }};
  • I now get a Unable to create draft entry. error on submit (no required field are kept empty before submit);

This is the stack trace:

[web.ERROR] [yii\web\HttpException:400] yii\web\BadRequestHttpException: Unable to create draft entry. in /vendor/verbb/workflow/src/controllers/ElementsController.php:36
Stack trace:
#0 [internal function]: verbb\workflow\controllers\ElementsController->actionSaveEntry()
#1 /vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#2 /vendor/yiisoft/yii2/base/Controller.php(178): yii\base\InlineAction->runWithParams(Array)
#3 /vendor/yiisoft/yii2/base/Module.php(552): yii\base\Controller->runAction('save-entry', Array)
#4 /vendor/craftcms/cms/src/web/Application.php(301): yii\base\Module->runAction('workflow/elemen...', Array)
#5 /vendor/craftcms/cms/src/web/Application.php(625): craft\web\Application->runAction('workflow/elemen...', Array)
#6 /vendor/craftcms/cms/src/web/Application.php(280): craft\web\Application->_processActionRequest(Object(craft\web\Request))
#7 /vendor/yiisoft/yii2/base/Application.php(384): craft\web\Application->handleRequest(Object(craft\web\Request))
#8 /htdocs/index.php(12): yii\base\Application->run()
#9 {main} {"memory":33040864,"exception":"[object] (yii\web\BadRequestHttpException(code: 0): Unable to create draft entry. at /vendor/verbb/workflow/src/controllers/ElementsController.php:36)"}
2023-01-16 09:43:28 [web.INFO] [application] Request context:
$_GET = []

$_POST = [
'CRAFT_CSRF_TOKEN' => '••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••'
'action' => 'workflow/elements/save-entry'
'sectionId' => '12'
'typeId' => '28'
'enabled' => '0'
'workflow-action' => 'save-submission'
'fields' => [
'entryLanguage' => 'fr'
'museum' => [
0 => '1309'
]
'translation' => ''
'jobsType' => [
0 => '6204'
]
'jobsDeadline' => [
'date' => '2023-01-31'
]
'jobsLink' => [
'value' => ''
]
]
'title' => 'Title'
]

@engram-design
Copy link
Member

engram-design commented Jan 18, 2023

You're still getting that issue after starting a new front-end submission, and having changed the form to use {{ actionInput('workflow/elements/save-entry') }}?

Sorry, looking at the error message, it looks like you are.

Because I'm struggling to replicate this one, maybe you could add some debugging to Craft's entries/create controller action, to see where it might be failing. I'd start maybe be adding some Craft::dd() statements in there to see where processing gets to.

@romainpoirier
Copy link
Author

Sorry, looking at the error message, it looks like you are.

Yes I am.

I'd start maybe be adding some Craft::dd() statements in there to see where processing gets to.

Sorry, I'm not sure of what I should do. I should edit the Craft source code in my /vendor? If yes, what variable should I Craft::dd() and at what step of this function? The $response? I don't see where the Unable to create draft entry. error I get is returned from here.

@engram-design
Copy link
Member

Ah, managed to figure out a scenario that wouldn't work, if you can try again after a composer update!

@romainpoirier
Copy link
Author

I have run a composer update, but have a few issues with my local composer (not related to workflow plugin).

Please can you confirm that your edits are the one I can see here? If yes, I'm sorry: I'm still getting the error.

FYI I'm still using {{ actionInput('workflow/elements/save-entry') }} as action input.

@engram-design
Copy link
Member

Yep, those were the edits in that commit.

Any chance you'd be able to send over your site project files, including the database? Understand if you can't share, but if you can send to support at verbb.io as that'll really help trying to replicate things on my end. Despite your detailed explanations, I can't figure out why you're getting that error!

@romainpoirier
Copy link
Author

Of course, thank you, it's in your inbox 📧

@engram-design
Copy link
Member

Thanks @romainpoirier so running with your setup, I'm having no issue adding things. Here's the exact form I'm using:

<form method="post" accept-charset="UTF-8">
    <input type="hidden" name="action" value="workflow/elements/save-entry">
    <input type="hidden" name="sectionId" value="12">
    <input type="hidden" name="typeId" value="28">
    <input type="hidden" name="enabled" value="0">
    {{ csrfInput() }}

    <input type="text" name="title" value="Some Title">

    <input type="submit" value="Submit">
</form>

Having created the draft entry:
image

Is there something I'm maybe missing about your setup?

@romainpoirier
Copy link
Author

Thank you for your tests.

I can make it works without getting the Unable to create draft entry. error using your reduced test example.

But I still get an error in my context, where I'm submitting the form thought AJAX:

$('form').submit(function(event) {
    event.preventDefault();
    var formData = new FormData();
    $.post({
        url: '/',
        data: formData,
        type: 'POST',
        cache: false,
        contentType: false,
        processData: false,
        dataType: 'json',
        success: function(response) {
            console.log('success');
            console.log(response);
        },
        error: function(jqXHR) {
            console.log('error');
            console.log(jqXHR.responseText);
        }
    });
});

Is there something else to adapt (url?) ?

Also, with your test, the downside is that I don't get the email notification as soon as the Entry is submitted from front-end. I receive it just after the submitted Entry is reviewed and published from the CP: i.e., until the Save draft and submit for review is clicked.

This is not the behavior I'm looking for: how can I trigger this sending of the notification on front-end submit (with a not enabled Entry)?

@engram-design
Copy link
Member

Ah, that makes sense! Those issues should be sorted in the latest craft-4 branch.

@romainpoirier
Copy link
Author

romainpoirier commented Jan 23, 2023

Now it works, perfect, thank you!

Do you have any update for the ability to enable for all sites when clicking on Approve and publish?

@engram-design
Copy link
Member

Fixed in 2.0.4.

Still testing out the "Approve and publish" behaviour - best to open up a new issue.

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

No branches or pull requests

2 participants