craftcms / webhooks

Plugin for integrating Craft with Zapier and IFTTT.
https://plugins.craftcms.com/webhooks
MIT License
84 stars 12 forks source link

Webhook data returns wrong siteId #65

Open vlradstake opened 2 years ago

vlradstake commented 2 years ago

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

joshuabaker commented 2 years ago

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 commented 2 years ago

@vlradstake How are you getting the site ID?

vlradstake commented 2 years ago

@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
}}
joshuabaker commented 2 years ago

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?

martin-coded commented 1 year ago

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.

Stalex89 commented 10 months ago

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.