You are here

function addtocopy_page_attachments in Add link to copied text 8

Implements hook_page_attachments().

Adds the addtocopy library to the page for any user who do not have 'bypass addtocopy' permission.

File

./addtocopy.module, line 14
Module file for addtocopy.

Code

function addtocopy_page_attachments(array &$page) {

  // Return if user has 'bypass addtocopy' permission.
  if (\Drupal::currentUser()
    ->hasPermission('bypass addtocopy')) {
    return;
  }
  $selector = \Drupal::config('addtocopy.settings')
    ->get('addtocopy.selector') ? \Drupal::config('addtocopy.settings')
    ->get('addtocopy.selector') : '#content';
  $minlen = \Drupal::config('addtocopy.settings')
    ->get('addtocopy.minlen') ? \Drupal::config('addtocopy.settings')
    ->get('addtocopy.minlen') : '25';
  $htmlcopytxt = \Drupal::config('addtocopy.settings')
    ->get('addtocopy.htmlcopytxt') ? \Drupal::config('addtocopy.settings')
    ->get('addtocopy.htmlcopytxt') : '<br>More: <a href="[link]">[link]</a><br>';
  $addcopyfirst = \Drupal::config('addtocopy.settings')
    ->get('addtocopy.addcopyfirst') ? \Drupal::config('addtocopy.settings')
    ->get('addtocopy.addcopyfirst') : '000';
  $page['#attached']['library'][] = 'addtocopy/addtocopy';
  $page['#attached']['drupalSettings']['addtocopy']['addtocopy']['selector'] = $selector;
  $page['#attached']['drupalSettings']['addtocopy']['addtocopy']['minlen'] = $minlen;
  $page['#attached']['drupalSettings']['addtocopy']['addtocopy']['htmlcopytxt'] = $htmlcopytxt;
  $page['#attached']['drupalSettings']['addtocopy']['addtocopy']['addcopyfirst'] = $addcopyfirst;
}