You are here

function entity_browser_generic_embed_ajax_render_alter in Varbase Media 8.6

Same name and namespace in other branches
  1. 8.7 modules/entity_browser_generic_embed/entity_browser_generic_embed.module \entity_browser_generic_embed_ajax_render_alter()
  2. 8.5 modules/entity_browser_generic_embed/entity_browser_generic_embed.module \entity_browser_generic_embed_ajax_render_alter()
  3. 9.0.x modules/entity_browser_generic_embed/entity_browser_generic_embed.module \entity_browser_generic_embed_ajax_render_alter()

Implements hook_ajax_render_alter().

File

modules/entity_browser_generic_embed/entity_browser_generic_embed.module, line 249
Core media asset support for .

Code

function entity_browser_generic_embed_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();
    }
  }
}