addtoany.install 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the AddToAny module.
  5. */
  6. use Drupal\Core\Entity\Entity\EntityViewDisplay;
  7. use Drupal\node\Entity\NodeType;
  8. /**
  9. * Implements hook_install().
  10. */
  11. function addtoany_install() {
  12. // Build a structured list of node bundles and view modes that have AddToAny
  13. // enabled by default.
  14. $node_enable_by_default = [
  15. 'article' => ['default', 'teaser'],
  16. 'page' => ['default', 'teaser'],
  17. ];
  18. // Get the config entity storage handler.
  19. $storage = \Drupal::entityTypeManager()->getStorage('entity_view_display');
  20. foreach ($node_enable_by_default as $bundle => $viewmodes) {
  21. // Set a default weight per node type.
  22. $display_weight = ($bundle == 'article') ? 5 : 101;
  23. foreach ($viewmodes as $viewmode) {
  24. // Get the config entity for this bundle and view mode.
  25. $display = $storage->load('node.' . $bundle . '.' . $viewmode);
  26. // Enable the AddToAny extra field and save the config.
  27. if (!empty($display)) {
  28. $display->setComponent('addtoany', ['weight' => $display_weight])->save();
  29. }
  30. }
  31. }
  32. }
  33. /**
  34. * Migrate global placement settings to settings per view mode.
  35. */
  36. function addtoany_update_8101(&$sandbox) {
  37. $config_factory = \Drupal::configFactory();
  38. // Gather the settings we need to migrate.
  39. $addtoany_settings = $config_factory->getEditable('addtoany.settings');
  40. $node_types = $addtoany_settings->getOriginal('nodetypes');
  41. $display_in_teasers = $addtoany_settings->getOriginal('display_in_teasers');
  42. $display_in_nodecont = $addtoany_settings->getOriginal('display_in_nodecont');
  43. $display_weight = $addtoany_settings->getOriginal('display_weight');
  44. $new_display_weight = $display_weight;
  45. // The entity display repository stores which view modes are enabled.
  46. $entity_display_repository = \Drupal::getContainer()->get('entity_display.repository');
  47. // Check if AddToAny is currently shown on at least one node type.
  48. if (!empty($node_types) && $display_in_nodecont) {
  49. // Loop over the AddToAny-enabled node types.
  50. foreach ($node_types as $bundle) {
  51. // Get enabled view modes.
  52. $view_modes = $entity_display_repository->getViewModeOptionsByBundle('node', $bundle);
  53. // If the teaser view mode is enabled but the old settings excluded it,
  54. // take it out.
  55. if (!empty($view_modes['teaser']) && $display_in_teasers == FALSE) {
  56. unset($view_modes['teaser']);
  57. }
  58. // Check if the old default weight was set.
  59. if ($display_weight == 10) {
  60. // Set the new default weight per node type.
  61. $new_display_weight = ($bundle == 'article') ? 5 : 101;
  62. }
  63. // Loop over the view modes and apply new settings to match the old.
  64. foreach ($view_modes as $view_mode => $view_mode_info) {
  65. $display = EntityViewDisplay::load('node.' . $bundle . '.' . $view_mode);
  66. $display->setComponent('addtoany', ['weight' => $new_display_weight])->save();
  67. }
  68. }
  69. }
  70. // Convert Display Suite fields to regular "extra fields".
  71. if (\Drupal::moduleHandler()->moduleExists('ds')) {
  72. // The DsField implementation only supported node entities.
  73. $all_node_types = NodeType::loadMultiple();
  74. foreach ($all_node_types as $node_type) {
  75. // Get enabled view modes.
  76. $view_modes = $entity_display_repository->getViewModeOptionsByBundle('node', $node_type->id());
  77. // Loop over the view modes and apply new settings to match the old.
  78. foreach ($view_modes as $view_mode => $view_mode_info) {
  79. // Get the entity view display config entity.
  80. $display = EntityViewDisplay::load('node.' . $node_type->id() . '.' . $view_mode);
  81. // Check if this view mode uses Display Suite fields and if the Display
  82. // Suite AddToAny field ("addtoany_field") was enabled.
  83. $ds_fields = $display->getThirdPartySetting('ds', 'fields');
  84. if (!empty($ds_fields) && isset($ds_fields['addtoany_field'])) {
  85. // Remove the DS field, add the extra field.
  86. unset($ds_fields['addtoany_field']);
  87. $display->setComponent('addtoany');
  88. // Keep the rest of the DS fields.
  89. $display->setThirdPartySetting('ds', 'fields', $ds_fields);
  90. // Find where the DS field was placed and put the AddToAny extra field
  91. // in its place.
  92. $ds_regions = $display->getThirdPartySetting('ds', 'regions');
  93. foreach ($ds_regions as $label => $ds_region_fields) {
  94. $key = array_search('addtoany_field', $ds_region_fields);
  95. if ($key !== FALSE) {
  96. $ds_regions[$label][$key] = 'addtoany';
  97. }
  98. }
  99. // Apply the altered region settings.
  100. $display->setThirdPartySetting('ds', 'regions', $ds_regions);
  101. // Save the view mode.
  102. $display->save();
  103. }
  104. }
  105. }
  106. }
  107. // Finally, clean up no longer used settings.
  108. $addtoany_settings->clear('nodetypes')
  109. ->clear('display_in_teasers')
  110. ->clear('display_in_nodecont')
  111. ->clear('display_weight')
  112. ->save();
  113. }