Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

captions v2 #826

Merged
merged 5 commits into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions modules/islandora_audio/islandora_audio.module
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ function islandora_audio_help($route_name, RouteMatchInterface $route_match) {
default:
}
}

/**
* Implements hook_theme().
*/
function islandora_audio_theme() {
return [
'islandora_file_audio' => [
'variables' => [
'files' => [],
'tracks' => NULL,
'attributes' => NULL,
],
],
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Drupal\islandora_audio\Plugin\Field\FieldFormatter;

use Drupal\islandora\Plugin\Field\FieldFormatter\IslandoraFileMediaFormatterBase;

/**
* Plugin implementation of the 'file_audio' formatter.
*
* @FieldFormatter(
* id = "islandora_file_audio",
* label = @Translation("Audio with Captions"),
* description = @Translation("Display the file using an HTML5 audio tag."),
* field_types = {
* "file"
* }
* )
*/
class IslandoraFileAudioFormatter extends IslandoraFileMediaFormatterBase {

/**
* {@inheritdoc}
*/
public static function getMediaType() {
return 'audio';
}

}
28 changes: 28 additions & 0 deletions modules/islandora_audio/templates/islandora-file-audio.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{#
/**
* @file
* Default theme implementation to display the file entity as an audio tag.
*
* Available variables:
* - attributes: An array of HTML attributes, intended to be added to the
* audio tag.
* - files: And array of files to be added as sources for the audio tag. Each
* element is an array with the following elements:
* - file: The full file object.
* - source_attributes: An array of HTML attributes for to be added to the
* source tag.
*
* @ingroup themeable
*/
#}
<audio {{ attributes }}>
{% for file in files %}
<source {{ file.source_attributes }} />
{% if tracks %}
{% for track in tracks %}
<track {{ track.track_attributes }}
{% endfor %}
{% endif %}
{% endfor %}
</audio>

15 changes: 15 additions & 0 deletions modules/islandora_video/islandora_video.module
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,18 @@ function islandora_video_help($route_name, RouteMatchInterface $route_match) {
default:
}
}

/**
* Implements hook_theme().
*/
function islandora_video_theme() {
return [
'islandora_file_video' => [
'variables' => [
'files' => [],
'tracks' => NULL,
'attributes' => NULL,
],
],
];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Drupal\islandora_video\Plugin\Field\FieldFormatter;

use Drupal\Core\Form\FormStateInterface;
use Drupal\islandora\Plugin\Field\FieldFormatter\IslandoraFileMediaFormatterBase;

/**
* Plugin implementation of the 'file_video' formatter.
*
* @FieldFormatter(
* id = "islandora_file_video",
* label = @Translation("Video with Captions"),
* description = @Translation("Display the file using an HTML5 video tag."),
* field_types = {
* "file"
* }
* )
*/
class IslandoraFileVideoFormatter extends IslandoraFileMediaFormatterBase {

/**
* {@inheritdoc}
*/
public static function getMediaType() {
return 'video';
}

/**
* {@inheritdoc}
*/
public static function defaultSettings() {
return [
'muted' => FALSE,
'width' => 640,
'height' => 480,
] + parent::defaultSettings();
}

/**
* {@inheritdoc}
*/
public function settingsForm(array $form, FormStateInterface $form_state) {
return parent::settingsForm($form, $form_state) + [
'muted' => [
'#title' => $this->t('Muted'),
'#type' => 'checkbox',
'#default_value' => $this->getSetting('muted'),
],
'width' => [
'#type' => 'number',
'#title' => $this->t('Width'),
'#default_value' => $this->getSetting('width'),
'#size' => 5,
'#maxlength' => 5,
'#field_suffix' => $this->t('pixels'),
'#min' => 0,
'#required' => TRUE,
],
'height' => [
'#type' => 'number',
'#title' => $this->t('Height'),
'#default_value' => $this->getSetting('height'),
'#size' => 5,
'#maxlength' => 5,
'#field_suffix' => $this->t('pixels'),
'#min' => 0,
'#required' => TRUE,
],
];
}

/**
* {@inheritdoc}
*/
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary[] = $this->t('Muted: %muted', ['%muted' => $this->getSetting('muted') ? $this->t('yes') : $this->t('no')]);
$summary[] = $this->t('Size: %width x %height pixels', [
'%width' => $this->getSetting('width'),
'%height' => $this->getSetting('height'),
]);
return $summary;
}

/**
* {@inheritdoc}
*/
protected function prepareAttributes(array $additional_attributes = []) {
return parent::prepareAttributes(['muted'])
->setAttribute('width', $this->getSetting('width'))
->setAttribute('height', $this->getSetting('height'));
}

}
27 changes: 27 additions & 0 deletions modules/islandora_video/templates/islandora-file-video.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{#
/**
* @file
* Default theme implementation to display the file entity as a video tag.
*
* Available variables:
* - attributes: An array of HTML attributes, intended to be added to the
* video tag.
* - files: And array of files to be added as sources for the video tag. Each
* element is an array with the following elements:
* - file: The full file object.
* - source_attributes: An array of HTML attributes for to be added to the
* source tag.
*
* @ingroup themeable
*/
#}
<video {{ attributes }}>
{% for file in files %}
<source {{ file.source_attributes }} />
{% endfor %}
{% if tracks %}
{% for track in tracks %}
<track {{ track.track_attributes }}
{% endfor %}
{% endif %}
</video>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Drupal\islandora\Plugin\Field\FieldFormatter;

use Drupal\file\Plugin\Field\FieldFormatter\FileMediaFormatterBase;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Template\Attribute;

/**
* Extension of FileMediaFormatterBase that enables captions.
*/
abstract class IslandoraFileMediaFormatterBase extends FileMediaFormatterBase {

/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];

$source_files = $this->getSourceFiles($items, $langcode);
$track_files = $this->getTrackFiles($items, $langcode);
if (!empty($source_files)) {
$attributes = $this->prepareAttributes();
foreach ($source_files as $delta => $files) {
$elements[$delta] = [
'#theme' => $this->getPluginId(),
'#attributes' => $attributes,
'#files' => $files,
'#tracks' => isset($track_files[$delta]) ? $track_files[$delta] : [],
'#cache' => ['tags' => []],
];

$cache_tags = [];
foreach ($files as $file) {
$cache_tags = Cache::mergeTags($cache_tags, $file['file']->getCacheTags());
}
$elements[$delta]['#cache']['tags'] = $cache_tags;
}
}

return $elements;
}

/**
* Gets the track files with attributes.
*
* @param \Drupal\Core\Field\EntityReferenceFieldItemListInterface $items
* The items.
* @param string $langcode
* The langcode.
*
* @return array
* Numerically indexed array, which again contains an associative array with
* the following key/values:
* - file => \Drupal\file\Entity\File
* - track_attributes => \Drupal\Core\Template\Attribute
*/
protected function getTrackFiles(EntityReferenceFieldItemListInterface $items, $langcode) {
$track_files = [];
$media_entity = $items->getParent()->getEntity();
$fields = $media_entity->getFields();
foreach ($fields as $key => $field) {
$definition = $field->getFieldDefinition();
if (method_exists($definition, 'get')) {
if ($definition->get('field_type') == 'media_track') {
// Extract the info for each track.
$entities = $field->referencedEntities();
$values = $field->getValue();
foreach ($entities as $delta => $file) {
$track_attributes = new Attribute();
$track_attributes
->setAttribute('src', $file->createFileUrl())
->setAttribute('srclang', $values[$delta]['srclang'])
->setAttribute('label', $values[$delta]['label'])
->setAttribute('kind', $values[$delta]['kind']);
if ($values[$delta]['default']) {
$track_attributes->setAttribute('default', 'default');
}
$track_files[0][] = [
'file' => $file,
'track_attributes' => $track_attributes,
];
}
}
}
}
return $track_files;
}

}
Loading