Book Image

Drupal 10 Development Cookbook - Third Edition

By : Matt Glaman, Kevin Quillen
Book Image

Drupal 10 Development Cookbook - Third Edition

By: Matt Glaman, Kevin Quillen

Overview of this book

This new and improved third edition cookbook is packed with the latest Drupal 10 features such as a new, flexible default frontend theme - Olivero, and improved administrative experience with a new theme - Claro. This comprehensive recipe book provides updated content on the WYSIWYG (What You See Is What You Get) editing experience, improved core code performance, and code cleanup. Drupal 10 Development Cookbook begins by helping you create and manage a Drupal site. Next, you’ll get acquainted with configuring the content structure and editing content. You’ll also get to grips with all new updates of this edition, such as creating custom pages, accessing and working with entities, running and writing tests with Drupal, migrating external data into Drupal, and turning Drupal into an API platform. As you advance, you’ll learn how to customize Drupal’s features with out-of-the-box modules, contribute extensions, and write custom code to extend Drupal. By the end of this book, you’ll be able to create and manage Drupal sites, customize them to your requirements, and build custom code to deliver your projects.
Table of Contents (17 chapters)

Using Workspaces to create content staging areas

The Workspaces module provides a new way of working with content on your Drupal site. It allows you to have a live version of your site’s content and parallel draft versions. Normal content workflows involve multiple pieces of content that may be drafted and published at various times. The Workspaces module provides a way to create and prepare published drafts that release at the same time.

For example, during a big sporting event, articles are prepared based on whichever team wins. Once the winner is announced, that version of the site’s content can be published. In this recipe, we will install the Workspaces module and walk through using site versions.

Important note

At the time of writing, the Workspaces module is an Experimental module. Modules that are marked as experimental are under active development and not considered stable. Experimental modules provide a way of adding new functionality to Drupal core more easily. You can read more about the experimental module policy on Drupal.org: https://www.drupal.org/about/core/policies/core-change-policies/experimental/policy-and-list.

Getting ready

In this recipe, we will be using the Standard installation, which provides the Basic Page content type. Any content type will suffice.

How to do it…

  1. Begin by installing the Workspaces module:
    php vendor/bin/drush en workspaces --yes
  2. Visit your Drupal site; you will notice the Live tab on the right of the toolbar; this is the current workspace identifier.
  3. Click on Live to open the Workspaces menu.
  4. Click on the Stage workspace’s name and then click Confirm in the modal asking if we would like to activate and switch to the Stage workspace.
  5. Create three or four new basic pages while using the Stage workspace and be sure to check Promoted to front page in the Promotion options group.
  6. When you visit the front page of your Drupal site, you should see the pages you created in the front page list.
  7. Now, open your Drupal site in another browser, or private mode, where you are not logged in. You will see that the home page shows No front-page content has been created yet. This shows that the content is only published in the Stage workspace, not the live site.
  8. Back in your Drupal site, click on the Stage tab in your toolbar to open the workspace menu.
  9. Click Publish content to begin publishing your Stage content into the Live site.
  10. A confirmation form will appear. Click Publish items to Live to finish the process.
  11. If you test your site in another browser, or private mode, again, you will see the home page now lists all your new pages!

How it works…

The Workspace module uses the existing revision capabilities of content entities. Revisions are then tracked against a workspace until they are published to the Live workspace. The Workspace module also adds safeguards. Forms that manipulate site configuration cannot be saved unless in the Live workspace; the module displays a warning and disables the Submit button. When using multiple workspaces, the Workspace module only allows a piece of content to be edited in one workspace.

Workspaces also have a user account associated with them. This allows segmented workspaces for specific users. This allows content creators to create a new workspace, but not view or modify another content creator’s workspace.

There’s more…

The Workspaces module provides other user interfaces not covered in this recipe, and there is another way to use a workspace beyond just content.

When will Workspaces become a stable module?

Effort is being made to make the Workspaces module stable. These issues are tagged in the Drupal core issue queue as WI critical (short for Workflow Initiative Critical). The list of issues can be found here: https://www.drupal.org/project/issues/search/drupal?status%5B%5D=Open&issue_tags_op=%3D&issue_tags=WI+critical.

Managing content changes in a workspace

When the workspace menu is open in the toolbar, you can click the Manage workspace link to see all of the active changes in the workspace. This makes it easier for a content manager to review what content has been modified in a workspace. It also allows for deleting those changes to revert to the original content.

This overview is useful for reviewing all the changes that may be published to the Live workspace.

Creating child workspaces

A workspace may also have a parent workspace. This allows you to maintain a centralized Stage workspace but forces content creators to have their child workspace under Stage. All content modifications can then merge into Stage instead of each contributor’s workspace publishing to Live.

Using a workspace to test a new site redesign

Drupal has a mechanism for determining the active theme, which, by default, is the default theme. Code can be written to override the current theme based on specific conditions. The Workspace Theme module (https://www.drupal.org/project/workspace_theme) does just that.

It adds a new field to a workspace that allows you to specify a different theme to be used when that workspace is active. This allows you to preview a site’s redesign with a new theme without making it the default theme on the production site, or purely relying on a test server.

See also