View source
<?php
namespace Drupal\socialbase\Plugin\Preprocess;
use Drupal\bootstrap\Plugin\Preprocess\PreprocessBase;
use Drupal\block_content\Entity\BlockContent;
use Drupal\block\Entity\Block as BlockEntity;
use Drupal\file\Entity\File;
use Drupal\image\Entity\ImageStyle;
class Block extends PreprocessBase {
public function preprocess(array &$variables, $hook, array $info) {
parent::preprocess($variables, $hook, $info);
if (empty($variables['elements']['#id'])) {
return;
}
$theme = \Drupal::theme()
->getActiveTheme();
$route_name = \Drupal::routeMatch()
->getRouteName();
$region = '';
$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']['#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_id = \Drupal::moduleHandler()
->moduleExists('social_geolocation') ? 'geolocation_search_content_block_header' : 'search_content_block_header';
$block = BlockEntity::load($block_id);
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'])) {
$imageitem = $variables['elements']['content']['field_hero_image'][0]['#item']
->getEntity();
$imagestyle = $variables['elements']['content']['field_hero_image'][0]['#image_style'];
$entity = BlockContent::load($imageitem
->id());
$file_id = $entity
->get('field_hero_image')->target_id;
if (isset($imagestyle)) {
if ($file = File::load($file_id)) {
$styled_image_url = ImageStyle::load($imagestyle)
->buildUrl($file
->getFileUri());
$variables['image_url'] = $styled_image_url;
$variables['has_image'] = TRUE;
unset($variables['content']['field_hero_image']);
}
}
}
}
}
}
}