You are here

function fitvids_page_attachments in FitVids 8

Implements hook_page_attachments() to insert JavaScript to the appropriate scope/region of the page.

File

./fitvids.module, line 51
Includes the FitVids.js jQuery plugin for fluid width video embeds.

Code

function fitvids_page_attachments(array &$page) {

  // Get config variables
  $config = \Drupal::config('fitvids.settings');
  $selectors = $config
    ->get('selectors');
  $custom_vendors = $config
    ->get('custom_vendors');
  $ignore_selectors = $config
    ->get('ignore_selectors');

  // Get video containers
  $selectors_for_js = implode(',', explode(PHP_EOL, $selectors));

  // Get custom vendor iframes
  if (strlen(trim($custom_vendors))) {
    $custom_vendors_for_js = explode(PHP_EOL, $custom_vendors);
    foreach ($custom_vendors_for_js as $key => $value) {
      $custom_vendors_for_js[$key] = 'iframe[src^="' . trim($value) . '"]';
    }
    $custom_vendors_for_js = implode(',', $custom_vendors_for_js);
  }
  else {
    $custom_vendors_for_js = '';
  }

  // Get ignored containers
  $ignore_selectors_for_js = implode(',', explode(PHP_EOL, $ignore_selectors));

  // Export settings
  $page['#attached']['drupalSettings']['fitvids']['selectors'] = $selectors_for_js;
  $page['#attached']['drupalSettings']['fitvids']['custom_vendors'] = $custom_vendors_for_js;
  $page['#attached']['drupalSettings']['fitvids']['ignore_selectors'] = $ignore_selectors_for_js;

  // Attach libraries
  $page['#attached']['library'][] = 'fitvids/fitvids';
  $page['#attached']['library'][] = 'fitvids/init';
}