class OpignoUserSelectionController in Opigno Learning path 3.x
Returns responses for Opigno User Selection routes.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\opigno_user_selection\Controller\OpignoUserSelectionController
Expanded class hierarchy of OpignoUserSelectionController
1 string reference to 'OpignoUserSelectionController'
- opigno_user_selection.routing.yml in modules/
opigno_user_selection/ opigno_user_selection.routing.yml - modules/opigno_user_selection/opigno_user_selection.routing.yml
File
- modules/
opigno_user_selection/ src/ Controller/ OpignoUserSelectionController.php, line 21
Namespace
Drupal\opigno_user_selection\ControllerView source
class OpignoUserSelectionController extends ControllerBase {
/**
* Service "request_stack" definition.
*
* @var \Symfony\Component\HttpFoundation\Request|null
*/
protected $currentRequest;
/**
* Service "group.membership_loader" definition.
*
* @var \Drupal\group\GroupMembershipLoaderInterface
*/
protected $groupMembershipLoader;
/**
* {@inheritdoc}
*/
public function __construct(RequestStack $request_stack, GroupMembershipLoaderInterface $membership_loader) {
$this->currentRequest = $request_stack
->getCurrentRequest();
$this->groupMembershipLoader = $membership_loader;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack'), $container
->get('group.membership_loader'));
}
/**
* {@inheritdoc}
*
* @opigno_deprecated
*/
protected function getEntityField(EntityInterface $entity, string $field_name) {
/** @var \Drupal\Core\Entity\FieldableEntityInterface $user */
if ($entity
->hasField($field_name) && !($field = $entity
->get($field_name))
->isEmpty()) {
if (($child_entity = $field->entity) instanceof EntityInterface) {
return $child_entity;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*
* @opigno_deprecated
*/
public function getuserAvatar($user) {
$default_image = file_create_url(drupal_get_path('module', 'opigno_user_selection') . '/assets/profile.svg');
$image_style = ImageStyle::load('thumbnail');
if (!$image_style instanceof ImageStyle) {
return $default_image;
}
/** @var \Drupal\Core\Entity\FieldableEntityInterface $user */
if (!($file = $this
->getEntityField($user, 'user_picture'))) {
return $default_image;
}
return $image_style
->buildUrl($file
->getFileUri());
}
/**
* {@inheritdoc}
*
* @opigno_deprecated
*/
public function getGroupImage($type, $group) {
$default_image = file_create_url(drupal_get_path('module', 'opigno_user_selection') . '/assets/' . $type . '.svg');
$image_style = ImageStyle::load('thumbnail');
if (!$image_style instanceof ImageStyle) {
return $default_image;
}
$media = $this
->getEntityField($group, 'field_learning_path_media_image');
if (!$media instanceof Media) {
return $default_image;
}
$file = $this
->getEntityField($media, 'field_media_image');
if (!$file instanceof File) {
return $default_image;
}
return $image_style
->buildUrl($file
->getFileUri());
}
/**
* {@inheritdoc}
*/
public function post($data = NULL) {
$content = $this->currentRequest
->getContent();
if (!empty($content)) {
// 2nd param to get as array.
$data = json_decode($content, TRUE);
}
$groups_id = [];
$response_data = [];
$meta = new CacheableMetadata();
$meta
->setCacheMaxAge(Cache::PERMANENT);
/** @var \Drupal\user\Entity\User[] $users */
$users = $this
->entityTypeManager()
->getStorage('user')
->loadMultiple($data ?: []);
$map = [
'learning_path' => 'training',
'opigno_class' => 'class',
];
$response_data['users'] = array_map(function ($user) use (&$groups_id, $meta, $map) {
$meta
->addCacheableDependency($user);
$memberships = $this->groupMembershipLoader
->loadByUser($user);
/** @var \Drupal\user\Entity\User $user */
return [
'id' => $user
->id(),
'name' => $user
->getDisplayName(),
'email' => '',
'avatar' => $this
->getuserAvatar($user),
'member' => array_filter(array_map(function (GroupMembership $membership) use (&$groups_id, $map) {
$group = $membership
->getGroup();
if (array_key_exists($group
->bundle(), $map)) {
$groups_id[] = $gid = (int) $group
->id();
return $gid;
}
return NULL;
}, $memberships)),
];
}, $users);
/** @var \Drupal\group\Entity\Group[] $groups */
$groups = $this
->entityTypeManager()
->getStorage('group')
->loadMultiple($groups_id ?: []);
$response_data['members'] = array_map(function ($group) use ($meta, $map) {
$meta
->addCacheableDependency($group);
$memberships = $this->groupMembershipLoader
->loadByGroup($group);
/** @var \Drupal\group\Entity\Group $group */
return [
"id" => $group
->id(),
"type" => $map[$group
->bundle()],
"info" => [
"name" => $group
->label(),
'avatar' => $this
->getGroupImage($map[$group
->bundle()], $group),
],
"key" => $map[$group
->bundle()] . "_" . $group
->id(),
"loaded" => TRUE,
"members" => array_map(function (GroupMembership $membership) {
return (int) $membership
->getGroupContent()
->getEntity()
->id();
}, $memberships),
];
}, $groups);
$response = new CacheableJsonResponse($response_data);
$response
->addCacheableDependency($meta);
return $response;
}
/**
* {@inheritdoc}
*/
public function training($data = NULL) {
$content = $this->currentRequest
->getContent();
if (!empty($content)) {
// 2nd param to get as array.
$data = json_decode($content, TRUE);
}
$response_data = [];
$meta = new CacheableMetadata();
$meta
->setCacheMaxAge(Cache::PERMANENT);
$map = [
'learning_path' => 'training',
'opigno_class' => 'class',
];
/** @var \Drupal\group\Entity\Group[] $groups */
$groups = $this
->entityTypeManager()
->getStorage('group')
->loadMultiple($data ?: []);
// Response_data key should be "users",
$response_data['users'] = array_map(function ($group) use ($meta, $map) {
$meta
->addCacheableDependency($group);
/** @var \Drupal\group\Entity\Group $group */
return [
'id' => $group
->id(),
'name' => $group
->label(),
'email' => '',
'avatar' => $this
->getGroupImage($map[$group
->bundle()], $group),
];
}, $groups);
$response = new CacheableJsonResponse($response_data);
$response
->addCacheableDependency($meta);
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function | Returns a redirect response object for the specified route. | |
ControllerBase:: |
protected | function | Returns the state storage service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
MessengerTrait:: |
public | function | Sets the messenger. | |
OpignoUserSelectionController:: |
protected | property | Service "request_stack" definition. | |
OpignoUserSelectionController:: |
protected | property | Service "group.membership_loader" definition. | |
OpignoUserSelectionController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
OpignoUserSelectionController:: |
protected | function | @opigno_deprecated | |
OpignoUserSelectionController:: |
public | function | @opigno_deprecated | |
OpignoUserSelectionController:: |
public | function | @opigno_deprecated | |
OpignoUserSelectionController:: |
public | function | ||
OpignoUserSelectionController:: |
public | function | ||
OpignoUserSelectionController:: |
public | function | ||
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 4 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |