This repository has been archived by the owner on Jan 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
file_entity.install
126 lines (112 loc) · 3.59 KB
/
file_entity.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
/**
* @file
* Install, update and uninstall functions for the file_entity module.
*/
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\views\Entity\View;
/**
* Implements hook_schema().
*/
function file_entity_schema() {
$schema['file_metadata'] = array(
'description' => 'Cache images dimensions.',
'fields' => array(
'fid' => array(
'description' => 'The {file_managed}.fid of the metadata.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
),
'name' => array(
'description' => "The name of the metadata (e.g. 'width').",
'type' => 'varchar',
'length' => '128',
'not null' => TRUE,
),
'value' => array(
'description' => "The value of the metadata (e.g. '200px').",
'type' => 'blob',
'not null' => FALSE,
'size' => 'big',
'serialize' => TRUE,
),
),
'primary key' => array('fid', 'name'),
'foreign keys' => array(
'file_managed' => array(
'table' => 'file_managed',
'columns' => array('fid' => 'fid'),
),
),
);
return $schema;
}
/**
* Implements hook_install().
*/
function file_entity_install() {
$type_storage_definition = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions('file')['type'];
\Drupal::entityDefinitionUpdateManager()->installFieldStorageDefinition('type', 'file', 'file_entity', $type_storage_definition);
// Set permissions.
$roles = user_roles();
foreach ($roles as $rid => $role) {
user_role_grant_permissions($rid, array('view files'));
}
// Classify existing files according to the currently defined file types.
// Queue all files to be classified during cron runs using the Queue API.
$queue = \Drupal::queue('file_entity_type_determine');
$ids = \Drupal::entityQuery('file')
->execute();
foreach ($ids as $id) {
$queue->createItem($id);
}
// Warn users that existing files will not have a file type until the queue
// has been processed.
if ($queue->numberOfItems()) {
drupal_set_message(t('Existing files must be classified according to the currently defined file types. These files have been queued for processing and will have their file type determined during cron runs.'));
}
// Disable the core files view.
if ($view = View::load('files')) {
$view->set('status', FALSE);
$view->save();
}
}
/**
* Implements hook_uninstall().
*/
function file_entity_uninstall() {
// Remove the added column to the core {file_managed} table.
db_drop_field('file_managed', 'type');
}
/**
* Update existing bogus entries for File types Document.
*/
function file_entity_update_8002(&$sandbox) {
foreach (\Drupal::configFactory()->listAll('file_entity') as $config_name) {
$config = \Drupal::configFactory()->getEditable($config_name);
$config->set('mimetypes', str_replace("\r", "", $config->get('mimetypes')));
$config->save();
}
}
/**
* Update entity form displays to use the new entity browser widget.
*/
function file_entity_update_8003() {
/** @var \Drupal\Core\Entity\Entity\EntityFormDisplay $form_display */
foreach (EntityFormDisplay::loadMultiple() as $form_display) {
$components = $form_display->getComponents();
$changed = FALSE;
foreach ($components as $name => $options) {
if (isset($options['type']) && $options['type'] == 'file_entity_browser') {
$options['type'] = 'entity_browser_file';
$form_display->setComponent($name, $options);
$changed = TRUE;
}
}
if ($changed) {
$form_display->save();
}
}
}