You are here

function varbase_media_preprocess_field in Varbase Media 8.7

Same name and namespace in other branches
  1. 8.6 varbase_media.module \varbase_media_preprocess_field()
  2. 9.0.x varbase_media.module \varbase_media_preprocess_field()

Implements hook_preprocess_field().

File

./varbase_media.module, line 30
Contains varbase_media.module.

Code

function varbase_media_preprocess_field(&$variables) {
  if ($variables['element']['#formatter'] == 'varbase_oembed') {

    // Provide an extra variable to the field template when the field uses
    // a formatter of type 'varbase_oembed'.
    $iframe_url_helper = \Drupal::service('media.oembed.iframe_url_helper');
    $entity = $variables['element']['#object'];
    $view_mode = $variables['element']['#view_mode'];
    $field_name = $variables['element']['#field_name'];
    $bundle = $variables['element']['#bundle'];

    // Get the field formatter settings...
    $entity_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
    $field_display = $entity_display
      ->getComponent($field_name);
    if ($bundle == "remote_video") {
      $max_width = $field_display['settings']['max_width'];
      $max_height = $field_display['settings']['max_height'];
      $item = $variables['element']["#items"]
        ->first();
      $main_property = $item
        ->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getMainPropertyName();
      $value = $item->{$main_property};
      $provider = $entity->field_provider->value;
      $url = Url::fromRoute('media.oembed_iframe', [], [
        'query' => [
          'url' => $value,
          'max_width' => $max_width,
          'max_height' => $max_height,
          'type' => "remote_video",
          'provider' => strtolower($provider),
          'view_mode' => $view_mode,
          'hash' => $iframe_url_helper
            ->getHash($value, $max_width, $max_height, $provider, $view_mode),
        ],
      ]);
      $variables['items'][0]['content']['#attributes']['src'] = $url
        ->toString();
    }
  }
  elseif ($variables['element']['#formatter'] == 'oembed') {

    // Fallback option for oembed old way, In case of change back to oembed.
    // -------
    // Provide an extra variable to the field template when the field uses
    // a formatter of type 'oembed'.
    $resource_fetcher = \Drupal::service('media.oembed.resource_fetcher');
    $url_resolver = \Drupal::service('media.oembed.url_resolver');
    $iframe_url_helper = \Drupal::service('media.oembed.iframe_url_helper');
    $entity = $variables['element']['#object'];
    $view_mode = $variables['element']['#view_mode'];
    $field_name = $variables['element']['#field_name'];
    $bundle = $variables['element']['#bundle'];

    // Get the field formatter settings...
    $entity_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
    $field_display = $entity_display
      ->getComponent($field_name);
    if ($bundle == "remote_video") {
      $max_width = $field_display['settings']['max_width'];
      $max_height = $field_display['settings']['max_height'];
      $item = $variables['element']["#items"]
        ->first();
      $main_property = $item
        ->getFieldDefinition()
        ->getFieldStorageDefinition()
        ->getMainPropertyName();
      $value = $item->{$main_property};

      // Fallback option for oembed old way, In case of change back to oembed
      // fetch resource way.
      $resource_url = $url_resolver
        ->getResourceUrl($value, $max_width, $max_height);
      $resource = $resource_fetcher
        ->fetchResource($resource_url);
      $provider = $resource
        ->getProvider()
        ->getName();
      $url = Url::fromRoute('media.oembed_iframe', [], [
        'query' => [
          'url' => $value,
          'max_width' => $max_width,
          'max_height' => $max_height,
          'type' => "remote_video",
          'provider' => strtolower($provider),
          'view_mode' => $view_mode,
          'hash' => $iframe_url_helper
            ->getHash($value, $max_width, $max_height, $provider, $view_mode),
        ],
      ]);
      $variables['items'][0]['content']['#attributes']['src'] = $url
        ->toString();
    }
  }

  // Add the Varbase Media Blazy Blurry behaviour.
  $use_blazy_blurry = \Drupal::config('varbase_media.settings')
    ->get('use_blazy_blurry');
  if (isset($use_blazy_blurry) && $use_blazy_blurry === TRUE) {
    if (!empty($variables['items'])) {
      foreach ($variables['items'] as &$item) {
        if (!empty($item['content']['#theme']) && $item['content']['#theme'] === 'blazy') {
          $entity = $variables['element']['#object'];
          $view_mode = $variables['element']['#view_mode'];
          $field_name = $variables['element']['#field_name'];
          $entity_display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
          $field_display = $entity_display
            ->getComponent($field_name);
          $build_item = $item['content']['#build']['item'];
          $value = $build_item
            ->getValue();
          $file = File::load($value['target_id']);
          $value['_attributes']['uri'] = $file
            ->getFileUri();
          if (isset($item['content']['#build']) && isset($item['content']['#build']['settings']) && isset($item['content']['#build']['settings']['responsive_image_style'])) {
            $value['_attributes']['responsive_image_style'] = $item['content']['#build']['settings']['responsive_image_style'];
          }
          if (!empty($item['content']['#image_style'])) {
            $value['_attributes']['image_style'] = $item['content']['#image_style'];
          }
          $build_item
            ->setValue($value);
        }
      }
    }
  }
}