You are here

closeblock.module in Close Block 8

Same filename and directory in other branches
  1. 6 closeblock.module
  2. 7 closeblock.module

Module's hooks implementations.

File

closeblock.module
View source
<?php

/**
 * @file
 * Module's hooks implementations.
 */
use Drupal\block\Entity\Block;
use Drupal\Core\Config\Entity\ThirdPartySettingsInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Component\Utility\Html;

/**
 * Implements hook_page_attachments().
 */
function closeblock_page_attachments(array &$attachments) {
  $permission = \Drupal::currentUser()
    ->hasPermission('close block');
  if ($permission) {
    $factory = \Drupal::configFactory()
      ->getEditable('closeblock.settings');
    foreach ($factory
      ->get() as $key => $item) {
      $attachments['#attached']['drupalSettings']['closeBlockSettings'][$key] = $item;
    }
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function closeblock_preprocess_block(&$variables) {
  $permission = \Drupal::currentUser()
    ->hasPermission('close block');
  if ($permission) {
    if (!empty($variables['elements']['#id'])) {
      $block = Block::load($variables['elements']['#id']);
      if ($block instanceof ThirdPartySettingsInterface) {
        $settings = $block
          ->getThirdPartySetting('closeblock', 'closeblock_active');
        if ($settings) {
          $variables['attributes']['class'][] = Html::cleanCssIdentifier('close-block', []);
          $variables['#attached']['library'][] = 'closeblock/closeblock';
        }
      }
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function closeblock_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  /** @var \Drupal\block\Entity\Block $block */
  $block = $form_state
    ->getFormObject()
    ->getEntity();
  $form['third_party_settings']['#tree'] = TRUE;
  $form['third_party_settings']['closeblock'] = [
    '#type' => 'fieldset',
    '#title' => t('Closeblock selectors'),
    '#weight' => 0,
    '#attributes' => [
      'id' => 'closeblock_form',
    ],
  ];
  $link = Link::fromTextAndUrl(t('here'), Url::fromRoute('closeblock.settings_form'))
    ->toString();
  $form['third_party_settings']['closeblock']['closeblock_active'] = [
    '#type' => 'checkbox',
    '#title' => t('Use close button'),
    '#default_value' => $block
      ->getThirdPartySetting('closeblock', 'closeblock_active'),
    '#description' => t('Settings @link', [
      '@link' => $link,
    ]),
  ];
}

/**
 * Implements hook_theme().
 */
function closeblock_theme($existing, $type, $theme, $path) {
  return [
    'custom_block_closeblock' => [
      'variables' => [
        'content' => [],
      ],
    ],
  ];
}