forked from discoverygarden/islandora
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
697 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
modules/islandora_audio/src/Plugin/Field/FieldFormatter/IslandoraFileAudioFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
modules/islandora_audio/templates/islandora-file-audio.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
modules/islandora_video/src/Plugin/Field/FieldFormatter/IslandoraFileVideoFormatter.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
27
modules/islandora_video/templates/islandora-file-video.html.twig
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
92 changes: 92 additions & 0 deletions
92
src/Plugin/Field/FieldFormatter/IslandoraFileMediaFormatterBase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
Oops, something went wrong.