Problem/Motivation
When creating a playground session via PlaygroundService::createSession(), the metadata parameter is passed directly to FlowDropPlaygroundSession::create() as a PHP array. However, the metadata field is defined as a string_long field type that expects a JSON string.
Drupal's string field type does not handle arrays - it casts the array to the literal string "Array". This means any metadata passed to createSession() is lost, and subsequent calls to $session->getMetadata() return an empty array.
Steps to reproduce
- Call
PlaygroundService::createSession()with metadata:
$metadata = ['my_key' => 'my_value']; $session = $playgroundService->createSession($workflow, 'Test Session', $metadata);
- Retrieve the metadata:
$retrievedMetadata = $session->getMetadata(); // Expected: ['my_key' => 'my_value'] // Actual: []
- If you inspect the database, the
metadatacolumn contains the literal string"Array"instead of valid JSON.
Root cause
In PlaygroundService::createSession() (around line 175):
$session = FlowDropPlaygroundSession::create([ 'workflow_id' => $workflow->id(), 'name' => $sessionName, 'status' => 'idle', 'uid' => $this->currentUser->id(), 'metadata' => $metadata, // Array passed directly to string_long field ]);
The metadata field is defined in FlowDropPlaygroundSession::baseFieldDefinitions() as:
$fields['metadata'] = BaseFieldDefinition::create('string_long')
->setLabel(new TranslatableMarkup('Metadata'))
->setDescription(new TranslatableMarkup('Custom session metadata in JSON format.'))
->setDefaultValue('{}');
When an array is assigned to a string_long field during entity creation, PHP casts it to the string "Array", which is not valid JSON.
Proposed resolution
JSON-encode the metadata array before passing it to create():
$session = FlowDropPlaygroundSession::create([ 'workflow_id' => $workflow->id(), 'name' => $sessionName, 'status' => 'idle', 'uid' => $this->currentUser->id(), 'metadata' => Json::encode($metadata), // Properly encode as JSON ]);
Alternatively, use setMetadata() after creation (which already handles JSON encoding):
$session = FlowDropPlaygroundSession::create([ 'workflow_id' => $workflow->id(), 'name' => $sessionName, 'status' => 'idle', 'uid' => $this->currentUser->id(), ]); $session->setMetadata($metadata); $session->save();
Workaround
Until this is fixed, callers can work around the issue by re-setting the metadata after session creation:
$session = $playgroundService->createSession($workflow, $name, $metadata); $session->setMetadata($metadata); $session->save();
Comments
Comment #3
d34dman commented