jsonapi.install 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * @file
  4. * Module install file.
  5. */
  6. use Drupal\Core\Url;
  7. /**
  8. * Implements hook_install().
  9. */
  10. function jsonapi_install() {
  11. $module_handler = \Drupal::moduleHandler();
  12. $potential_conflicts = [
  13. 'content_translation',
  14. 'config_translation',
  15. 'language',
  16. ];
  17. $should_warn = array_reduce($potential_conflicts, function ($should_warn, $module_name) use ($module_handler) {
  18. return $should_warn ?: $module_handler->moduleExists($module_name);
  19. }, FALSE);
  20. if ($should_warn) {
  21. \Drupal::messenger()->addWarning(t('Some multilingual features currently do not work well with JSON:API. See the <a href=":jsonapi-docs">JSON:API multilingual support documentation</a> for more information on the current status of multilingual support.', [
  22. ':jsonapi-docs' => 'https://www.drupal.org/docs/8/modules/jsonapi/translations',
  23. ]));
  24. }
  25. }
  26. /**
  27. * Implements hook_requirements().
  28. */
  29. function jsonapi_requirements($phase) {
  30. $requirements = [];
  31. if ($phase === 'runtime') {
  32. $module_handler = \Drupal::moduleHandler();
  33. $potential_conflicts = [
  34. 'content_translation',
  35. 'config_translation',
  36. 'language',
  37. ];
  38. $should_warn = array_reduce($potential_conflicts, function ($should_warn, $module_name) use ($module_handler) {
  39. return $should_warn ?: $module_handler->moduleExists($module_name);
  40. }, FALSE);
  41. if ($should_warn) {
  42. $requirements['jsonapi_multilingual_support'] = [
  43. 'title' => t('JSON:API multilingual support'),
  44. 'value' => t('Limited'),
  45. 'severity' => REQUIREMENT_INFO,
  46. 'description' => t('Some multilingual features currently do not work well with JSON:API. See the <a href=":jsonapi-docs">JSON:API multilingual support documentation</a> for more information on the current status of multilingual support.', [
  47. ':jsonapi-docs' => 'https://www.drupal.org/docs/8/modules/jsonapi/translations',
  48. ]),
  49. ];
  50. }
  51. $requirements['jsonapi_revision_support'] = [
  52. 'title' => t('JSON:API revision support'),
  53. 'value' => t('Limited'),
  54. 'severity' => REQUIREMENT_INFO,
  55. 'description' => t('Revision support is currently read-only and only for the "Content" and "Media" entity types in JSON:API. See the <a href=":jsonapi-docs">JSON:API revision support documentation</a> for more information on the current status of revision support.', [
  56. ':jsonapi-docs' => 'https://www.drupal.org/docs/8/modules/jsonapi/revisions',
  57. ]),
  58. ];
  59. $requirements['jsonapi_read_only_mode'] = [
  60. 'title' => t('JSON:API allowed operations'),
  61. 'value' => t('Read-only'),
  62. 'severity' => REQUIREMENT_INFO,
  63. ];
  64. if (!\Drupal::configFactory()->get('jsonapi.settings')->get('read_only')) {
  65. $requirements['jsonapi_read_only_mode']['value'] = t('All (create, read, update, delete)');
  66. $requirements['jsonapi_read_only_mode']['description'] = t('It is recommended to <a href=":configure-url">configure</a> JSON:API to only accept all operations if the site requires it. <a href=":docs">Learn more about securing your site with JSON:API.</a>', [
  67. ':docs' => 'https://www.drupal.org/docs/8/modules/jsonapi/security-considerations',
  68. ':configure-url' => Url::fromRoute('jsonapi.settings')->toString(),
  69. ]);
  70. }
  71. }
  72. return $requirements;
  73. }
  74. /**
  75. * Enable BC: default the new read-only mode to "off" on existing sites.
  76. */
  77. function jsonapi_update_8701() {
  78. $config_factory = \Drupal::configFactory();
  79. $jsonapi_settings = $config_factory->getEditable('jsonapi.settings');
  80. $jsonapi_settings->set('read_only', FALSE)
  81. ->save(TRUE);
  82. }