Using AutoID plugin and page.update:after

I am using page.update:after to duplicate a structure field from a page to a user when the page is updated. I am using the AutoID plugin on the structure fields. The duplication is working fine, however the autoid field is appearing as null on the updated structure when I am adding a new row onto the structure. For example if I save this on the page as two new rows:

- 
  autoid: k46l0zaw
  task: Something
  progress: incomplete
- 
  autoid: kykg3dk4
  task: Something else
  progress: incomplete

The following is on the user:

- 
  autoid: null
  task: Something
  progress: incomplete
- 
  autoid: null
  task: Something else
  progress: incomplete

If I add an additional row, the existing rows now update their autoid correctly (though the new row appears as null).

I am not sure what is happening here, has anyone had anything similar with this plugin? It is like the autoid is not being generated until too late to be included in the update process?

You don’t really want the plugin to add new AutoIds to your user structure fields, because they should inherit the ones from the pages field. This will of course not make these IDs unique anymore, but you need them to be identical across page and users to find which items to update and which ones to add.

Thanks @texnixe - sorry I don’t understand your feedback. The ids should be identical between the page and the user, but currently are appearing as null on any new rows on the user when the hook is called. The user structure is not editable in the panel, so will always be a match to what is on the page.

Could you please post the code you used to update the user structure fields? I guess it’s the code you posted in the user thread, so we shouldn’t really duplicate here.

It is, but for future visitors it’s good to have it here I guess.

So to confirm the issue - any new rows, do not have their autoid duplicated across, it appears as null in the users file

'page.update:after' => function ($newPage, $oldPage) {
      if ($newPage->template() == 'building-block') {

        // Now I am sure it is a building block page being updated

        $blockName = $newPage->slug();
        function toUpper($matches) {
          return strtoupper($matches[1]);
        }
        $blockName = preg_replace_callback('/[-_](.)/', 'toUpper', $blockName);

        // This generates the structure names that match those on the user template
        $leadershipAndCommunicationKeyTasks = $blockName . 'LeadershipAndCommunicationKeyTasks';
        $curriculumKeyTasks = $blockName . 'CurriculumKeyTasks';
        $timetableAndPlanningKeyTasks = $blockName . 'TimetableAndPlanningKeyTasks';

        //This gets the structures off the page
        $updatedleadershipAndCommunicationKeyTasks = $newPage->leadershipAndCommunicationKeyTasks()->yaml();
        $updatedCurriculumKeyTasks = $newPage->curriculumKeyTasks()->yaml();
        $updatedTimetableAndPlanningKeyTasks = $newPage->timetableAndPlanningKeyTasks()->yaml();

        // Get all participant users
        $kirby = kirby();
        $users = $kirby->users()->role('participant');

        // Loop through and make the changes
        foreach ($users as $user) {

          $currentUserLeadershipAndCommunicationKeyTasks = $user->$leadershipAndCommunicationKeyTasks()->yaml();
          $currentUserCurriculumKeyTasks = $user->$curriculumKeyTasks()->yaml();
          $currentUserTimetableAndPlanningKeyTasks = $user->$timetableAndPlanningKeyTasks()->yaml();

          foreach ($currentUserLeadershipAndCommunicationKeyTasks as $a) {
            $one[$a['autoid']] = $a;
          }

          foreach ($currentUserCurriculumKeyTasks as $b) {
            $two[$b['autoid']] = $b;
          }

          foreach ($currentUserTimetableAndPlanningKeyTasks as $c) {
            $three[$c['autoid']] = $c;
          }

          foreach ($updatedleadershipAndCommunicationKeyTasks as $a){
            if ( isset($one[$a['autoid']])){
              $a['progress'] = $one[$a['autoid']]['progress'];
          }

          $updatedleadershipAndCommunicationKeyTasks2[] = $a;

          }

          foreach ($updatedCurriculumKeyTasks as $b){
            if ( isset($two[$b['autoid']])){
              $b['progress'] = $two[$b['autoid']]['progress'];
          }

          $updatedCurriculumKeyTasks2[] = $b;

          }

          foreach ($updatedTimetableAndPlanningKeyTasks as $c){
            if ( isset($three[$c['autoid']])){
              $c['progress'] = $three[$c['autoid']]['progress'];
          }

          $updatedTimetableAndPlanningKeyTasks2[] = $c;

          }

          $user->save([
            $leadershipAndCommunicationKeyTasks => Data::encode($updatedleadershipAndCommunicationKeyTasks2, "yaml"),
            $curriculumKeyTasks => Data::encode($updatedCurriculumKeyTasks2, "yaml"),
            $timetableAndPlanningKeyTasks => Data::encode($updatedTimetableAndPlanningKeyTasks2, "yaml")
          ]);
        }
      }
    },
  1. autoid runs on page.update:after hook itself. so inside the hook there might not be an updated autoid value yet (depending on execution order of hook listeners). its a bit like this but from within another hook.

  2. autoid plugin only works for site, pages and file objects but not users. see hooks

Thanks for the reply. There are no edits being made to the users other than with this hook, so autoid doesn’t need to “work” with users, I just need a field there to accept the autoid generated in the page structure.

I think it is point 1 that is causing me the problem - my hook is sending the data before the autoid is being generated. I don’t know what, if anything, I can do about that!