webhooks icon indicating copy to clipboard operation
webhooks copied to clipboard

Webhook data returns wrong siteId

Open vlradstake opened this issue 2 years ago • 6 comments

Description

We use craft CMS as a headless CMS and the GraphQL api to get the content. With the Webhook plugin we send webhooks to the application to clear the cache and get the new content from Craft CMS. When a single type is saved the webhook plugins sends a request with the wrong siteId. We have different sites with the same single names.

Steps to reproduce

  1. Create multiple sites with the same single handel (Home)
  2. Save the single entry
  3. Webhook plugins sends a request with another sites id

Saved from default site Screenshot 2022-03-04 at 15 22 49

Webhook data Screenshot 2022-03-04 at 15 21 38

GraphQL data Screenshot 2022-03-04 at 15 21 45

Additional info

  • Craft CMS version: 3.7.24
  • Webhooks version: 2.4.1
  • PHP version: 8.0.15
  • Database driver & version: MySQL 5.5.5
  • Plugins & versions: Screenshot 2022-03-04 at 15 10 38

vlradstake avatar Mar 04 '22 14:03 vlradstake

Update: This is not correct. Hiding.

Also running into this issue. I’d imagine this is happening because the webhook is running as a task and the siteId value isn’t passed in the event parameters.

Possibly an upstream craftcms/cms issue. Craft’s ModelEvent might need a $siteId property so this is carried into the task.

joshuabaker avatar Apr 20 '22 18:04 joshuabaker

@vlradstake How are you getting the site ID?

joshuabaker avatar Apr 21 '22 07:04 joshuabaker

@joshuabaker Thanks for answering! This is the payload template:

Do you think entry.getSite().handle is not working correctly?

{% set entry = event.sender %}
{% set section = entry.getSection() %}
{{
  {
    time: now|atom,
    name: event.name,
    entry: {
      id: entry.id,
      handle: section.handle,
    },
    site: {
      handle: entry.getSite().handle
    }
  }|json_encode|raw
}}

vlradstake avatar Apr 21 '22 12:04 vlradstake

This is roughly what I was doing too.

The entry.getSite() call you have will load the site from the database based on the entry.siteId property on the entry so will match up correctly to whatever that’s set to.

I think what’s happening here is that the webhook task just gets the first entry (i.e. most recent), instead of the one that was saved. Perhaps my above comment is right after all?

@brandonkelly Are you able to provide some steer on this?

joshuabaker avatar Apr 21 '22 13:04 joshuabaker

I think I have the same issue. I'm getting always the data from the wrong siteId @joshuabaker Did you find a solution or workaround for this problem.

~~Probably I am wrong, but when the fields are related to the entry and the sites are related to the fields, then it's impossible to get the correct site from the entry, because the event is for the entry, which isn't directly related to one site.~~

EDIT: It happened only when the option "Element is being propagated" is enabled.

martin-coded avatar May 30 '23 23:05 martin-coded

Hello, having the similar issue when trying to save globals, I have two sites, and {{event.sender.site.baseUrl}} returns the opposite site (saving globals for site 1, but {{event.sender.site.baseUrl}} returns 2 and vice-versa).

Here is my webhook configuration: Screenshot 2023-11-06 at 14 34 05

I have another webhook with similar configuration, the only difference is that the sender class is craft\elements\Entry, and it works correct.

Stalex89 avatar Nov 06 '23 13:11 Stalex89