class EnrollActionForm in Open Social 8
Same name and namespace in other branches
- 8.9 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 8.2 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 8.3 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 8.4 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 8.5 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 8.6 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 8.7 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 8.8 modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 10.3.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 10.0.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 10.1.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
- 10.2.x modules/social_features/social_event/src/Form/EnrollActionForm.php \Drupal\social_event\Form\EnrollActionForm
Class EnrollActionForm.
@package Drupal\social_event\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\social_event\Form\EnrollActionForm implements ContainerInjectionInterface
Expanded class hierarchy of EnrollActionForm
File
- modules/
social_features/ social_event/ src/ Form/ EnrollActionForm.php, line 28
Namespace
Drupal\social_event\FormView source
class EnrollActionForm extends FormBase implements ContainerInjectionInterface {
/**
* The routing matcher to get the nid.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;
/**
* The node storage for event enrollments.
*
* @var \Drupal\Core\entity\EntityStorageInterface
*/
protected $entityStorage;
/**
* The user storage.
*
* @var \Drupal\user\UserStorageInterface
*/
protected $userStorage;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'enroll_action_form';
}
/**
* Constructs an Enroll Action Form.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
* The entity storage.
* @param \Drupal\user\UserStorageInterface $user_storage
* The user storage.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Session\AccountProxyInterface $currentUser
* The current user.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* The config factory.
*/
public function __construct(RouteMatchInterface $route_match, EntityStorageInterface $entity_storage, UserStorageInterface $user_storage, EntityTypeManagerInterface $entityTypeManager, AccountProxyInterface $currentUser, ConfigFactoryInterface $configFactory) {
$this->routeMatch = $route_match;
$this->entityStorage = $entity_storage;
$this->userStorage = $user_storage;
$this->entityTypeManager = $entityTypeManager;
$this->currentUser = $currentUser;
$this->configFactory = $configFactory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('current_route_match'), $container
->get('entity.manager')
->getStorage('event_enrollment'), $container
->get('entity.manager')
->getStorage('user'), $container
->get('entity_type.manager'), $container
->get('current_user'), $container
->get('config.factory'));
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$nid = $this->routeMatch
->getRawParameter('node');
$current_user = $this->currentUser;
$uid = $current_user
->id();
// We check if the node is placed in a Group I am a member of. If not,
// we are not going to build anything.
if (!empty($nid)) {
if (!is_object($nid) && !is_null($nid)) {
$node = $this->entityTypeManager
->getStorage('node')
->load($nid);
}
$groups = $this
->getGroups($node);
if (!empty($groups)) {
$group_type_ids = $this->configFactory
->getEditable('social_event.settings')
->get('enroll');
foreach ($groups as $group) {
$group_type_id = $group
->bundle();
if (in_array($group_type_id, $group_type_ids) && $group
->hasPermission('join group', $current_user)) {
break;
}
if ($group
->hasPermission('enroll to events in groups', $current_user) == FALSE) {
return [];
}
}
}
}
$form['event'] = [
'#type' => 'hidden',
'#value' => $nid,
];
$submit_text = $this
->t('Enroll');
$enrollment_open = TRUE;
// Add the enrollment closed label.
if ($this
->eventHasBeenFinished($node)) {
$submit_text = $this
->t('Event has passed');
$enrollment_open = FALSE;
}
$conditions = [
'field_account' => $uid,
'field_event' => $nid,
];
$to_enroll_status = '1';
$enrollments = $this->entityStorage
->loadByProperties($conditions);
if ($enrollment = array_pop($enrollments)) {
$current_enrollment_status = $enrollment->field_enrollment_status->value;
if ($current_enrollment_status === '1') {
$submit_text = $this
->t('Enrolled');
$to_enroll_status = '0';
}
}
$form['to_enroll_status'] = [
'#type' => 'hidden',
'#value' => $to_enroll_status,
];
$form['enroll_for_this_event'] = [
'#type' => 'submit',
'#value' => $submit_text,
'#disabled' => !$enrollment_open,
];
$form['#attributes']['name'] = 'enroll_action_form';
if (isset($enrollment->field_enrollment_status->value) && $enrollment->field_enrollment_status->value === '1') {
// Extra attributes needed for when a user is logged in. This will make
// sure the button acts like a dropwdown.
$form['enroll_for_this_event']['#attributes'] = [
'class' => [
'btn',
'btn-accent brand-bg-accent',
'btn-lg btn-raised',
'dropdown-toggle',
'waves-effect',
],
'autocomplete' => 'off',
'data-toggle' => 'dropdown',
'aria-haspopup' => 'true',
'aria-expanded' => 'false',
'data-caret' => 'true',
];
$cancel_text = $this
->t('Cancel enrollment');
// Add markup for the button so it will be a dropdown.
$form['feedback_user_has_enrolled'] = [
'#markup' => '<ul class="dropdown-menu dropdown-menu-right"><li><a href="#" class="enroll-form-submit"> ' . $cancel_text . ' </a></li></ul>',
];
$form['#attached']['library'][] = 'social_event/form_submit';
}
return $form;
}
/**
* Function to determine if an event has been finished.
*
* @param \Drupal\node\Entity\Node $node
* The event.
*
* @return bool
* TRUE if the evens is finished / completed.
*/
protected function eventHasBeenFinished(Node $node) {
// Use the start date when the end date is not set to determine if the
// event is closed.
/** @var \Drupal\Core\Datetime\DrupalDateTime $check_end_date */
$check_end_date = $node->field_event_date->date;
if (isset($node->field_event_date_end->date)) {
$check_end_date = $node->field_event_date_end->date;
}
$current_time = new DrupalDateTime();
// The event has finished if the end date is smaller than the current date.
if ($current_time > $check_end_date) {
return TRUE;
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$current_user = $this->currentUser;
$uid = $current_user
->id();
$nid = $form_state
->getValue('event');
// Redirect anonymous use to login page before enrolling to an event.
if ($uid === 0) {
$node_url = Url::fromRoute('entity.node.canonical', [
'node' => $nid,
])
->toString();
$form_state
->setRedirect('user.login', [], [
'query' => [
'destination' => $node_url,
],
]);
// Check if user can register accounts.
if ($this->configFactory
->get('user.settings')
->get('register') != USER_REGISTER_ADMINISTRATORS_ONLY) {
$log_in_url = Url::fromUserInput('/user/login');
$log_in_link = Link::fromTextAndUrl($this
->t('log in'), $log_in_url)
->toString();
$create_account_url = Url::fromUserInput('/user/register');
$create_account_link = Link::fromTextAndUrl($this
->t('create a new account'), $create_account_url)
->toString();
$message = $this
->t('Please @log_in or @create_account_link so that you can enroll to the event.', [
'@log_in' => $log_in_link,
'@create_account_link' => $create_account_link,
]);
}
else {
$log_in_url = Url::fromUserInput('/user/login');
$log_in_link = Link::fromTextAndUrl($this
->t('log in'), $log_in_url)
->toString();
$message = $this
->t('Please @log_in so that you can enroll to the event.', [
'@log_in' => $log_in_link,
]);
}
drupal_set_message($message);
return;
}
$to_enroll_status = $form_state
->getValue('to_enroll_status');
$conditions = [
'field_account' => $uid,
'field_event' => $nid,
];
$enrollments = $this->entityStorage
->loadByProperties($conditions);
// Invalidate cache for our enrollment cache tag in
// social_event_node_view_alter().
$cache_tag = 'enrollment:' . $nid . '-' . $uid;
Cache::invalidateTags([
$cache_tag,
]);
if ($enrollment = array_pop($enrollments)) {
$current_enrollment_status = $enrollment->field_enrollment_status->value;
if ($to_enroll_status === '0' && $current_enrollment_status === '1') {
$enrollment->field_enrollment_status->value = '0';
$enrollment
->save();
}
elseif ($to_enroll_status === '1' && $current_enrollment_status === '0') {
$enrollment->field_enrollment_status->value = '1';
$enrollment
->save();
}
}
else {
// Create a new enrollment for the event.
$enrollment = EventEnrollment::create([
'user_id' => $uid,
'field_event' => $nid,
'field_enrollment_status' => '1',
'field_account' => $uid,
]);
$enrollment
->save();
}
}
/**
* Get group object where event enrollment is posted in.
*
* Returns an array of Group Objects.
*
* @return array
* Array of group entities.
*/
public function getGroups($node) {
$groupcontents = GroupContent::loadByEntity($node);
$groups = [];
// Only react if it is actually posted inside a group.
if (!empty($groupcontents)) {
foreach ($groupcontents as $groupcontent) {
/* @var \Drupal\group\Entity\GroupContent $groupcontent */
$group = $groupcontent
->getGroup();
/* @var \Drupal\group\Entity\Group $group */
$groups[] = $group;
}
}
return $groups;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
EnrollActionForm:: |
protected | property |
The config factory. Overrides FormBase:: |
|
EnrollActionForm:: |
protected | property | The current user. | |
EnrollActionForm:: |
protected | property | The node storage for event enrollments. | |
EnrollActionForm:: |
protected | property | The entity type manager. | |
EnrollActionForm:: |
protected | property |
The routing matcher to get the nid. Overrides FormBase:: |
|
EnrollActionForm:: |
protected | property | The user storage. | |
EnrollActionForm:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
EnrollActionForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
EnrollActionForm:: |
protected | function | Function to determine if an event has been finished. | |
EnrollActionForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
EnrollActionForm:: |
public | function | Get group object where event enrollment is posted in. | |
EnrollActionForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
EnrollActionForm:: |
public | function | Constructs an Enroll Action Form. | |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator 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. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
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. | 1 |
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. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |