Score:4

non-existent service, yet cache is cleared and module is enabled

ca flag

I'm trying to access a Drupal service for normalizing content, yet the service is non-existent, even though the module "hal" is enabled and the service is defined. How can I get the service "serializer.normalizer.entity.hal"?

root@0968b6d03cdb:/var/www/html# drush cr
 [success] Cache rebuild complete.

root@0968b6d03cdb:/var/www/html# drush pml|grep hal
  Web services                      HAL (hal)                                               Enabled    9.3.0           

root@0968b6d03cdb:/var/www/html# drush ev "\Drupal::service('serializer.normalizer.entity.hal');"

In Container.php line 156:
                                                                                 
  You have requested a non-existent service "serializer.normalizer.entity.hal".  
                                                                                 
root@0968b6d03cdb:/var/www/html# cat core/modules/hal/hal.services.yml
services:
  serializer.normalizer.entity_reference_item.hal:
    class: Drupal\hal\Normalizer\EntityReferenceItemNormalizer
    arguments: ['@hal.link_manager', '@serializer.entity_resolver', '@entity_type.manager']
    tags:
      - { name: normalizer, priority: 10 }
  serializer.normalizer.field_item.hal:
    class: Drupal\hal\Normalizer\FieldItemNormalizer
    tags:
      - { name: normalizer, priority: 10 }
  serializer.normalizer.field.hal:
    class: Drupal\hal\Normalizer\FieldNormalizer
    tags:
      - { name: normalizer, priority: 10 }
  serializer.normalizer.file_entity.hal:
    class: Drupal\hal\Normalizer\FileEntityNormalizer
    arguments: ['@entity_type.manager', '@hal.link_manager', '@module_handler', '@config.factory', '@entity_type.repository', '@entity_field.manager']
    tags:
      - { name: normalizer, priority: 20 }
  serializer.normalizer.timestamp_item.hal:
   class: Drupal\hal\Normalizer\TimestampItemNormalizer
   tags:
     # Priority must be higher than serializer.normalizer.field_item.hal.
     - { name: normalizer, priority: 20 }
  serializer.normalizer.entity.hal:
    class: Drupal\hal\Normalizer\ContentEntityNormalizer
    arguments: ['@hal.link_manager', '@entity_type.manager', '@module_handler', '@entity_type.repository', '@entity_field.manager']
    tags:
      - { name: normalizer, priority: 10 }
  serializer.encoder.hal:
    class: Drupal\hal\Encoder\JsonEncoder
    tags:
      - { name: encoder, priority: 10, format: hal_json }

  # Link managers.
  hal.link_manager:
    class: Drupal\hal\LinkManager\LinkManager
    arguments: ['@hal.link_manager.type', '@hal.link_manager.relation']
  hal.link_manager.type:
    class: Drupal\hal\LinkManager\TypeLinkManager
    arguments: ['@cache.default', '@module_handler', '@config.factory', '@request_stack', '@entity_type.bundle.info', '@entity_type.manager']
  hal.link_manager.relation:
    class: Drupal\hal\LinkManager\RelationLinkManager
    arguments: ['@cache.default', '@entity_type.manager', '@module_handler', '@config.factory', '@request_stack', '@entity_type.bundle.info', '@entity_field.manager']

I can get other services, just not the "hal" one I want. For example, drush ev '\Drupal::service("node.node_route_context");' works fine.

Score:6
bd flag

You can't get it directly because normalizer services are private since Drupal 8.6, see https://www.drupal.org/node/2936397:

Using encoder/normalizer services directly is not supported, one must use the @serializer service, which will then call the appropriate encoder/normalizer services. Services that have the normalizer or encoder tag really are internal/private implementation details to culminate in an operational @serializer service. These never were APIs. They implement Symfony's "Serializer component" API.

This is also why the service is not listed when looking at all available services:

/var/www/html# drupal debug:container | grep 'hal'
hal.link_manager             Drupal\hal\LinkManager\LinkManager                                        
hal.link_manager.relation    Drupal\hal\LinkManager\RelationLinkManager                                
hal.link_manager.type        Drupal\hal\LinkManager\TypeLinkManager  

As you can see, none of the services from core/modules/hal/hal.services.yml, that are tagged as normalizer are listed.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.