Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EWPP-3318: Adapt text with featured media rendering to accommodate wwebtools. #1274

Merged
merged 1 commit into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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