You are here

function wysiwyg_template_editor_js_settings_alter in Wysiwyg API template plugin 8.2

Same name and namespace in other branches
  1. 3.0.x wysiwyg_template.module \wysiwyg_template_editor_js_settings_alter()

Implements hook_editor_js_settings_alter().

File

./wysiwyg_template.module, line 12

Code

function wysiwyg_template_editor_js_settings_alter(array &$settings) {

  // Change url to be node-type-specific if creating or editing a node.
  $route_match = Drupal::routeMatch();
  $node_type = NULL;
  if ($route_match
    ->getRouteName() === 'entity.node.edit_form') {
    $node_type = $route_match
      ->getParameter('node')
      ->getType();
  }
  else {
    if ($route_match
      ->getRouteName() === 'node.add') {
      $node_type = $route_match
        ->getParameter('node_type')
        ->get('type');
    }
  }
  if ($node_type !== NULL) {
    foreach ($settings['editor']['formats'] as $format => $config) {
      if (isset($config['editorSettings']['templates_files'])) {
        $url = Url::fromRoute('wysiwyg_template.list_js.type', [
          'node_type' => $node_type,
        ]);
        $settings['editor']['formats'][$format]['editorSettings']['templates_files'] = [
          $url
            ->toString(),
        ];
      }
    }
  }
}