Commit 7388281e authored by Nikolay Lobachev's avatar Nikolay Lobachev
Browse files

Issue #3308964 by LOBsTerr: Refactor GroupInvitationLoader class

parent 66ce2943
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -45,10 +45,10 @@ function ginvite_help($route_name, RouteMatchInterface $route_match) {
 * Implements hook_form_BASE_FORM_ID_alter() for group_content_form.
 */
function ginvite_form_group_content_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  /** @var \Drupal\Core\Entity\ContentEntityFormInterface $formObject */
  $formObject = $form_state->getFormObject();
  /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
  $form_object = $form_state->getFormObject();
  /** @var \Drupal\group\Entity\GroupContentInterface $group_content */
  $group_content = $formObject->getEntity();
  $group_content = $form_object->getEntity();
  $content_plugin = $group_content->getContentPlugin();
  if ($content_plugin->getPluginId() === 'group_invitation') {
    $form += [
+56 −51
Original line number Diff line number Diff line
@@ -27,6 +27,20 @@ class GroupInvitationLoader implements GroupInvitationLoaderInterface {
   */
  protected $currentUser;

  /**
   * The group content type storage.
   *
   * @var \Drupal\group\Entity\Storage\GroupContentTypeStorageInterface
   */
  protected $groupContentTypeStorage;

  /**
   * The group content storage.
   *
   * @var \Drupal\group\Entity\Storage\GroupContentStorageInterface
   */
  protected $groupContentStorage;

  /**
   * Constructs a new GroupTypeController.
   *
@@ -38,32 +52,28 @@ class GroupInvitationLoader implements GroupInvitationLoaderInterface {
  public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user) {
    $this->entityTypeManager = $entity_type_manager;
    $this->currentUser = $current_user;
    $this->groupContentTypeStorage = $this->entityTypeManager->getStorage('group_content_type');
    $this->groupContentStorage = $this->entityTypeManager->getStorage('group_content');
  }

  /**
   * Gets the group content storage.
   *
   * @return \Drupal\group\Entity\Storage\GroupContentStorageInterface
   *   The group_content storage class.
   */
  protected function groupContentStorage() {
    return $this->entityTypeManager->getStorage('group_content');
  }

  /**
   * Wraps GroupContent entities in a GroupInvitation object.
   * Load group content entities and wrap in a GroupInvitation object.
   *
   * @param \Drupal\group\Entity\GroupContentInterface[] $entities
   *   An array of GroupContent entities to wrap.
   * @param array $filters
   *   An associative array where the keys are the property names and the
   *   values are the values those properties must have.
   *
   * @return \Drupal\ginvite\GroupInvitation[]
   *   A list of GroupInvitation wrapper objects.
   */
  protected function wrapGroupContentEntities(array $entities) {
  protected function loadGroupInvitations(array $filters) {
    $group_invitations = [];

    $entities = $this->groupContentStorage->loadByProperties($filters);
    foreach ($entities as $group_content) {
      $group_invitations[] = new GroupInvitationWrapper($group_content);
    }

    return $group_invitations;
  }

@@ -71,9 +81,10 @@ class GroupInvitationLoader implements GroupInvitationLoaderInterface {
   * {@inheritdoc}
   */
  public function load(GroupInterface $group, AccountInterface $account) {
    $filters = ['entity_id' => $account->id()];
    $group_contents = $this->groupContentStorage()->loadByGroup($group, 'group_invitation', $filters);
    $group_invitations = $this->wrapGroupContentEntities($group_contents);
    $group_invitations = $this->loadByProperties([
      'entity_id' => $account->id(),
      'gid' => $group->id(),
    ]);
    return $group_invitations ? reset($group_invitations) : FALSE;
  }

@@ -83,6 +94,7 @@ class GroupInvitationLoader implements GroupInvitationLoaderInterface {
  public function loadByGroup(GroupInterface $group, $roles = NULL, $mail = NULL, $status = GroupInvitation::INVITATION_PENDING) {
    $filters = [
      'invitation_status' => $status,
      'gid' => $group->id(),
    ];

    if (isset($roles)) {
@@ -92,8 +104,7 @@ class GroupInvitationLoader implements GroupInvitationLoaderInterface {
      $filters['invitee_mail'] = $mail;
    }

    $group_contents = $this->groupContentStorage()->loadByGroup($group, 'group_invitation', $filters);
    return $this->wrapGroupContentEntities($group_contents);
    return $this->loadByProperties($filters);
  }

  /**
@@ -108,62 +119,56 @@ class GroupInvitationLoader implements GroupInvitationLoaderInterface {
      return [];
    }

    // Load all group content types for the invitation content enabler plugin.
    $group_content_types = $this->entityTypeManager
      ->getStorage('group_content_type')
      ->loadByProperties(['content_plugin' => 'group_invitation']);

    // If none were found, there can be no invitations either.
    if (empty($group_content_types)) {
      return [];
    }

    // Try to load all possible invitation group content for the user.
    $group_content_type_ids = [];
    foreach ($group_content_types as $group_content_type) {
      $group_content_type_ids[] = $group_content_type->id();
    }

    $properties = [
      'type' => $group_content_type_ids,
    $filters = [
      'entity_id' => $account->id(),
      'invitation_status' => $status,
      'invitee_mail' => $account->getEmail(),
    ];

    if (isset($roles)) {
      $properties['group_roles'] = (array) $roles;
      $filters['group_roles'] = (array) $roles;
    }

    /** @var \Drupal\group\Entity\GroupContentInterface[] $group_contents */
    $group_contents = $this->groupContentStorage()->loadByProperties($properties);
    return $this->wrapGroupContentEntities($group_contents);
    return $this->loadByProperties($filters);
  }

  /**
   * {@inheritdoc}
   */
  public function loadByProperties(array $values) {
    // Load all group content types for the invitation content enabler plugin.
  public function loadByProperties(array $filters = []) {
    // Try to load all possible invitation group content for the user.
    $group_content_type_ids = $this->loadGroupContentTypeIds();
    if (empty($group_content_type_ids)) {
      return [];
    }

    $filters['type'] = $group_content_type_ids;
    return $this->loadGroupInvitations($filters);
  }

  /**
   * Load group content type ids.
   *
   * @return array
   *   Group content type ids.
   */
  protected function loadGroupContentTypeIds() {
    $group_content_type_ids = [];
    // Load all group content types for the invitation group relation plugin.
    $group_content_types = $this->entityTypeManager
      ->getStorage('group_content_type')
      ->loadByProperties(['content_plugin' => 'group_invitation']);

    // If none were found, there can be no invitations either.
    if (empty($group_content_types)) {
      return [];
      return $group_content_type_ids;
    }

    // Try to load all possible invitation group content for the user.
    $group_content_type_ids = [];
    foreach ($group_content_types as $group_content_type) {
      $group_content_type_ids[] = $group_content_type->id();
    }

    $values['type'] = $group_content_type_ids;

    /** @var \Drupal\group\Entity\GroupContentInterface[] $group_contents */
    $group_contents = $this->groupContentStorage()->loadByProperties($values);
    return $this->wrapGroupContentEntities($group_contents);
    return $group_content_type_ids;
  }

}
+9 −5
Original line number Diff line number Diff line
@@ -32,11 +32,15 @@ interface GroupInvitationLoaderInterface {
   * @param string|array $roles
   *   (optional) A group role machine name or a list of group role machine
   *   names to filter on. Valid results only need to match on one role.
   * @param string $mail
   *   Mail.
   * @param int $status
   *   Invitation status.
   *
   * @return \Drupal\ginvite\GroupInvitation[]
   *   The loaded GroupInvitations matching the criteria.
   */
  public function loadByGroup(GroupInterface $group, $roles = NULL);
  public function loadByGroup(GroupInterface $group, $roles = NULL, $mail = NULL, $status = GroupInvitation::INVITATION_PENDING);

  /**
   * Loads all invitations for a user.
@@ -58,13 +62,13 @@ interface GroupInvitationLoaderInterface {
  /**
   * Load Invitations by their property values.
   *
   * @param array $values
   *   An associative array where the keys are the property names and the
   *   values are the values those properties must have.
   * @param array $filters
   *   An associative array of extra filters where the keys are
   *   property or field names and the values are the value to filter on.
   *
   * @return \Drupal\ginvite\GroupInvitation[]
   *   The loaded GroupInvitations matching the criteria.
   */
  public function loadByProperties(array $values);
  public function loadByProperties(array $filters = []);

}