function varbase_media_preprocess_field in Varbase Media 8.6
Same name and namespace in other branches
- 8.7 varbase_media.module \varbase_media_preprocess_field()
- 9.0.x varbase_media.module \varbase_media_preprocess_field()
Implements hook_preprocess_field().
File
- ./
varbase_media.module, line 34 - Contains varbase_media.module.
Code
function varbase_media_preprocess_field(&$variables) {
// 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');
if ($variables['element']['#formatter'] == 'oembed') {
$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};
$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();
}
}
}