You are here

function mediaelement_field_formatter_view in MediaElement 7

Same name and namespace in other branches
  1. 7.2 mediaelement.module \mediaelement_field_formatter_view()

Implements hook_field_formatter_view().

File

./mediaelement.field.inc, line 214
Field integration for the MediaElement module.

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) {

    // Support the link field type which stores it's data in 'url'.
    $src = $field['type'] == 'link_field' ? $item['url'] : $item['uri'];
    $display['settings']['description'] = empty($item['description']) ? '' : $item['description'];
    $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'] = $settings['height'];
      $js_settings['opts']['videoWidth'] = $settings['width'];
    }
    elseif ($display['type'] == 'mediaelement_audio') {
      $js_settings['opts']['audioHeight'] = $settings['height'];
      $js_settings['opts']['audioWidth'] = $settings['width'];
    }
    $js_settings['opts']['features'] = array();
    foreach ($settings['features'] as $feature => $value) {
      if (!empty($value)) {
        $js_settings['opts']['features'][] = $feature;
      }
    }
    $js_settings['opts']['startVolume'] = (double) $settings['volume'];
    $class = 'mediaelement-formatter-identifier-' . time() . '-' . $id++;
    $element[$delta] = array(
      '#file' => (object) $item,
      '#attributes' => array(
        'src' => file_create_url($src),
        'class' => array(
          $class,
        ),
        'preload' => $display['settings']['preload'],
      ),
      '#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 (!empty($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'];
      if (!empty($display['settings']['autoplay'])) {

        // "autoplay" is boolean attribute, i.e. it only needs to be present.
        // http://www.w3.org/TR/html5/media-elements.html#attr-media-autoplay.
        $element[$delta]['#attributes']['autoplay'] = 'autoplay';
      }
      if (!empty($display['settings']['loop'])) {
        $element[$delta]['#attributes']['loop'] = 'loop';
      }
    }
    elseif ($display['type'] == 'mediaelement_audio') {
      $element[$delta]['#theme'] = 'mediaelement_audio';
      if (!empty($display['settings']['autoplay'])) {
        $element[$delta]['#attributes']['autoplay'] = '';
      }
      if (!empty($display['settings']['loop'])) {
        $element[$delta]['#attributes']['loop'] = '';
      }
    }
  }
  return $element;
}