Media.php in GridStack 8.2
File
src/Plugin/gridstack/stylizer/Media.php
View source
<?php
namespace Drupal\gridstack\Plugin\gridstack\stylizer;
use Drupal\Component\Serialization\Json;
use Drupal\media\Entity\Media as MediaEntity;
use Drupal\gridstack\GridStackDefault;
use Symfony\Component\DependencyInjection\ContainerInterface;
class Media extends Background {
protected $fieldName;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
if (!empty($configuration['field_name'])) {
$instance
->setFieldName($configuration['field_name']);
}
return $instance;
}
public function getFieldName() {
return $this->fieldName;
}
public function setFieldName($name) {
$this->fieldName = $name;
return $this;
}
public function buildMedia(array &$attributes, array &$settings) {
$data = $this
->prepareMedia($settings);
$config = $data['settings'];
if (isset($config['media_id']) && ($mid = $config['media_id'])) {
$media = MediaEntity::load($mid);
if (isset($config['responsive_image_style']) && ($resimage = $config['responsive_image_style'])) {
$data['settings']['resimage'] = $this->blazyEntity
->blazyManager()
->entityLoad($resimage, 'responsive_image_style');
}
$settings['has_media'] = TRUE;
$attributes['class'][] = 'is-gs-media';
return $this->blazyEntity
->build($data, $media, $media
->label());
}
return [];
}
protected function prepareMedia(array $settings) {
$build = [];
$data = $this
->mediaSettings($settings);
if (!empty($data['media_id'])) {
$build['wrapper_attributes']['class'][] = 'box__bg';
$build['attributes']['class'][] = 'b-noratio b-gs';
$build['attributes']['class'][] = $this
->getStyle('background', $settings) ? 'b-gs--bg' : 'b-gs--media';
if ($animations = $this
->getAnimation($settings, 'all')) {
foreach ($animations as $key => $value) {
$key = str_replace('_', '-', $key);
$key = $key == 'animation' ? $key : 'animation-' . $key;
if ($key == 'animation') {
$data['fx'] = $value;
}
else {
$build['attributes']['data-' . $key] = $value;
}
}
}
if ($data['media_source'] == 'image') {
$this
->prepareOverlay($data, $settings);
}
if (!empty($data['opacity']) && $data['opacity'] < 1) {
$build['attributes']['style'] = 'opacity: ' . $data['opacity'] . ';';
}
}
$build['settings'] = $data;
return $build;
}
protected function prepareOverlay(array &$data, array $settings) {
$bg = $this
->getBackgroundColor($settings, FALSE);
$use_overlay = $this
->getStyle('overlay', $settings);
$overlay = [
'#theme' => 'container',
'#attributes' => [
'class' => [
'media__overlay',
],
],
];
if ($use_overlay && $bg && !empty($settings['_ipe'])) {
$overlay['#attributes']['style'] = $bg;
}
$data['icon']['gridstack_overlay'] = $overlay;
$data['icon']['gridstack_overlay']['#weight'] = 100;
if (empty($settings['_ipe'])) {
if ($use_overlay && empty($bg)) {
unset($data['icon']['gridstack_overlay']);
}
}
}
protected function mediaSettings(array $settings) {
$styles = empty($settings['styles']) ? [] : $settings['styles'];
$data = empty($styles['metadata']) ? [] : Json::decode($styles['metadata']);
if ($data) {
$data['background'] = $this
->getStyle('background', $settings);
$data['media_switch'] = 'media';
$data['ratio'] = '';
$data['_detached'] = FALSE;
$data = array_merge($styles, $data);
return array_merge(GridStackDefault::entitySettings(), $data);
}
return [];
}
}
Classes
Name |
Description |
Media |
Provides the media styles for front-end. |