Commit 4f57d200 authored by Andrii Chyrskyi's avatar Andrii Chyrskyi Committed by Andrii Chyrskyi
Browse files

Issue #3273988 by tBKoT: Shows titles corectly on the group invites page

parent c8778c70
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ use Drupal\group\Entity\GroupInterface;
use Drupal\group\Entity\GroupRoleInterface;
use Drupal\group\Entity\GroupTypeInterface;
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\social_group_invite\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatterOverrider;

/**
 * Implements hook_menu_local_actions_alter().
@@ -600,3 +601,18 @@ function social_group_invite_tokens_alter(array &$replacements, array $context,
    }
  }
}

/**
 * Implements hook_field_formatter_info_alter().
 */
function social_group_invite_field_formatter_info_alter(array &$info): void {
  // If a user has been invited to the group to which user does not have an
  // access, the user cannot see the group title on the group invite page.
  // The problem is default group title rendered through the
  // "entity_reference_label" field formatter checks access to the entity.
  // Altered class to skip this check for the group entity on the group invites
  // page.
  if (isset($info['entity_reference_label'])) {
    $info['entity_reference_label']['class'] = EntityReferenceLabelFormatterOverrider::class;
  }
}
+56 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\social_group_invite\Plugin\Field\FieldFormatter;

use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceLabelFormatter;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Provides overrider for 'entity reference label' formatter.
 */
class EntityReferenceLabelFormatterOverrider extends EntityReferenceLabelFormatter {

  /**
   * The entity repository.
   */
  protected EntityRepositoryInterface $entityRepository;

  /**
   * The current route match.
   */
  protected RouteMatchInterface $routeMatch;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    $instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
    $instance->entityRepository = $container->get('entity.repository');
    $instance->routeMatch = $container->get('current_route_match');
    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntitiesToView(EntityReferenceFieldItemListInterface $items, $langcode) {
    if ($items->getName() !== 'gid' || $this->routeMatch->getRouteName() !== 'view.social_group_user_invitations.page_1') {
      return parent::getEntitiesToView($items, $langcode);
    }

    $entities = [];
    /**
     * @var int $delta
     * @var \Drupal\group\Entity\GroupInterface $entity
     */
    foreach ($items->referencedEntities() as $delta => $entity) {
      $entities[$delta] = $this->entityRepository->getTranslationFromContext($entity, $langcode);
    }

    return $entities;
  }

}