You are here

function lightning_media_ajax_render_alter in Lightning Media 8.3

Same name and namespace in other branches
  1. 8.4 lightning_media.module \lightning_media_ajax_render_alter()
  2. 8 lightning_media.module \lightning_media_ajax_render_alter()
  3. 8.2 lightning_media.module \lightning_media_ajax_render_alter()

Implements hook_ajax_render_alter().

File

./lightning_media.module, line 472
Core media asset support for Lightning.

Code

function lightning_media_ajax_render_alter(array &$data) {
  $route = \Drupal::routeMatch()
    ->getRouteName();
  $query = \Drupal::request()->query;
  if ($route == 'entity_embed.dialog') {
    foreach ($data as &$command) {
      if ($command['command'] == 'settings' && isset($command['settings']['ajaxPageState']['libraries'])) {
        $libraries = explode(',', $command['settings']['ajaxPageState']['libraries']);
        $libraries = array_diff($libraries, [
          'entity_browser/iframe',
        ]);
        $command['settings']['ajaxPageState']['libraries'] = implode(',', $libraries);
      }
    }
  }
  elseif ($route == 'embed.preview' && $query
    ->has('editor')) {
    $style_sheets = [];
    foreach ($data as $command) {

      // Any CSS being added should be replicated in the editor.
      if ($command['command'] == 'add_css') {
        $matched = [];

        // Assume (perhaps naively) that all the style sheets are embedded as
        // <link /> tags.
        preg_match_all('/href="([^"]+)"/', $command['data'], $matched);
        $style_sheets = array_merge($style_sheets, $matched[1]);
      }
    }
    if ($style_sheets) {
      $command = new AddStyleSheetCommand($query
        ->get('editor'), $style_sheets);
      $data[] = $command
        ->render();
    }
  }
}