Creating Workspace entities programmatically

May 17, 2018

Submitted by twfahey on Thu, 05/17/2018 - 08:04

With Drupal 8.6.x, the exciting new "Workspaces" functionality is coming to core! I was diving in and playing with it, and I have to say I am pleased and excited with how this new feature will enhance core functionality provided by Drupal. Plenty of projects I've worked on or been involved in have some need for workflow - Stage and prepare content, and then at some point, deploy it out to the live site. Rather than "unchecking published", the Workflow module allows for environments to be created, such as "Dev", "Test", "Live", and new content created in these environments can be staged until it's time to deploy, which is a simple process.

image

 

Since a Workspace is an Entity, creating one programmatically is pretty straightforward:

use Drupal\workspace\Entity\Workspace;

function generateWorkspace() {
  $flex_workspace = Workspace::create([
    'id' => 'flex_workspace',
    'revision_id' => 4,
    'uid' => 1,
    'label' => 'Flex Workspace',
    'target' => 'live',
  ]);
  $flex_workspace->save();
}

It seems that Workspace right now works really well for creating nodes and having them as "Unpublished" in Live until they are deployed. I noticed that configuration like the site homepage does not stage in this same way - a change in "Stage" environment to the site homepage is immediately taking place on "Live", so hopefully this is functionality that can be provided some day.

Excited to see how this new functionality grows over time!

See the roadmap on D.O.

 

Add new comment