soundcite-1.1.2/soundcite.module

soundcite.module
<?php

/**
 * @file
 * Soundcite module.
 */


/**
 * Implements hook_preprocess_field().
 */
function soundcite_preprocess_field(&$variables) {
  $field_type = $variables['element']['#field_type'];

  // Check for text fields with soundcite content OR file fields using soundcite formatter
  $load_soundcite = FALSE;

  // Check text fields for soundcite content
  if (in_array($field_type, ['text_long', 'text_with_summary']) &&
      isset($variables['element'][0]['#text']) &&
      strpos($variables['element'][0]['#text'], 'soundcite') !== FALSE) {
    $load_soundcite = TRUE;
  }

  // Check file fields using soundcite formatter
  if ($field_type === 'file' &&
      isset($variables['element']['#formatter']) &&
      $variables['element']['#formatter'] === 'soundcite_audio') {
    $load_soundcite = TRUE;
  }

  if ($load_soundcite) {
    $variables['#attached']['library'][] = 'soundcite/soundcite-behavior';

    $config = \Drupal::config('soundcite.settings');
    $soundcloud_client_id = $config->get('soundcloud_client_id');
    $background_color = $config->get('background_color');

    if ($soundcloud_client_id || $background_color) {
      $js = 'var SOUNDCITE_CONFIG = {';
      if ($soundcloud_client_id) {
        $js .= "soundcloud_client_id: '$soundcloud_client_id'";
      }
      if ($soundcloud_client_id && $background_color) {
        $js .= ',';
      }
      if ($background_color) {
        $js .= "background_color: '$background_color'";
      }
      $js .= ' }';

      $variables['#attached']['html_head'][] = [
        [
          '#tag' => 'script',
          '#value' => $js,
        ],
        'soundcite_settings',
      ];

      $css = '.soundcite{direction: ltr;}';
      $variables['#attached']['html_head'][] = [
        [
          '#tag' => 'style',
          '#value' => $css,
        ],
        'soundcite_css',
      ];
    }
  }
}

/**
 * Implements hook_theme().
 */
function soundcite_theme($existing, $type, $theme, $path) {
  return [
    'soundcite_audio_formatter' => [
      'variables' => [
        'file' => NULL,
        'start_time' => '',
        'end_time' => '',
        'plays' => 1,
        'link_text' => '',
        'use_description_as_link_text' => FALSE,
        'attributes' => [],
      ],
      'template' => 'soundcite-audio-formatter',
    ],
  ];
}

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

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