You are here

public function OpignoTincanFieldFormatter::viewElements in Opigno module 8

Same name and namespace in other branches
  1. 3.x ActivityTypes/opigno_tincan_activity/src/Plugin/Field/FieldFormatter/OpignoTincanFieldFormatter.php \Drupal\opigno_tincan_activity\Plugin\Field\FieldFormatter\OpignoTincanFieldFormatter::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_tincan_activity/src/Plugin/Field/FieldFormatter/OpignoTincanFieldFormatter.php, line 24

Class

OpignoTincanFieldFormatter
Plugin implementation of the 'opigno_evaluation_method_formatter' formatter.

Namespace

Drupal\opigno_tincan_activity\Plugin\Field\FieldFormatter

Code

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;

    // Create query parameters for Tincan launch file.
    $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)) . '&registration=' . rawurlencode($registration);

    // Returning data.
    $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;
}