diff --git a/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php b/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php index a4e0f4574840340a353a10531f874780ddcd3537..578487c3b3c22b2eaf105e30815c9d91298116a8 100644 --- a/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php +++ b/core/modules/file/src/Plugin/Field/FieldFormatter/FileVideoFormatter.php @@ -55,7 +55,6 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#maxlength' => 5, '#field_suffix' => $this->t('pixels'), '#min' => 0, - '#required' => TRUE, ], 'height' => [ '#type' => 'number', @@ -65,7 +64,6 @@ public function settingsForm(array $form, FormStateInterface $form_state) { '#maxlength' => 5, '#field_suffix' => $this->t('pixels'), '#min' => 0, - '#required' => TRUE, ], ]; } @@ -76,10 +74,19 @@ public function settingsForm(array $form, FormStateInterface $form_state) { public function settingsSummary() { $summary = parent::settingsSummary(); $summary[] = $this->t('Muted: %muted', ['%muted' => $this->getSetting('muted') ? $this->t('yes') : $this->t('no')]); - $summary[] = $this->t('Size: %width x %height pixels', [ - '%width' => $this->getSetting('width'), - '%height' => $this->getSetting('height'), - ]); + + if ($width = $this->getSetting('width')) { + $summary[] = $this->t('Width: %width pixels', [ + '%width' => $width, + ]); + } + + if ($height = $this->getSetting('height')) { + $summary[] = $this->t('Height: %height pixels', [ + '%height' => $height, + ]); + } + return $summary; } @@ -87,9 +94,14 @@ public function settingsSummary() { * {@inheritdoc} */ protected function prepareAttributes(array $additional_attributes = []) { - return parent::prepareAttributes(['muted']) - ->setAttribute('width', $this->getSetting('width')) - ->setAttribute('height', $this->getSetting('height')); + $attributes = parent::prepareAttributes(['muted']); + if (($width = $this->getSetting('width'))) { + $attributes->setAttribute('width', $width); + } + if (($height = $this->getSetting('height'))) { + $attributes->setAttribute('height', $height); + } + return $attributes; } } diff --git a/core/modules/file/tests/src/Functional/Formatter/FileVideoFormatterTest.php b/core/modules/file/tests/src/Functional/Formatter/FileVideoFormatterTest.php index e7e2d36c42fbc7c38bc19f38bdb8d290a8c817c9..fd4fe6899fa7195429b02221d2c052eb31768bde 100644 --- a/core/modules/file/tests/src/Functional/Formatter/FileVideoFormatterTest.php +++ b/core/modules/file/tests/src/Functional/Formatter/FileVideoFormatterTest.php @@ -73,6 +73,8 @@ public function testAttributes() { 'autoplay' => TRUE, 'loop' => TRUE, 'muted' => TRUE, + 'width' => 800, + 'height' => 600, ] ); @@ -100,6 +102,42 @@ public function testAttributes() { $assert_session->elementExists('css', "video[autoplay='autoplay'] > source[src='$file_url'][type='video/mp4']"); $assert_session->elementExists('css', "video[loop='loop'] > source[src='$file_url'][type='video/mp4']"); $assert_session->elementExists('css', "video[muted='muted'] > source[src='$file_url'][type='video/mp4']"); + $assert_session->elementExists('css', "video[width='800'] > source[src='$file_url'][type='video/mp4']"); + $assert_session->elementExists('css', "video[height='600'] > source[src='$file_url'][type='video/mp4']"); + + /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $displayRepository */ + $displayRepository = $this->container->get('entity_display.repository'); + $entityDisplay = $displayRepository->getViewDisplay('entity_test', 'entity_test', 'full'); + $fieldName = $field_config->get('field_name'); + $fieldDisplay = $entityDisplay->getComponent($fieldName); + + // Tests only setting width. + $fieldDisplay['settings']['height'] = NULL; + $entityDisplay->setComponent($fieldName, $fieldDisplay); + $entityDisplay->save(); + + $this->drupalGet($entity->toUrl()); + $assert_session->elementAttributeNotExists('css', 'video', 'height'); + + // Tests only setting height. + $fieldDisplay['settings']['height'] = 600; + $fieldDisplay['settings']['width'] = NULL; + $entityDisplay->setComponent($fieldName, $fieldDisplay); + $entityDisplay->save(); + + $this->drupalGet($entity->toUrl()); + $assert_session->elementAttributeNotExists('css', 'video', 'width'); + + // Tests both height and width empty. + $fieldDisplay['settings']['height'] = NULL; + $fieldDisplay['settings']['width'] = NULL; + $entityDisplay->setComponent($fieldName, $fieldDisplay); + $entityDisplay->save(); + + $this->drupalGet($entity->toUrl()); + $assert_session->elementAttributeNotExists('css', 'video', 'height'); + $assert_session->elementAttributeNotExists('css', 'video', 'width'); + } }