elevenlabs_field-1.0.0-beta7/src/Batch/BatchGenerate.php

src/Batch/BatchGenerate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
 
namespace Drupal\elevenlabs_field\Batch;
 
use Drupal\Core\Entity\ContentEntityInterface;
 
/**
 * Batch Job.
 */
class BatchGenerate {
 
  /**
   * {@inheritdoc}
   */
  public static function setBatchJob(ContentEntityInterface $entity, $field) {
    $operations = [];
    foreach ($entity->{$field} as $key => $value) {
 
      $operations[] = [
        '\Drupal\elevenlabs_field\Batch\BatchGenerate::generateItem', [
          [
            'entity' => $entity,
            'item' => $value,
            'field' => $field,
            'key' => $key,
          ],
        ],
      ];
    }
    $batch = [
      'title' => 'Creating Audio',
      'init_message' => 'Generating Audios',
      'operations' => $operations,
      'finished' => '\Drupal\elevenlabs_field\Batch\BatchGenerate::finishedBatching',
    ];
    batch_set($batch);
  }
 
  /**
   * Batch function.
   */
  public static function generateItem($item, &$context) {
    $eItem = $item['item'];
    // If already set.
    if ($eItem->target_id || !$eItem->text) {
      return;
    }
    $generator = \Drupal::service('elevenlabs_field.generator_service');
    $model = $generator->validateModel($eItem->model_id, $eItem->getEntity());
    $data = $generator->generateFile($eItem->text, $eItem->speaker, $eItem->getFieldDefinition(), $model, [
      'stability' => ($eItem->stability / 100),
      'similarity_boost' => ($eItem->similarity_boost / 100),
      'style' => ($eItem->style_exaggeration / 100),
      'use_speaker_boost' => $eItem->speaker_boost ? TRUE : FALSE,
    ]);
    if (!empty($data['file'])) {
      // Remove old data if exists.
      if ($eItem->target_id) {
        $generator->removeFile($eItem->target_id);
      }
      $eItem->entity = $data['file'];
      $eItem->set('target_id', $data['file']->id());
      $eItem->set('history_item_id', $data['history_item_id']);
    }
    // Resave them.
    if (!isset($context['results']['entity'])) {
      $context['results']['entity'] = $item['entity'];
    }
    $context['results']['entity']->{$item['field']}[$item['key']] = $eItem;
 
  }
 
  /**
   * Finished function.
   */
  public static function finishedBatching($success, $results, $operations) {
    if (isset($results['entity'])) {
      $results['entity']->save();
    }
  }
 
}

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

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