Implement event trigger publisher plugins

Edit on GitHub

To publish or republish the model data manually for all or a particular resource, you need to implement an event trigger publisher plugin.

Follow these steps to implement and register a new event trigger publisher plugin.

Pyz\Zed\HelloWorldStorage\Communication\Plugin\Publisher
<?php

namespace Pyz\Zed\HelloWorldStorage\Communication\Plugin\Publisher;

use Spryker\Zed\Kernel\Communication\AbstractPlugin;
use Spryker\Zed\PublisherExtension\Dependency\Plugin\PublisherTriggerPluginInterface;

/**
 * @method \Pyz\Zed\HelloWorldStorage\Business\HelloWorldStorageFacadeInterface getFacade()
 * @method \Pyz\Zed\HelloWorldStorage\Communication\HelloWorldStorageCommunicationFactory getFactory()
 * @method \Pyz\Zed\HelloWorldStorage\HelloWorldStorageConfig getConfig()
 */
class HelloWorldPublisherTriggerPlugin extends AbstractPlugin implements PublisherTriggerPluginInterface
{
    /**
     * @return string
     */
    public function getResourceName(): string
    {
        return HelloWorldStorageConfig::HELLO_WORLD_RESOURCE_NAME;
    }

    /**
     * @param int $offset
     * @param int $limit
     *
     * @return \Generated\Shared\Transfer\GlossaryKeyTransfer[]
     */
    public function getData(int $offset, int $limit): array
    {
        return $this->getFacade()->findHelloWorldEntities($offset, $limit);
    }

    /**
     * @return string
     */
    public function getEventName(): string
    {
        return HelloWorldStorageConfig::HELLO_WORLD_PUBLISH_WRITE;
    }

    /**
     * @return string|null
     */
    public function getIdColumnName(): ?string
    {
        return PyzHelloWorldTableMap::COL_ID_HELLO_WORLD;
    }
}

Find method descriptions below:

  • HelloWorldPublisherTriggerPlugin::getResourceName()—defines the resource name for key generation.

  • HelloWorldPublisherTriggerPlugin::getData()—retrieves a collection of data transfer objects for publishing according to a provided offset and limit.

  • HelloWorldPublisherTriggerPlugin::getEventName()—defines an event name for publishing.

  • HelloWorldPublisherTriggerPlugin::getIdColumnName()—defines an ID column name for publishing.

Make sure to fulfill the requirements:

  1. The resource name must be the same as in the Propel schema definition.

  2. The plugin must implement \Spryker\Zed\PublisherExtension\Dependency\Plugin\PublisherTriggerPluginInterface.

Register the event trigger publisher plugin in \Pyz\Zed\Publisher\PublisherDependencyProvider:

<?php

namespace Pyz\Zed\Publisher;

...
use Pyz\Zed\HelloWorldStorage\Communication\Plugin\Publisher\HelloWorldPublisherTriggerPlugin;
use Spryker\Zed\Publisher\PublisherDependencyProvider as SprykerPublisherDependencyProvider;

class PublisherDependencyProvider extends SprykerPublisherDependencyProvider
{
    ...

    /**
     * @return \Spryker\Zed\PublisherExtension\Dependency\Plugin\PublisherTriggerPluginInterface[]
     */
    protected function getPublisherTriggerPlugins(): array
    {
        return [
            ......,
            new HelloWorldPublisherTriggerPlugin(),
            ......,
        ];
    }

    ...
}