Skip to content

Basic Structure of FlowBuilder

This document explains the fundamental structure of FlowBuilder, including the architecture of nodes and flows.

Node Structure

BaseNode Class

The BaseNode class is the foundation for all node types in the FlowBuilder module. It implements the NodeContract interface and provides a standardized structure and common functionality that all nodes should inherit.

Overview

  • Namespace: Modules\FlowBuilder\Support
  • Type: Abstract class
  • Implements: Modules\FlowBuilder\Contracts\Node

Purpose

The BaseNode class provides a consistent interface and implementation for all nodes in the FlowBuilder system. It handles common operations such as:

  • Managing node properties (ID, label, icon, etc.)
  • Rendering HTML templates
  • Handling input and output connections
  • Managing node data

Node Properties

PropertyTypeDescription
idstringUnique identifier for the node
labelstringDisplay name for the node
iconstringIcon name for the node
categorystringCategory name for the node
inputCountintNumber of inputs for the node
outputCountintNumber of outputs for the node
dataarrayData associated with the node (Avaliable in execute method)
validationRulesarrayArray of validation rule class names to apply
supportsOutputVariableboolIndicates if the node supports output variables

Main Methods

MethodDescription
getId()Returns the unique identifier for the node
getLabel()Returns the display name for the node
getIcon()Returns the icon name for the node
getCategory()Returns the category name for the node
getInputCount()Returns the number of inputs for the node
getOutputCount()Returns the number of outputs for the node
getData()Returns the data associated with the node (derived from its fields)
setData()Sets the data for the node.
getFormattedData()Returns a JSON string of the node's data, typically based on the default values of its defined fields.
hasInputs()Returns a boolean indicating whether the node has inputs
hasOutputs()Returns a boolean indicating whether the node has outputs
fields()Abstract method that must be implemented by child classes to define the input fields for the node.
render()Renders the node HTML structure, including its fields, for the Drawflow canvas.

Node Types

FlowBuilder supports different types of nodes with specific functionalities:

  1. Action Nodes: Execute actions like sending HTTP requests, manipulating data, etc.
  2. Logic Nodes: Control the flow of execution based on conditions.
  3. Trigger Nodes: Initiate the execution of a flow (explained in detail in a separate document).

Flow Structure

Flow Model

The Flow model stores and manages the flows created in the system.

Main Properties

PropertyTypeDescription
idintUnique identifier for the flow
namestringName of the flow
datajsonJSON structure containing all nodes and connections

Flow Data Structure

The data property in a flow contains a nested structure in JSON format, where the primary key is "data" and its value is another array containing the node data.

Structure Example

php
dump($flow->data);

// Example output
array:1 [
  "data" => array:2 [
    "5e624ae4-9630-4359-91ab-2d9086440a4b" => array:10 [/* node properties */]
    "fd00d0b8-a8d4-4475-96d7-f5e0a1fb2795" => array:10 [/* node properties */]
  ]
]

Accessing Node Data

While you can access the data array directly, the Flow model provides a convenient nodes attribute that returns a Laravel Collection of nodes. This allows you to easily iterate over the nodes and access their properties.

php
$nodes = $flow->nodes;

foreach ($nodes as $node) {
    echo $node->getId();
}

Difference Between Node Contracts and Instances

It's important to understand the difference between a node definition and its instance in a flow:

Modules\FlowBuilder\Contracts\Node

The Node interface defines the blueprint for a node type that can be used in a flow. It specifies essential metadata like the node's label, ID, icon, and the configuration options it supports.

Modules\FlowBuilder\Support\DrawflowNode

The DrawflowNode class represents a node instance that has been placed into a flow. When a flow is exported or processed, nodes that conform to the contract are transformed into DrawflowNode objects to capture their actual usage, including their data, position, and connections (inputs/outputs).

  • Contracts\Node: Defines the capabilities and identity of node types.
  • DrawflowNode: Represents a concrete instance of a node used in a specific flow, derived from the contract definition.

Transforming DrawflowNode to BaseNode

You can transform a DrawflowNode using toBaseNode() to get the base node structure, which is useful for exporting or processing flows:

php
$myFlow = Flow::find(1);

/** @var DrawflowNode $triggerDrawflowNode */
$triggerDrawflowNode = $myFlow->findNode(ManualTrigger::class);

/** @var BaseNode $baseNode */
$baseNode = $triggerDrawflowNode->toBaseNode();

// Now you can call methods on the BaseNode instance

Interacting with Flows

FlowBuilder provides several ways to interact with flows programmatically:

Finding a Flow

php
use Modules\FlowBuilder\Models\Flow;

// Find a flow by ID
$flow = Flow::find(1);

// Find a flow by slug/name
$flow = Flow::where('name', 'my-flow')->first();

Working with Nodes in a Flow

php
// Get all nodes
$nodes = $flow->nodes;

// Find a specific node by type (class)
$triggerNode = $flow->findNode(ManualTrigger::class);

// Transform to a BaseNode to access specific methods
$baseNode = $triggerNode->toBaseNode();

In the next document, we'll learn how to create and customize your own custom nodes.