You are here

function mediaelement_field_formatter_view in MediaElement 7.2

Same name and namespace in other branches
  1. 7 mediaelement.field.inc \mediaelement_field_formatter_view()

Implements hook_field_formatter_view().

File

./mediaelement.module, line 124
Provides HTML5 video and audio elements using Mediaelement.js for HTML4 browsers.

Code

function mediaelement_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {

  // A unique caller per page for the JS specific settings to use. This may be
  // different for ever page view and should not be used for styling.
  static $id = 0;
  $element = array();
  $path = drupal_get_path('module', 'mediaelement');
  foreach ($items as $delta => $item) {

    // file and link_field keep the path/link in a different property name.
    $settings = $display['settings'];
    $js_settings = array();
    $js_settings['opts'] = array();
    $js_settings['controls'] = (bool) $settings['controls'];
    if ($display['type'] == 'mediaelement_video') {
      $js_settings['opts']['videoHeight'] = (int) $settings['height'];
      $js_settings['opts']['videoWidth'] = (int) $settings['width'];
    }
    elseif ($display['type'] == 'mediaelement_audio') {
      $js_settings['opts']['audioHeight'] = (int) $settings['height'];
      $js_settings['opts']['audioWidth'] = (int) $settings['width'];
    }
    $class = 'mediaelement-formatter-identifier-' . time() . '-' . $id++;
    $element[$delta] = array(
      '#attributes' => array(
        'src' => file_create_url($item['uri']),
        'class' => $class,
      ),
      '#settings' => $settings,
      '#attached' => array(
        'library' => array(
          array(
            'mediaelement',
            'mediaelement',
          ),
        ),
        'js' => array(
          $path . '/mediaelement.js' => array(),
          0 => array(
            'type' => 'setting',
            'data' => array(
              'mediaelement' => array(
                '.' . $class => $js_settings,
              ),
            ),
          ),
        ),
      ),
    );
    if ($settings['controls']) {
      $element[$delta]['#attributes']['controls'] = 'controls';
    }
    if ($display['type'] == 'mediaelement_video') {
      $element[$delta]['#theme'] = 'mediaelement_video';
      $element[$delta]['#attributes']['height'] = $display['settings']['height'];
      $element[$delta]['#attributes']['width'] = $display['settings']['width'];
    }
    elseif ($display['type'] == 'mediaelement_audio') {
      $element[$delta]['#theme'] = 'mediaelement_audio';
    }
  }
  return $element;
}