Skip to content
This repository was archived by the owner on Jan 5, 2018. It is now read-only.

Commit af41313

Browse files
Berdirslashrsm
authored andcommitted
Issue #2656400 by Berdir: Add entity browser widget for file and image fields
1 parent 127ab88 commit af41313

9 files changed

+402
-58
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
config:
5+
- core.entity_view_mode.file.thumbnail
6+
- file_entity.type.audio
7+
id: file.audio.thumbnail
8+
targetEntityType: file
9+
bundle: audio
10+
mode: thumbnail
11+
content:
12+
filename:
13+
type: string
14+
weight: 0
15+
settings:
16+
link_to_entity: false
17+
third_party_settings: { }
18+
label: hidden
19+
hidden:
20+
filemime: true
21+
filesize: true
22+
uid: true
23+
uri: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
config:
5+
- core.entity_view_mode.file.thumbnail
6+
- file_entity.type.document
7+
id: file.document.thumbnail
8+
targetEntityType: file
9+
bundle: document
10+
mode: thumbnail
11+
content:
12+
filename:
13+
type: string
14+
weight: 0
15+
settings:
16+
link_to_entity: false
17+
third_party_settings: { }
18+
label: hidden
19+
hidden:
20+
filemime: true
21+
filesize: true
22+
uid: true
23+
uri: true
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
config:
5+
- core.entity_view_mode.file.thumbnail
6+
- field.field.file.image.field_image_alt_text
7+
- field.field.file.image.field_image_title_text
8+
- file_entity.type.image
9+
module:
10+
- file_entity
11+
id: file.image.thumbnail
12+
targetEntityType: file
13+
bundle: image
14+
mode: thumbnail
15+
content:
16+
filename:
17+
type: string
18+
weight: 1
19+
label: visually_hidden
20+
settings:
21+
link_to_entity: false
22+
third_party_settings: { }
23+
uri:
24+
type: file_image
25+
label: hidden
26+
weight: 0
27+
settings:
28+
image_style: thumbnail
29+
image_link: ''
30+
third_party_settings: { }
31+
hidden:
32+
field_image_alt_text: true
33+
field_image_title_text: true
34+
filemime: true
35+
filesize: true
36+
uid: true
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
config:
5+
- core.entity_view_mode.file.thumbnail
6+
- file_entity.type.video
7+
id: file.video.thumbnail
8+
targetEntityType: file
9+
bundle: video
10+
mode: thumbnail
11+
content:
12+
filename:
13+
type: string
14+
weight: 1
15+
label: hidden
16+
settings:
17+
link_to_entity: false
18+
third_party_settings: { }
19+
hidden:
20+
filemime: true
21+
filesize: true
22+
uid: true
23+
uri: true
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
langcode: en
2+
status: true
3+
dependencies:
4+
module:
5+
- file
6+
- file_entity
7+
id: file.thumbnail
8+
label: Thumbnail
9+
targetEntityType: file
10+
cache: true

file_entity.module

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -210,17 +210,16 @@ function template_preprocess_file(&$variables) {
210210
$file = $variables['file'];
211211

212212
$variables['id'] = $file->id();
213-
$variables['date'] = format_date($file->getCreatedTime()); // @TODO: Check this out
213+
$variables['date'] = \Drupal::service('date.formatter')->format($file->getCreatedTime());
214214
$username = array(
215215
'#theme' => 'username',
216216
'#account' => $file->getOwner(),
217217
'#link_options' => array('attributes' => array('rel' => 'author')),
218218
);
219-
$variables['name'] = drupal_render($username);
219+
$variables['name'] = $username;
220220

221221
$variables['file_url'] = $file->url('canonical');
222-
$label = $file->label();
223-
$variables['label'] = \Drupal\Component\Utility\SafeMarkup::checkPlain($label);
222+
$variables['label'] = $file->label();
224223
$variables['page'] = $view_mode == 'full' && $file->isPage();
225224

226225
// Hide the file name from being displayed until we can figure out a better
@@ -231,45 +230,14 @@ function template_preprocess_file(&$variables) {
231230
$variables['title_attributes_array']['class'][] = 'element-invisible';
232231
}
233232

234-
// Flatten the file object's member fields.
235-
$variables = array_merge((array) $file, $variables);
236-
237233
// Helpful $content variable for templates.
238234
$variables += array('content' => array());
239235
foreach (\Drupal\Core\Render\Element::children($variables['elements']) as $key) {
240236
$variables['content'][$key] = $variables['elements'][$key];
241237
}
242238

243-
// Make the field variables available with the appropriate language.
244-
//field_attach_preprocess('file', $file, $variables['content'], $variables);
245-
246239
// Attach the file object to the content element.
247240
$variables['content']['file']['#file'] = $file;
248-
249-
// Display post information only on certain file types.
250-
//if (variable_get('file_submitted_' . $file->type, FALSE)) { @TODO: What todo with this?
251-
if (FALSE) {
252-
$variables['display_submitted'] = TRUE;
253-
$variables['submitted'] = t('Uploaded by @username on @datetime', array('@username' => $variables['name'], '@datetime' => $variables['date']));
254-
$variables['user_picture'] = theme_get_setting('toggle_file_user_picture') ? theme('user_picture', array('account' => $account)) : '';
255-
}
256-
else {
257-
$variables['display_submitted'] = FALSE;
258-
$variables['submitted'] = '';
259-
$variables['user_picture'] = '';
260-
}
261-
262-
// Gather file classes.
263-
$variables['classes_array'][] = Html::getClass('file-' . $file->bundle());
264-
$variables['classes_array'][] = Html::getClass('file-' . $file->getMimeType());
265-
if (!$file->isPermanent()) {
266-
$variables['classes_array'][] = 'file-temporary';
267-
}
268-
269-
// Change the 'file-entity' class into 'file'
270-
if ($variables['classes_array'][0] == 'file-entity') {
271-
$variables['classes_array'][0] = 'file';
272-
}
273241
}
274242

275243
/**

src/Entity/FileEntity.php

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -325,10 +325,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
325325
))
326326
->setDisplayConfigurable('view', TRUE);
327327

328-
$fields['uid']->setDisplayOptions('view', array(
329-
'type' => 'uri_link',
330-
'weight' => 1,
331-
))
328+
$fields['uid']
329+
->setDisplayOptions('view', array(
330+
'type' => 'uri_link',
331+
'weight' => 1,
332+
))
333+
->setDisplayConfigurable('view', TRUE)
332334
->setDisplayOptions('form', array(
333335
'type' => 'entity_reference_autocomplete',
334336
'weight' => -1,
@@ -340,14 +342,18 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
340342
)
341343
));
342344

343-
$fields['filemime']->setDisplayOptions('view', array(
344-
'type' => 'string',
345-
'weight' => 2,
346-
));
347-
$fields['filesize']->setDisplayOptions('view', array(
348-
'type' => 'file_size',
349-
'weight' => 3,
350-
));
345+
$fields['filemime']
346+
->setDisplayOptions('view', array(
347+
'type' => 'string',
348+
'weight' => 2,
349+
))
350+
->setDisplayConfigurable('view', TRUE);
351+
$fields['filesize']
352+
->setDisplayOptions('view', array(
353+
'type' => 'file_size',
354+
'weight' => 3,
355+
))
356+
->setDisplayConfigurable('view', TRUE);
351357

352358
return $fields;
353359
}

0 commit comments

Comments
 (0)