Serial not generating on entity save in certain contexts

Created on 15 January 2024, 10 months ago
Updated 29 February 2024, 9 months ago

Problem/Motivation

The serial is functioning correctly on the actual entity form, but it fails to generate in specific contexts such as feeds import, submit handlers on hook forms, etc.

Steps to reproduce

Using feeds import
1) Create new content type or edit an existing one.
2) Add the serial field with any starting value. For example, 10000. Then, set "No" for "Start on existing entities".
3) Save.
4) Create new feed type, then add feed content.
5) Import.
6) Observe that the serial is not generated as expected.

Using custom form submit handler
1) Create an entity via hook_form_alter().
2) Observe that the serial is not generated as expected.

Upon checking the node__field_serial table, the created node via sample contexts is not being created. When editing the node created, it always returning "1" when it supposed to follow the number of the last serial value.

Proposed resolution

Use hook_entity_insert to ensure generation of serial.

Any proposed solutions other than hook_entity_insert is greatly appreciated. Thanks!

πŸ› Bug report
Status

Needs work

Version

2.0

Component

Code

Created by

πŸ‡΅πŸ‡­Philippines madelyncruz

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @madelyncruz
  • πŸ‡΅πŸ‡­Philippines madelyncruz
  • πŸ‡΅πŸ‡­Philippines madelyncruz
  • Assigned to madelyncruz
  • Status changed to Needs review 10 months ago
  • πŸ‡΅πŸ‡­Philippines madelyncruz
  • Status changed to Needs work 9 months ago
  • πŸ‡ΊπŸ‡ΈUnited States benjifisher Boston area

    @madelyncruz:

    Thanks for reporting this issue, and working to fix it.

    The usual practice is to assign an issue to yourself when you are working on it, then unassign it when it is ready for review.

    I made some suggestions on the MR, but there are some bigger points as well.

    First, I think that hook_entity_insert() is the wrong hook to use here. It means an extra save, which might even lead to an infinite loop. We should use hook_entity_create() or hook_entity_presave() instead, both of which are called before the entity is saved. See Entity CRUD, editing, and view hooks.

    I suggest using hook_entity_create(). This will be called on new entities, not updated ones. That should be enough: it saves the trouble of checking whether the field is already populated and quitting if it is. Also, there might be some edge cases where the site uses custom code to remove the serial field in special cases. In that case, we should not add it back whenever the entity is updated.

    The second point is that a change like this needs to have an automated test to go along with it. The test should do at least the following:

    1. Prove that there is really a problem, and that the proposed changes fix that problem. The test should fail without the change and pass with the change.
    2. Confirm that there is not an off-by-one error. Especially since the code comes with a comment about subtracting one, we have to be careful about that.
    3. Guard against future changes to the codebase. Maybe some day we will think of a simpler way to manage the serial field so that you do not have to subtract one. If that happens, then we have to make sure that this code still works, or the test fails so that we know to update it.

    The second and third items can be covered by the same test.

    If you are not familiar with writing PHPUnit tests, then at least come up with some lines of code to show the steps. I often use drush php for that sort of thing. Then I can convert it into a test.

    The last point is that we may be able to simplify other code in this module once we implement the right hook. (Maybe we can remove code in Drupal\serial\Plugin\Field\FieldType\SerialItem that populates the field.) We might do that as part of this issue or as a follow-up.

Production build 0.71.5 2024