deepseek-1.x-dev/src/Plugin/Block/AiChatbotBlock.php

src/Plugin/Block/AiChatbotBlock.php
<?php

namespace Drupal\deepseek\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleExtensionList;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Theme\ThemeManagerInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides a chatbot block.
 */
#[Block(
  id: 'ai_chatbot_block',
  admin_label: new TranslatableMarkup('Ai Chatbot'),
  category: new TranslatableMarkup('block'),
)]
class AiChatbotBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * Constructs the plugin instance.
   */
  public function __construct(
    array $configuration,
    $plugin_id,
    $plugin_definition,
    protected ConfigFactoryInterface $config,
    protected AccountInterface $currentUser,
    protected EntityTypeManagerInterface $entityTypeManager,
    protected FileUrlGeneratorInterface $fileUrlGenerator,
    protected ThemeManagerInterface $themeManager,
    protected ?ModuleExtensionList $moduleExtensionList = NULL,
  ) {
    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('config.factory'),
      $container->get('current_user'),
      $container->get('entity_type.manager'),
      $container->get('file_url_generator'),
      $container->get('theme.manager'),
      $container->get('extension.list.module'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration(): array {
    return [
      'btn_float' => TRUE,
      'voice' => NULL,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state): array {
    $voice = $this->configuration['voice'] ?? $this->config->get('deepseek.settings')->get('voice');
    $form['btn_float'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Float button'),
      '#default_value' => $this->configuration['btn_float'],
    ];
    $form['voice'] = [
      '#type' => 'select',
      '#title' => $this->t('Language for voice chat'),
      '#default_value' => $voice,
      '#options' => $this->languages(),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state): void {
    $this->configuration['btn_float'] = $form_state->getValue('btn_float');
    $this->configuration['voice'] = $form_state->getValue('voice');
  }

  /**
   * {@inheritdoc}
   */
  public function build(): array {
    $url_avatar = '';

    $logo_path = $this->themeManager->getActiveTheme()->getLogo();
    $logo_url = $this->fileUrlGenerator->generateAbsoluteString($logo_path);

    $user = $this->entityTypeManager->getStorage('user')->load($this->currentUser->id());
    if ($user->hasField('user_picture') && $user->get('user_picture')->entity) {
      $avatar = $user->get('user_picture')->entity->getFileUri();
      $url_avatar = $this->fileUrlGenerator->generateAbsoluteString($avatar);
    }

    $chatBox = [
      '#theme' => 'ai_chatbot_block',
      '#label' => $this->configuration['label'],
      '#btn_float' => $this->configuration['btn_float'],
      '#attached' => [
        'library' => [
          'deepseek/chat-box',
        ],
        'drupalSettings' => [
          'deepseek' => [
            'label' => $this->configuration['label'],
            'avatar' => $url_avatar,
            'logo_site' => $logo_url,
            'url_chat' => Url::fromRoute('deepseek.chat')->toString(),
            'voice' => $this->configuration['voice'],
            'url_module' => Url::fromUri('base:' . $this->moduleExtensionList->getPath('deepseek'), ['absolute' => TRUE])->toString(),
            'url_agent' => Url::fromRoute('deepseek.ai_connect')->toString(),
            'url_upload' => Url::fromRoute('deepseek.file_upload')->toString(),
            'provider' => $this->config->get('deepseek.settings')->get('provider'),
          ],
        ],
      ],
    ];
    $loadBootstrap = $this->config->get('deepseek.settings')->get('bootstrap');
    if ($loadBootstrap) {
      $chatBox['#attached']['library'][] = 'deepseek/bootstrap';
    }
    return $chatBox;
  }

  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account): AccessResult {
    // @todo Evaluate the access condition here.
    return AccessResult::allowedIf(TRUE);
  }

  /**
   * {@inheritdoc}
   */
  private function languages() {
    return [
      'en-US' => 'English',
      'fr-FR' => 'Français',
      'it-IT' => 'Italiano',
      'es-ES' => 'Español',
      'ca-ES' => 'Català',
      'gl-ES' => 'Galego',
      'pt-PT' => 'Português',
      'pt-BR' => 'Brasil',
      'vi-VN' => 'Tiếng Việt',
      'af-ZA' => 'Afrikaans',
      'bs-BA' => 'Bosanski',
      'id-ID' => 'Bahasa',
      'jv-ID' => 'Basa Java',
      'cy-GB' => 'Cymraeg',
      'da-DK' => 'Dansk',
      'de-DE' => 'Deutsch',
      'et-EE' => 'Eesti',
      'eu-ES' => 'Euskera',
      'fa-IR' => 'Farsi',
      'fil-PH' => 'Filipino',
      'ga-IE' => 'Gaeilge',
      'hr-HR' => 'Hrvatski',
      'sw-KE' => 'Kiswahili',
      'kk-KZ' => 'Қазақ',
      'lt-LT' => 'Lietuvių',
      'lv-LV' => 'Latviešu',
      'mt-MT' => 'Malti',
      'hu-HU' => 'Magyar',
      'nl-NL' => 'Nederlands',
      'uz-UZ' => 'Oʻzbekcha',
      'pl-PL' => 'Polski',
      'sq-AL' => 'Shqip',
      'sv-SE' => 'Svenska',
      'fi-FI' => 'Suomi',
      'cs-CZ' => 'Čeština',
      'is-IS' => 'Íslenska',
      'ro-RO' => 'Română',
      'tr-TR' => 'Türkçe',
      'sk-SK' => 'Slovenčina',
      'el-GR' => 'Ελληνικά',
      'az-AZ' => 'Azərbaycanca',
      'bg-BG' => 'Български',
      'sr-RS' => 'Српски',
      'mk-MK' => 'Македонски',
      'mn-MN' => 'монгол',
      'ru-RU' => 'Русский',
      'uk-UA' => 'Українська',
      'ja-JP' => '日本語',
      'ko-KR' => '한국어',
      'th-TH' => 'ไทย',
      'km-KH' => 'ភាសាខ្មែរ',
      'lo-LA' => 'ພາສາລາວ',
      'ar-SA' => 'العربية',
      'ms-MY' => 'بهاس ملايو',
      'ps-AF' => 'پښتو',
      'am-ET' => 'አማርኛ',
      'bn-IN' => 'বাংলা',
      'he-IL' => 'עברית',
      'hy-AM' => 'Հայերեն',
      'ka-GE' => 'ქართული ენა',
      'my-MM' => 'ဗမာစကား',
      'si-LK' => 'සිංහල',
      'gu-IN' => 'ગુજરાતી',
      'hi-IN' => 'हिन्दी',
      'kn-IN' => 'ಕನ್ನಡ',
      'ml-IN' => 'മലയാളം',
      'mr-IN' => 'मराठी',
      'or-IN' => 'ଓଡିଆ',
      'pa-IN' => 'ਪੰਜਾਬੀ',
      'ta-IN' => 'தமிழ்',
      'te-IN' => 'తెలుగు',
      'ur-IN' => 'اردو',
      'ne-NP' => 'नेपाली',
      'zh-CN' => '中文',
      'zh-HK' => '中文 (香港)',
      'zh-TW' => '繁體中文',
    ];
  }

}

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

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