The goal for this exercise is to create a custom block route in Drupal that loads a React app. Alternatively, a custom controller route can be used to load the React app instead of a block. All of the code for the block or controller should be placed in a custom Drupal module.
- A new Drupal module has been created
- The new module contains a custom block plugin or controller
- A React app been added to the module
- The react app is added to the custom block or route and is displayed on a page
- Tutorial: Connect React to a Drupal Theme or Module
- Blog Post: Recipe for Embedded React Component
- Documentation: Creating Custom Blocks in Drupal
- Documentation: Adding assets (CSS, JS) to a Drupal module via *.libraries.yml
Skip to step 3 if Docker and DDEV are already installed.
- Install Docker or Colima
- Install DDEV
- Clone this repo to your local machine
cd
to the root of the project- Run the setup commands below in you terminal
ddev start
ddev composer install
ddev site-install p1
Run the launch command to open the site unauthenticated
ddev launch
Or run the drush user login command to generate a one-time login
ddev drush uli
This solution adds a React timer widget as a drupal block.
The block should display in the sidebar of each page. The code for this solution is all contained in a custom module located at:
/web/modules/custom/ca_custom_block_react_timer
The frontend react app code is located at:
/web/modules/custom/ca_custom_block_react_timer/src/frontend
The compiled frontend that gets sent to the browser is located at:
/web/modules/custom/ca_custom_block_react_timer/dist
There are a few custom DDEV commands provided for convenience
Install Drupal, enable the ca_custom_block_react_timer module, add the custom block to the sidebar region, and clear drupal cache.
ddev site-install p1
Build the module frontend and export compiled React app to the dist
folder
ddev build-timer-block
Run all Frontend tests for the React Timer app
ddev test-timer-block