View source
<?php
namespace Drupal\social_event_managers\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Render\RendererInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
use Drupal\social_event\Entity\EventEnrollment;
use Drupal\node\NodeInterface;
class SocialEventManagersAddEnrolleeForm extends FormBase {
protected $entityTypeManager;
protected $renderer;
public function __construct(EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer) {
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('renderer'));
}
public function getFormId() {
return 'social_event_managers_enrollment_add';
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$enroll_uid = $form_state
->getValue('entity_id_new');
$event = $form_state
->getValue('node_id');
$count = 0;
if (!empty($event) && !empty($enroll_uid)) {
foreach ($enroll_uid as $uid => $target_id) {
$enrollment = EventEnrollment::create([
'user_id' => \Drupal::currentUser()
->id(),
'field_event' => $event,
'field_enrollment_status' => '1',
'field_account' => $uid,
]);
$enrollment
->save();
$count++;
}
if (!empty($count)) {
$singular = '@count new member is enrolled to this event.';
$plural = '@count new members are enrolled to this event.';
if (social_event_manager_or_organizer(NULL, TRUE)) {
$singular = '@count new member is enrolled to your event.';
$plural = '@count new members are enrolled to your event.';
}
$message = $this
->formatPlural($count, $singular, $plural);
\Drupal::messenger()
->addMessage($message, 'status');
}
$url = Url::fromRoute('view.event_manage_enrollments.page_manage_enrollments', [
'node' => $event,
]);
$form_state
->setRedirectUrl($url);
}
}
public function buildForm(array $form, FormStateInterface $form_state) {
$form['#attributes']['class'][] = 'card card__block form--default form-wrapper form-group';
if (empty($nid)) {
$node = \Drupal::routeMatch()
->getParameter('node');
if ($node instanceof NodeInterface) {
$nid = $node
->id();
}
elseif (!is_object($node)) {
$nid = $node;
}
}
$storage = \Drupal::entityTypeManager()
->getStorage('event_enrollment');
$enrollments = $storage
->loadByProperties([
'field_event' => $nid,
]);
$enrollmentIds = [];
foreach ($enrollments as $enrollment) {
$enrollmentIds[] = $enrollment
->getAccount();
}
$form['name'] = [
'#type' => 'social_enrollment_entity_autocomplete',
'#selection_handler' => 'social',
'#selection_settings' => [
'skip_entity' => $enrollmentIds,
],
'#target_type' => 'user',
'#tags' => TRUE,
'#description' => $this
->t('To add multiple members, separate each member with a comma ( , ).'),
'#title' => $this
->t('Select members to add'),
];
$form['actions']['cancel'] = [
'#type' => 'link',
'#title' => t('Cancel'),
'#url' => Url::fromRoute('view.event_manage_enrollments.page_manage_enrollments', [
'node' => $nid,
]),
];
$form['actions']['submit'] = [
'#prefix' => '<div class="form-actions">',
'#suffix' => '</div>',
'#type' => 'submit',
'#value' => $this
->t('Save'),
'#button_type' => 'primary',
];
$form['#cache']['contexts'][] = 'user';
return $form;
}
}