Skip to content

Commit

Permalink
Merge branch 'SLB-328-two-steps-node-creation' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
chindris committed May 7, 2024
2 parents 86cd831 + 920222c commit 9fa491c
Show file tree
Hide file tree
Showing 24 changed files with 545 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ jobs:
storybookBaseDir: packages/ui
onlyChanged: true
exitOnceUploaded: true
externals: |
static/stories/webforms/**
if: ${{ steps.chromatic-check.outputs.available == 'true' }}

- name: Deploy storybook to netlify
Expand Down
17 changes: 15 additions & 2 deletions .github/workflows/test_without_turbo_cache.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ on:
workflow_dispatch:

jobs:

test:
name: Test
runs-on: ubuntu-20.04
steps:
- name: Init check
if: ${{ github.repository != 'AmazeeLabs/silverback-template'}}
run: echo 'Please run the INIT script. See the root README.md for instructions.' && false

- name: Checkout
uses: actions/checkout@v3
with:
Expand All @@ -16,8 +19,18 @@ jobs:
- name: Setup
uses: ./.github/actions/setup

- name: TurboRepo local server
uses: felixmosh/turborepo-gh-artifacts@v2
with:
server-token: 'local'
repo-token: ${{ secrets.GITHUB_TOKEN }}

- name: Test
run: pnpm turbo:test
run: pnpm turbo:test:force
env:
TURBO_API: 'http://127.0.0.1:9080'
TURBO_TOKEN: 'local'
TURBO_TEAM: 'local'

- name: Upload Playwright report
uses: actions/upload-artifact@v3
Expand Down
3 changes: 3 additions & 0 deletions apps/cms/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@
},
"amazeelabs/silverback_gatsby": {
"Autosave preview": "./patches/fetch-entity.patch"
},
"drupal/gutenberg": {
"Gutenberg enabled hook": "https://www.drupal.org/files/issues/2024-05-07/gutenberg_enabled_hook_3445677-2.patch"
}
},
"patchLevel": {
Expand Down
103 changes: 103 additions & 0 deletions apps/cms/config/sync/core.entity_form_display.node.page.split.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
uuid: a39d0011-2ebf-4b18-a9f0-8b5aeff5b9aa
langcode: en
status: true
dependencies:
config:
- core.entity_form_mode.node.split
- field.field.node.page.body
- field.field.node.page.field_metatags
- node.type.page
- workflows.workflow.basic
module:
- content_moderation
- metatag
- path
id: node.page.split
targetEntityType: node
bundle: page
mode: split
content:
created:
type: datetime_timestamp
weight: 3
region: content
settings: { }
third_party_settings: { }
field_metatags:
type: metatag_firehose
weight: 11
region: content
settings:
sidebar: true
use_details: true
third_party_settings: { }
langcode:
type: language_select
weight: 1
region: content
settings:
include_locked: true
third_party_settings: { }
moderation_state:
type: moderation_state_default
weight: 9
region: content
settings: { }
third_party_settings: { }
path:
type: path
weight: 7
region: content
settings: { }
third_party_settings: { }
promote:
type: boolean_checkbox
weight: 5
region: content
settings:
display_label: true
third_party_settings: { }
status:
type: boolean_checkbox
weight: 10
region: content
settings:
display_label: true
third_party_settings: { }
sticky:
type: boolean_checkbox
weight: 6
region: content
settings:
display_label: true
third_party_settings: { }
title:
type: string_textfield
weight: 0
region: content
settings:
size: 60
placeholder: ''
third_party_settings: { }
translation:
weight: 4
region: content
settings: { }
third_party_settings: { }
uid:
type: entity_reference_autocomplete
weight: 2
region: content
settings:
match_operator: CONTAINS
match_limit: 10
size: 60
placeholder: ''
third_party_settings: { }
url_redirects:
weight: 8
region: content
settings: { }
third_party_settings: { }
hidden:
body: true
11 changes: 11 additions & 0 deletions apps/cms/config/sync/core.entity_form_mode.node.split.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
uuid: 5f26940d-fd06-41e0-8e59-9dfcdc9c86df
langcode: en
status: true
dependencies:
module:
- node
id: node.split
label: Split
description: ''
targetEntityType: node
cache: true
1 change: 1 addition & 0 deletions apps/cms/config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module:
dropzonejs: 0
dynamic_page_cache: 0
editor: 0
entity_create_split: 0
entity_usage: 0
environment_indicator: 0
field: 0
Expand Down
Empty file removed apps/cms/patches/.gitkeep
Empty file.
2 changes: 1 addition & 1 deletion apps/website/publisher.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default defineConfig({
// cannot report it.
// Workaround: Do a double build on the first build.
'if test -d public; then echo "Single build" && pnpm build:gatsby; else echo "Double build" && pnpm build:gatsby && pnpm build:gatsby; fi'
: 'pnpm build:gatsby',
: 'DRUPAL_EXTERNAL_URL=http://127.0.0.1:8888 pnpm build:gatsby',
outputTimeout: 1000 * 60 * 10,
},
clean: 'pnpm clean',
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"test:format:workspaces": "pnpm --workspace-concurrency=1 -r exec prettier '**/*.{js,cjs,mjs,ts,jsx,tsx,gql,graphql,graphqls,md,mdx,json,htm,html}' --ignore-path='./.gitignore'",
"turbo:local": "if [ -z $CI ]; then echo $(date)$RANDOM > apps/cms/turbo-seed.txt; fi",
"turbo:test": "pnpm turbo:local && pnpm tb test:unit --no-daemon --go-fallback --output-logs=new-only && pnpm tb test:integration --no-daemon --go-fallback --output-logs=new-only --concurrency=1",
"turbo:test:force": "pnpm tb test:unit --no-daemon --go-fallback --output-logs=new-only --force && pnpm tb test:integration --no-daemon --go-fallback --output-logs=new-only --concurrency=1 --force",
"turbo:test:quick": "pnpm turbo:local && pnpm tb test:unit --no-daemon --go-fallback --output-logs=new-only",
"turbo:prep": "pnpm turbo:local && pnpm tb prep --no-daemon --go-fallback --output-logs=new-only",
"turbo:prep:force": "rm -f apps/cms/web/sites/default/files/.sqlite && pnpm tb prep --no-daemon --go-fallback --force",
Expand Down
13 changes: 13 additions & 0 deletions packages/drupal/entity_create_split/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Entity Create Split

