You are here

class Block in Open Social 8

Same name and namespace in other branches
  1. 8.9 themes/socialbase/src/Plugin/Preprocess/Block.php \Drupal\socialbase\Plugin\Preprocess\Block
  2. 8.2 themes/socialbase/src/Plugin/Preprocess/Block.php \Drupal\socialbase\Plugin\Preprocess\Block
  3. 8.3 themes/socialbase/src/Plugin/Preprocess/Block.php \Drupal\socialbase\Plugin\Preprocess\Block
  4. 8.4 themes/socialbase/src/Plugin/Preprocess/Block.php \Drupal\socialbase\Plugin\Preprocess\Block
  5. 8.5 themes/socialbase/src/Plugin/Preprocess/Block.php \Drupal\socialbase\Plugin\Preprocess\Block
  6. 8.6 themes/socialbase/src/Plugin/Preprocess/Block.php \Drupal\socialbase\Plugin\Preprocess\Block
  7. 8.7 themes/socialbase/src/Plugin/Preprocess/Block.php \Drupal\socialbase\Plugin\Preprocess\Block
  8. 8.8 themes/socialbase/src/Plugin/Preprocess/Block.php \Drupal\socialbase\Plugin\Preprocess\Block

Pre-processes variables for the "block" theme hook.

Plugin annotation

@BootstrapPreprocess("block");

Hierarchy

Expanded class hierarchy of Block

8 string references to 'Block'
paragraphs.paragraphs_type.block.yml in modules/social_features/social_landing_page/config/install/paragraphs.paragraphs_type.block.yml
modules/social_features/social_landing_page/config/install/paragraphs.paragraphs_type.block.yml
SocialPostPostForm::buildOptionsForm in modules/social_features/social_post/src/Plugin/views/area/SocialPostPostForm.php
Provide a form to edit options for this plugin.
views.view.activity_stream_notifications.yml in modules/social_features/social_activity/config/install/views.view.activity_stream_notifications.yml
modules/social_features/social_activity/config/install/views.view.activity_stream_notifications.yml
views.view.activity_stream_profile.yml in modules/social_features/social_activity/config/install/views.view.activity_stream_profile.yml
modules/social_features/social_activity/config/install/views.view.activity_stream_profile.yml
views.view.event_enrollments.yml in modules/social_features/social_event/config/install/views.view.event_enrollments.yml
modules/social_features/social_event/config/install/views.view.event_enrollments.yml

... See full list

File

themes/socialbase/src/Plugin/Preprocess/Block.php, line 18

Namespace

Drupal\socialbase\Plugin\Preprocess
View source
class Block extends PreprocessBase {

  /**
   * {@inheritdoc}
   */
  public function preprocess(array &$variables, $hook, array $info) {
    parent::preprocess($variables, $hook, $info);

    // Early return because block missing ID, for example because
    // Rendered in panels display
    // https://www.drupal.org/node/2873726
    if (empty($variables['elements']['#id'])) {
      return;
    }

    // Find out what the active theme is first.
    $theme = \Drupal::theme()
      ->getActiveTheme();
    $route_name = \Drupal::routeMatch()
      ->getRouteName();

    // Get the region of a block.
    $region = '';
    $block_entity = BlockEntity::load($variables['elements']['#id']);
    if ($block_entity) {
      $region = $block_entity
        ->getRegion();
    }
    $prefix = '';

    // If socialbase is one of the basetheme, we need a prefix for block ids.
    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',
      ];
      if (in_array($variables['elements']['#id'], $block_buttons)) {
        $variables['card'] = FALSE;
      }
    }
    if (isset($variables['elements']['kpi_analytics'])) {
      $variables['card'] = TRUE;
    }

    // Wrap the group/membership table in a card element.
    if (isset($variables['elements']['#plugin_id']) && $variables['elements']['#plugin_id'] == 'system_main_block' && $route_name == 'entity.group_content.collection') {
      $variables['card'] = TRUE;
    }

    // Show group managers block in a card.
    if ($variables['elements']['#derivative_plugin_id'] == 'group_managers-block_list_managers') {
      $variables['card'] = TRUE;
    }

    // For all platform_intro blocks we want them to appear as cards.
    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'];

    // Fix label for Views exposed filter blocks.
    if (!empty($variables['configuration']['views_label']) && empty($variables['configuration']['label'])) {
      $variables['label'] = $variables['configuration']['views_label'];
    }

    // Check if the block is a views exposed form filter, add condition to add
    // classes in twig file.
    if (isset($variables['content']['#form_id']) && $variables['content']['#form_id'] == 'views_exposed_form') {
      $variables['complementary'] = TRUE;
    }

    // Add search_block to main menu.
    if ($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;
      }
    }

    // Preprocess search block header.
    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';
      }
    }

    // Add Group ID for "See all groups link".
    if ($variables['attributes']['id'] === 'block-views-block-group-members-block-newest-members') {
      $group = \Drupal::routeMatch()
        ->getParameter('group');
      $variables['group_id'] = $group
        ->id();
    }

    // Add User ID for "See all link".
    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;
    }

    // AN Homepage block.
    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;

          // First filter out image_style,
          // So responsive image module doesn't break.
          if (isset($imagestyle)) {

            // If it's an existing file.
            if ($file = File::load($file_id)) {

              // Style and set it in the content.
              $styled_image_url = ImageStyle::load($imagestyle)
                ->buildUrl($file
                ->getFileUri());
              $variables['image_url'] = $styled_image_url;

              // Add extra class.
              $variables['has_image'] = TRUE;

              // Remove the original.
              unset($variables['content']['field_hero_image']);
            }
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Block::preprocess public function Preprocess theme hook variables. Overrides PreprocessBase::preprocess
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::$theme protected property The currently set theme object.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct 1
PreprocessBase::$hook protected property The theme hook invoked.
PreprocessBase::$info protected property The theme hook info array from the theme registry.
PreprocessBase::$variables protected property The Variables object.
PreprocessBase::preprocessAttributes protected function Ensures all attributes have been converted to an Attribute object.
PreprocessBase::preprocessDescription protected function Converts any set description variable into a traversable array.
PreprocessBase::preprocessElement protected function Preprocess the variables array if an element is present. 8
PreprocessBase::preprocessVariables protected function Preprocess the variables array. 15
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.