linkit.install 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. /**
  3. * @file
  4. * Contains install and update functions for Linkit.
  5. */
  6. /**
  7. * Implements hook_uninstall().
  8. */
  9. function linkit_uninstall() {
  10. $config_factory = \Drupal::configFactory();
  11. // Remove the image styles that Linkit has installed.
  12. $config_factory->getEditable('image.style.linkit_result_thumbnail')->delete();
  13. }
  14. /**
  15. * Updates Linkit from 4 to 5.
  16. *
  17. * Removes linkit profile attributes.
  18. * Replaces the linkit ckeditor plugin with drupallink.
  19. * Enables the linkit filter on text formats where linkit is used.
  20. * Adds 'data-entity-type' and 'data-entity-uuid' attributes to the <a> tag
  21. * if filter_html is activated.
  22. */
  23. function linkit_update_8500() {
  24. $config_factory = \Drupal::configFactory();
  25. // Iterate on all profile configuration entities.
  26. foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) {
  27. $profile = $config_factory->getEditable($id);
  28. // Remove all attributes.
  29. $profile->clear('attributes')->save(TRUE);
  30. }
  31. // An array that will be filled with text format ids that we should enable the
  32. // new linkit_filter for.
  33. $linkit_enabled_formats = [];
  34. // Iterate on all editor configuration entities.
  35. foreach ($config_factory->listAll('editor.editor.') as $id) {
  36. $editor_config = $config_factory->getEditable($id);
  37. // Save the old settings.
  38. $old_linkit_settings = $editor_config->get('settings.plugins.linkit');
  39. $drupal_link_in_use = FALSE;
  40. $old_linkit_button_path = NULL;
  41. // If the editor has old linkit settings, perform update tasks.
  42. if (!is_null($old_linkit_settings)) {
  43. // Remove the old linkit settings.
  44. $editor_config->clear('settings.plugins.linkit');
  45. $editor_config->save(TRUE);
  46. // Add the format id that the editor is using.
  47. $linkit_enabled_formats[] = $editor_config->get('format');
  48. // Remove the old linkit plugin from the toolbar, and check if DrupalLink
  49. // is present in the toolbar as linkit now extends this plugin.
  50. $toolbar_rows = $editor_config->get('settings.toolbar.rows');
  51. foreach ($toolbar_rows as $row_index => $row) {
  52. foreach ($row as $button_group_index => $button_group) {
  53. foreach ($button_group['items'] as $item__name) {
  54. if ($item__name === 'Linkit') {
  55. $old_linkit_button_path = 'settings.toolbar.rows.' . $row_index . '.' . $button_group_index . '.items';
  56. }
  57. if ($item__name === 'DrupalLink') {
  58. $drupal_link_in_use = TRUE;
  59. }
  60. }
  61. }
  62. }
  63. if ($old_linkit_button_path) {
  64. $buttons = $editor_config->get($old_linkit_button_path);
  65. $index = array_search('Linkit', $buttons);
  66. // If the DrupalLink plugin is not present in the toolbar, lets add it
  67. // in the same button group as the old linkit plugin was in.
  68. if (!$drupal_link_in_use) {
  69. $buttons[$index] = 'DrupalLink';
  70. }
  71. else {
  72. unset($buttons[$index]);
  73. }
  74. $editor_config->set($old_linkit_button_path, array_values($buttons));
  75. $editor_config->save(TRUE);
  76. }
  77. // Set the linkit settings to the DrupalLink.
  78. $drupal_link_settings = [
  79. 'linkit_enabled' => TRUE,
  80. 'linkit_profile' => $old_linkit_settings['linkit_profile'],
  81. ];
  82. $editor_config->set('settings.plugins.drupallink', $drupal_link_settings);
  83. $editor_config->save(TRUE);
  84. }
  85. }
  86. // Enable the linkit_filter for all formats that is used with linkit.
  87. foreach ($linkit_enabled_formats as $filter_id) {
  88. $config = $config_factory->getEditable('filter.format.' . $filter_id);
  89. $filter_html_weight = $config->get('filters.filter_html.weight');
  90. $linkit_filter = [
  91. 'id' => 'linkit',
  92. 'provider' => 'linkit',
  93. 'status' => TRUE,
  94. 'weight' => $filter_html_weight ? ($filter_html_weight - 1) : -15,
  95. 'settings' => [
  96. 'title' => FALSE,
  97. ],
  98. ];
  99. $config->set('filters.linkit', $linkit_filter);
  100. $allowed_html = $config->get('filters.filter_html.settings.allowed_html');
  101. if (!empty($allowed_html)) {
  102. preg_match_all('/<([\w]+)[^>]*>/', $allowed_html, $out);
  103. $current_mapping = array_combine($out[1], $out[0]);
  104. if (isset($current_mapping['a'])) {
  105. $allowed_html = str_replace($current_mapping['a'], str_replace('>', ' data-entity-type data-entity-uuid>', $current_mapping['a']), $allowed_html);
  106. }
  107. else {
  108. $allowed_html .= ' <a href hreflang data-entity-type data-entity-uuid>';
  109. }
  110. $config->set('filters.filter_html.settings.allowed_html', $allowed_html);
  111. }
  112. $config->save(TRUE);
  113. }
  114. }
  115. /**
  116. * Prepare anchor attributes for substitution plugins.
  117. */
  118. function linkit_update_8501() {
  119. $config_factory = \Drupal::configFactory();
  120. // Update all filter formats that allow the data-entity-uuid attribute to also
  121. // allow the data-entity-substitution attribute.
  122. foreach ($config_factory->listAll('filter.format.') as $id) {
  123. $filter = $config_factory->getEditable($id);
  124. if ($allowed_html = $filter->get('filters.filter_html.settings.allowed_html')) {
  125. $allowed_html = str_replace('data-entity-uuid', 'data-entity-uuid data-entity-substitution', $allowed_html);
  126. $filter->set('filters.filter_html.settings.allowed_html', $allowed_html);
  127. $filter->save(TRUE);
  128. }
  129. }
  130. // Update all "file" matchers to the "file" substitution plugin, to maintain
  131. // existing behavior out of the box.
  132. $config_factory = \Drupal::configFactory();
  133. foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) {
  134. $profile = $config_factory->getEditable($id);
  135. foreach ($profile->get('matchers') as $key => $matcher) {
  136. $settings = $profile->get('matchers.' . $key . '.settings');
  137. $settings['substitution_type'] = $matcher['id'] === 'entity:file' ? 'file' : 'canonical';
  138. $profile->set('matchers.' . $key . '.settings', $settings);
  139. }
  140. $profile->save(TRUE);
  141. }
  142. }
  143. /**
  144. * Rename profile property 'result_description' to 'metadata'.
  145. */
  146. function linkit_update_8502() {
  147. $config_factory = \Drupal::configFactory();
  148. foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) {
  149. $profile = $config_factory->getEditable($id);
  150. foreach ($profile->get('matchers') as $key => $matcher) {
  151. $old_value = $profile->get('matchers.' . $key . '.settings.result_description');
  152. $profile->set('matchers.' . $key . '.settings.metadata', $old_value);
  153. $profile->clear('matchers.' . $key . '.settings.result_description');
  154. }
  155. $profile->save(TRUE);
  156. }
  157. }
  158. /**
  159. * Remove imce integration settings.
  160. */
  161. function linkit_update_8503() {
  162. $config_factory = \Drupal::configFactory();
  163. foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) {
  164. $profile = $config_factory->getEditable($id);
  165. $profile->clear('third_party_settings.imce');
  166. $profile->save(TRUE);
  167. }
  168. }