Skip to content

Altering live turns

hook_flowdrop_office_turn_alter() runs on every message sent from the office chat, before anything is stored or run. It also runs on the answer to a question the workflow asked.

Why it exists

The office posts each message to a FlowDrop session, which keeps it as history and sends it again on later turns. A guardrail inside the workflow only sees the current turn, so personal data that must not be stored or sent to a model has to be masked here.

Signature

function hook_flowdrop_office_turn_alter(array &$turn, FlowDropWorkflowInterface $workflow): void
Key in $turn Type Meaning
message string What will be stored and run. Change it to rewrite the message.
stopped string\|\Stringable\|null Set it to a reply text, such as t() output, to refuse the message. The office shows the text as the answer and stores the message nowhere.

$workflow is the workflow the office runs, so you can limit the alter to some workflows.

Example

Mask Spanish DNI numbers and refuse long messages, using a #[Hook] class:

<?php

declare(strict_types=1);

namespace Drupal\my_module\Hook;

use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\flowdrop_workflow\FlowDropWorkflowInterface;

final class OfficeTurnHooks {

  use StringTranslationTrait;

  #[Hook('flowdrop_office_turn_alter')]
  public function turnAlter(array &$turn, FlowDropWorkflowInterface $workflow): void {
    $turn['message'] = preg_replace('/\b\d{8}[A-Z]\b/u', '[DNI]', $turn['message']);
    if (mb_strlen($turn['message']) > 500) {
      $turn['stopped'] = $this->t('Write a shorter message.');
    }
  }

}

The office already rejects empty messages and messages over 2000 characters before the hook runs.