Skip to content

Commit 8c74110

Browse files
author
Alex Skrypnyk
committed
Fixed media trait not correctly processing non-image files.
1 parent 898460a commit 8c74110

File tree

2 files changed

+27
-12
lines changed

2 files changed

+27
-12
lines changed

src/MediaTrait.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -138,20 +138,26 @@ protected function mediaExpandEntityFields($entity_type, $stub) {
138138
protected function mediaExpandEntityFieldsFixtures($stub) {
139139
$fixture_path = $this->getMinkParameter('files_path') ? rtrim(realpath($this->getMinkParameter('files_path')), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR : NULL;
140140

141-
foreach (get_object_vars($stub) as $name => $value) {
141+
$fields = get_object_vars($stub);
142+
143+
$field_types = $this->getDrupal()->getDriver()->getCore()->getEntityFieldTypes('media', array_keys($fields));
144+
145+
foreach ($fields as $name => $value) {
142146
if (strpos($name, 'field_') === FALSE) {
143147
continue;
144148
}
145149

146-
if (is_array($value)) {
147-
if (!empty($value[0])) {
148-
if (is_file($fixture_path . $value[0])) {
149-
$stub->{$name}[0] = $fixture_path . $value[0];
150+
if (!empty($field_types[$name]) && $field_types[$name] == 'image') {
151+
if (is_array($value)) {
152+
if (!empty($value[0])) {
153+
if (is_file($fixture_path . $value[0])) {
154+
$stub->{$name}[0] = $fixture_path . $value[0];
155+
}
150156
}
151157
}
152-
}
153-
elseif (is_file($fixture_path . $value)) {
154-
$stub->{$name} = $fixture_path . $value;
158+
elseif (is_file($fixture_path . $value)) {
159+
$stub->{$name} = $fixture_path . $value;
160+
}
155161
}
156162
}
157163
}

tests/behat/features/media.feature

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@ Feature: Check that MediaTrait works for D8 or D9
33

44
@api
55
Scenario: Assert "When I attach the file :file to :field_name media field"
6-
Given "image" media:
7-
| name | field_media_image |
8-
| Test media | example_image.png |
6+
Given managed file:
7+
| path |
8+
| example_document.pdf |
9+
10+
And "image" media:
11+
| name | field_media_image |
12+
| Test media image | example_image.png |
13+
And "document" media:
14+
| name | field_media_document |
15+
| Test media document | example_document.pdf |
16+
917
And I am logged in as a user with the "administrator" role
1018
When I visit "/admin/content/media"
11-
Then I should see the text "Test media"
19+
Then I should see the text "Test media image"
20+
And I should see the text "Test media document"

0 commit comments

Comments
 (0)