Error When Referencing Same User in Multiple PBF Fields

Created on 6 April 2024, 3 months ago
Updated 17 April 2024, 2 months ago

Encountering a SQL error when referencing the same user in two separate PBF fields within a content type.
Any assistance to resolve this issue would be greatly appreciated.

πŸ› Bug report
Status

Needs work

Version

1.4

Component

Code

Created by

πŸ‡²πŸ‡¦Morocco achrafon

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @achrafon
  • πŸ‡¬πŸ‡§United Kingdom aaron.ferris

    Hello @drupaler99

    Can you please provide complete testing steps/steps to reproduce? Ive tried recreating this with 2 user reference fields on the same content type, with the same user in both and im not seeing any error

    Also, can you please provide the error you're seeing?

    Thanks
    Aaron

  • πŸ‡²πŸ‡¦Morocco achrafon

    Hello @aaron.ferris
    Thank you for your response.

    To reproduce the issue:

    Create a content type, add two user reference fields (PBF) to the content type: (for example) one named "Manager" and another named "Chief".

    Configure both fields to grant access for viewing and updating.

    Add the same user (e.g., user1) to both the "Manager" and "Chief" fields.

    Save the project content.

    You should encounter the following error message:

    Le site Web a rencontrΓ© une erreur inattendue.

    Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '1006-6-pbf_user_id-fr' for key 'PRIMARY': INSERT INTO "node_access" ("nid", "langcode", "fallback", "realm", "gid", "grant_view", "grant_update", "grant_delete") VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7), (:db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14, :db_insert_placeholder_15), (:db_insert_placeholder_16, :db_insert_placeholder_17, :db_insert_placeholder_18, :db_insert_placeholder_19, :db_insert_placeholder_20, :db_insert_placeholder_21, :db_insert_placeholder_22, :db_insert_placeholder_23), (:db_insert_placeholder_24, :db_insert_placeholder_25, :db_insert_placeholder_26, :db_insert_placeholder_27, :db_insert_placeholder_28, :db_insert_placeholder_29, :db_insert_placeholder_30, :db_insert_placeholder_31); Array ( [:db_insert_placeholder_0] => 1006 [:db_insert_placeholder_1] => fr [:db_insert_placeholder_2] => 1 [:db_insert_placeholder_3] => pbf_user_id [:db_insert_placeholder_4] => 6 [:db_insert_placeholder_5] => 1 [:db_insert_placeholder_6] => 1 [:db_insert_placeholder_7] => 0 [:db_insert_placeholder_8] => 1006 [:db_insert_placeholder_9] => fr [:db_insert_placeholder_10] => 1 [:db_insert_placeholder_11] => pbf_user_id [:db_insert_placeholder_12] => 6 [:db_insert_placeholder_13] => 1 [:db_insert_placeholder_14] => 1 [:db_insert_placeholder_15] => 0 [:db_insert_placeholder_16] => 1006 [:db_insert_placeholder_17] => fr [:db_insert_placeholder_18] => 1 [:db_insert_placeholder_19] => pbf_author [:db_insert_placeholder_20] => 1 [:db_insert_placeholder_21] => 1 [:db_insert_placeholder_22] => 1 [:db_insert_placeholder_23] => 1 [:db_insert_placeholder_24] => 1006 [:db_insert_placeholder_25] => fr [:db_insert_placeholder_26] => 1 [:db_insert_placeholder_27] => tac_lite_scheme_1 [:db_insert_placeholder_28] => 2 [:db_insert_placeholder_29] => 1 [:db_insert_placeholder_30] => 0 [:db_insert_placeholder_31] => 0 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 817 of core\lib\Drupal\Core\Entity\Sql\SqlContentEntityStorage.php).
    Drupal\Core\Database\StatementWrapperIterator->execute(Array, Array) (Line: 44)
    Drupal\mysql\Driver\Database\mysql\Insert->execute() (Line: 242)
    Drupal\node\NodeGrantDatabaseStorage->write(Object, Array, NULL, 1) (Line: 150)
    Drupal\node\Entity\Node->postSave(Object, 1) (Line: 563)
    Drupal\Core\Entity\EntityStorageBase->doPostSave(Object, 1) (Line: 781)
    Drupal\Core\Entity\ContentEntityStorageBase->doPostSave(Object, 1) (Line: 489)
    Drupal\Core\Entity\EntityStorageBase->save(Object) (Line: 806)
    Drupal\Core\Entity\Sql\SqlContentEntityStorage->save(Object) (Line: 352)
    Drupal\Core\Entity\EntityBase->save() (Line: 270)
    Drupal\node\NodeForm->save(Array, Object)
    call_user_func_array(Array, Array) (Line: 129)
    Drupal\Core\Form\FormSubmitter->executeSubmitHandlers(Array, Object) (Line: 67)
    Drupal\Core\Form\FormSubmitter->doSubmitForm(Array, Object) (Line: 597)
    Drupal\Core\Form\FormBuilder->processForm('node_projet_edit_form', Array, Object) (Line: 325)
    Drupal\Core\Form\FormBuilder->buildForm(Object, Object) (Line: 73)
    Drupal\Core\Controller\FormController->getContentResult(Object, Object) (Line: 39)
    Drupal\layout_builder\Controller\LayoutBuilderHtmlEntityFormController->getContentResult(Object, Object)
    call_user_func_array(Array, Array) (Line: 123)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 627)
    Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 121)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->wrapControllerExecutionInRenderContext(Array, Array) (Line: 97)
    Drupal\Core\EventSubscriber\EarlyRenderingControllerWrapperSubscriber->Drupal\Core\EventSubscriber\{closure}() (Line: 181)
    Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
    Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 58)
    Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
    Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
    Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 48)
    Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
    Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
    Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 704)
    Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

  • πŸ‡¬πŸ‡§United Kingdom aaron.ferris

    Thanks @drupaler99 I can indeed replicate that, although to be quite honest im not entirely sure on the approach we would take to resolve it because it'll need some thought. Should this be an expected use case, which field would take preference? Would we override any duplication? Would we ignore duplication? Would we try and so some sort of merge from multiple fields?

    This may need a module maintainer to jump in on this to give some thought on expected behaviour.

  • πŸ‡²πŸ‡¦Morocco achrafon

    In that case, we could opt to choose the field with the higher permissions.

    For example, if both fields reference the same user, and if one field has read and update permissions while another has only read permissions, we would prioritize the one with higher permissions.

    Any assistance in developing a patch to implement and test this solution effectively would be greatly appreciated.

  • πŸ‡¬πŸ‡§United Kingdom aaron.ferris

    I think you'd need to tweak what's going in in pbf_node_access_records

    When recreating this I believe the $grants array is where the duplication lies. Off the top of my head, we could loop through this array, do a comparison between those that have the duplicate key, and assign permissions accordingly.

    There may well be a nicer place to do this earlier in the chain however, this is just a bit of a brain dump.

    I'll attach a patch, although as a disclaimer im not sure of unintentional consequences!!!

  • πŸ‡¬πŸ‡§United Kingdom aaron.ferris
  • Open in Jenkins β†’ Open on Drupal.org β†’
    Core: 10.2.1 + Environment: PHP 8.1 & MySQL 8
    last update 2 months ago
    6 pass, 3 fail
  • πŸ‡²πŸ‡¦Morocco achrafon

    Hello aaron.ferris

    The patch applied cleanly, and I tested several scenarios with no errors, so I think it works perfectly. Thank you for your help & assistance.

  • Assigned to achrafon
  • πŸ‡¬πŸ‡§United Kingdom aaron.ferris

    You're welcome!

  • Status changed to Needs work 2 months ago
  • πŸ‡«πŸ‡·France flocondetoile Lyon

    Tests are failing. So set as Need Work.

Production build 0.69.0 2024