You are here

JoinService.php in Opigno Learning path 3.x

Same filename and directory in other branches
  1. 8 src/JoinService.php

File

src/JoinService.php
View source
<?php

namespace Drupal\opigno_learning_path;

use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Entity\EntityFormBuilderInterface;
use Drupal\group\Entity\GroupContent;

/**
 * Class JoinService.
 */
class JoinService {

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The entity form builder.
   *
   * @var \Drupal\Core\Entity\EntityFormBuilderInterface
   */
  protected $entityFormBuilder;

  /**
   * Constructs a new JoinService object.
   */
  public function __construct(AccountInterface $current_user, EntityFormBuilderInterface $entity_form_builder) {
    $this->currentUser = $current_user;
    $this->entityFormBuilder = $entity_form_builder;
  }

  /**
   * {@inheritdoc}
   */
  public function getForm($group) {
    $plugin = $group
      ->getGroupType()
      ->getContentPlugin('group_membership');

    // Pre-populate a group membership with the current user.
    $group_content = GroupContent::create([
      'type' => $plugin
        ->getContentTypeConfigId(),
      'gid' => $group
        ->id(),
      'entity_id' => $this->currentUser
        ->id(),
    ]);
    return $this->entityFormBuilder
      ->getForm($group_content, 'group-join');
  }

}

Classes

Namesort descending Description
JoinService Class JoinService.