You are here

function collapsiblock_page_attachments in Collapsiblock 8

Implements hook_page_attachments().

File

./collapsiblock.module, line 59
Make blocks collapsible.

Code

function collapsiblock_page_attachments(array &$attachments) {
  $default_config = \Drupal::config('collapsiblock.settings');
  $theme_name = \Drupal::theme()
    ->getActiveTheme()
    ->getName();

  // Theme settings.
  $collapsiblock_title = !empty(theme_get_setting('collapsiblock_title', $theme_name)) ? theme_get_setting('collapsiblock_title', $theme_name) : $default_config
    ->get('title');
  $collapsiblock_block = !empty(theme_get_setting('collapsiblock_block', $theme_name)) ? theme_get_setting('collapsiblock_block', $theme_name) : $default_config
    ->get('block');
  $collapsiblock_content = !empty(theme_get_setting('collapsiblock_content', $theme_name)) ? theme_get_setting('collapsiblock_content', $theme_name) : $default_config
    ->get('content');

  // Load all blocks.
  $blocks = Block::loadMultiple();
  if (!empty($blocks)) {
    $collapsi_blocks = [];
    foreach ($blocks as $key => $block) {
      if (!empty($block
        ->get('collapsiblock'))) {
        $collapsi_blocks['collapsiblock.block-' . str_replace('_', '-', $key)] = $block
          ->get('collapsiblock.block-' . str_replace('_', '-', $key));
      }
    }

    // Add block settings for js.
    $attachments['#attached']['drupalSettings']['collapsiblock']['blocks'] = $collapsi_blocks;
    $attachments['#attached']['drupalSettings']['collapsiblock']['default_state'] = $default_config
      ->get('default_state');
    $attachments['#attached']['drupalSettings']['collapsiblock']['active_pages'] = $default_config
      ->get('active_pages');
    $attachments['#attached']['drupalSettings']['collapsiblock']['slide_type'] = $default_config
      ->get('slide_type');
    $attachments['#attached']['drupalSettings']['collapsiblock']['slide_speed'] = $default_config
      ->get('slide_speed');
    $attachments['#attached']['drupalSettings']['collapsiblock']['block_title'] = $collapsiblock_title;
    $attachments['#attached']['drupalSettings']['collapsiblock']['block'] = $collapsiblock_block;
    $attachments['#attached']['drupalSettings']['collapsiblock']['block_content'] = $collapsiblock_content;

    // Load css and js libraries.
    $attachments['#attached']['library'][] = 'collapsiblock/corescripts';
  }
}