View source
<?php
namespace Drupal\social_event_managers\Plugin\views\field;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\Core\TempStore\PrivateTempStoreFactory;
use Drupal\views_bulk_operations\Plugin\views\field\ViewsBulkOperationsBulkForm;
use Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionManager;
use Drupal\views_bulk_operations\Service\ViewsBulkOperationsActionProcessorInterface;
use Drupal\views_bulk_operations\Service\ViewsbulkOperationsViewDataInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Drupal\node\NodeInterface;
class SocialEventManagersViewsBulkOperationsBulkForm extends ViewsBulkOperationsBulkForm {
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, ViewsbulkOperationsViewDataInterface $viewData, ViewsBulkOperationsActionManager $actionManager, ViewsBulkOperationsActionProcessorInterface $actionProcessor, PrivateTempStoreFactory $tempStoreFactory, AccountInterface $currentUser, RequestStack $requestStack, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $viewData, $actionManager, $actionProcessor, $tempStoreFactory, $currentUser, $requestStack);
$this->entityTypeManager = $entity_type_manager;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('views_bulk_operations.data'), $container
->get('plugin.manager.views_bulk_operations_action'), $container
->get('views_bulk_operations.processor'), $container
->get('tempstore.private'), $container
->get('current_user'), $container
->get('request_stack'), $container
->get('entity_type.manager'));
}
public function getBulkOptions() {
$bulk_options = parent::getBulkOptions();
if ($this->view
->id() !== 'event_manage_enrollments') {
return $bulk_options;
}
foreach ($bulk_options as $id => &$label) {
if (!empty($this->options['preconfiguration'][$id]['label_override'])) {
$real_label = $this->options['preconfiguration'][$id]['label_override'];
}
else {
$real_label = $this->actions[$id]['label'];
}
$label = $this
->t('<b>@action</b> selected enrollees', [
'@action' => $real_label,
]);
}
return $bulk_options;
}
public function viewsForm(array &$form, FormStateInterface $form_state) {
$this->view
->setExposedInput([
'status' => TRUE,
]);
parent::viewsForm($form, $form_state);
if ($this->view
->id() !== 'event_manage_enrollments') {
return;
}
$action_options = $this
->getBulkOptions();
if (!empty($this->view->result) && !empty($action_options)) {
$list =& $form[$this->options['id']];
foreach ($this->view->result as $row_index => $row) {
$entity = $this
->getEntity($row);
$list[$row_index]['#title'] = $this
->getEntityLabel($entity);
}
}
$event = social_event_get_current_event();
if (!$event) {
return;
}
$tempstoreData = $this
->getTempstoreData($this->view
->id(), $this->view->current_display);
if (!empty($tempstoreData['event_id']) && $tempstoreData['event_id'] !== $event
->id()) {
$this
->deleteTempstoreData($this->view
->id(), $this->view->current_display);
$this
->updateTempstoreData();
$tempstoreData = $this
->getTempstoreData($this->view
->id(), $this->view->current_display);
}
$tempstoreData['event_id'] = $event
->id();
$this
->setTempstoreData($tempstoreData, $this->view
->id(), $this->view->current_display);
if (!empty($form['header'])) {
$multipage = $form['header'][$this->options['id']]['multipage'];
unset($form['header'][$this->options['id']]['multipage']);
$form['header'][$this->options['id']]['multipage'] = $multipage;
}
$wrapper =& $form['header'][$this->options['id']];
if (!empty($event
->id())) {
$wrapper['multipage']['#attributes']['event-id'] = $event
->id();
if (!empty($wrapper['multipage']['#attributes']['data-display-id'])) {
$current_display = $wrapper['multipage']['#attributes']['data-display-id'];
$wrapper['multipage']['#attributes']['data-display-id'] = $current_display . '/' . $event
->id();
}
}
$wrapper['#attributes']['class'][] = 'card';
$wrapper['#attributes']['class'][] = 'card__block';
$form['#attached']['library'][] = 'social_event_managers/views_bulk_operations.frontUi';
if (!empty($wrapper['select_all'])) {
$wrapper['select_all']['#title'] = $this
->t('Select / unselect all @count members across all the pages', [
'@count' => $this->tempStoreData['total_results'] ? ' ' . $this->tempStoreData['total_results'] : '',
]);
$form['header'][$this->options['id']]['select_all']['#attributes']['class'][] = 'form-no-label';
$form['header'][$this->options['id']]['select_all']['#attributes']['class'][] = 'checkbox';
}
$count = 0;
if (!empty($wrapper['multipage']) && !empty($wrapper['multipage']['#title'])) {
$title = $wrapper['multipage']['#title'];
$arguments = $title
->getArguments();
$count = empty($arguments['%count']) ? 0 : $arguments['%count'];
}
$title = $this
->formatPlural($count, '<b><em class="placeholder">@count</em> enrollee</b> is selected', '<b><em class="placeholder">@count</em> enrollees</b> are selected');
$wrapper['multipage']['#title'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#value' => $title,
];
$wrapper['multipage']['#attributes']['class'][] = 'vbo-multipage-selector';
$tempstoreData = $this
->getTempstoreData($this->view
->id(), $this->view->current_display);
if (!empty($wrapper['multipage']['list']['#items']) && count($wrapper['multipage']['list']['#items']) > 0) {
$excluded = FALSE;
if (!empty($tempstoreData['exclude_mode']) && $tempstoreData['exclude_mode']) {
$excluded = TRUE;
}
$wrapper['multipage']['list']['#title'] = !$excluded ? $this
->t('See selected enrollees on other pages') : $this
->t('See excluded enrollees on other pages');
}
if (!empty($wrapper['multipage']['clear'])) {
$wrapper['multipage']['clear']['#value'] = $this
->t('Clear all selected enrollees');
$wrapper['multipage']['clear']['#attributes']['class'][] = 'btn-default dropdown-toggle waves-effect waves-btn margin-top-l margin-left-m';
}
$actions =& $wrapper['actions'];
if (!empty($actions) && !empty($wrapper['action'])) {
$actions['#theme'] = 'links__dropbutton__operations__actions';
$actions['#label'] = $this
->t('Actions');
$actions['#type'] = 'dropbutton';
$items = [];
foreach ($wrapper['action']['#options'] as $key => $value) {
if (!empty($key) && array_key_exists($key, $this->bulkOptions)) {
$items[] = [
'#type' => 'submit',
'#value' => $value,
];
}
}
$actions['#links'] = $items;
}
$form['actions']['#type'] = 'hidden';
$form['header']['social_views_bulk_operations_bulk_form_enrollments_1']['action']['#access'] = FALSE;
}
public function viewsFormValidate(&$form, FormStateInterface $form_state) {
if ($this->view
->id() === 'event_manage_enrollments') {
$user_input = $form_state
->getUserInput();
$available_options = $this
->getBulkOptions();
foreach (Element::children($this->actions) as $action) {
if (empty($available_options[$action])) {
continue;
}
$label = $available_options[$action];
if (strip_tags($label
->render()) === $user_input['op']) {
$user_input['action'] = $action;
$form_state
->setUserInput($user_input);
$form_state
->setValue('action', $action);
$form_state
->setTriggeringElement($this->actions[$action]);
break;
}
}
}
parent::viewsFormValidate($form, $form_state);
}
public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
parent::viewsFormSubmit($form, $form_state);
if ($form_state
->get('step') === 'views_form_views_form' && $this->view
->id() === 'event_manage_enrollments') {
$url = $form_state
->getRedirect();
if ($url
->getRouteName() === 'views_bulk_operations.execute_configurable') {
$parameters = $url
->getRouteParameters();
if (empty($parameters['node'])) {
$node = \Drupal::routeMatch()
->getParameter('node');
if ($node instanceof NodeInterface) {
$parameters['node'] = $node
->id();
}
elseif (!is_object($node)) {
$parameters['node'] = $node;
}
}
$url = Url::fromRoute('social_event_managers.vbo.execute_configurable', [
'node' => $parameters['node'],
]);
$form_state
->setRedirectUrl($url);
}
}
}
public function getEntityLabel(EntityInterface $entity) {
$profiles = $this->entityTypeManager
->getStorage('profile')
->loadByProperties([
'uid' => $entity->field_account->target_id,
]);
$profile = reset($profiles);
if (!$profile) {
$name = '';
$first_name = $entity
->get('field_first_name')
->getValue()[0]['value'];
$last_name = $entity
->get('field_last_name')
->getValue()[0]['value'];
if (!empty($first_name)) {
$name .= $first_name;
$name .= ' ';
}
if (!empty($last_name)) {
$name .= $last_name;
}
return $name;
}
$label = $profile
->label();
return $label
->getArguments()['@name'];
}
}