deepseek-1.x-dev/module/ai_auto_embedding/src/Hook/AiEmbeddingHooks.php
module/ai_auto_embedding/src/Hook/AiEmbeddingHooks.php
<?php
namespace Drupal\ai_auto_embedding\Hook;
use Drupal\ai_auto_embedding\Event\EntityUpdateEvent;
use Drupal\Component\Utility\Html;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Hook\Attribute\Hook;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\filter\FilterPluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
/**
* {@inheritdoc}
*/
class AiEmbeddingHooks {
/**
* Constructs a new auto embedding AI Hook instance.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory service.
* @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $eventDispatcher
* The event dispatcher service.
* @param \Drupal\filter\FilterPluginManager|null $filterManager
* The filter plugin manager service.
*/
public function __construct(
protected ModuleHandlerInterface $moduleHandler,
protected ConfigFactoryInterface $configFactory,
protected EventDispatcherInterface $eventDispatcher,
protected ?FilterPluginManager $filterManager = NULL,
) {
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler'),
$container->get('config.factory'),
$container->get('event_dispatcher'),
$container->get('plugin.manager.filter'),
);
}
/**
* Implements hook_help().
*/
#[Hook('help')]
public function help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.ai_auto_embedding':
$text = file_get_contents(__DIR__ . '/../../Readme.md');
if (!$this->moduleHandler->moduleExists('markdown')) {
return '<pre>' . Html::escape($text) . '</pre>';
}
else {
// Use the Markdown filter to render the README.
$settings = $this->configFactory->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $this->filterManager?->createInstance('markdown', $config);
return $filter->process($text, 'en');
}
default:
break;
}
return FALSE;
}
/**
* Implements hook_entity_update().
*/
#[Hook('entity_update')]
public function entityUpdate(EntityInterface $entity): void {
$this->eventDispatcher->dispatch(new EntityUpdateEvent($entity), EntityUpdateEvent::EVENT_NAME);
}
/**
* Implements hook_entity_insert().
*/
#[Hook('entity_insert')]
public function entityInsert(EntityInterface $entity): void {
$this->eventDispatcher->dispatch(new EntityUpdateEvent($entity), EntityUpdateEvent::EVENT_NAME);
}
}
