View source
<?php
namespace Drupal\socialbase\Plugin\Preprocess;
use Drupal\block_content\Entity\BlockContent;
use Drupal\bootstrap\Plugin\Preprocess\PreprocessBase;
use Drupal\block\Entity\Block as BlockEntity;
use Drupal\Component\Utility\Html;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
use Drupal\image\Plugin\Field\FieldType\ImageItem;
class Block extends PreprocessBase {
public function preprocess(array &$variables, $hook, array $info) {
parent::preprocess($variables, $hook, $info);
$region = '';
if (empty($variables['elements']['#id']) && !empty($variables['content']['_layout_builder'])) {
$region = '_LAYOUT_BUILDER_DO_NOT_CHANGE';
$variables['elements']['#id'] = Html::getUniqueId('_LAYOUT_BUILDER_DO_NOT_CHANGE');
$variables['attributes']['id'] = $variables['elements']['#id'];
}
if (empty($variables['elements']['#id'])) {
return;
}
$theme = \Drupal::theme()
->getActiveTheme();
$route_name = \Drupal::routeMatch()
->getRouteName();
$block_entity = BlockEntity::load($variables['elements']['#id']);
if ($block_entity) {
$region = $block_entity
->getRegion();
}
$prefix = '';
if (array_key_exists('socialbase', $theme
->getBaseThemes())) {
$prefix = $theme
->getName();
}
$variables['card'] = FALSE;
$regions_card = [
'complementary',
'complementary_top',
'complementary_bottom',
'content_top',
'content_bottom',
'',
];
if (in_array($region, $regions_card)) {
$variables['card'] = TRUE;
if (array_key_exists('socialbase', $theme
->getBaseThemes())) {
$prefix = $theme
->getName() . '_';
}
$block_buttons = [
$prefix . 'event_add_block',
$prefix . 'topic_add_block',
$prefix . 'group_add_block',
$prefix . 'group_add_event_block',
$prefix . 'group_add_topic_block',
$prefix . 'add_data_policy_revision',
];
if (in_array($variables['elements']['#id'], $block_buttons)) {
$variables['card'] = FALSE;
}
}
if (isset($variables['elements']['kpi_analytics'])) {
$variables['card'] = TRUE;
}
if (isset($variables['elements']['#plugin_id']) && $variables['elements']['#plugin_id'] == 'system_main_block') {
$route_names = [
'entity.group_content.collection',
'data_policy.data_policy',
'social_gdpr.data_policy.revision',
'social_gdpr.data_policy.revisions',
];
if (in_array($route_name, $route_names)) {
$variables['card'] = TRUE;
if ($route_name == 'social_gdpr.data_policy.revision') {
$variables['attributes']['class'][] = 'card__body';
}
}
}
if ($variables['elements']['#plugin_id'] == 'social_group_tags_block') {
$variables['card'] = TRUE;
}
if ($variables['elements']['#derivative_plugin_id'] == 'group_managers-block_list_managers') {
$variables['card'] = TRUE;
}
if (isset($variables['elements']['content']['#block_content'])) {
if ($variables['elements']['content']['#block_content']
->bundle() == 'platform_intro') {
$variables['card'] = TRUE;
}
}
$variables['content']['#attributes']['block'] = $variables['attributes']['id'];
if (!empty($variables['configuration']['views_label']) && empty($variables['configuration']['label'])) {
$variables['label'] = $variables['configuration']['views_label'];
}
if (isset($variables['content']['#form_id']) && $variables['content']['#form_id'] == 'views_exposed_form') {
$variables['complementary'] = TRUE;
}
if (\Drupal::moduleHandler()
->moduleExists('social_search') && ($variables['elements']['#id'] == 'mainnavigation' || $variables['elements']['#id'] == $prefix . '_mainnavigation')) {
$block = BlockEntity::load('search_content_block_header');
if (!empty($block)) {
$block_output = \Drupal::entityManager()
->getViewBuilder('block')
->view($block);
$variables['content']['links']['search_block'] = $block_output;
}
}
if (isset($variables['content']['search_form'])) {
$variables['content']['search_form']['#attributes']['role'] = 'search';
$variables['content']['search_form']['actions']['submit']['#is_button'] = FALSE;
$variables['content']['search_form']['actions']['#addsearchicon'] = TRUE;
if ($region == 'hero') {
$variables['content']['search_form']['#attributes']['class'][] = 'hero-form';
$variables['content']['search_form']['#region'] = 'hero';
$variables['content']['search_form']['actions']['submit']['#addsearchicon'] = TRUE;
}
elseif ($region == 'content_top') {
$variables['content']['search_form']['#region'] = 'content-top';
$variables['content']['search_form']['search_input_content']['#attributes']['placeholder'] = t('What are you looking for ?');
$variables['content']['search_form']['search_input_content']['#attributes']['autocomplete'] = 'off';
}
else {
$variables['content']['search_form']['#attributes']['class'][] = 'navbar-form';
}
}
if ($variables['attributes']['id'] === 'block-views-block-group-members-block-newest-members') {
$group = \Drupal::routeMatch()
->getParameter('group');
$variables['group_id'] = $group
->id();
}
if ($variables['attributes']['id'] === 'block-views-block-events-block-events-on-profile' || $variables['attributes']['id'] === 'block-views-block-topics-block-user-topics' || $variables['attributes']['id'] === 'block-views-block-groups-block-user-groups') {
$profile_user_id = \Drupal::routeMatch()
->getParameter('user');
if (!is_null($profile_user_id) && is_object($profile_user_id)) {
$profile_user_id = $profile_user_id
->id();
}
$variables['profile_user_id'] = $profile_user_id;
}
if (isset($variables['elements']['content']['#block_content'])) {
if ($variables['elements']['content']['#block_content']
->bundle() == 'hero_call_to_action_block') {
if (isset($variables['elements']['content']['field_hero_image']) && isset($variables['elements']['content']['field_hero_image'][0])) {
$image_item = $variables['elements']['content']['field_hero_image'][0]['#item'];
$file_id = NULL;
if ($image_item && $image_item instanceof ImageItem) {
$block_content = $image_item
->getEntity();
if ($block_content && $block_content instanceof BlockContent) {
$file_id = $block_content
->get('field_hero_image')->target_id;
}
}
$image_style = $variables['elements']['content']['field_hero_image'][0]['#image_style'];
if (isset($image_style)) {
if ($file = File::load($file_id)) {
$styled_image_url = ImageStyle::load($image_style)
->buildUrl($file
->getFileUri());
$variables['image_url'] = $styled_image_url;
$variables['has_image'] = TRUE;
unset($variables['content']['field_hero_image']);
}
}
}
}
}
if ($region === '_LAYOUT_BUILDER_DO_NOT_CHANGE') {
unset($variables['elements']['#id'], $variables['attributes']['id']);
}
}
}