3

I am creating a queueworker plugin, and would need the QueryFactory and EntityManagerInterface. I set these as 'use' statements, and added them in my parameters in __construct(), but i get following error:

TypeError: Argument 1 passed to Drupal\custom_example\Plugin\QueueWorker\SalesQueueProcessor::__construct() must be an instance of Drupal\Core\Entity\Query\QueryFactory, array given

My QueueWorker plugin:

namespace Drupal\simplified_bookkeeping\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\custom_example\Entity\BookingEntity;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityManagerInterface;


/**
 * Processes Sales Tasks.
 *
 * @QueueWorker(
 *   id = "sales_queue_processor",
 *   title = @Translation("Generate sales from bank/cash statements in queue"),
 *   cron = {"time" = 10}
 * )
 */
class SalesQueueProcessor extends QueueWorkerBase {

  public function __construct(QueryFactory $entity_query, EntityManagerInterface $entityManager) {
    $this->entity_query = $entity_query;
    $this->entityManager = $entityManager;
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($bid) {}
}

What am I doing wrong?

10

Long story short, your plugin needs to implement ContainerFactoryPluginInterface for services to be injected.

Something like this:

namespace Drupal\simplified_bookkeeping\Plugin\QueueWorker;

use Drupal\Core\Queue\QueueWorkerBase;
use Drupal\custom_example\Entity\BookingEntity;
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Processes Sales Tasks.
 *
 * @QueueWorker(
 *   id = "sales_queue_processor",
 *   title = @Translation("Generate sales from bank/cash statements in queue"),
 *   cron = {"time" = 10}
 * )
 */
class SalesQueueProcessor extends QueueWorkerBase implements ContainerFactoryPluginInterface {

  public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryFactory $entity_query, EntityTypeManagerInterface $entityTypeManager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->entity_query = $entity_query;
    $this->entityTypeManager = $entityTypeManager;
  }

  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity.query'),
      $container->get('entity_type.manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function processItem($bid) {}
}

As an aside, the entity manager service is deprecated; the entity type manager should be used instead. You also don't strictly need to inject the entity query service as you can get an instance from the manager, e.g.:

$query = $this->entityTypeManager->getStorage('node')->getQuery();
  • Thanks! One smaller question, if I remove 'QueryFactory $entity_query' from the __construct and clear cache, i get an error: TypeError: Argument 4 passed to Drupal\my_example\Plugin\QueueWorker\SalesQueueProcessor::__construct() must implement interface Drupal\Core\Entity\EntityTypeManagerInterface, instance of Drupal\Core\Entity\Query\QueryFactory given Where is QueryFactory defined then? – betz Oct 25 '17 at 19:33
  • 1
    You also need to remove it from the create method (which is returning a new instance of the class and passing the arguments into the constructor) – Clive Oct 25 '17 at 20:12

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.