You are here

function brightcove_field_formatter_view in Brightcove Video Connect 7.6

Same name and namespace in other branches
  1. 7.7 brightcove.module \brightcove_field_formatter_view()

Implements hook_field_formatter_view().

Parameters

$entity_type:

$entity:

$field:

$instance:

$langcode:

$items:

$display:

Return value

array

File

./brightcove.module, line 1526
Brightcove module is an integration layer between any modules using Brightcove API. It makes all necessary checks for the API and makes settings available to the user.

Code

function brightcove_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = [];
  $theme = NULL;
  $variables = [];
  $settings = $display['settings'];
  $variables['#brightcove_widget_type'] = $instance['widget']['type'];
  if ($display['type'] === 'brightcove_default') {
    $theme = 'brightcove_field_formatter_default';
  }
  if ($display['type'] === 'brightcove_iframe') {
    $theme = 'brightcove_field_formatter_iframe';
  }
  if ($display['type'] === 'brightcove_image') {
    $theme = 'brightcove_field_image';
    $variables['#brightcove_image_style'] = $settings['brightcove_image_style'];
    $variables['#brightcove_image_link'] = $settings['brightcove_image_link'];
    $variables['#brightcove_image_type'] = $settings['brightcove_image_type'];
  }
  if ($display['type'] === 'brightcove_metadata') {
    $metadata_options = _brightcove_field_get_object_formatter_keys();
    $theme = 'brightcove_field_metadata';
    $variables['#key'] = $settings['brightcove_metadata_type'];
    $variables['#label'] = $metadata_options[$settings['brightcove_metadata_type']];
  }
  if (isset($settings['brightcove_image_link']) && $settings['brightcove_image_link'] == 'dialog') {
    $variables['#attached']['library'][] = [
      'system',
      'drupal.ajax',
    ];
    $variables['#attached']['library'][] = [
      'system',
      'ui.dialog',
    ];
    $variables['#attached']['js'][] = drupal_get_path('module', 'brightcove') . '/js/brightcove.js';
  }
  $variables['#attached']['css'][] = drupal_get_path('module', 'brightcove') . '/styles/brightcove.css';
  if ($theme) {
    switch ($instance['widget']['type']) {
      case BRIGHTCOVE_VIDEO_WIDGET:
        foreach ($items as $delta => $item) {
          $video = FALSE;
          if (isset($item['brightcove_id'])) {
            $client = brightcove_client_load_or_default($item['bcid']);
            if (!$client) {
              return [];
            }
            $video = brightcove_load_video($item['brightcove_id'], $client);
          }
          $element[$delta] = [
            '#theme' => $theme,
            '#type' => BRIGHTCOVE_EMBED_TYPE_VIDEO,
            '#element' => $item,
            '#delta' => $delta,
            '#entity_type' => $entity_type,
            '#entity' => $entity,
            '#field' => $field,
            '#instance' => $instance,
            '#display' => $display,
            '#video' => $video,
            '#width' => isset($settings['width']) ? $settings['width'] : NULL,
            '#height' => isset($settings['height']) ? $settings['height'] : NULL,
          ] + $variables;
        }
        break;
      case BRIGHTCOVE_PLAYLIST_WIDGET:
        foreach ($items as $delta => $item) {
          $playlist = FALSE;
          if (isset($item['brightcove_id'])) {
            if (!$item['bcid'] && ($defaultclient = brightcove_client_load_or_default())) {
              $item['bcid'] = $defaultclient->bcid;
            }
            $playlist = brightcove_load_playlist($item['brightcove_id'], $item['bcid']);
            if (!$playlist) {
              return [];
            }
          }
          $element[$delta] = [
            '#theme' => $theme,
            '#type' => BRIGHTCOVE_EMBED_TYPE_PLAYLIST,
            '#element' => $item,
            '#delta' => $delta,
            '#entity_type' => $entity_type,
            '#entity' => $entity,
            '#field' => $field,
            '#instance' => $instance,
            '#display' => $display,
            '#playlist' => $playlist,
            '#width' => isset($settings['width']) ? $settings['width'] : NULL,
            '#height' => isset($settings['height']) ? $settings['height'] : NULL,
          ] + $variables;
        }
        break;
    }
  }
  return $element;
}