gotem_content_moderation-1.1.6-alpha1/gotem.module
gotem.module
<?php
/**
* @file
* Contains the help page and theme implementations for the Gotem module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\gotem\Event\ContentModerationEvent;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Implements hook_help().
*/
function gotem_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.gotem':
$build = [
'#theme' => 'gotem_help',
'#attached' => [
'library' => [
'gotem/bootstrap',
],
],
];
return \Drupal::service('renderer')->render($build);
default:
return t('Provides content moderation using OpenAI to check text fields and media library images for policy compliance. <a href=":link">Learn more</a>.', [
':link' => Url::fromRoute('help.page.gotem')->toString(),
]);
}
}
/**
* Implements hook_theme().
*/
/**
* Implements hook_theme().
*/
function gotem_theme($existing, $type, $theme, $path) {
return [
'gotem_help' => [
'variables' => [],
'template' => 'gotem-help',
'path' => $path . '/templates',
],
'flagged_items_page' => [
'variables' => [
'flagged_items' => [],
],
'template' => 'flagged-items-page',
'path' => $path . '/templates',
],
];
}
/**
* Implements hook_entity_presave().
*/
function gotem_entity_presave(EntityInterface $entity) {
if ($entity instanceof \Drupal\node\NodeInterface) {
// Use the logger service to log the node presave event.
$logger = \Drupal::logger('gotem_content_moderation');
$logger->notice('Node presave triggered for node ID: @nid', ['@nid' => $entity->id()]);
// Check the state to see if the node has already been flagged.
$state = \Drupal::state();
$flagged_node_id = $state->get('gotem_flagged_node_id');
// If the node has already been flagged, avoid re-triggering moderation.
if ($flagged_node_id === $entity->id()) {
$logger->info('Node ID @nid has already been flagged, skipping moderation.', ['@nid' => $entity->id()]);
$entity->set('status', FALSE);
return;
}
// Dispatch the custom event.
$event = new ContentModerationEvent($entity);
// Get the event dispatcher service.
/** @var EventDispatcherInterface $dispatcher */
$dispatcher = \Drupal::service('event_dispatcher');
$dispatcher->dispatch($event, ContentModerationEvent::EVENT_NAME);
}
}
/**
* Implements hook_uninstall().
*/
function gotem_uninstall() {
// Retrieve the config factory service once to avoid repetition.
$config_factory = \Drupal::configFactory();
// Delete the primary module settings.
$config_factory->getEditable('gotem.settings')->delete();
// List of configuration keys to delete.
$config_keys = [
'core.entity_view_display.node.moderation_flag.default',
'core.entity_view_display.node.moderation_flag.teaser',
'field.field.node.moderation_flag.body',
'core.entity_type.node.moderation_flag',
'field.field.node.moderation_flag.field_json_result',
'field.field.node.moderation_flag.field_user',
'field.storage.node.field_json_result',
'field.storage.node.field_user',
'language.content_settings.node.moderation_flag',
'core.entity_form_display.node.moderation_flag.default',
'node.type.moderation_flag',
];
// Loop through and delete each config.
foreach ($config_keys as $config_key) {
$config_factory->getEditable($config_key)->delete();
}
}
