Logo BrocksiNet

BrocksiNet

Try to clean the web

Flow Builder, early developers overview

With the fresh new shopware 6 version 6.4.6.0 there is a new much awaited (!) feature the flow builder. In this blog post we will check the shopware 6 core how easy or hard it is to create own actions for the flow builder. To have an general idea what the flow builder can do for you, you should at least read the flow builder documention on shopware 6 docs.

In short words: You can create custom (business) processes without coding. You can create this custom process via admin by defining triggers, rules and actions. Flow builder is replacing the business events. Flow builder can use rules from the rule builder. But now let's check some code.

vendor/shopware/core/Content/Flow

What class do you need to extend to create an own flow builder action class?

You need to extend Shopware\Core\Content\Flow\Dispatching\Action\FlowAction

<?php declare(strict_types=1);

namespace Shopware\Core\Content\Flow\Dispatching\Action;

use Shopware\Core\Framework\Event\FlowEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

abstract class FlowAction implements EventSubscriberInterface
{
    abstract public function requirements(): array;

    abstract public function handle(FlowEvent $event): void;

    abstract public static function getName(): string;
}

There are two tables. The so called flow table looks like this:

create table flow
(
    id            binary(16)           not null
        primary key,
    name          varchar(255)         not null,
    description   mediumtext           null,
    event_name    varchar(255)         not null,
    priority      int        default 1 not null,
    payload       longblob             null,
    invalid       tinyint(1) default 0 not null,
    active        tinyint(1) default 0 not null,
    custom_fields json                 null,
    created_at    datetime(3)          not null,
    updated_at    datetime(3)          null
);

create index `idx.flow.event_name`
    on flow (event_name, priority);

and the flow sequence table like this:

create table flow_sequence
(
    id            binary(16)           not null
        primary key,
    flow_id       binary(16)           not null,
    parent_id     binary(16)           null,
    rule_id       binary(16)           null,
    action_name   varchar(255)         null,
    config        json                 null,
    position      int        default 1 not null,
    display_group int        default 1 not null,
    true_case     tinyint(1) default 0 not null,
    custom_fields json                 null,
    created_at    datetime(3)          not null,
    updated_at    datetime(3)          null,
    constraint `fk.flow_sequence.flow_id`
        foreign key (flow_id) references flow (id)
            on update cascade on delete cascade,
    constraint `fk.flow_sequence.parent_id`
        foreign key (parent_id) references flow_sequence (id)
            on update cascade on delete cascade,
    constraint `fk.flow_sequence.rule_id`
        foreign key (rule_id) references rule (id)
            on update cascade
);

Do I need to add some xml to register my own flow builder action?

Sure, there is a new tag called flow.action and this also has a priority you should set.

<service id="FlowBuilderAction\Core\Content\Flow\Dispatching\Action\ExampleAction">
    <argument type="service" id="logger"/>
    <tag name="kernel.event_subscriber" />
    <tag name="flow.action" priority="300" />
</service>

Check also the core flow.xml File at vendor/shopware/core/Content/DependencyInjection/flow.xml

What kind of actions are delivered via the shopware 6 core?

The slack action (shopware is showing in the videos) is not integrated in the core.

Do I need to extend the administration to see my example action?

No and yes. Sadly the label is not generated from the action name ... so you will see that there is some new action ... but the label is empty and for the default actions the label is hard coded ... so you will get some issues when you want to save the action. Check this core file: flow-builder.service.js - Line 18.

How to proceed?

We should wait for the developer docs to be updated and then check the documentation about the flow builder.

How does a example action class file looks like?

<?php

declare(strict_types=1);

namespace FlowBuilderAction\Core\Content\Flow\Dispatching\Action;

use Shopware\Core\Content\Flow\Dispatching\Action\FlowAction;
use Shopware\Core\Framework\Event\FlowEvent;
use Psr\Log\LoggerInterface;

class ExampleAction extends FlowAction
{
    private LoggerInterface $logger;

    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function requirements(): array
    {
        return [];
    }

    public static function getSubscribedEvents(): array
    {
        return [
            self::getName() => 'handle',
        ];
    }

    public function handle(FlowEvent $event): void
    {
        $this->logger->info('Example Action with name:');
        $this->logger->info($event->getActionName());
        $this->logger->info('was handled ;-)');
    }

    public static function getName(): string
    {
        return 'action.example.action';
    }
}
Released - 08.11.2021

Comments


About the author

Bild Bjoern
Björn MeyerSoftware Engineer
Björn is interested in the details of web technologies. He started with Joomla, Drupal, Typo3, vBulletin, Fireworks and Photoshop. Today his focus is on PHP for example with Magento 2, Shopware 6 and Symfony. He also likes JavaScript with Angular, React, VueJs and TypeScript. In his free time, he seeks constant balance with swimming, running and yoga.