How to normalize / resolve membership ID to all account UID per group_content I'm trying to add the owner's membership to every GroupContent. By default ownership is only identified by the Account ID.

It seems a Normalizer and/or custom resolver is necessary. Beyond my struggles to get the correct dependency injection to add my serialization service to my controller, I'm not even sure if this is possible since it seems the $context variable never includes anything related to the URL or parent entity (Group). 

So far, I'm basically parsing the group's ID from $_SERVER['REQUEST_URI'] and then SELECT id as memberid from group_content_field_data where type = 'groups-group_membership' AND entity_id = :uid AND gid = :gid. 


Could some caching making $_SERVER['REQUEST_URI'] unreliable from within a Normalizer?
How can I add normalize GroupContent to include both the owner's account ID and membership id?
I was using extending Drupal\serialization\Normalizer\FieldItemNormalizer. I added this service extending 
 Drupal\serialization\Normalizer\ContentEntityNormalizer to get the group id from group_content

  serializer.normalizer.group_content:
    class: Drupal\tam_api\Normalizer\GroupContentNormalizer
    arguments: ['@entity.manager']
    tags:
      - { name: normalizer, priority: 8 }


&lt;?php
namespace Drupal\tam_api\Normalizer;

use Drupal\group\Entity\Group;
use Drupal\group\Entity\GroupContent;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Drupal\serialization\Normalizer\ContentEntityNormalizer;

class GroupContentNormalizer extends ContentEntityNormalizer implements NormalizerInterface {

  protected $supportedInterfaceOrClass = 'Drupal\group\Entity\GroupContent';
  const FORMAT = 'json';

  public function normalize($entity, $format = NULL, array $context = []) {

    $attributes = parent::normalize($entity, $format, $context);
    $gid = $attributes['gid'][0]['target_id'];

    $connection = \Drupal::database();
    $sql = "SELECT G.id as memberid, G.label, F.field_name_value, P.field_headshot_target_id as fid
            FROM group_content_field_data G LEFT JOIN group_content__field_name F
            ON F.entity_id = G.id
            LEFT JOIN group_content__field_headshot P
            ON P.entity_id = G.id
            WHERE G.type = 'groups-group_membership' AND P.deleted = 0 AND F.deleted = 0
            AND G.entity_id = :uid AND G.gid = :gid";

    $fields = ['uid', 'field_leaders', 'field_editors']; // my fields that need the membership data
    foreach($fields as $field_name) {
      if (isset($attributes[$field_name])) {
        foreach($attributes[$field_name] as &amp;$entry) {
          $uid = $entry['target_id'];
          $membership =  $connection-&gt;query($sql, array(':uid'=&gt;$uid, ':gid'=&gt;$gid))-&gt;fetchAssoc();
          $entry['target_gc'] = intVal($membership['memberid']);
          if (!empty($membership['field_name_value'])) {
            $entry['target_label'] = $membership['field_name_value'];
          } else {
            $entry['target_label'] .= ' FALLBACK';
          }
          if (!empty($membership['fid'])) {
            $file = \Drupal\file\Entity\File::load($membership['fid']);
            if ($file-&gt;getSize() &gt; 600000) {
              $entry['url'] = \Drupal\image\Entity\ImageStyle::load('medium')-&gt;buildUrl($file-&gt;getFileUri());
            } else {
              $entry['url'] = $file-&gt;url('canonical');
            }
          }
        }
      }
    }

    return $attributes;
  }

  public function supportsNormalization($data, $format = NULL) {
    return $format === static::FORMAT &amp;&amp; $data instanceof \Drupal\group\Entity\GroupContent;
  }

}