You are here

class ThemeSuggestions in Open Social 8.8

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

Implements hook_theme_suggestions_alter().

Plugin annotation

@BootstrapAlter("theme_suggestions");

Hierarchy

Expanded class hierarchy of ThemeSuggestions

1 file declares its use of ThemeSuggestions
ThemeSuggestions.php in themes/socialblue/src/Plugin/Alter/ThemeSuggestions.php

File

themes/socialbase/src/Plugin/Alter/ThemeSuggestions.php, line 15

Namespace

Drupal\socialbase\Plugin\Alter
View source
class ThemeSuggestions extends BaseThemeSuggestions {

  /**
   * {@inheritdoc}
   */
  public function alter(&$suggestions, &$context1 = NULL, &$hook = NULL) {
    parent::alter($suggestions, $context1, $hook);
    $variables = $this->variables;
    switch ($hook) {
      case 'block':
        if (isset($variables['elements']['#base_plugin_id']) && $variables['elements']['#base_plugin_id'] == 'system_menu_block') {
          $menu_name = $variables['elements']['content']['#menu_name'];
          $suggestions[] = 'block__block_menu__' . $menu_name;
        }
        if (isset($variables['elements']['content']['#block_content'])) {

          // Keep the theme suggestion before the most specific plugin based
          // suggestion. This allows cases like layout builder blocks to take
          // precedence over our generic theme based use of the block.
          // See block_theme_suggestions_block() for how tis is constructed.
          $parts = explode(':', $variables['elements']['#plugin_id']);
          $insert_before = 'block__' . implode('__', array_map(static function ($part) {
            return str_replace('-', '_', $part);
          }, $parts));
          $new_suggestion = 'block__' . $variables['elements']['content']['#block_content']
            ->bundle();
          array_splice($suggestions, array_search($insert_before, $suggestions, TRUE), 0, $new_suggestion);
        }
        if (isset($variables['elements']['content']['#lazy_builder']) && $variables['elements']['content']['#lazy_builder'][0] === 'social_content_block.content_builder:build') {

          // Add a block--block-type suggestion just above the layout builder so
          // it can be shared with other places the block is shown. This works
          // for properly designed blocks and gives plenty of opportunities for
          // misbehaving blocks.
          $block_content_bundle = $variables['elements']['content']['#lazy_builder'][1][2];
          $new_suggestion = 'block__' . $block_content_bundle;
          $insert_before = 'block__inline_block__' . $block_content_bundle;
          array_splice($suggestions, array_search($insert_before, $suggestions, TRUE), 0, $new_suggestion);
        }
        $block_id = $variables['elements']['#derivative_plugin_id'];
        $blocks_id = [
          'upcoming_events-block_my_upcoming_events',
          'upcoming_events-block_community_events',
          'latest_topics-block_latest_topics',
          'newest_groups-block_newest_groups',
          'newest_users-block_newest_users',
          'events-block_events_on_profile',
          'topics-block_user_topics',
          'groups-block_user_groups',
          'group_members-block_newest_members',
          'upcoming_events-upcoming_events_group',
          'latest_topics-group_topics_block',
        ];
        if (in_array($block_id, $blocks_id)) {
          $suggestions = [
            $variables['theme_hook_original'] . '__' . 'views_block__sidebar',
          ];
        }
        if (isset($variables['elements']['kpi_analytics'])) {
          $suggestions = [
            $variables['theme_hook_original'] . '__' . 'charts',
          ];
        }
        if (isset($variables['elements']['#id'])) {
          $theme = \Drupal::theme()
            ->getActiveTheme()
            ->getName();
          $name = 'data_policy_page_title_block';
          if ($variables['elements']['#id'] == $theme . '_' . $name) {
            $suggestions[] = $variables['theme_hook_original'] . '__' . $name;
          }
        }
        break;
      case 'confirm_form':
        if (isset($variables['form']['#form_id']) && $variables['form']['#form_id'] == 'data_policy_data_policy_revision_revert_confirm') {
          $suggestions[] = $variables['theme_hook_original'] . '__modal';
        }
        break;
      case 'container':
        if (isset($variables['element']['#id']) && $variables['element']['#id'] == 'edit-field-post-image-wrapper') {
          $suggestions[] = 'container__post_image';
        }
        break;
      case 'details':
        $suggestions[] = 'details__plain';
        if (in_array('image-data__crop-wrapper', $variables['element']['#attributes']['class'])) {
          $suggestions[] = 'details__crop';
        }

        // Template suggestion for upload attachments in comments.
        if (isset($variables['element']['#id']) && strpos($variables['element']['#id'], 'edit-group-add-attachment') === 0) {
          $suggestions[] = 'details__comment';
        }
        break;
      case 'file_link':

        // For the new Social Comment we need a different theme hook suggestion.
        if (\Drupal::config('social_comment_upload.settings')
          ->get('allow_upload_comments')) {
          $file = $variables['file'];

          // For comments in activities we show the amount of attachments.
          if ($file->_referringItem !== NULL) {

            /* @var $item \Drupal\file\Plugin\Field\FieldType\FileItem  */
            $item = $file->_referringItem;
            $name = $item
              ->getFieldDefinition()
              ->getName();

            // For field comment files we add a new suggestion.
            if ($name === 'field_comment_files') {
              $suggestions[] = 'file_link__comment';
            }
          }
        }

        // Get the route name for file links.
        $route_name = \Drupal::routeMatch()
          ->getRouteName();

        // If the file link is part of a node field, suggest another template.
        if ($route_name == 'entity.node.canonical') {

          /** @var \Drupal\file\Entity\File $c_file */
          $c_file = $context1['file'];
          $file_id = $c_file
            ->id();
          $node = \Drupal::routeMatch()
            ->getParameter('node');

          // We do not know the name of the file fields. These can be custom.
          $field_definitions = $node
            ->getFieldDefinitions();

          // Loop over all fields and target only file fields.
          foreach ($field_definitions as $field_name => $field_definition) {

            /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
            if ($field_definition
              ->getType() == 'file') {
              $files = $node
                ->get($field_name)
                ->getValue();
              foreach ($files as $file) {
                if ($file['target_id'] == $file_id) {
                  $suggestions[] = 'file_link__card';
                  break 2;
                }
              }
            }
          }
        }

        // If the file link is part of a group field, suggest another template.
        if ($route_name == 'entity.group.canonical') {
          $suggestions[] = 'file_link__card';
        }
        break;
      case 'form':

        // Alter comment form.
        if ($variables['element']['#form_id'] == 'comment_comment_form') {
          if (\Drupal::routeMatch()
            ->getRouteName() === 'entity.comment.edit_form') {
            $suggestions = [
              $variables['theme_hook_original'] . '__' . 'comment_edit',
            ];
          }
          else {
            $suggestions = [
              $variables['theme_hook_original'] . '__' . 'comment',
            ];
          }
        }
        if ($variables['element']['#form_id'] == 'comment_post_comment_form') {
          if (\Drupal::routeMatch()
            ->getRouteName() === 'entity.comment.edit_form') {
            $suggestions = [
              $variables['theme_hook_original'] . '__' . 'comment_edit',
            ];
          }
          else {
            $suggestions[] = $variables['theme_hook_original'] . '__comment';
          }
        }

        // Distinguish message create form from thread form.
        if ($variables['element']['#form_id'] == 'private_message_add_form') {
          if (\Drupal::routeMatch()
            ->getRouteName() === 'entity.private_message_thread.canonical') {
            $suggestions = [
              $variables['theme_hook_original'] . '__' . 'private_message_thread',
            ];
          }
          else {
            $suggestions = [
              $variables['theme_hook_original'] . '__' . 'private_message_create',
            ];
          }
        }

        // Add templates for post add/edit forms.
        if ($variables['element']['#form_id'] == 'social_post_entity_form') {
          if (\Drupal::routeMatch()
            ->getRouteName() === 'entity.post.edit_form') {
            $suggestions[] = $variables['theme_hook_original'] . '__post_edit';
          }
          else {
            $suggestions[] = $variables['theme_hook_original'] . '__post_create';
          }
        }
        break;
      case 'form_element':

        // Lets add the form element parent to the theme suggestions.
        if (isset($variables['element']['#parents'][0])) {
          $hook = Html::escape($variables['element']['#parents'][0]);
          $suggestions[] = $variables['theme_hook_original'] . '__' . $hook;
        }
        if (!empty($variables['element']['#attributes']['data-switch'])) {
          $suggestions[] = $variables['theme_hook_original'] . '__switch';
        }
        break;
      case 'form_element_label':
        if (isset($variables['element']['#id'])) {
          if (strpos($variables['element']['#id'], 'field-visibility') !== FALSE) {
            $suggestions[] = $variables['theme_hook_original'] . '__' . 'dropdown';
          }
        }
        if (isset($variables['element']['#switch']) && $variables['element']['#switch'] == TRUE) {
          $suggestions[] = $variables['theme_hook_original'] . '__switch';
        }
        break;
      case 'input':

        // Add the form element parent to the theme suggestions.
        if (isset($variables['element']['#id'])) {
          if (strpos($variables['element']['#id'], 'field-visibility') !== FALSE) {
            $suggestions[] = $variables['theme_hook_original'] . '__' . 'dropdown';
          }
        }
        if (isset($variables['element']['#comment_button'])) {
          $suggestions[] = 'input__button__comment';
        }
        break;
      case 'views_view':
        $view_id = $variables['view']
          ->id();
        $display_id = $variables['view']
          ->getDisplay()->display['id'];
        if (isset($display_id)) {
          if ($display_id == 'wholiked') {
            $suggestions[] = $variables['theme_hook_original'] . '__members_list';
          }
        }
        if (isset($view_id)) {
          if ($view_id == 'view_enrollments') {
            $suggestions[] = $variables['theme_hook_original'] . '__page';
          }
          if ($view_id == 'group_managers') {
            $suggestions[] = $variables['theme_hook_original'] . '__group_managers';
          }
          if ($view_id == 'activity_stream' || $view_id == 'activity_stream_profile' || $view_id == 'activity_stream_group') {
            $suggestions[] = $variables['theme_hook_original'] . '__stream';
          }
        }
        break;
      case 'views_view_fields':

        /** @var \Drupal\views\ViewExecutable $view */
        $view = $variables['view'];
        if ($view && $view
          ->id() == 'who_liked_this_entity') {
          $suggestions[] = $variables['theme_hook_original'] . '__wholiked';
        }
        break;
      case 'profile':

        // Add an anonymous variant to all the default profile theme
        // suggestions.
        if (\Drupal::currentUser()
          ->isAnonymous()) {
          $default_suggestions = profile_theme_suggestions_profile($variables
            ->getArrayCopy());
          foreach ($default_suggestions as $suggestion) {

            // Find the position of the original suggestion.
            $reference_pos = array_search($suggestion, $suggestions);
            $anonymous_suggestion = $suggestion . '__anonymous';

            // If we can't find the reference suggestion we just add it to the
            // most important spot in the suggestions list.
            if ($reference_pos === FALSE) {
              $suggestions[] = $anonymous_suggestion;
            }
            else {
              array_splice($suggestions, $reference_pos + 1, 0, $anonymous_suggestion);
            }
          }
        }
        break;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
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.
ThemeSuggestions::$bootstrapPanelTypes protected property
ThemeSuggestions::$element protected property An element object provided in the variables array, may not be set.
ThemeSuggestions::$hook protected property The theme hook invoked.
ThemeSuggestions::$hookSuggestions protected property The theme hook suggestions, exploded by the "__" delimiter.
ThemeSuggestions::$ignoreFormControlTypes protected property The types of elements to ignore for the "input__form_control" suggestion.
ThemeSuggestions::$originalHook protected property The original "hook" value passed via hook_theme_suggestions_alter().
ThemeSuggestions::$suggestions protected property The array of suggestions to return.
ThemeSuggestions::$variables protected property The variables array object passed via hook_theme_suggestions_alter().
ThemeSuggestions::addEntitySuggestions Deprecated public function Adds "bundle" and "view mode" suggestions for an entity.
ThemeSuggestions::addSuggestion protected function Adds suggestions based on an array of hooks.
ThemeSuggestions::addSuggestionsForEntity protected function Adds "bundle" and "view mode" suggestions for an entity.
ThemeSuggestions::alter public function Alters data for a specific hook_TYPE_alter() implementation. Overrides ThemeSuggestions::alter 1
ThemeSuggestions::alterInput protected function Dynamic alter method for "input".
ThemeSuggestions::alterLinksDropbutton protected function Dynamic alter method for "links__dropbutton".
ThemeSuggestions::alterUser protected function Dynamic alter method for "user".
ThemeSuggestions::buildSuggestions protected function Builds a list of suggestions.
ThemeSuggestions::getAlterMethods protected function Retrieves the methods to invoke to process the theme hook suggestion.
ThemeSuggestions::getEntity Deprecated public function Extracts the entity from the element(s) passed in the Variables object.
ThemeSuggestions::getEntityObject protected function Extracts the entity from the element(s) passed in the Variables object.
ThemeSuggestions::processSuggestions protected function Processes the necessary theme hook suggestions.