Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
… into feat/SLB-288
  • Loading branch information
HagerDakroury committed May 14, 2024
2 parents 0eef255 + 57aea91 commit 3630dc8
Show file tree
Hide file tree
Showing 51 changed files with 1,726 additions and 84 deletions.
1 change: 1 addition & 0 deletions .idea/prettier.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
2 changes: 0 additions & 2 deletions apps/cms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
"content:import": "pnpm drush php-script web/modules/custom/test_content/import.php",
"config:export": "pnpm drush cex -y",
"config:import": "pnpm drush -y cim",
"schema:test": "pnpm jest --testMatch '<rootDir>/generated/__tests__/test-queries.js' --passWithNoTests",
"schema:test:update": "pnpm schema:test -u",
"import-translations": "pnpm drush scr scripts/translations-import.php"
},
"peerDependencies": {
Expand Down
Empty file removed apps/cms/patches/.gitkeep
Empty file.
1 change: 1 addition & 0 deletions apps/preview/.lagoon.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
PROJECT_NAME="example"
DRUPAL_URL="https://nginx.${LAGOON_ENVIRONMENT}.${LAGOON_PROJECT}.ch4.amazee.io"
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
67 changes: 67 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,67 @@
<?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\ContentEntityForm;
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;
}
}

/**
* Implements hook_form_alter().
*/
function entity_create_split_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
$formObject = $form_state->getFormObject();
if ($formObject instanceof ContentEntityForm && $formObject->getFormDisplay($form_state)->getMode() === 'split') {
if (empty($form['#submit'])) {
$form['#submit'] = [];
}
$form['#submit'][] = 'entity_create_split_submit_redirect';
if (!empty($form['actions']['submit']['#submit'])) {
$form['actions']['submit']['#submit'][] = 'entity_create_split_submit_redirect';
}
}
}

/**
* Submit handler for the entity create from, to redirect the user to the entity
* edit form.
*
* @param array $form
* @param \Drupal\Core\Form\FormStateInterface $form_state
*
* @return void
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
function entity_create_split_submit_redirect(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
/* @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $form_state->getFormObject()->getEntity();
if (!empty($entity) && $entity->id()) {
$url = $entity->toUrl('edit-form');
$form_state->setRedirectUrl($url);
}
}
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 3630dc8

Please sign in to comment.