Skip to content
Snippets Groups Projects

Issue #3414661: Serial not generating on entity save in certain contexts

Open Issue #3414661: Serial not generating on entity save in certain contexts
6 unresolved threads
6 unresolved threads

Closes #3414661

Merge request reports

Members who can merge are allowed to add commits.
Approval is optional
Ready to merge by members who can write to the target branch.
  • The source branch is 8 commits behind the target branch.
  • 1 commit will be added to 2.0.x.
  • Source branch will not be deleted.

Activity

Filter activity
  • Approvals
  • Assignees & reviewers
  • Comments (from bots)
  • Comments (from users)
  • Commits & branches
  • Edits
  • Labels
  • Lock status
  • Mentions
  • Merge request status
  • Tracking
75 77 }
76 78 }
77 79
80 /**
81 * Implements hook_entity_insert().
82 *
83 * Generate serial value for other contexts
84 * such as hook_form_alter(), feeds import etc.
85 */
86 function serial_entity_insert(EntityInterface $entity) {
87 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
88 $entity_type_manager = \Drupal::entityTypeManager();
89
90 /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $content_entity_type */
91 $content_entity_type = $entity_type_manager->getDefinition($entity->getEntityTypeId());
  • 86 function serial_entity_insert(EntityInterface $entity) {
    87 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
    88 $entity_type_manager = \Drupal::entityTypeManager();
    89
    90 /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $content_entity_type */
    91 $content_entity_type = $entity_type_manager->getDefinition($entity->getEntityTypeId());
    92
    93 // Check if definition belongs to
    94 // \Drupal\Core\Entity\ContentEntityTypeInterface.
    95 // Skip generation of serial if not.
    96 if (!$content_entity_type instanceof ContentEntityTypeInterface) {
    97 return;
    98 }
    99
    100 // Load the field definitions.
    101 $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle());
  • 89
    90 /** @var \Drupal\Core\Entity\ContentEntityTypeInterface $content_entity_type */
    91 $content_entity_type = $entity_type_manager->getDefinition($entity->getEntityTypeId());
    92
    93 // Check if definition belongs to
    94 // \Drupal\Core\Entity\ContentEntityTypeInterface.
    95 // Skip generation of serial if not.
    96 if (!$content_entity_type instanceof ContentEntityTypeInterface) {
    97 return;
    98 }
    99
    100 // Load the field definitions.
    101 $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle());
    102
    103 // Loop through field definitions to look for serial field
    104 // and pre-populate serial value if empty.
    • Comment on lines +103 to +104
      Suggested change
      103 // Loop through field definitions to look for serial field
      104 // and pre-populate serial value if empty.
      103 // Loop through field definitions to look for serial field, and pre-populate
      104 // serial value if empty.

      Comments should generally get as close to 80 characters as they can. The comma is because I am a grammar nerd.

    • Madelyn Cruz changed this line in version 3 of the diff

      changed this line in version 3 of the diff

    • Please register or sign in to reply
  • 92
    93 // Check if definition belongs to
    94 // \Drupal\Core\Entity\ContentEntityTypeInterface.
    95 // Skip generation of serial if not.
    96 if (!$content_entity_type instanceof ContentEntityTypeInterface) {
    97 return;
    98 }
    99
    100 // Load the field definitions.
    101 $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle());
    102
    103 // Loop through field definitions to look for serial field
    104 // and pre-populate serial value if empty.
    105 foreach ($field_definitions as $field_name => $field_definition) {
    106 if (!empty($field_definition->getTargetBundle())
    107 && $field_definition->getType() == SerialStorageInterface::SERIAL_FIELD_TYPE
  • 95 // Skip generation of serial if not.
    96 if (!$content_entity_type instanceof ContentEntityTypeInterface) {
    97 return;
    98 }
    99
    100 // Load the field definitions.
    101 $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($entity->getEntityTypeId(), $entity->bundle());
    102
    103 // Loop through field definitions to look for serial field
    104 // and pre-populate serial value if empty.
    105 foreach ($field_definitions as $field_name => $field_definition) {
    106 if (!empty($field_definition->getTargetBundle())
    107 && $field_definition->getType() == SerialStorageInterface::SERIAL_FIELD_TYPE
    108 && $entity->$field_name->isEmpty()) {
    109 /** @var \Drupal\serial\SerialStorageInterface $serial_storage */
    110 $serial_value = \Drupal::service('serial.sql_storage')->generateValue($field_definition, $entity, TRUE);
  • 107 && $field_definition->getType() == SerialStorageInterface::SERIAL_FIELD_TYPE
    108 && $entity->$field_name->isEmpty()) {
    109 /** @var \Drupal\serial\SerialStorageInterface $serial_storage */
    110 $serial_value = \Drupal::service('serial.sql_storage')->generateValue($field_definition, $entity, TRUE);
    111
    112 // Get the starting value from the storage settings.
    113 $start_value = $field_definition->getSetting('start_value');
    114
    115 // Subtract one as it is already added serial storage.
    116 $serial = ($serial_value + $start_value) - 1;
    117
    118 // Set serial.
    119 $entity->set($field_name, $serial);
    120
    121 // Save entity.
    122 $entity->save();
  • Madelyn Cruz added 1 commit

    added 1 commit

    • 44dd51aa - Issue #3414661: Serial not generating on entity save in certain contexts

    Compare with previous version

  • Please register or sign in to reply
    Loading