v1/web/modules/custom/hsblock/hsblock.module

389 lines
13 KiB
PHP

<?php
/**
* @file
* Module for adding classes and custom ID to blocks.
*/
//use Drupal\Component\Utility\Html;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\block\Entity\Block;
use Drupal\block\BlockInterface;
use Drupal\Core\Url;
define( 'HSBLOCK_PATH', \Drupal::service('extension.list.module')->getPath('hsblock'));
function hsblock_get_blocks_options() {
static $_blocks_array = array();
if (empty($_blocks_array)) {
// Get default theme for user.
$theme_default = \Drupal::config('system.theme')->get('default');
// Get storage handler of block.
$block_storage = $this->entityTypeManager()->getStorage('block');
// Get the enabled block in the default theme.
$entity_ids = $block_storage->getQuery()->accessCheck(TRUE)->condition('theme', $theme_default)->execute();//WACKE FIXED
$entities = $block_storage->loadMultiple($entity_ids);
$_blocks_array = [];
foreach ($entities as $block_id => $block) {
$_blocks_array[$block_id] = $block->label();
}
asort($_blocks_array);
}
return $_blocks_array;
}
function hsblock_render_block($key) {
$block = \Drupal\block\Entity\Block::load($key);
$block_content = $this->entityTypeManager()
->getViewBuilder('block')
->view($block);
return \Drupal::service('renderer')->render($block_content);
}
function hsblock_update_projects_alter(&$projects){
unset($projects['hsblock']);
unset($projects['views_bootstrap_grid']);
unset($projects['carousel']);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function hsblock_form_block_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
if (\Drupal::currentUser()->hasPermission('administer blocks') && ($form_id == 'block_form' || $form_id == 'block_content_basic_edit_form')) {
$form['hsblock']['#attached']['library'][] = 'hsblock/hsblock_style';
/** @var \Drupal\block\BlockInterface $block */
$block = $form_state->getFormObject()->getEntity();
$_id = $block->ID();
// This will automatically be saved in the third party settings.
$form['third_party_settings']['#tree'] = TRUE;
$form['third_party_settings']['hsblock'] = [
'#title' => t('Block Configuration Options'),
'#type' => 'details',
'#open' => TRUE,
'#attributes' => array(
'class' => array('hsblock-config'),
),
];
// BLOCK LAYOUT
$form['third_party_settings']['hsblock']['block_layout'] = [
'#type' => 'select',
'#title' => t('Block Layout'),
'#type' => 'select',
'#options' => [
'norm-width' => t('Standard'),
'fullwidth' => t('Full Width'),
'fullwidth framewidth' => t('Frame Width'),
],
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_layout'),
];
// BLOCK TITLE
// BLOCK SUBTITLE
$form['third_party_settings']['hsblock']['block_subtitle'] = [
'#type' => 'textfield',
'#title' => t('Block Sub Title Text'),
'#maxlength' => 350,
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_subtitle'),
'#description' => t('Subtitle for the block. Leave blank if you do not need a subtitle.'),
];
// TITLE SETTINGS
// BLOCK TITLE STYLE
$form['third_party_settings']['hsblock']['block_title_style'] = [
'#title' => t('Block Title Style'),
'#type' => 'select',
'#options' => [
'block-title-1' => t('Style 1'),
'block-title-2' => t('Style 2'),
],
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_title_style'),
];
//BLOCK TITLE ALIGN
$form['third_party_settings']['hsblock']['block_title_align'] = [
'#title' => t('Block Title align'),
'#type' => 'select',
'#options' => [
'block-title-left' => t('Left'),
'block-title-center' => t('Center'),
'block-title-right' => t('Right'),
],
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_title_align'),
];
// BLOCK CLASS
$form['third_party_settings']['hsblock']['classes'] = [
'#type' => 'textfield',
'#title' => t('CSS Class(es)'),
'#description' => t('Add CSS classes to block. Separate multiple classes by spaces.'),
'#default_value' => $block->getThirdPartySetting('hsblock', 'classes'),
];
// BLOCK ID
$form['third_party_settings']['hsblock']['id'] = [
'#type' => 'textfield',
'#title' => t('Block ID'),
'#description' => t('Add a unique Custom ID to this block.'),
'#default_value' => $block->getThirdPartySetting('hsblock', 'id'),
];
// BLOCK PADDING
$form['third_party_settings']['hsblock']['block_padding_top'] = [
'#type' => 'textfield',
'#title' => t('Block Padding Top'),
'#description' => t('Add CSS Block Padding Top (e.g 15px, 2em).'),
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_padding_top'),
];
$form['third_party_settings']['hsblock']['block_padding_right'] = [
'#type' => 'textfield',
'#title' => t('Block Padding Right'),
'#description' => t('Add CSS Block Padding Right (e.g 15px, 2em).'),
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_padding_right'),
];
$form['third_party_settings']['hsblock']['block_padding_bottom'] = [
'#type' => 'textfield',
'#title' => t('Block Padding Bottom'),
'#description' => t('Add CSS Block Padding Bottom (e.g 15px, 2em).'),
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_padding_bottom'),
];
$form['third_party_settings']['hsblock']['block_padding_left'] = [
'#type' => 'textfield',
'#title' => t('Block Padding Left'),
'#description' => t('Add CSS Block Padding Left (e.g 15px, 2em).'),
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_padding_left'),
];
// BLOCK THEME
// TEXT COLOR
$form['third_party_settings']['hsblock']['block_text_color'] = [
'#title' => t('Text Color Style'),
'#description' => t('Select text color. Use light color if background is dark.'),
'#type' => 'select',
'#options' => [
'' => t('Default'),
'text-light' => t('Light'),
'title-light' => t('Title Light'),
],
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_text_color'),
];
// BACKGROUND COLOR
$form['third_party_settings']['hsblock']['block_bg_color'] = [
'#type' => 'textfield',
'#title' => t('Block Background Color'),
'#description' => t('Add Background Color to this block.'),
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_bg_color'),
'#attributes' => array(
'class' => array('basic-color-palette'),
),
];
// BACKGROUND IMAGE PATH
$form['third_party_settings']['hsblock']['block_bg_image_path'] = [
'#type' => 'textfield',
'#title' => t('Block Background Image'),
'#description' => t('Enter Block Background Image Path'),
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_bg_image_path'),
];
// BACKGROUND IMAGE REPEAT
$form['third_party_settings']['hsblock']['block_bg_repeat'] = [
'#type' => 'select',
'#title' => t('Block Background Repeat'),
'#description' => t('Setting background image repeat for Block.'),
'#options' => [
'' => t('-Select-'),
'no-repeat' => t('no-repeat'),
'repeat' => t('repeat'),
'repeat-x' => t('repeat-x'),
'repeat-y' => t('repeat-y')
],
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_bg_repeat'),
];
// BACKGROUND IMAGE POSITION
$form['third_party_settings']['hsblock']['block_bg_position'] = [
'#type' => 'select',
'#title' => t('Block Background Position'),
'#description' => t('Setting background image position for Block.'),
'#options' => [
'' => t('-Select-'),
'center top' => t('center top'),
'center right' => t('center right'),
'center bottom' => t('center bottom'),
'center center' => t('center center'),
'left top' => t('left top'),
'left center' => t('left center'),
'left bottom' => t('left bottom'),
'right top' => t('right top'),
'right center' => t('right center'),
'right bottom' => t('right bottom')
],
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_bg_position'),
];
// BACKGROUND SIZE
$form['third_party_settings']['hsblock']['block_bg_size'] = [
'#type' => 'select',
'#title' => t('Block Background Size'),
'#description' => t('Setting background image size for Block.'),
'#options' => [
'' => t('-Select-'),
'cover' => t('Cover'),
'contain' => t('Contain'),
],
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_bg_size'),
];
// PARALLAX BACKGROUND IMAGE
$form['third_party_settings']['hsblock']['block_parallax_bg'] = [
'#type' => 'select',
'#title' => t('Parrallax Background Image'),
'#options' => [
'' => t('Disable'),
'parallax-bg' => t('Enable'),
],
'#default_value' => $block->getThirdPartySetting('hsblock', 'block_parallax_bg'),
];
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function hsblock_preprocess_block(&$variables) {
// Blocks coming from page manager widget does not have id.
if (!empty($variables['elements']['#id'])) {
$block = Block::load($variables['elements']['#id']);
if ($id = $block->getThirdPartySetting('hsblock', 'id')) {
$variables['attributes']['id'] = $id;
}
if ($classes = $block->getThirdPartySetting('hsblock', 'classes')) {
$variables['attributes']['class'][] = $classes;
}
$variables['block_subtitle'] = $block->getThirdPartySetting('hsblock', 'block_subtitle');
$variables['block_title_style'] = $block->getThirdPartySetting('hsblock', 'block_title_style');
$styles = array();
$bg_image = '';
$bg_color = '';
$bg_position = 'center center';
$bg_repeat = 'no-repeat';
$bg_size = '';
$layout = '';
$title_style = '';
$title_align = '';
$text_color = '';
$parallax_bg = '';
$overlay = '';
$padding_top = '';
$padding_bottom = '';
$padding_right = '';
$padding_left = '';
if (!empty($variables['elements']['#id'])) {
$block = Block::load($variables['elements']['#id']);
$_id = $variables['elements']['#id'];
$block_bg_color = $block->getThirdPartySetting('hsblock', 'block_bg_color');
$block_bg_image_path = $block->getThirdPartySetting('hsblock', 'block_bg_image_path');
$block_bg_repeat = $block->getThirdPartySetting('hsblock', 'block_bg_repeat');
$block_bg_position = $block->getThirdPartySetting('hsblock', 'block_bg_position');
$block_bg_size = $block->getThirdPartySetting('hsblock', 'block_bg_size');
$block_padding_top = $block->getThirdPartySetting('hsblock', 'block_padding_top');
$block_padding_left = $block->getThirdPartySetting('hsblock', 'block_padding_left');
$block_padding_bottom = $block->getThirdPartySetting('hsblock', 'block_padding_bottom');
$block_padding_right = $block->getThirdPartySetting('hsblock', 'block_padding_right');
$block_text_color = $block->getThirdPartySetting('hsblock', 'block_text_color');
$block_parallax_bg = $block->getThirdPartySetting('hsblock', 'block_parallax_bg');
$block_layout = $block->getThirdPartySetting('hsblock', 'block_layout');
$block_subtitle = $block->getThirdPartySetting('hsblock', 'block_subtitle');
$block_title_style = $block->getThirdPartySetting('hsblock', 'block_title_style');
$block_title_align = $block->getThirdPartySetting('hsblock', 'block_title_align');
if($block_subtitle){
$subtitle = $block_subtitle;
}
if($block_layout){
$layout = $block_layout;
}
if($block_text_color){
$text_color = $block_text_color;
}
if($block_parallax_bg){
$parallax_bg = $block_parallax_bg;
}
if($block_title_style){
$title_style = $block_title_style;
}
if($block_title_align){
$title_align = $block_title_align;
}
if($block_bg_color){
$bg_color = $block_bg_color;
}
if($block_bg_image_path){
$bg_image = $block_bg_image_path;
}
if($block_bg_repeat){
$bg_repeat = $block_bg_repeat;
}
if($block_bg_position){
$bg_position = $block_bg_position;
}
if($block_bg_size){
$bg_size = $block_bg_size;
}
$padding_top = $block_padding_top;
$padding_right = $block_padding_right;
$padding_bottom = $block_padding_bottom;
$padding_left = $block_padding_left;
}
$variables['attributes']['class'][] = $layout;
$variables['attributes']['class'][] = $text_color;
$variables['attributes']['class'][] = $parallax_bg;
$variables['attributes']['class'][] = $title_style;
$variables['attributes']['class'][] = $title_align;
if($block_bg_color){
$styles[] = "background-color: {$block_bg_color};";
}
if($bg_image){
$styles[] = "background-image: url('{$bg_image}');";
if($bg_position){
$styles[] = "background-position: {$bg_position};";
}
if($bg_repeat){
$styles[] = "background-repeat: {$bg_repeat};";
}
if($bg_size){
$styles[] = "background-size: {$bg_size};";
}
}
if($padding_top){
$styles[] = "padding-top: {$padding_top};";
}
if($padding_right){
$styles[] = "padding-right: {$padding_right};";
}
if($padding_bottom){
$styles[] = "padding-bottom: {$padding_bottom};";
}
if($padding_left){
$styles[] = "padding-left: {$padding_left};";
}
$variables['block_style'] = implode('', $styles);
}
}