google_text_to_speech-8.x-1.3-alpha4/google_text_to_speech.module
google_text_to_speech.module
<?php
/**
* @file
* Contains \Drupal\Routing\Form\RouteMatchInterface.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use \Drupal\media\MediaInterface;
/**
* Implements hook_help().
*/
function google_text_to_speech_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.google_text_to_speech':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Google Text to Speech module uses Google Cloud Text-to-Speech library which allows you to convert words and sentences audio data of natural human speech. You can then convert the audio data into a playable audio file like an MP3. The Cloud Text-to-Speech API accepts input as raw text or Speech Synthesis Markup Language (SSML).') . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Configuring Google Text to Speech') . '</dt>';
$output .= '<dd>' . t('The Google Text to Speech module provides page for configuring the necessary <a href=":config">Google Text to Speech settings</a>. Configure json & other settings. Test it by downloading sample text audio', [':config' => Url::fromRoute('google_text_to_speech.admin')]) . '</dd>';
$output .= '</dl>';
return $output;
case 'google_text_to_speech.admin':
return '<p>' . t('This page shows you all available administration tasks for Google text to speech module.') . '</p>';
}
}
/**
* Implements hook_entity_type_presave().
*/
function google_text_to_speech_media_presave(MediaInterface $media) {
if (valid_google_text_to_speech_media($media)) {
$gtts = \Drupal::service('google_text_to_speech.manager');
if(!empty($gtts->entityHasChanged($media)) || $media->isNew()) {
$parameters = $gtts->getParametersFromMedia($media);
foreach ($parameters as $pkey => $pvalue) {
$file = $gtts->generateFile($pvalue);
$files[$pkey] = [
'target_id' => $file->id(),
'display' => 1,
];
}
$media->set('field_media_audio_file', $files);
}
}
}
function valid_google_text_to_speech_media($media) {
$config = \Drupal::config('google_text_to_speech.settings');
$string = $config->get('google_text_to_speech_media_type');
$medias = explode(',', $string);
return in_array($media->bundle(), $medias) || $media->bundle() == 'google_text_to_speech';
}
