image.install 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the image module.
  5. */
  6. use Drupal\Core\File\Exception\FileException;
  7. use Drupal\Core\File\FileSystemInterface;
  8. /**
  9. * Implements hook_install().
  10. */
  11. function image_install() {
  12. // Create the styles directory and ensure it's writable.
  13. $directory = \Drupal::config('system.file')->get('default_scheme') . '://styles';
  14. \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
  15. }
  16. /**
  17. * Implements hook_uninstall().
  18. */
  19. function image_uninstall() {
  20. // Remove the styles directory and generated images.
  21. /** @var \Drupal\Core\File\FileSystemInterface $file_system */
  22. $file_system = \Drupal::service('file_system');
  23. try {
  24. $file_system->deleteRecursive(\Drupal::config('system.file')->get('default_scheme') . '://styles');
  25. }
  26. catch (FileException $e) {
  27. // Ignore failed deletes.
  28. }
  29. }
  30. /**
  31. * Implements hook_requirements() to check the PHP GD Library.
  32. *
  33. * @param $phase
  34. */
  35. function image_requirements($phase) {
  36. if ($phase != 'runtime') {
  37. return [];
  38. }
  39. $toolkit = \Drupal::service('image.toolkit.manager')->getDefaultToolkit();
  40. if ($toolkit) {
  41. $plugin_definition = $toolkit->getPluginDefinition();
  42. $requirements = [
  43. 'image.toolkit' => [
  44. 'title' => t('Image toolkit'),
  45. 'value' => $toolkit->getPluginId(),
  46. 'description' => $plugin_definition['title'],
  47. ],
  48. ];
  49. foreach ($toolkit->getRequirements() as $key => $requirement) {
  50. $namespaced_key = 'image.toolkit.' . $toolkit->getPluginId() . '.' . $key;
  51. $requirements[$namespaced_key] = $requirement;
  52. }
  53. }
  54. else {
  55. $requirements = [
  56. 'image.toolkit' => [
  57. 'title' => t('Image toolkit'),
  58. 'value' => t('None'),
  59. 'description' => t("No image toolkit is configured on the site. Check PHP installed extensions or add a contributed toolkit that doesn't require a PHP extension. Make sure that at least one valid image toolkit is enabled."),
  60. 'severity' => REQUIREMENT_ERROR,
  61. ],
  62. ];
  63. }
  64. return $requirements;
  65. }
  66. /**
  67. * Flush caches as we changed field formatter metadata.
  68. */
  69. function image_update_8201() {
  70. // Empty update to trigger a cache flush.
  71. // Use hook_post_update_NAME() instead to clear the cache. The use of
  72. // hook_update_N() to clear the cache has been deprecated see
  73. // https://www.drupal.org/node/2960601 for more details.
  74. }