A Drupal module which exposes routes to split an entity create form into two parts:
- on the first step, only the required fields are presented. After submitting the form, a entity is already created
- the second step is actually just the entity edit form, containing all the other optional form fields.

To enable this feature, you must create a form mode with the machine name "split" and enable it on the bundle for which you want to have this feature.

## Special case for the Gutenberg editor

The Gutenberg editor does a lot of alterations on the create form. For this reason, it is better that the form alter hook of the gutenberg module to not run at all. This is not easy possible, so right now the easiest approach is to just patch the module with the patch from https://www.drupal.org/project/gutenberg/issues/3445677/

The functionality should also work without the patch, but the initial form will not look that nice.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: Entity Create Split
type: module
description: 'Provides a route for splitting the entity creation in two steps: first for the mandatory fields and a second one, which is actually the edit form, for the rest of the fields.'
package: Custom
core_version_requirement: ^9 || ^10
31 changes: 31 additions & 0 deletions packages/drupal/entity_create_split/entity_create_split.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* @file
* Provides a route for splitting the entity creation in two steps: first for
* the mandatory fields and a second one, which is actually the edit form, for
* the rest of the fields.
*/

use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Entity\EntityInterface;

/**
* Implements hook_entity_type_build().
*/
function entity_create_split_entity_type_build(array &$entity_types) {
foreach ($entity_types as $entity_type) {
if ($entity_type instanceof ContentEntityType) {
$entity_type->setFormClass('split', $entity_type->getFormClass('default'));
}
}
}

/**
* Implements hook_gutenberg_enabled().
*/
function entity_create_split_gutenberg_enabled(EntityInterface $entity) {
if (isset($entity->disableGutenberg) && $entity->disableGutenberg === TRUE) {
return FALSE;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
entity_create_split.create:
path: '/entity/create/{entity_type}/{bundle}'
defaults:
_controller: '\Drupal\entity_create_split\Controller\EntityCreateSplitController::createForm'
_title_callback: '\Drupal\entity_create_split\Controller\EntityCreateSplitController::getTitle'
requirements:
_custom_access: '\Drupal\entity_create_split\Controller\EntityCreateSplitController::access'
options:
_admin_route: TRUE
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
services:
entity_create_split.route_subscriber:
class: Drupal\entity_create_split\EventSubscriber\EntityCreateSplitRequestSubscriber
arguments:
- "@current_route_match"
- "@entity_type.manager"
- "@entity_display.repository"
tags:
- { name: event_subscriber }
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drupal\entity_create_split\Controller;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;

class EntityCreateSplitController extends ControllerBase {

/**
* Route callback to show the first part of the entity create form, which
* contains the required fields.
*
* @param string $entity_type
* @param string $bundle
*
* @return array
* @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
* @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
*/
public function createForm($entity_type, $bundle) {
$entityTypeDefinition = $this->entityTypeManager()->getDefinition($entity_type);
$bundleKey = $entityTypeDefinition->getKey('bundle');
$entity = $this->entityTypeManager()->getStorage($entity_type)->create([$bundleKey => $bundle]);
$entity->disableGutenberg = TRUE;
$editForm = $this->entityTypeManager()->getFormObject($entity_type, 'split')->setEntity($entity);
return \Drupal::formBuilder()->getForm($editForm);
}

/**
* Title callback for the createForm() route.
*/
public function getTitle($entity_type, $bundle) {
$entityTypeDefinition = $this->entityTypeManager()->getDefinition($entity_type);
$bundleEntityType = $entityTypeDefinition->getBundleEntityType();
$bundleLabel = $this->entityTypeManager()->getStorage($bundleEntityType)->load($bundle)->label();
return $this->t("Create %entity_type: %entity_bundle", [
'%entity_type' => $entityTypeDefinition->getLabel(),
'%entity_bundle' => $bundleLabel,
]);
}

/**
* Access callback for the createForm() route.
*/
public function access($entity_type, $bundle) {
if (!$this->entityTypeManager()->hasDefinition($entity_type)) {
return AccessResult::forbidden();
}
$entityTypeDefinition = $this->entityTypeManager()->getDefinition($entity_type);
$bundleEntityType = $entityTypeDefinition->getBundleEntityType();
$bundleEntity = $this->entityTypeManager()->getStorage($bundleEntityType)->load($bundle);
if (!$bundleEntity) {
return AccessResult::forbidden();
}

return $this->entityTypeManager()
->getAccessControlHandler($entity_type)
->createAccess($bundle, NULL, [], TRUE);
}
}
Loading

0 comments on commit 9fa491c

Please sign in to comment.