1adfc3c5dd284bb36c8bb7189264470ae2ac2c5e
[yaffs-website] / web / modules / contrib / media_entity / media_entity.install
1 <?php
2
3 /**
4  * @file
5  * Install, uninstall and update hooks for Media entity module.
6  */
7
8 /**
9  * Checks if required version of the Entity API is installed.
10  *
11  * @return bool
12  *   TRUE if dependency is met and FALSE if not.
13  */
14 function _media_entity_check_entity_version() {
15   if (\Drupal::moduleHandler()->moduleExists('entity')) {
16     $info = system_get_info('module', 'entity');
17     if (version_compare($info['version'], '8.x-1.0-alpha3') >= 0) {
18       return TRUE;
19     }
20   }
21
22   return FALSE;
23 }
24
25 /**
26  * Implements hook_requirements().
27  */
28 function media_entity_requirements($phase) {
29   $requirements = [];
30   if ($phase == 'update' && !_media_entity_check_entity_version()) {
31     $requirements['entity'] = [
32       'title' => t('Media entity'),
33       'value' => t('Entity API missing'),
34       'description' => t(
35         '<a href=":url">Entity API >= 8.x-1.0-alpha3</a> module is now a dependency and needs to be installed before running updates.',
36         [':url' => 'https://www.drupal.org/project/entity']
37       ),
38       'severity' => REQUIREMENT_ERROR,
39     ];
40   }
41   if ($phase == 'install' && \Drupal::moduleHandler()->moduleExists('media')) {
42     $version = explode('.', \Drupal::VERSION);
43     if ($version[1] >= 4) {
44       $requirements['media_module_incompatibility'] = [
45         'title' => t('Media Entity'),
46         'value' => t('The Media Entity module is not compatible with media in core.'),
47         'description' => t('The 1.x branch of Media Entity cannot be used in conjunction with the media module in core. Please check the 2.x branch for an upgrade path.'),
48         'severity' => REQUIREMENT_ERROR,
49       ];
50     }
51   }
52
53   return $requirements;
54 }
55
56 /**
57  * Implements hook_install().
58  */
59 function media_entity_install() {
60   $source = drupal_get_path('module', 'media_entity') . '/images/icons';
61   $destination = \Drupal::config('media_entity.settings')->get('icon_base');
62   media_entity_copy_icons($source, $destination);
63 }
64
65 /**
66  * Remove "type" base field.
67  */
68 function media_entity_update_8001() {
69   $fields = \Drupal::database()->query('DESCRIBE {media_field_data}')->fetchCol();
70   if (in_array('type', $fields)) {
71     \Drupal::database()->update('media_field_data')
72       ->fields(['type' => NULL])
73       ->execute();
74   }
75
76   $manager = \Drupal::entityDefinitionUpdateManager();
77   if ($field = $manager->getFieldStorageDefinition('type', 'media')) {
78     $manager->uninstallFieldStorageDefinition($field);
79   }
80 }
81
82 /**
83  * Ensure media entity status value (defaulting to 1).
84  */
85 function media_entity_update_8002() {
86   // Ensure status values in 'media_field_data' table.
87   if (\Drupal::database()->schema()->tableExists('media_field_data')) {
88     \Drupal::database()
89       ->update('media_field_data')
90       ->fields(['status' => 1])
91       ->condition('status', NULL, 'IS NULL')
92       ->execute();
93   }
94
95   // Ensure status values in 'media_field_revision' table.
96   if (\Drupal::database()->schema()->tableExists('media_field_revision')) {
97     \Drupal::database()
98       ->update('media_field_revision')
99       ->fields(['status' => 1])
100       ->condition('status', NULL, 'IS NULL')
101       ->execute();
102   }
103
104   // Flush all caches.
105   drupal_flush_all_caches();
106 }
107
108 /**
109  * Ensure Entity API is installed.
110  */
111 function media_entity_update_8003() {
112   if (!_media_entity_check_entity_version()) {
113     throw new \Drupal\Core\Utility\UpdateException('Entity API >= 8.x-1.0-alpha3 (drupal.org/project/entity) module is now a dependency and needs to be installed before running updates.');
114   }
115 }