Commit 9a8b4216 authored by maxwellkeeble's avatar maxwellkeeble
Browse files

Issue #3293360: Allow the Mermaid JS flowchart direction (TB, BT, LR, RL) to be passed to the theme

parent 8d232612
Loading
Loading
Loading
Loading
+32 −11
Original line number Diff line number Diff line
@@ -10,36 +10,54 @@ use Drupal\workflows\WorkflowInterface;
 */
class WorkflowsDiagramController extends ControllerBase {

  const DIAGRAM_DIRECTIONS = [
    'TB', // Top to bottom
    'BT', // Bottom to top
    'LR', // Left to right
    'RL', // Right to left
  ];

  /**
   * Produce a Mermaid compatible string from a workflow.
   *
   * @param WorkflowInterface $workflow
   *   Workflow object.
   * @param array $classes
   * @param array|null $classes
   *   Classes to apply per state or transition,
   *   keyed by 'transitions' and 'states'.
   * @param string $title
   * @param string|null $title
   *   Flowchart title.
   * @param array $disabled
   * @param array|null $disabled
   *   IDs of states or transitions to exclude from diagram,
   *   keyed by 'transitions' and 'states'.
   * @param string $direction
   *   One of static DIAGRAM_DIRECTIONS, e.g.
   *   TB top to bottom, BT bottom to top,
   *   LR left to right, RL right to left
   *
   * @return string
   *   Mermaid-compatible string.
   */
  public static function convertWorkflowToMermaid(WorkflowInterface $workflow, array $classes = NULL, string $title = NULL, array $disabled = NULL) {
  public static function convertWorkflowToMermaid(WorkflowInterface $workflow, array $classes = NULL, string $title = NULL, array $disabled = NULL, string $direction = 'TB'): string {
    $workflowType = $workflow->getTypePlugin();
    $states = $workflowType->getStates();

    if (!$title) {
      $title = $workflow->label();
    // @todo mermaid currently doesn't support a title directly.
    // This may change: https://github.com/mermaid-js/mermaid/issues/1433
    //    if (is_null($title)) {
    //      $title = $workflow->label();
    //    }

    // Set a direction if the one given is not valid
    if (!$direction || !in_array($direction, static::DIAGRAM_DIRECTIONS)) {
      $direction = 'TB';
    }

    // We collect classes to add to the end:
    $mermaid_classes = '';

    // Start mermaid output string with graph type:
    $mermaid = 'graph TB;';
    $mermaid = 'graph ' . $direction . ';';

    // Initial state, put at start:
    $initialState = $workflowType->getInitialState();
@@ -79,7 +97,10 @@ class WorkflowsDiagramController extends ControllerBase {
        $to = $transition->to()->id();

        // Do not render transition if any of the involved states are disabled:
        if ($disabled && isset($disabled['states']) && count(array_intersect($disabled['states'], [$from, $to])) > 0) {
        if ($disabled && isset($disabled['states']) && count(array_intersect($disabled['states'], [
            $from,
            $to,
          ])) > 0) {
          continue;
        }

@@ -116,8 +137,8 @@ class WorkflowsDiagramController extends ControllerBase {
   * @return string
   *   Safe mermaid string.
   */
  public static function convertToMermaidString(string $string) {
    $string = str_replace('"', '#quot;', $string);
    return $string;
  public static function convertToMermaidString(string $string): string {
    return str_replace('"', '#quot;', $string);
  }

}
+1 −1
Original line number Diff line number Diff line
@@ -10,4 +10,4 @@ mermaid-init:
  js:
    "js/mermaid-init.js": {}
  dependencies:
    - mermaid/mermaid
    - workflows_diagram/mermaid
+4 −3
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ function workflows_diagram_theme() {
        'workflow' => NULL,
        'classes' => [],
        'disabled' => [],
        'direction' => 'TB',
      ],
    ],
  ];
@@ -31,7 +32,7 @@ function workflows_diagram_theme() {
 * Implements template_process_THEME().
 */
function template_preprocess_workflows_diagram(&$variables) {
  $mermaid = WorkflowsDiagramController::convertWorkflowToMermaid($variables['workflow'], $variables['classes'] ?? [], NULL, $variables['disabled'] ?? []);
  $mermaid = WorkflowsDiagramController::convertWorkflowToMermaid($variables['workflow'], $variables['classes'] ?? [], NULL, $variables['disabled'] ?? [], $variables['direction'] ?? 'TB');
  $variables['mermaid'] = $mermaid;
}