deepseek-1.x-dev/module/ai_auto_embedding/src/Form/AiAutoEmbeddingConfiguration.php
module/ai_auto_embedding/src/Form/AiAutoEmbeddingConfiguration.php
<?php
namespace Drupal\ai_auto_embedding\Form;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Configure Ai auto embedding settings for this site.
*/
final class AiAutoEmbeddingConfiguration extends ConfigFormBase {
/**
* Name of the config.
*
* @var string
*/
public static $configName = 'ai_auto_embedding.settings';
/**
* Constructs a AiAutoEmbeddingConfiguration object.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The config factory service.
* @param \Drupal\Core\Config\TypedConfigManagerInterface $typedConfigManager
* The type config service.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
* Then entity field manager service.
* @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entityDisplayRepository
* The entity display service.
* @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entityTypeBundleInfo
* The entity type bundle info service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager info service.
*/
public function __construct(
ConfigFactoryInterface $config_factory,
TypedConfigManagerInterface $typedConfigManager,
protected EntityFieldManagerInterface $entityFieldManager,
protected EntityDisplayRepositoryInterface $entityDisplayRepository,
protected EntityTypeBundleInfoInterface $entityTypeBundleInfo,
protected EntityTypeManagerInterface $entityTypeManager,
) {
parent::__construct($config_factory, $typedConfigManager);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('config.typed'),
$container->get('entity_field.manager'),
$container->get('entity_display.repository'),
$container->get('entity_type.bundle.info'),
$container->get('entity_type.manager'),
);
}
/**
* {@inheritdoc}
*/
public function getFormId(): string {
return 'ai_auto_embedding_settings';
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames(): array {
return [self::$configName];
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state): array {
$configAI = $this->configFactory->get('deepseek.settings');
if (empty($configAI->get('db_vector'))) {
return [
'#markup' => $this->t(
'Active @embedding', [
'@embedding' => Link::fromTextAndUrl($this->t('Database for embedding'), Url::fromRoute('deepseek.settings'))->toString(),
]
),
];
}
$config = $this->config(self::$configName);
$list_entity = $this->entityTypeManager->getDefinitions();
foreach ($list_entity as $entity_type => $definition) {
$bundles = $this->entityTypeBundleInfo->getBundleInfo($entity_type);
if (!$definition->hasLinkTemplate('canonical') || !$definition->isCommonReferenceTarget()) {
continue;
}
$key = "entity_group_$entity_type";
$form[$key] = [
'#type' => 'details',
'#title' => $this->t("Auto embedding @entity_type", ['@entity_type' => $definition->getLabel()]),
];
foreach ($bundles as $bundle => $info) {
$checkbox_name = "entity-$bundle-$entity_type";
$select_name = "view_mode-$bundle-$entity_type";
$form[$key][$checkbox_name] = [
'#type' => 'checkbox',
'#title' => $this->t("Bundle @bundle of Entity @entity_type",
['@bundle' => $bundle, '@entity_type' => $definition->getLabel()]
),
'#default_value' => $config->get($checkbox_name),
];
$form[$key][$select_name] = [
'#type' => 'select',
'#title' => $this->t("View mode @bundle of @entity_type",
['@bundle' => $bundle, '@entity_type' => $entity_type]
),
'#options' => $this->getViewModeList($entity_type),
'#empty_option' => $this->t('- Select -'),
'#default_value' => $config->get($select_name),
'#states' => [
'visible' => [
":input[name='$checkbox_name']" => ['checked' => TRUE],
],
],
];
}
}
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state): void {
$form_value = $form_state->getValues();
foreach ($form_value as $key => $value) {
if (str_starts_with($key, 'entity-') || str_starts_with($key, 'view_mode-')) {
$this->config(self::$configName)->set($key, $value)->save();
}
}
}
/**
* {@inheritdoc}
*/
public function getViewModeList($entity_type) {
$view_modes = $this->entityDisplayRepository->getViewModes($entity_type);
return array_combine(array_keys($view_modes), array_column($view_modes, 'label'));
}
}
