View source
<?php
namespace Drupal\social_user_export\Form;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\user\PrivateTempStoreFactory;
use Drupal\user\UserStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Url;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\User;
class ExportUserConfirm extends ConfirmFormBase {
protected $tempStoreFactory;
protected $userStorage;
protected $entityManager;
public function __construct(PrivateTempStoreFactory $temp_store_factory, UserStorageInterface $user_storage, EntityTypeManagerInterface $entity_manager) {
$this->tempStoreFactory = $temp_store_factory;
$this->userStorage = $user_storage;
$this->entityManager = $entity_manager;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('user.private_tempstore'), $container
->get('entity.manager')
->getStorage('user'), $container
->get('entity.manager'));
}
public function getFormId() {
return 'user_export_confirm';
}
public function getQuestion() {
return $this
->t('Export users');
}
public function getCancelUrl() {
return new Url('entity.user.collection');
}
public function getConfirmText() {
return $this
->t('Export users');
}
public function buildForm(array $form, FormStateInterface $form_state) {
$data = $this->tempStoreFactory
->get('user_operations_export')
->get($this
->currentUser()
->id());
if (!$data) {
return $this
->redirect('entity.user.collection');
}
$export_params = [
'apply_all' => !empty($data['apply_all']),
'accounts' => [],
'query' => [],
];
if (empty($data['apply_all'])) {
$form['accounts'] = [
'#prefix' => '<ul>',
'#suffix' => '</ul>',
'#tree' => TRUE,
];
foreach ($data['entities'] as $account) {
$export_params['accounts'][] = $account
->id();
$form['accounts'][] = [
'#type' => 'markup',
'#markup' => '<li>' . $account
->getDisplayName() . '</li>',
];
}
}
else {
if (empty($data['query'])) {
$data['query'] = [];
}
$export_params['query'] = $data['query'];
$view = _social_user_export_get_view($data['query']);
$form['message'] = [
'#type' => 'item',
'#markup' => t('@count users will be exported', [
'@count' => $view->total_rows,
]),
];
}
$form_state
->set('export_params', $export_params);
$form = parent::buildForm($form, $form_state);
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$export_params = $form_state
->get('export_params');
if ($form_state
->getValue('confirm')) {
$batch = [
'title' => t('Exporting users'),
'operations' => [],
'finished' => [
'\\Drupal\\social_user_export\\ExportUser',
'finishedCallback',
],
];
if ($export_params['apply_all']) {
$batch['operations'][] = [
[
'\\Drupal\\social_user_export\\ExportUser',
'exportUsersAllOperation',
],
[
$export_params['query'],
],
];
}
else {
foreach ($export_params['accounts'] as $uid) {
$batch['operations'][] = [
[
'\\Drupal\\social_user_export\\ExportUser',
'exportUserOperation',
],
[
User::load($uid),
],
];
}
$batch['operations'] = array_reverse($batch['operations']);
}
batch_set($batch);
}
$form_state
->setRedirect('entity.user.collection');
}
public function getDescription() {
return $this
->t('Users will be exported to CSV file');
}
}