Skip to content

Commit

Permalink
EWPP-3318: Adapt text with featured media rendering to accommodate we…
Browse files Browse the repository at this point in the history
…btools media.
  • Loading branch information
nagyad authored and 22Alexandra committed Jun 23, 2023
1 parent 35f117b commit 18d155b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 9 deletions.
19 changes: 11 additions & 8 deletions oe_theme.theme
Original file line number Diff line number Diff line change
Expand Up @@ -991,20 +991,20 @@ function oe_theme_preprocess_paragraph__oe_text_feature_media(array &$variables)
// Get the media source.
$source = $media->getSource();

// Load information about the media and the display.
$media_type = \Drupal::entityTypeManager()->getStorage('media_type')->load($media->bundle());
$cacheability->addCacheableDependency($media_type);
$source_field = $source->getSourceFieldDefinition($media_type);
$display = EntityViewDisplay::collectRenderDisplay($media, 'oe_theme_main_content');
$cacheability->addCacheableDependency($display);
$display_options = $display->getComponent($source_field->getName());

if ($source instanceof MediaAvPortalVideoSource || $source instanceof OEmbed || $source instanceof Iframe) {
if ($source instanceof MediaAvPortalVideoSource) {
// Default video aspect ratio is set to 16:9 for AV Portal Video.
$variables['ratio'] = '16:9';
}

// Load information about the media and the display.
$media_type = \Drupal::entityTypeManager()->getStorage('media_type')->load($media->bundle());
$cacheability->addCacheableDependency($media_type);
$source_field = $source->getSourceFieldDefinition($media_type);
$display = EntityViewDisplay::collectRenderDisplay($media, 'oe_theme_main_content');
$cacheability->addCacheableDependency($display);
$display_options = $display->getComponent($source_field->getName());

// If it is an OEmbed resource, render it and pass it as embeddable data
// only if it is of type video or html.
if ($source instanceof OEmbed) {
Expand Down Expand Up @@ -1037,6 +1037,9 @@ function oe_theme_preprocess_paragraph__oe_text_feature_media(array &$variables)
return;
}

// Otherwise we render the webtools media.
$variables['video'] = $media->{$source_field->getName()}->view($display_options);

$cacheability->applyTo($variables);
}

Expand Down
24 changes: 23 additions & 1 deletion tests/src/Kernel/Paragraphs/MediaParagraphsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ class MediaParagraphsTest extends ParagraphsTestBase {
'media',
'oe_media',
'oe_media_oembed_mock',
'oe_media_webtools',
'oe_paragraphs_media',
'oe_webtools',
'oe_webtools_media',
'json_field',
'allowed_formats',
'oe_paragraphs_media_field_storage',
'oe_paragraphs_iframe_media',
Expand Down Expand Up @@ -54,20 +58,23 @@ protected function setUp(): void {
oe_paragraphs_media_field_storage_install(FALSE);
$this->installEntitySchema('media');
$this->installConfig([
'json_field',
'media',
'oe_media',
'oe_paragraphs_media',
'media_avportal',
'oe_media_avportal',
'oe_media_webtools',
'oe_paragraphs_banner',
'oe_theme_paragraphs_banner',
'oe_paragraphs_iframe_media',
'oe_webtools_media',
'options',
'oe_media_iframe',
'oe_paragraphs_carousel',
'oe_paragraphs_av_media',
]);
// Call the install hook of the Media module.
// Call the installation hook of the Media module.
\Drupal::moduleHandler()->loadInclude('media', 'install');
media_install();
}
Expand Down Expand Up @@ -315,6 +322,21 @@ public function testTextWithMedia(): void {
// Since link doesn't exist variant is recognized as "left_simple".
$assert->assertVariant('left_simple', $html);

// Create a webtools chart and add it to the paragraph.
$media = $media_storage->create([
'bundle' => 'webtools_chart',
'name' => 'Chart',
'oe_media_webtools' => '{"service":"charts","data":{"series":[{"name":"Y","data":[{"name":"1","y":0.5}]}]},"provider":"highcharts"}',
]);
$media->save();
$paragraph->set('field_oe_media', ['target_id' => $media->id()]);
$paragraph->save();

$html = $this->renderParagraph($paragraph);
$assert->assertVariant('left_simple', $html);
$crawler = new Crawler($html);
$this->assertEquals('{"service":"charts","data":{"series":[{"name":"Y","data":[{"name":"1","y":0.5}]}]},"provider":"highcharts"}', $crawler->filter('script')->text());

// Create iframe video with aspect ratio 1:1 and add it to the paragraph.
$media = $media_storage->create([
'bundle' => 'video_iframe',
Expand Down

0 comments on commit 18d155b

Please sign in to comment.