summaryrefslogtreecommitdiffstats
path: root/linkit.install
blob: bf4dc35bed5f136b7fcdb044dd96bd8b4ce38010 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php

/**
 * @file
 * Contains install and update functions for Linkit.
 */

/**
 * Implements hook_uninstall().
 */
function linkit_uninstall() {
  $config_factory = \Drupal::configFactory();

  // Remove the image styles that Linkit has installed.
  $config_factory->getEditable('image.style.linkit_result_thumbnail')->delete();
}

/**
 * Updates Linkit from 4 to 5.
 *
 * Removes linkit profile attributes.
 * Replaces the linkit ckeditor plugin with drupallink.
 * Enables the linkit filter on text formats where linkit is used.
 * Adds 'data-entity-type' and 'data-entity-uuid' attributes to the <a> tag
 * if filter_html is activated.
 */
function linkit_update_8500() {
  $config_factory = \Drupal::configFactory();

  // Iterate on all profile configuration entities.
  foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) {
    $profile = $config_factory->getEditable($id);
    // Remove all attributes.
    $profile->clear('attributes')->save(TRUE);
  }

  // An array that will be filled with text format ids that we should enable the
  // new linkit_filter for.
  $linkit_enabled_formats = [];

  // Iterate on all editor configuration entities.
  foreach ($config_factory->listAll('editor.editor.') as $id) {
    $editor_config = $config_factory->getEditable($id);

    // Save the old settings.
    $old_linkit_settings = $editor_config->get('settings.plugins.linkit');

    $drupal_link_in_use = FALSE;
    $old_linkit_button_path = NULL;

    // If the editor has old linkit settings, perform update tasks.
    if (!is_null($old_linkit_settings)) {
      // Remove the old linkit settings.
      $editor_config->clear('settings.plugins.linkit');
      $editor_config->save(TRUE);

      // Add the format id that the editor is using.
      $linkit_enabled_formats[] = $editor_config->get('format');

      // Remove the old linkit plugin from the toolbar, and check if DrupalLink
      // is present in the toolbar as linkit now extends this plugin.
      $toolbar_rows = $editor_config->get('settings.toolbar.rows');
      foreach ($toolbar_rows as $row_index => $row) {
        foreach ($row as $button_group_index => $button_group) {
          foreach ($button_group['items'] as $item__name) {
            if ($item__name === 'Linkit') {
              $old_linkit_button_path = 'settings.toolbar.rows.' . $row_index . '.' . $button_group_index . '.items';
            }

            if ($item__name === 'DrupalLink') {
              $drupal_link_in_use = TRUE;
            }
          }
        }
      }

      if ($old_linkit_button_path) {
        $buttons = $editor_config->get($old_linkit_button_path);
        $index = array_search('Linkit', $buttons);
        // If the DrupalLink plugin is not present in the toolbar, lets add it
        // in the same button group as the old linkit plugin was in.
        if (!$drupal_link_in_use) {
          $buttons[$index] = 'DrupalLink';
        }
        else {
          unset($buttons[$index]);
        }
        $editor_config->set($old_linkit_button_path, array_values($buttons));
        $editor_config->save(TRUE);
      }

      // Set the linkit settings to the DrupalLink.
      $drupal_link_settings = [
        'linkit_enabled' => TRUE,
        'linkit_profile' => $old_linkit_settings['linkit_profile'],
      ];
      $editor_config->set('settings.plugins.drupallink', $drupal_link_settings);
      $editor_config->save(TRUE);
    }
  }

  // Enable the linkit_filter for all formats that is used with linkit.
  foreach ($linkit_enabled_formats as $filter_id) {
    $config = $config_factory->getEditable('filter.format.' . $filter_id);
    $filter_html_weight = $config->get('filters.filter_html.weight');

    $linkit_filter = [
      'id' => 'linkit',
      'provider' => 'linkit',
      'status' => TRUE,
      'weight' => $filter_html_weight ? ($filter_html_weight - 1) : -15,
      'settings' => [
        'title' => FALSE,
      ],
    ];
    $config->set('filters.linkit', $linkit_filter);

    $allowed_html = $config->get('filters.filter_html.settings.allowed_html');
    if (!empty($allowed_html)) {
      preg_match_all('/<([\w]+)[^>]*>/', $allowed_html, $out);
      $current_mapping = array_combine($out[1], $out[0]);

      if (isset($current_mapping['a'])) {
        $allowed_html = str_replace($current_mapping['a'], str_replace('>', ' data-entity-type data-entity-uuid>', $current_mapping['a']), $allowed_html);
      }
      else {
        $allowed_html .= ' <a href hreflang data-entity-type data-entity-uuid>';
      }
      $config->set('filters.filter_html.settings.allowed_html', $allowed_html);
    }

    $config->save(TRUE);
  }
}

/**
 * Prepare anchor attributes for substitution plugins.
 */
function linkit_update_8501() {
  $config_factory = \Drupal::configFactory();

  // Update all filter formats that allow the data-entity-uuid attribute to also
  // allow the data-entity-substitution attribute.
  foreach ($config_factory->listAll('filter.format.') as $id) {
    $filter = $config_factory->getEditable($id);
    if ($allowed_html = $filter->get('filters.filter_html.settings.allowed_html')) {
      $allowed_html = str_replace('data-entity-uuid', 'data-entity-uuid data-entity-substitution', $allowed_html);
      $filter->set('filters.filter_html.settings.allowed_html', $allowed_html);
      $filter->save(TRUE);
    }
  }

  // Update all "file" matchers to the "file" substitution plugin, to maintain
  // existing behavior out of the box.
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) {
    $profile = $config_factory->getEditable($id);
    foreach ($profile->get('matchers') as $key => $matcher) {
      $settings = $profile->get('matchers.' . $key . '.settings');
      $settings['substitution_type'] = $matcher['id'] === 'entity:file' ? 'file' : 'canonical';
      $profile->set('matchers.' . $key . '.settings', $settings);
    }
    $profile->save(TRUE);
  }
}

/**
 * Rename profile property 'result_description' to 'metadata'.
 */
function linkit_update_8502() {
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) {
    $profile = $config_factory->getEditable($id);

    foreach ($profile->get('matchers') as $key => $matcher) {
      $old_value = $profile->get('matchers.' . $key . '.settings.result_description');
      $profile->set('matchers.' . $key . '.settings.metadata', $old_value);
      $profile->clear('matchers.' . $key . '.settings.result_description');
    }

    $profile->save(TRUE);
  }
}

/**
 * Remove imce integration settings.
 */
function linkit_update_8503() {
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory->listAll('linkit.linkit_profile.') as $id) {
    $profile = $config_factory->getEditable($id);
    $profile->clear('third_party_settings.imce');
    $profile->save(TRUE);
  }
}