verbb / social-poster

A Craft CMS plugin for automatically posting entries to social media.
Other
89 stars 12 forks source link

Add extra events to edit the post message that is being send to social media. #47

Closed TomDeSmet closed 1 year ago

TomDeSmet commented 1 year ago

What are you trying to do?

I want more control over the content I add to a social post. The plugin now supports posting data from entries and selecting fields within that entry (e.g. asset field). But it doesn't play nice with Matrix fields. We want to post certain data from within a matrix field. I know I can write twig code inside the message box, and it works, but not completely as I would like it to.

What's your proposed solution?

Add events to the plugin so we can alter the message before it is being send to the social media platform.

Additional context

No response

engram-design commented 1 year ago

Added for the next release. To get this early, run composer require verbb/social-poster:"dev-craft-4 as 4.0.0"

The beforeSendPost event

The event that is triggered before an account sends a post.

The isValid event property can be set to false to prevent the post from being sent.

use verbb\socialposter\accounts\Twitter;
use verbb\socialposter\events\SendPostEvent;
use yii\base\Event;

Event::on(Twitter::class, Twitter::EVENT_BEFORE_SEND_POST, function(SendPostEvent $event) {
    $payload = $event->payload;
    $account = $event->account;
    $endpoint = $event->endpoint;
    $method = $event->method;
    // ...
});

The afterSendPost event

The event that is triggered after an account sends a post.

The isValid event property can be set to false to flag a post-sending response.

use verbb\socialposter\accounts\Twitter;
use verbb\socialposter\events\SendPostEvent;
use yii\base\Event;

Event::on(Twitter::class, Twitter::EVENT_AFTER_SEND_POST, function(SendPostEvent $event) {
    $payload = $event->payload;
    $account = $event->account;
    $response = $event->response;
    // ...
});
engram-design commented 1 year ago

Updated in 4.0.1

TomDeSmet commented 1 year ago

Hi there, sorry it took so long, but I only got time now to move on with this and I believe something important is missing. There's no connection to the entry where the posts originates from. Which means I cannot query any field data I want to use in the payload.

So would it be possible to add the Craft Element or at least its ID (so we can fetch the element ourselves) to the event?

engram-design commented 1 year ago

Updated for the next release. To get this early, run composer require verbb/social-poster:"dev-craft-4 as 4.0.1"