class FileStorageController extends EntityDatabaseStorageController {
protected function preSave(EntityInterface $entity) {
$entity->timestamp = REQUEST_TIME;
$entity->filesize = filesize($entity->uri);
if (!isset($entity->langcode)) {
$entity->langcode = LANGUAGE_NONE;
}
if (!isset($entity->type)) {
$entity->type = FILE_TYPE_NONE;
}
if ($entity->type === FILE_TYPE_NONE) {
$type = file_get_type($entity);
if (isset($type)) {
$entity->type = $type;
}
}
}
public function preDelete($entities) {
foreach ($entities as $entity) {
file_unmanaged_delete($entity->uri);
}
db_delete('file_usage')
->condition('fid', array_keys($entities), 'IN')
->execute();
}
public function buildContent(EntityInterface $file, $view_mode = 'full', $langcode = NULL) {
global $language_content;
$langcode = $langcode ? $langcode : $language_content->langcode;
$file->content = array();
$view_mode = key(entity_view_mode_prepare('file', array($file->fid => $file), $view_mode, $langcode));
field_attach_prepare_view('file', array($file->fid => $file), $view_mode, $langcode);
entity_prepare_view('file', array($file->fid => $file), $langcode);
$file->content['file'] = file_view_file($file, $view_mode, $langcode);
$file->content += field_attach_view('file', $file, $view_mode, $langcode);
$links = array();
$file->content['links'] = array(
'#theme' => 'links__file',
'#pre_render' => array('backdrop_pre_render_links'),
'#attributes' => array('class' => array('links', 'inline')),
);
$file->content['links']['file'] = array(
'#theme' => 'links__file__file',
'#links' => $links,
'#attributes' => array('class' => array('links', 'inline')),
);
module_invoke_all('file_view', $file, $view_mode, $langcode);
module_invoke_all('entity_view', $file, 'file', $view_mode, $langcode);
}
public function view($files, $view_mode = 'full', $langcode = NULL, $page = NULL) {
global $language_content;
$langcode = $langcode ? $langcode : $language_content->langcode;
$view = array();
foreach ($files as $file) {
$this->buildContent($file, $view_mode, $langcode);
$build = $file->content;
unset($file->content);
$build += array(
'#theme' => 'file_entity',
'#file' => $file,
'#view_mode' => $view_mode,
'#language' => $langcode,
'#page' => $page,
);
if (!empty($file->fid) && !($view_mode == 'full' && file_is_page($file))) {
$build['#contextual_links']['file'] = array('file', array($file->fid));
}
$type = 'file';
backdrop_alter(array('file_view', 'entity_view'), $build, $type);
$view[$type][$file->id()] = $build;
}
return $view;
}
}