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\user\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('user.private_tempstore'), $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);
}
}
if (!empty($this->view->pager) && method_exists($this->view->pager, 'hasMoreRecords')) {
$pagerData = [
'current' => $this->view->pager
->getCurrentPage(),
'more' => $this->view->pager
->hasMoreRecords(),
];
}
$display_select_all = isset($pagerData) && ($pagerData['more'] || $pagerData['current'] > 0);
if ($display_select_all) {
$form['header'][$this->options['id']]['select_all'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Select all @count results in this view', [
'@count' => $this->tempStoreData['total_results'] ? ' ' . $this->tempStoreData['total_results'] : '',
]),
'#attributes' => [
'class' => [
'vbo-select-all',
'form-no-label',
'checkbox',
],
],
];
}
$wrapper =& $form['header'][$this->options['id']];
$wrapper['#attributes']['class'][] = 'card';
$wrapper['#attributes']['class'][] = 'card__block';
$form['#attached']['library'][] = 'social_event_managers/views_bulk_operations.frontUi';
$count = isset($this->tempStoreData['list']) ? count($this->tempStoreData['list']) : 0;
$title = $this
->formatPlural($count, '<b>@count enrollee</b> is selected', '<b>@count enrollees</b> are selected');
$wrapper['multipage']['#title'] = [
'#type' => 'html_tag',
'#tag' => 'div',
'#attributes' => [
'class' => [
'placeholder',
],
],
'#value' => $title,
];
$wrapper['multipage']['list']['#title'] = $this
->t('See selected enrollees on other pages');
$items = isset($wrapper['multipage']['list']['#items']) ? count($wrapper['multipage']['list']['#items']) : 0;
if ($items < 1) {
unset($wrapper['multipage']['list']);
}
$actions =& $wrapper['actions'];
$actions['#theme'] = 'links__dropbutton__operations__actions';
$actions['#label'] = $this
->t('Actions');
unset($actions['#type']);
unset($wrapper['multipage']['clear']);
$labels = [];
foreach (Element::children($actions) as $action_id) {
$labels[$action_id] = $actions[$action_id]['#value'];
}
asort($labels);
foreach (array_keys($labels) as $weight => $action_id) {
$actions[$action_id]['#weight'] = $weight;
}
$items = [];
foreach (Element::children($actions, TRUE) as $key) {
$items[$key] = $actions[$key];
}
$actions['#links'] = $items;
$form['actions']['#access'] = FALSE;
}
public function viewsFormValidate(&$form, FormStateInterface $form_state) {
if ($this->view
->id() === 'event_manage_enrollments' && $this->options['buttons']) {
$user_input = $form_state
->getUserInput();
$actions =& $form['header'][$this->options['id']]['actions'];
foreach (Element::children($actions) as $action_id) {
$action =& $actions[$action_id];
if (isset($action['#access']) && !$action['#access']) {
continue;
}
$label = $action['#value'];
if (strip_tags($label
->render()) === $user_input['op']) {
$form_state
->setTriggeringElement($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'];
}
}