You are here

function wysiwyg_template_editor_js_settings_alter in Wysiwyg API template plugin 3.0.x

Same name and namespace in other branches
  1. 8.2 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 bundle specific if creating or editing an entity.
  $route_match = Drupal::routeMatch();
  $entity_type = NULL;
  $bundle = NULL;
  foreach (Drupal::service('wysiwyg_template.services')
    ->getEntityTypesAndBundles() as $entity_type => $defintion) {
    if (empty($defintion['bundles'])) {

      // No templates for this entity type.
      continue;
    }
    if ($route_match
      ->getRouteName() === 'entity.' . $entity_type . '.edit_form') {
      $bundle = $route_match
        ->getParameter($entity_type)
        ->bundle();
    }
    else {
      if ($route_match
        ->getRouteName() === $entity_type . '.add') {
        $bundle = $route_match
          ->getParameter($entity_type . '_type')
          ->get('type');
      }
    }
    if ($bundle !== NULL && isset($defintion['bundles'][$bundle])) {
      break;
    }
    $bundle = NULL;
  }
  if ($bundle !== NULL) {
    foreach ($settings['editor']['formats'] as $format => $config) {
      if (isset($config['editorSettings']['templates_files'])) {
        $url = Url::fromRoute('wysiwyg_template.list_js.type', [
          'entity_type' => $entity_type,
          'bundle' => $bundle,
        ]);
        $settings['editor']['formats'][$format]['editorSettings']['templates_files'] = [
          $url
            ->toString(),
        ];
      }
    }
  }
}