diff --git a/config/schema/video_embed_field.schema.yml b/config/schema/video_embed_field.schema.yml index 27eb7b3..870933e 100644 --- a/config/schema/video_embed_field.schema.yml +++ b/config/schema/video_embed_field.schema.yml @@ -16,6 +16,9 @@ field.formatter.settings.video_embed_field_video: autoplay: type: boolean label: 'Autoplay' + responsive: + type: boolean + label: 'Responsive' width: type: string label: 'Width' diff --git a/css/video_embed_field.responsive-video.css b/css/video_embed_field.responsive-video.css new file mode 100644 index 0000000..4f21735 --- /dev/null +++ b/css/video_embed_field.responsive-video.css @@ -0,0 +1,19 @@ +.video-embed-field-responsive-video { + position: relative; +} + +.video-embed-field-responsive-video:after { + content: ''; + display: block; + padding-bottom: 56.25%; +} + +.video-embed-field-responsive-video iframe { + position: absolute; + top: 0; + right: 0; + bottom: 0; + left: 0; + width: 100%; + height: 100%; +} diff --git a/src/Plugin/Field/FieldFormatter/Video.php b/src/Plugin/Field/FieldFormatter/Video.php index 0829304..9c40c9b 100644 --- a/src/Plugin/Field/FieldFormatter/Video.php +++ b/src/Plugin/Field/FieldFormatter/Video.php @@ -39,10 +39,15 @@ class Video extends FormatterBase implements ContainerFactoryPluginInterface { */ public function viewElements(FieldItemListInterface $items, $langcode) { $element = []; - $settings = $this->getSettings(); foreach ($items as $delta => $item) { $provider = $this->providerManager->loadProviderFromInput($item->value); - $element[$delta] = $provider->renderEmbedCode($settings['width'], $settings['height'], $settings['autoplay']); + $element[$delta] = $provider->renderEmbedCode($this->getSetting('width'), $this->getSetting('height'), $this->getSetting('autoplay')); + } + // Attach a library and attributes to the field renderable array in the case + // of responsive videos. + if ($this->getSetting('responsive')) { + $element['#attached']['library'][] = 'video_embed_field/responsive-video'; + $element['#attributes']['class'][] = 'video-embed-field-responsive-video'; } return $element; } @@ -52,6 +57,7 @@ class Video extends FormatterBase implements ContainerFactoryPluginInterface { */ public static function defaultSettings() { return [ + 'responsive' => FALSE, 'width' => '854', 'height' => '480', 'autoplay' => TRUE, @@ -67,17 +73,32 @@ class Video extends FormatterBase implements ContainerFactoryPluginInterface { '#type' => 'checkbox', '#default_value' => $this->getSetting('autoplay'), ]; + $form['responsive'] = [ + '#title' => t('Responsive Video'), + '#type' => 'checkbox', + '#description' => $this->t("Make the video fill the width of it's container, adjusting to the size of the user's screen."), + '#default_value' => $this->getSetting('responsive'), + ]; + $responsive_checked_state = [ + 'visible' => [ + [ + ':input[name="fields[' . $this->fieldDefinition->getName() . '][settings_edit_form][settings][responsive]"]' => ['checked' => FALSE], + ] + ], + ]; $form['width'] = [ '#title' => t('Width'), '#type' => 'textfield', '#default_value' => $this->getSetting('width'), '#required' => TRUE, + '#states' => $responsive_checked_state, ]; $form['height'] = [ '#title' => t('Height'), '#type' => 'textfield', '#default_value' => $this->getSetting('height'), '#required' => TRUE, + '#states' => $responsive_checked_state, ]; return $form; } @@ -89,7 +110,7 @@ class Video extends FormatterBase implements ContainerFactoryPluginInterface { $summary[] = t('Embedded Video (@widthx@height@autoplay).', [ '@width' => $this->getSetting('width'), '@height' => $this->getSetting('height'), - '@autoplay' => $this->getSetting('autoplay') ? t(', autoplaying') : '' , + '@autoplay' => $this->getSetting('autoplay') ? t(', autoplaying') : '', ]); return $summary; } diff --git a/src/Tests/EmbedFieldTest.php b/src/Tests/EmbedFieldTest.php index 3a1452e..26b1bbf 100644 --- a/src/Tests/EmbedFieldTest.php +++ b/src/Tests/EmbedFieldTest.php @@ -180,6 +180,35 @@ class EmbedFieldTest extends FieldUnitTestBase { ], ], ], + 'Video: Responsive' => [ + 'https://vimeo.com/80896303', + [ + 'type' => 'video_embed_field_video', + 'settings' => [ + 'width' => '100px', + 'height' => '100px', + 'autoplay' => TRUE, + 'responsive' => TRUE, + ], + ], + [ + '#type' => 'html_tag', + '#tag' => 'iframe', + '#attributes' => [ + 'width' => '100px', + 'height' => '100px', + 'frameborder' => '0', + 'allowfullscreen' => 'allowfullscreen', + 'src' => 'https://player.vimeo.com/video/80896303?autoplay=1', + ], + ], + [ + 'class' => ['video-embed-field-responsive-video'], + ], + [ + 'library' => ['video_embed_field/responsive-video'], + ], + ], ]; } @@ -188,20 +217,27 @@ class EmbedFieldTest extends FieldUnitTestBase { * * Test the embed field. */ - public function assertEmbedField($url, $settings, $expected_output) { + public function assertEmbedField($url, $settings, $expected_field_item_output, $field_attributes = NULL, $field_attachments = NULL) { $entity = entity_create('entity_test'); $entity->field_test->value = $url; $entity->save(); - $field = $entity->field_test->view($settings)[0]; + $field_output = $entity->field_test->view($settings); + $field_item_output = $field_output[0]; // Prevent circular references with comparing field output that // contains url objects. - array_walk_recursive($field, function (&$value) { + array_walk_recursive($field_item_output, function (&$value) { if ($value instanceof Url) { $value = $value->isRouted() ? $value->getRouteName() : $value->getUri(); } $value = trim($value); }); - $this->assertEqual($field, $expected_output); + if ($field_attributes !== NULL) { + $this->assertEqual($field_output['#attributes'], $field_attributes); + } + if ($field_attachments !== NULL) { + $this->assertEqual($field_output['#attached'], $field_attachments); + } + $this->assertEqual($field_item_output, $expected_field_item_output); } /** @@ -209,7 +245,13 @@ class EmbedFieldTest extends FieldUnitTestBase { */ public function testEmbedField() { foreach ($this->renderedFieldTestCases() as $test_case) { - $this->assertEmbedField($test_case[0], $test_case[1], $test_case[2]); + $this->assertEmbedField( + $test_case[0], + $test_case[1], + $test_case[2], + isset($test_case[3]) ? $test_case[3] : NULL, + isset($test_case[4]) ? $test_case[4] : NULL + ); } } diff --git a/video_embed_field.libraries.yml b/video_embed_field.libraries.yml index 1c5ceac..6852f61 100644 --- a/video_embed_field.libraries.yml +++ b/video_embed_field.libraries.yml @@ -3,3 +3,8 @@ colorbox: js/video-embed-field.colorbox.js: {} dependencies: - colorbox/colorbox + +responsive-video: + css: + component: + css/video_embed_field.responsive-video.css: {}