You are here

public function BackgroundMedia::buildBackgroundMediaImage in Bootstrap Styles 1.0.x

Helper function to the background media image style.

Parameters

object $media_entity: A media entity object.

object $field_name: The Media entity local video field name.

object $storage: The styles storage.

Return value

string Background media image style.

1 call to BackgroundMedia::buildBackgroundMediaImage()
BackgroundMedia::build in src/Plugin/BootstrapStyles/Style/BackgroundMedia.php

File

src/Plugin/BootstrapStyles/Style/BackgroundMedia.php, line 429

Class

BackgroundMedia
Class BackgroundMedia.

Namespace

Drupal\bootstrap_styles\Plugin\BootstrapStyles\Style

Code

public function buildBackgroundMediaImage($media_entity, $field_name, $storage) {
  $fid = $media_entity
    ->get($field_name)->target_id;
  $file = File::load($fid);
  $background_url = $file
    ->createFileUrl();
  $style = 'background-image: url(' . $background_url . ');';
  $background_position = 'background-position: center;';
  $background_repeat = 'background-repeat: no-repeat;';
  $background_size = 'background-size: cover;';
  $background_attachment = '';
  if (isset($storage['background_media']['background_options'])) {

    // Background position.
    if (isset($storage['background_media']['background_options']['background_position']) && !empty($storage['background_media']['background_options']['background_position'])) {
      $background_position = 'background-position: ' . $storage['background_media']['background_options']['background_position'] . ';';
    }

    // Background repeat.
    if (isset($storage['background_media']['background_options']['background_repeat']) && !empty($storage['background_media']['background_options']['background_repeat'])) {
      $background_repeat = 'background-repeat: ' . $storage['background_media']['background_options']['background_repeat'] . ';';
    }

    // Background attachment.
    if (isset($storage['background_media']['background_options']['background_attachment']) && !empty($storage['background_media']['background_options']['background_attachment'])) {
      if ($storage['background_media']['background_options']['background_attachment'] != 'not_fixed') {
        $background_attachment = 'background-attachment: ' . $storage['background_media']['background_options']['background_attachment'] . ';';
      }
    }

    // Background size.
    if (isset($storage['background_media']['background_options']['background_size']) && !empty($storage['background_media']['background_options']['background_size'])) {
      $background_size = 'background-size: ' . $storage['background_media']['background_options']['background_size'] . ';';
    }
  }
  $style .= ' ' . $background_position;
  $style .= ' ' . $background_repeat;
  $style .= ' ' . $background_size;
  if ($background_attachment) {
    $style .= ' ' . $background_attachment;
  }
  return $style;
}