You are here

function rrssb_gen_css in Ridiculously Responsive Social Sharing Buttons 8.2

Same name and namespace in other branches
  1. 7.2 rrssb.module \rrssb_gen_css()

Auto-generate CSS for buttons.

The RRSSB library CSS is static. This function takes account the results of hook_rrssb_buttons and hook_rrssb_buttons_alter to create dynamic, site-specific CSS. It optimises by only including buttons that are enabled.

The fact that this module is not relying on library CSS for buttons means that there is greater compatibility to work with older library versions that are missing CSS for newer buttons.

1 call to rrssb_gen_css()
rrssb_get_buttons in ./rrssb.module
Returns a Drupal render array for the buttons.

File

./rrssb.module, line 333

Code

function rrssb_gen_css() {
  $state = \Drupal::state();
  $file_system = \Drupal::service('file_system');
  $css = "/* Auto-generated RRSSB CSS file. */\n";
  $buttons = [];
  foreach (rrssb_button_sets() as $buttonSet => $config) {
    $buttons += rrssb_settings($buttonSet);
  }
  $css .= rrssb_calc_css($buttons);

  // Save to a unique filename.
  $id = substr(hash('sha256', serialize($buttons) . microtime()), 0, 8);
  $dir = 'public://rrssb';
  $file = "{$dir}/rrssb.{$id}.css";
  $file_system
    ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY);
  $file_system
    ->saveData($css, $file, FileSystemInterface::EXISTS_REPLACE);

  // Delete the old file and record the new location.
  if ($old = $state
    ->get('rrssb_css_file')) {
    $file_system
      ->delete($old);
  }
  $state
    ->set('rrssb_css_file', $file);

  // Clear library cache as it contains this file name.
  \Drupal::service('library.discovery')
    ->clearCachedDefinitions();
  return $file;
}