You are here

function panopoly_wysiwyg_update_8207 in Panopoly WYSIWYG 8.2

Installs and configures editor_advanced_link and linkit.

File

./panopoly_wysiwyg.install, line 237
Install hooks for Panopoly WYSIWYG.

Code

function panopoly_wysiwyg_update_8207() {

  /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
  $module_installer = \Drupal::service('module_installer');
  $module_installer
    ->install([
    'editor_advanced_link',
    'linkit',
  ]);

  // Tweak allowed HTML config.
  $format = FilterFormat::load('panopoly_wysiwyg_basic');
  $filter = $format
    ->filters('filter_html');
  $val = $filter->settings['allowed_html'];
  $new_attrs = [
    'data-entity-type',
    'data-entity-uuid',
    'title',
    'target',
    'rel',
  ];
  if (preg_match('/<a(| [^>]+)>/', $val, $matches)) {
    $attrs = array_filter(explode(' ', $matches[1]));
    $attrs = array_unique(array_merge($attrs, $new_attrs));
    $attrs = ' ' . implode(' ', $attrs);

    // Only replace if the original tag had attributes defined.
    if ($matches[1]) {
      $new_tag = str_replace($matches[1], $attrs, $matches[0]);
      $val = str_replace($matches[0], $new_tag, $val);
    }
  }
  else {

    // No <a> tag set, append to the end.
    $val = trim($val) . ' <a ' . implode(' ', $new_attrs) . '>';
  }
  if ($val != $filter->settings['allowed_html']) {
    $filter->settings['allowed_html'] = $val;
    $format
      ->save();
  }

  // Add linkit to text formats.
  _panopoly_wysiwyg_set_text_format_linkit('panopoly_wysiwyg_basic');
  _panopoly_wysiwyg_set_text_format_linkit('panopoly_wysiwyg_full');

  // Add linkit to editors.
  _panopoly_wysiwyg_set_editor_linkit('panopoly_wysiwyg_basic');
  _panopoly_wysiwyg_set_editor_linkit('panopoly_wysiwyg_full');
}