deepseek-1.x-dev/src/Plugin/Action/AiNodeEmbedding.php

src/Plugin/Action/AiNodeEmbedding.php
<?php

namespace Drupal\deepseek\Plugin\Action;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Action\Attribute\Action;
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Config\Config;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\RendererInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\deepseek\EmbeddingHandlerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides an AI Embedding action.
 *
 * @DCG
 * For updating entity fields consider extending FieldUpdateActionBase.
 * @see \Drupal\Core\Field\FieldUpdateActionBase
 *
 * @DCG
 * In order to set up the action through admin interface the plugin has to be
 * configurable.
 * @see https://www.drupal.org/project/drupal/issues/2815301
 * @see https://www.drupal.org/project/drupal/issues/2815297
 *
 * @DCG
 * The whole action API is subject of change.
 * @see https://www.drupal.org/project/drupal/issues/2011038
 */
#[Action(
  id: 'ai_node_embedding',
  label: new TranslatableMarkup('AI Embedding'),
  category: new TranslatableMarkup('AI'),
  type: 'node',
)]
class AiNodeEmbedding extends ConfigurableActionBase implements ContainerFactoryPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    protected RendererInterface $renderer,
    protected EntityTypeManagerInterface $entityTypeManager,
    protected EntityDisplayRepositoryInterface $entityDisplayRepository,
    protected EmbeddingHandlerInterface $embeddingHandler,
    protected Config $aiSettings,
  ) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
    return new self(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('renderer'),
      $container->get('entity_type.manager'),
      $container->get('entity_display.repository'),
      $container->get('deepseek.embedding'),
      $container->get('config.factory')->get('deepseek.settings'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration(): array {
    return [
      'view_mode' => 'full',
      'skip_indexed' => FALSE,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state): array {
    $plugin_definition = $this->getPluginDefinition();
    $entity_type = $plugin_definition['type'] ?? 'node';
    $form['view_mode'] = [
      '#type' => 'select',
      '#title' => $this->t('Select the views mode, it will render the content to be embedding'),
      '#default_value' => $this->configuration['view_mode'],
      '#options' => $this->getViewModeList($entity_type),
    ];
    $form['skip_indexed'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Skip indexed elements'),
      '#default_value' => $this->configuration['skip_indexed'],
    ];
    $isEmbedding = $this->aiSettings->get('embedding');
    if (empty($isEmbedding)) {
      $form["actions"]['submit']['#access'] = FALSE;
      $this->messenger()->addError($this->t('Embedding is inactive.'));
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void {
    $this->configuration['view_mode'] = $form_state->getValue('view_mode');
    $this->configuration['skip_indexed'] = $form_state->getValue('skip_indexed');
  }

  /**
   * {@inheritdoc}
   */
  public function execute(?ContentEntityInterface $entity = NULL): void {
    $view_mode = $this->configuration['view_mode'];
    $view_builder = $this->entityTypeManager->getViewBuilder($entity->getEntityTypeId());
    $render_array = $view_builder->view($entity, $view_mode);
    $content = html_entity_decode($this->renderer->renderRoot($render_array));
    $entity_type = $entity->getEntityTypeId();
    $entity_id = $entity->id();
    $result = $this->embeddingHandler->saveEmbedding($entity_type, $entity_id, trim($content), $this->configuration['skip_indexed']);

    if (!$result) {
      $this->messenger()->addError($this->t('Failed to save embedding for @type @id', [
        '@type' => $entity_type,
        '@id' => $entity_id,
      ]));
    }

  }

  /**
   * {@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'));
  }

  /**
   * {@inheritDoc}
   */
  public function access($object, ?AccountInterface $account = NULL, $return_as_object = FALSE) {
    $result = AccessResult::allowed();
    return $return_as_object ? $result : $result->isAllowed();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc