Theme.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Drupal\Core\Updater;
  3. use Drupal\Core\Url;
  4. /**
  5. * Defines a class for updating themes using
  6. * Drupal\Core\FileTransfer\FileTransfer classes via authorize.php.
  7. */
  8. class Theme extends Updater implements UpdaterInterface {
  9. /**
  10. * Returns the directory where a theme should be installed.
  11. *
  12. * If the theme is already installed, drupal_get_path() will return a valid
  13. * path and we should install it there. If we're installing a new theme, we
  14. * always want it to go into /themes, since that's where all the
  15. * documentation recommends users install their themes, and there's no way
  16. * that can conflict on a multi-site installation, since the Update manager
  17. * won't let you install a new theme if it's already found on your system,
  18. * and if there was a copy in the top-level we'd see it.
  19. *
  20. * @return string
  21. * The absolute path of the directory.
  22. */
  23. public function getInstallDirectory() {
  24. if ($this->isInstalled() && ($relative_path = drupal_get_path('theme', $this->name))) {
  25. // The return value of drupal_get_path() is always relative to the site,
  26. // so prepend DRUPAL_ROOT.
  27. return DRUPAL_ROOT . '/' . dirname($relative_path);
  28. }
  29. else {
  30. // When installing a new theme, prepend the requested root directory.
  31. return $this->root . '/' . $this->getRootDirectoryRelativePath();
  32. }
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. public static function getRootDirectoryRelativePath() {
  38. return 'themes';
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function isInstalled() {
  44. // Check if the theme exists in the file system, regardless of whether it
  45. // is enabled or not.
  46. $themes = \Drupal::state()->get('system.theme.files', []);
  47. return isset($themes[$this->name]);
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public static function canUpdateDirectory($directory) {
  53. $info = static::getExtensionInfo($directory);
  54. return (isset($info['type']) && $info['type'] == 'theme');
  55. }
  56. /**
  57. * Determines whether this class can update the specified project.
  58. *
  59. * @param string $project_name
  60. * The project to check.
  61. *
  62. * @return bool
  63. */
  64. public static function canUpdate($project_name) {
  65. return (bool) drupal_get_path('theme', $project_name);
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function postInstall() {
  71. // Update the theme info.
  72. clearstatcache();
  73. \Drupal::service('theme_handler')->rebuildThemeData();
  74. }
  75. /**
  76. * {@inheritdoc}
  77. */
  78. public function postInstallTasks() {
  79. // Since this is being called outside of the primary front controller,
  80. // the base_url needs to be set explicitly to ensure that links are
  81. // relative to the site root.
  82. // @todo Simplify with https://www.drupal.org/node/2548095
  83. $default_options = [
  84. '#type' => 'link',
  85. '#options' => [
  86. 'absolute' => TRUE,
  87. 'base_url' => $GLOBALS['base_url'],
  88. ],
  89. ];
  90. return [
  91. $default_options + [
  92. '#url' => Url::fromRoute('system.themes_page'),
  93. '#title' => t('Install newly added themes'),
  94. ],
  95. $default_options + [
  96. '#url' => Url::fromRoute('system.admin'),
  97. '#title' => t('Administration pages'),
  98. ],
  99. ];
  100. }
  101. }