In short:
Utilizing Drupal 8, I'm hoping to to "hook" into an entity update event for a specific content type so that I can then take further action when that event occurs. In this case, delete the node if it's unpublished.
In long:
The Scheduler module allows scheduling of nodes to publish and unpublish as scheduled. My goal is to then delete a specific-type node when it gets unpublished.
At first I tried to use the Rules module to trigger removal of the unpublished node. However, Rules is not working properly on my website currently (getting errors when attempting to configure unpublish event node type). It spurs interest to see how I can accomplish the same task with a small custom module. I'm moving forward with a goal to understand Drupal 8 modules and module development better.
As a result, I wish to move handling of unpublish events for my content type into module code. Other examples I'm finding relate to Drupal 7. I'm building a Drupal 8 module.
Here is my starting module code so far (rough, draft, untested as I work out logistics).
Module structure:
hour_helper
hour_helper.info.yml
hour_helper.services.yml
src
EventSubscriber
HourUnpublishEventSubscriber.php
hour_helper.info.yml:
name: Hours of Operation Helper
type: module
description: 'Utilities to assist with hours of operation, starting with the removal of hour content once it is unpublished via the scheduler module.'
package: Custom
core: 8.x
hour_helper.services.yml:
services:
hour_helper.node_subscriber:
class: Drupal\hour_helper\EventSubscriber
arguments: ['@entity.manager']
tags:
- { name: event_subscriber }
HourUnpublishEventSubscriber.php:
<?php
/**
* @file
* Event subscriber
*/
namespace Drupal\hour_helper\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
//!!! use (... something entity or node related, we want to subscribe to entity updates?)
/**
* Remove hour of opperation nodes after/when they are scheduled to unsubscribe to prevent garbage accumulation
*/
class HourUnpublishSubscriber implements EventSubscriberInterface {
/**
* {@inheritdoc}
*/
public function getSubscribedEvents() {
// Subscribe to node update events with priority 0.
//!!! which event....
$events[SomethingEvents::SOMETHING][] = array('checkForRemoveStatus');
return $events;
}
/**
* This method is called whenever the ????? event is dispatched.
*
* @param GetResponseEvent $event
*/
public function checkForRemoveStatus(GetResponseEvent $event) {
//print '<pre>';
//print_r($event);
//print '</pre>';
die('Well, at least an event has been triggered to be handled here...');
}
}
I continue to ponder the Drupal events doc page...