View source
<?php
namespace Drupal\opigno_h5p\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\h5p\H5PDrupal\H5PDrupal;
use Drupal\h5p\Entity\H5PContent;
class H5PDefaultFormatter extends FormatterBase {
public function settingsSummary() {
$summary = [];
$summary[] = $this
->t('Displays interactive H5P content.');
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$moduleHandler = \Drupal::service('module_handler');
foreach ($items as $delta => $item) {
$value = $item
->getValue();
$h5p_content = H5PContent::load($value['h5p_content_id']);
if (empty($h5p_content)) {
continue;
}
$h5p_integration = H5PDrupal::getGenericH5PIntegrationSettings();
$content_id_string = 'cid-' . $h5p_content
->id();
$h5p_integration['contents'][$content_id_string] = $h5p_content
->getH5PIntegrationSettings($item
->getEntity()
->access('update'));
$core = H5PDrupal::getInstance('core');
$preloaded_dependencies = $core
->loadContentDependencies($h5p_content
->id(), 'preloaded');
$files = $core
->getDependenciesFiles($preloaded_dependencies, H5PDrupal::getRelativeH5PPath());
$loadpackages = [
'h5p/h5p.content',
];
foreach ($preloaded_dependencies as $dependency) {
$loadpackages[] = 'h5p/' . _h5p_library_machine_to_id($dependency);
}
$moduleHandler
->alter('h5p_scripts', $files['scripts'], $loadpackages, $h5p_content
->getLibrary()->embed_types);
$moduleHandler
->alter('h5p_styles', $files['styles'], $loadpackages, $h5p_content
->getLibrary()->embed_types);
$html = '<div class="h5p-content" data-content-id="' . $h5p_content
->id() . '"></div>';
$current_route = \Drupal::routeMatch()
->getRouteName();
if (in_array($current_route, [
'opigno_module.group.answer_form',
'opigno_module.manager.get_activity_preview',
])) {
$h5p_integration['contents'][$content_id_string]['contentUserData'] = [
0 => [
'state' => '{}',
],
];
}
$element[$delta] = [
'#type' => 'markup',
'#markup' => $html,
'#allowed_tags' => [
'div',
'iframe',
],
'#attached' => [
'drupalSettings' => [
'h5p' => [
'H5PIntegration' => $h5p_integration,
],
],
'library' => $loadpackages,
],
'#cache' => [
'tags' => [
'h5p_content:' . $h5p_content
->id(),
'h5p_content',
],
],
];
}
return $element;
}
}