elevenlabs_field-1.0.0-beta7/src/Plugin/Field/FieldFormatter/ElevenLabsFormatterBase.php

src/Plugin/Field/FieldFormatter/ElevenLabsFormatterBase.php
<?php

namespace Drupal\elevenlabs_field\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\elevenlabs_field\ElevenLabsApiService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Base for all the formatters.
 */
class ElevenLabsFormatterBase extends FormatterBase implements ContainerFactoryPluginInterface {

  /**
   * The ElevenLabs API object.
   */
  protected ElevenLabsApiService $elevenLabs;

  /**
   * Constructs a FormatterBase object.
   *
   * @param string $plugin_id
   *   The plugin_id for the formatter.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
   *   The definition of the field to which the formatter is associated.
   * @param array $settings
   *   The formatter settings.
   * @param string $label
   *   The formatter label display setting.
   * @param string $view_mode
   *   The view mode.
   * @param array $third_party_settings
   *   Any third party settings.
   * @param \Drupal\elevenlabs_field\ElevenLabsApiService $elevenLabs
   *   The ElevenLabs API.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ElevenLabsApiService $elevenLabs) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
    $this->elevenLabs = $elevenLabs;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $plugin_id,
      $plugin_definition,
      $configuration['field_definition'],
      $configuration['settings'],
      $configuration['label'],
      $configuration['view_mode'],
      $configuration['third_party_settings'],
      $container->get('elevenlabs_field.api_service'),
    );
  }

  /**
   * {@inheritDoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    return [];
  }

  /**
   * Gets the speakers.
   *
   * @return array
   *   The speakers array.
   */
  public function getSpeakers() {
    // Keep in memory, only load API call once.
    static $speakers;
    if (empty($speakers)) {
      $voices = $this->elevenLabs->getVoices();
      $speakers = [];
      foreach ($voices['voices'] as $voice) {
        $speakers[$voice['voice_id']] = $voice['name'];
      }
    }
    return $speakers;
  }

}

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

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