Source Provider#

You can register your own Source Provider to add support for other social media platforms, or even extend an existing Source Provider.

namespace modules\sitemodule;

use craft\events\RegisterComponentTypesEvent;
use modules\sitemodule\MySourceProvider;
use verbb\socialfeeds\services\Sources;
use yii\base\Event;

Event::on(Sources::class, Sources::EVENT_REGISTER_SOURCE_TYPES, function(RegisterComponentTypesEvent $event) {
    $event->types[] = MySourceProvider::class;
});

Example#

Create the following class to house your Source Provider logic.

namespace modules\sitemodule;

use Craft;
use Throwable;
use verbb\socialfeeds\base\OAuthSource;
use verbb\socialfeeds\models\Post;

use League\OAuth2\Client\Provider\SomeProvider;

class MySourceProvider extends OAuthSource
{
    // Static Methods
    // =========================================================================

    public static function displayName(): string
    {
        return 'My Source Provider';
    }

    public static function getOAuthProviderClass(): string
    {
        return SomeProvider::class;
    }


    // Properties
    // =========================================================================

    public static string $providerHandle = 'mySourceProvider';


    // Public Methods
    // =========================================================================

    public function getPrimaryColor(): ?string
    {
        return '#000000';
    }

    public function getIcon(): ?string
    {
        return '<svg>...</svg>';
    }

    public function getSettingsHtml(): ?string
    {
        return Craft::$app->getView()->renderTemplate('my-module/my-source/settings', [
            'source' => $this,
        ]);
    }

    public function fetchPosts(): ?array
    {
        try {
            // Construct your POST request according to the API
            $response = $this->request('POST', 'api/endpoint', [
                'json' => [
                    'text' => $payload->message,
                ],
            ]);

            $posts = [];

            foreach ($response as $item) {
                $posts[] = new Post([
                    'id' => $item['id'],
                    'text' => $item['text'],
                    // ...
                ]);
            }

            return $posts;
        } catch (Throwable $e) {
            self::apiError($this, $e, false);
        }
    }
}

This is the minimum amount of implementation required for a typical source provider.

Social Feeds source providers are built around the Auth (opens new window) which in turn is built around league/oauth2-client (opens new window). You can see that the getOAuthProviderClass() must return a League\OAuth2\Client\Provider\AbstractProvider class.

Previous ← Source Next Post →