6

I am building a custom service, a membership service. This service can be called from a controller. But what I don't seem to get working, is to use another service, the QueryFactory, to use in that custom service.

the membership service:

namespace Drupal\hsbxl_members;
use Drupal\Core\Entity\Query\QueryFactory;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class MembershipService.
 */
class MembershipService {
  protected $hsbxl_member;
  protected $entityQuery;

  public function __construct(QueryFactory $entityQuery) {
    $this->entity_query = $entity_query;
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity.query')
    );
  }

  public function MembershipsAll() {
    $query = $this->entity_query->get('node');
    $query->condition('status', 1);
    $nids = $query->execute();
    return "Hello World!";
  }
}

services file:

services:
  hsbxl_members.membership:
    class: Drupal\hsbxl_members\MembershipService
    arguments: ['@entity.query']

Can anyone point me out on this? What am I doing wrong / am I missing?

Tom

  • Which part isn't working? – Kevin Oct 16 '17 at 18:39
  • 1
    Couple of things: it’s entity_type.query (the other one is deprecated), and you don’t need the create() method as it’s a service and injection happens a different way. Not sure either of those are causing the problem but worth a look – Clive Oct 16 '17 at 18:40
  • Clive beat me to it. You don't need the create method. The constructor will receive the arguments from the services.yml definition. Other than that if you can list the error that would help. – Kevin Oct 16 '17 at 18:42
  • 1
    Some constructive advice, you may want to think about method names. Ex. instead of MembershipsAll, make it getAllMemberships, or getMemberships($filter_args). – Kevin Oct 16 '17 at 18:44
  • Thanks for your help all. The error get is Error: Call to a member function get() on null in Drupal\hsbxl_members\MembershipService->getMemberships() – betz Oct 16 '17 at 19:40
6

The service you need is entity_type.manager:

.services.yml

services:
  hsbxl_members.membership:
    class: Drupal\hsbxl_members\MembershipService
    arguments: ['@entity_type.manager']

MembershipService.php

namespace Drupal\hsbxl_members;

use Drupal\Core\Entity\EntityTypeManagerInterface;

/**
 * Class MembershipService.
 */
class MembershipService {
  protected $hsbxl_member;
  protected $entityTypeManager;

  public function __construct(EntityTypeManagerInterface $entity_type_manager) {
    $this->entityTypeManager = $entity_type_manager;
  }

  public function MembershipsAll() {
    $query = $this->entityTypeManager->getStorage('node')->getQuery();
    $query->condition('status', 1);
    $nids = $query->execute();
    return "Hello World!";
  }
}

The create() method is unnecessary as services use constructor injection.

| improve this answer | |
  • Why did you use EntityTypeManagerInterface rather than using EntityTypeManager directly? – Keven Mar 23 '19 at 14:54
  • 1
    Do you mean for the type hint @Kevin? It doesn’t make sense to use a concrete class when an interface is available, it would mean your code is less flexible/testable – Clive Mar 23 '19 at 14:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.