Issue #3414661: Serial not generating on entity save in certain contexts
6 unresolved threads
Closes #3414661
Merge request reports
Activity
added 1 commit
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()); Drupal coding standards say that
In general, all lines of code should not be longer than 80 characters.
Not everyone follows that, but I like to.
changed this line in version 3 of the diff
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()); changed this line in version 3 of the diff
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
Comments should generally get as close to 80 characters as they can. The comma is because I am a grammar nerd.
changed this line in version 3 of the diff
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 changed this line in version 3 of the diff
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); changed this line in version 3 of the diff
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(); - Comment on lines +121 to +122
According to the API docs for hook_entity_insert(),
Note that hook implementations may not alter the stored entity data.
I am a little afraid that saving here would lead to an infinite loop. I will say more in a comment on the issue.
added 1 commit
Please register or sign in to reply