{"id":4396,"date":"2016-08-13T13:42:14","date_gmt":"2016-08-13T18:42:14","guid":{"rendered":"http:\/\/www.carnaghan.com\/?p=4396"},"modified":"2019-09-23T21:17:03","modified_gmt":"2019-09-24T01:17:03","slug":"cron-queuing-in-drupal-8","status":"publish","type":"post","link":"https:\/\/www.carnaghan.com\/cron-queuing-in-drupal-8\/","title":{"rendered":"Cron Queuing in Drupal 8"},"content":{"rendered":"\n

Day 7<\/a> focuses mainly on the Drupal Queue API<\/a>. The main resource for this card is an article<\/span> called Drupal 8 Queue API \u2013 Powerful Manual and Cron Queueing<\/a>, which I had referenced in an earlier post<\/a>. Before completing any of the materials, I had no previous experience with the Queue API<\/span>, nor hook_entity_insert<\/a>, which was used to create a demo queue<\/span> system in the referenced article<\/span>. After going through the article<\/span> I gained a better understanding of how the queue<\/span> system works. At a high level<\/span> the following steps outline the process of constructing the module<\/span> that is documented in this article<\/span> along with relevant links to the API<\/span>.<\/p>\n\n\n\n

Creating the Queue<\/h2>\n\n\n\n
  1. Create the .module file and use hook_entity_insert to grab any newly inserted entities of type node, check publish status, and if unpublished create a queue via the QueueFactory (step).<\/li>
  2. Get the queue factory service via \\Drupal::service(‘queue’)<\/a> which references the QueueFactory<\/a> class and then use the get method<\/a> to create a new queue of type QueueInterface.<\/li>
  3. Instantiate a new class stdclass()<\/a> called $item and pass in the nid from the inserted entity.<\/li>
  4. Use the QueueInterface createItem<\/a> method to pass in $item.<\/li><\/ol>\n\n\n\n

    Building the QueueWorker Plugin<\/span><\/h2>\n\n\n\n

    We will be getting more into plugins<\/span> in Day 8<\/a>, however for the purpose of using the Queue<\/span> API<\/span> in this example, a plugin<\/span> was created to manage publishing<\/span> of and processing items in the queue<\/span>.<\/p>\n\n\n\n

    1. Create the plugin at src\/Plugin\/QueueWorker which extends QueueWorkerBase and implements the ContainerFactoryPluginInterface.<\/li>
    2. Inject EntityStorageInterface<\/a> into the plugin (required for publishing nodes).<\/li>
    3. Write the methods for processing queue items and publishing nodes.<\/li><\/ol>\n\n\n\n

      The Drupal<\/span> 8 Queue<\/span> API<\/span> article<\/span> setup the plugin<\/span> as an abstract class<\/span> and extended it to provide both cron<\/span> as well as manual calls to the plugin<\/span>. An additional form was buit to provide user<\/span> feedback on queue<\/span> items left and for a way for the user<\/span> to manually process the queue<\/span>. The article<\/span> also provides a git repo of all sourcecode<\/span> to download and test locally.<\/p>\n\n\n\n

      Registered Users Welcome Email<\/span> Queue<\/h2>\n\n\n\n

      The task for Day 7<\/a> was to build a custom module<\/span> that would similarly use a queue<\/span> within Drupal<\/span> 8, only this time instead of queueing inserted nodes, the module<\/span> would build a queue<\/span> of new users<\/span> and then email<\/span> a welcome message on the next cron<\/span> run. In order to write this module<\/span>, I followed similar steps to the article<\/span> above. First of all I found myself reviewing Drupal 8 Hooks<\/a> in order to find a registered user hook that I could use for my queue<\/span>. I quickly realized that no such hook existed and I could in fact use the same hook_entity<\/span>_insert on users<\/span> instead of nodes.<\/p>\n\n\n\n

      The next thing I needed to figure out was which service I could use for processing emails<\/span>. I took a look in core.services.yml<\/a> and came across MailManager<\/a> which provides a Mail plugin<\/span> manager. More detailed documentation for the MailManager function<\/a> is available and this helped figure out how to use it correctly in my code including what parameters needed to be passed in:<\/p>\n\n\n\n