Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
serial
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
serial
Merge requests
!5
Issue
#3414661
: Serial not generating on entity save in certain contexts
Code
Review changes
Check out branch
Download
Patches
Plain diff
Open
Issue
#3414661
: Serial not generating on entity save in certain contexts
issue/serial-3414661:3414661-generate-serial-entity-insert
into
2.0.x
Overview
6
Commits
1
Pipelines
0
Changes
1
Open
Madelyn Cruz
requested to merge
issue/serial-3414661:3414661-generate-serial-entity-insert
into
2.0.x
1 year ago
Overview
6
Commits
1
Pipelines
0
Changes
1
Expand
Closes
#3414661
0
0
Merge request reports
Compare
2.0.x
version 2
547ee144
1 year ago
version 1
5de6db15
1 year ago
2.0.x (HEAD)
and
latest version
latest version
44dd51aa
1 commit,
1 year ago
version 2
547ee144
1 commit,
1 year ago
version 1
5de6db15
1 commit,
1 year ago
1 file
+
52
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
serial.module
+
52
−
0
Options
@@ -5,6 +5,8 @@
* The Serial module main file.
*/
use
Drupal\Core\Entity\ContentEntityTypeInterface
;
use
Drupal\Core\Entity\EntityInterface
;
use
Drupal\Core\Routing\RouteMatchInterface
;
use
Drupal\Core\StringTranslation\TranslatableMarkup
;
use
Drupal\Core\Url
;
@@ -89,6 +91,56 @@ function serial_clone_node_alter(Node &$node, $context) {
}
}
/**
* Implements hook_entity_insert().
*
* Generate serial value for other contexts
* such as hook_form_alter(), feeds import etc.
*/
function
serial_entity_insert
(
EntityInterface
$entity
)
{
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager
=
\Drupal
::
entityTypeManager
();
/** @var \Drupal\Core\Entity\ContentEntityTypeInterface $content_entity_type */
$content_entity_type
=
$entity_type_manager
->
getDefinition
(
$entity
->
getEntityTypeId
());
// Check if definition belongs to
// \Drupal\Core\Entity\ContentEntityTypeInterface.
// Skip generation of serial if not.
if
(
!
$content_entity_type
instanceof
ContentEntityTypeInterface
)
{
return
;
}
// Load the field definitions.
$field_definitions
=
\Drupal
::
service
(
'entity_field.manager'
)
->
getFieldDefinitions
(
$entity
->
getEntityTypeId
(),
$entity
->
bundle
());
// Loop through field definitions to look for serial field, and pre-populate
// serial value if empty.
foreach
(
$field_definitions
as
$field_name
=>
$field_definition
)
{
if
(
!
empty
(
$field_definition
->
getTargetBundle
())
&&
$field_definition
->
getType
()
===
SerialStorageInterface
::
SERIAL_FIELD_TYPE
&&
$entity
->
$field_name
->
isEmpty
())
{
/** @var \Drupal\serial\SerialStorageInterface $serial_storage */
$serial_value
=
\Drupal
::
service
(
'serial.sql_storage'
)
->
generateValue
(
$field_definition
,
$entity
,
TRUE
);
// Get the starting value from the storage settings.
$start_value
=
$field_definition
->
getSetting
(
'start_value'
);
// Subtract one as it is already added serial storage.
$serial
=
(
$serial_value
+
$start_value
)
-
1
;
// Set serial.
$entity
->
set
(
$field_name
,
$serial
);
// Save entity.
$entity
->
save
();
}
}
}
/**
* Implements hook_theme().
*/
Loading