public function H5PDefaultFormatter::viewElements in Opigno module 3.x
Same name and namespace in other branches
- 8 ActivityTypes/opigno_h5p/src/Plugin/Field/FieldFormatter/H5PDefaultFormatter.php \Drupal\opigno_h5p\Plugin\Field\FieldFormatter\H5PDefaultFormatter::viewElements()
Builds a renderable array for a field value.
Parameters
\Drupal\Core\Field\FieldItemListInterface $items: The field values to be rendered.
string $langcode: The language that should be used to render the field.
Return value
array A renderable array for $items, as an array of child elements keyed by consecutive numeric indexes starting from 0.
Overrides FormatterInterface::viewElements
File
- ActivityTypes/
opigno_h5p/ src/ Plugin/ Field/ FieldFormatter/ H5PDefaultFormatter.php, line 40
Class
- H5PDefaultFormatter
- Plugin implementation of the 'h5p_default' formatter.
Namespace
Drupal\opigno_h5p\Plugin\Field\FieldFormatterCode
public function viewElements(FieldItemListInterface $items, $langcode) {
$element = [];
$moduleHandler = \Drupal::service('module_handler');
foreach ($items as $delta => $item) {
$value = $item
->getValue();
// Load H5P Content entity.
$h5p_content = H5PContent::load($value['h5p_content_id']);
if (empty($h5p_content)) {
continue;
}
// Grab generic integration settings.
$h5p_integration = H5PDrupal::getGenericH5PIntegrationSettings();
// Add content specific settings.
$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');
// Load dependencies.
$files = $core
->getDependenciesFiles($preloaded_dependencies, H5PDrupal::getRelativeH5PPath());
$loadpackages = [
'h5p/h5p.content',
];
// Load dependencies.
foreach ($preloaded_dependencies as $dependency) {
$loadpackages[] = 'h5p/' . _h5p_library_machine_to_id($dependency);
}
// Add alter hooks.
$moduleHandler
->alter('h5p_scripts', $files['scripts'], $loadpackages, $h5p_content
->getLibrary()->embed_types);
$moduleHandler
->alter('h5p_styles', $files['styles'], $loadpackages, $h5p_content
->getLibrary()->embed_types);
// Render always in Div.
$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',
])) {
// Remove preselected values from the last answer for H5P answer form.
$h5p_integration['contents'][$content_id_string]['contentUserData'] = [
0 => [
'state' => '{}',
],
];
}
// Render each element as markup.
$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;
}