What are the Slack Archives?

It’s a history of our time together in the Slack Community! There’s a ton of knowledge in here, so feel free to search through the archives for a possible answer to your question.

Because this space is not active, you won’t be able to create a new post or comment here. If you have a question or want to start a discussion about something, head over to our categories and pick one to post in! You can always refer back to a post from Slack Archives if needed; just copy the link to use it as a reference..

hello, how to pass arguments from subscriber to listener, my code ```Subscriber: $eventCollection-&g

USLUHK9SQ
USLUHK9SQ Posts: 92 πŸ§‘πŸ»β€πŸš€ - Cadet
edited June 2020 in Help

hello,
how to pass arguments from subscriber to listener, my code

Subscriber:
$eventCollection->addListenerQueued(ProductEvents::PRODUCT_ABSTRACT_AFTER_CREATE, new KacperListener('my argument'));
Listener:
public function __construct($URL_TO_API) {
        $this->URL_TO_API = $URL_TO_API;
}

give me an error:
Too few arguments to function Pyz\Zed\....\KacperListener::__construct(), 0 passed in

Comments

  • UKHR9UN00
    UKHR9UN00 Posts: 70 πŸ§‘πŸ»β€πŸš€ - Cadet

    Hi Kacper,
    your Listener should extend \Spryker\Zed\Kernel\Communication\AbstractPlugin
    This will give you access to the modules Config. Facade, Factory and QueryContainer by calling e.g. getConfig()

  • USLUHK9SQ
    USLUHK9SQ Posts: 92 πŸ§‘πŸ»β€πŸš€ - Cadet

    @UKHR9UN00 i need example how to pass my custom argument from subscriber to listener

  • UKHR9UN00
    UKHR9UN00 Posts: 70 πŸ§‘πŸ»β€πŸš€ - Cadet

    To answer your question: I guess there is no need to pass an argument from subscriber to listener. Can you give an example for when this is necessary

  • USLUHK9SQ
    USLUHK9SQ Posts: 92 πŸ§‘πŸ»β€πŸš€ - Cadet

    @UKHR9UN00 i created package, i install it from composer, and i want implement it in eventTransferProvider, i must pass one argument URL_API, to send specific data, i want that plugin will be univerasal

  • USLUHK9SQ
    USLUHK9SQ Posts: 92 πŸ§‘πŸ»β€πŸš€ - Cadet

    so my idea is pass argument in eventTrasfer -> subscriber -> listener -> senddata,
    I think the best option is to add const and pass there url but i dont know how to implement it on client side

  • USLUHK9SQ
    USLUHK9SQ Posts: 92 πŸ§‘πŸ»β€πŸš€ - Cadet

    @UKHR9UN00 maybe, i can set global const in spryker ?

  • UKHR9UN00
    UKHR9UN00 Posts: 70 πŸ§‘πŸ»β€πŸš€ - Cadet
    edited June 2020

    ~I think you can use any Transfer-Object for an event, so you can have your own KacperEventTransfer. Let me check if I can find an example~ Does not fit your example

  • UKHR9UN00
    UKHR9UN00 Posts: 70 πŸ§‘πŸ»β€πŸš€ - Cadet

    But if the value can be configured once, you might better extend your Listener-Logic in your Project and set the URL by using the getConfig()-Method.

  • USLUHK9SQ
    USLUHK9SQ Posts: 92 πŸ§‘πŸ»β€πŸš€ - Cadet

    but how i can pass an argument there, implementation my module will be only in EventDependencyProvider, ex.

    //here i can set this url, but how
    
    $eventSubscriberCollection->add(new MySubscriber());
    
  • UKHR9UN00
    UKHR9UN00 Posts: 70 πŸ§‘πŸ»β€πŸš€ - Cadet

    Sorry, I'm not sure if I got you right.
    Is the URL something that will be different for every fired event or this this an environment setting?

  • USLUHK9SQ
    USLUHK9SQ Posts: 92 πŸ§‘πŸ»β€πŸš€ - Cadet

    environment setting, look i explain, you set (but i dont know where
    url = my custom url,
    next copy paste

    $eventSubscriberCollection->add(new MySubscriber()); 
    

    to EventDependencyProvider, and data goes to "my_custom_url"

  • UKHR9UN00
    UKHR9UN00 Posts: 70 πŸ§‘πŸ»β€πŸš€ - Cadet

    Okay, for environment settings you should use the shared configuration files like config_default.php or config_default-ENVIRONMENT.php in config/Shared

    In Pyz/Shared/MyModule/MyModuleConstants.php you define a constant public const MY_SETTING = 'MY_MODULE:MY_SETTING which you will use a array key in the config file.

    Then in your Pyz/Zed/MyModule/MyModuleConfig.php you create a new method like

    public function getMySetting(): string
    {
        return $this->get(MyModuleConstants::MY_SETTING);
    }
    

    And finally in your Subscriber (e.g. Pyz/Zed/MyModule/Communication/Plugin/Event/Subscriber/MySubscriber.php) which extends the AbstractPlugin class you can your $this->getConfig()->getMySetting();

  • USLUHK9SQ
    USLUHK9SQ Posts: 92 πŸ§‘πŸ»β€πŸš€ - Cadet

    so i must add something to Pyz / Shared /, or only in config_default.php is enough ?, i want to make the implementation the easiest

  • UKHR9UN00
    UKHR9UN00 Posts: 70 πŸ§‘πŸ»β€πŸš€ - Cadet

    I guess you could only add it to config_default.php and use \Spryker\Shared\Config\Config::get('my-configuration-key', 'fallback-value); in your subscriber. But that's not how you should write Spryker code