Consider adding AutoSaveData::asResponse

Created on 18 February 2025, about 2 months ago

Overview

In ✨ Auto-save code components Active we added AutoSaveData object
We treat NULL data as no content and have logic in the auto save controller that is aware of this.
We could instead add AutoSaveData::asResponse and keep that as an internal implementation detail.

Proposed resolution

Consider adding this method

User interface changes

πŸ“Œ Task
Status

Active

Version

0.0

Component

Internal HTTP API

Created by

πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

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

Comments & Activities

  • Issue created by @larowlan
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10
  • πŸ‡§πŸ‡ͺBelgium wim leers Ghent πŸ‡§πŸ‡ͺπŸ‡ͺπŸ‡Ί

    How would ::asResponse() then get access to all this other information?

      /**
       * Get the auto saved changes.
       */
      public function get(): CacheableJsonResponse {
        $all = $this->autoSaveManager->getAllAutoSaveList();
        $userIds = \array_column($all, 'owner');
        $cache = new CacheableMetadata();
        /** @var \Drupal\user\UserInterface[] $users */
        $users = $this->entityTypeManager->getStorage('user')->loadMultiple($userIds);
        foreach ($users as $uid => $user) {
          $access = $user->access('view label', return_as_object: TRUE);
          $cache->addCacheableDependency($user);
          $cache->addCacheableDependency($access);
          if (!$access->isAllowed()) {
            unset($users[$uid]);
          }
        }
        // User display names depend on configuration.
        $cache->addCacheableDependency($this->configFactory->get('user.settings'));
    
        // Remove 'data' key because this will reduce the amount of data sent to the
        // client and back to the server.
        $all = \array_map(fn(array $item) => \array_diff_key($item, ['data' => '']), $all);
    
        $withUserDetails = \array_map(fn(array $item) => [
          // @phpstan-ignore-next-line
          'owner' => \array_key_exists($item['owner'], $users) ? [
            'name' => $users[$item['owner']]->getDisplayName(),
            'avatar' => $this->buildAvatarUrl($users[$item['owner']]),
            'uri' => $users[$item['owner']]->toUrl()->toString(),
            'id' => $item['owner'],
          ] : [
            'name' => new TranslatableMarkup('User @uid', ['@uid' => $item['owner']]),
            'avatar' => NULL,
            'uri' => NULL,
            'id' => $item['owner'],
          ],
        ] + $item, $all);
        return (new CacheableJsonResponse($withUserDetails))->addCacheableDependency($cache->addCacheTags([AutoSaveManager::CACHE_TAG]));
      }
    
  • πŸ‡¦πŸ‡ΊAustralia larowlan πŸ‡¦πŸ‡ΊπŸ.au GMT+10

    I think we can close this now we have ::isEmpty

    My main gripe was that we were checking $autosave->data === NULL outside of the AutoSave Data class which was leaking an implementation detail.

    The special case of NULL is unfortunate but I can live with it if _only_ the class itself is aware of what it means.

    With ::isEmpty that is now the case (and I fixed the other is null check when we added isEmpty).

    So yep, closed duplicate on πŸ“Œ Code Components should render with their auto-saved state(if any) when rendered in the XB UI Active which fixed that

Production build 0.71.5 2024