OpignoTincanFieldFormatter.php in Opigno module 3.x
File
ActivityTypes/opigno_tincan_activity/src/Plugin/Field/FieldFormatter/OpignoTincanFieldFormatter.php
View source
<?php
namespace Drupal\opigno_tincan_activity\Plugin\Field\FieldFormatter;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
class OpignoTincanFieldFormatter extends EntityReferenceFormatterBase {
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
foreach ($this
->getEntitiesToView($items, $langcode) as $delta => $file) {
$tincan_content_service = \Drupal::service('opigno_tincan_activity.tincan');
$uri = $tincan_content_service
->getExtractPath($file);
$url = file_create_url($uri);
$package_properties = $tincan_content_service
->tincanLoadByFileEntity($file);
$launch_file = $package_properties->launch_filename;
$tincan_answer_assistant = \Drupal::service('opigno_tincan_activity.answer_assistant');
$config = \Drupal::config('opigno_tincan_api.settings');
$endpoint = $config
->get('opigno_tincan_api_endpoint');
$username = $config
->get('opigno_tincan_api_username');
$password = $config
->get('opigno_tincan_api_password');
$user = \Drupal::currentUser();
$auth = 'Basic ' . base64_encode($username . ':' . $password);
$actor = [
'mbox_sha1sum' => sha1('mailto:' . $user
->getEmail()),
'name' => $user
->getAccountName(),
];
$activity = \Drupal::routeMatch()
->getParameter('opigno_activity');
$registration = $tincan_answer_assistant
->getRegistration($activity, $user);
$launch_file .= '?endpoint=' . rawurlencode($endpoint) . '&auth=' . rawurlencode($auth) . '&actor=' . rawurlencode(json_encode($actor)) . '®istration=' . rawurlencode($registration);
$elements[$delta] = [
'#type' => 'inline_template',
'#template' => '<iframe style="{{ style }}" src="{{ url }}"></iframe>',
'#context' => [
'url' => $url . '/' . $launch_file,
'style' => "width: 100%; min-height: 800px; border: 0;",
],
];
}
return $elements;
}
}