You are here

class ExportUserConfirm in Open Social 8.4

Same name and namespace in other branches
  1. 8 modules/social_features/social_user_export/src/Form/ExportUserConfirm.php \Drupal\social_user_export\Form\ExportUserConfirm
  2. 8.2 modules/social_features/social_user_export/src/Form/ExportUserConfirm.php \Drupal\social_user_export\Form\ExportUserConfirm
  3. 8.3 modules/social_features/social_user_export/src/Form/ExportUserConfirm.php \Drupal\social_user_export\Form\ExportUserConfirm

Class ExportUserConfirm.

@package Drupal\social_user_export\Form

Hierarchy

Expanded class hierarchy of ExportUserConfirm

1 string reference to 'ExportUserConfirm'
social_user_export.routing.yml in modules/social_features/social_user_export/social_user_export.routing.yml
modules/social_features/social_user_export/social_user_export.routing.yml

File

modules/social_features/social_user_export/src/Form/ExportUserConfirm.php, line 19

Namespace

Drupal\social_user_export\Form
View source
class ExportUserConfirm extends ConfirmFormBase {

  /**
   * The temp store factory.
   *
   * @var \Drupal\user\PrivateTempStoreFactory
   */
  protected $tempStoreFactory;

  /**
   * The user storage.
   *
   * @var \Drupal\user\UserStorageInterface
   */
  protected $userStorage;

  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
   * Constructs a new ExportUserConfirm.
   *
   * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
   *   The temp store factory.
   * @param \Drupal\user\UserStorageInterface $user_storage
   *   The user storage.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_manager
   *   The entity manager.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('user.private_tempstore'), $container
      ->get('entity.manager')
      ->getStorage('user'), $container
      ->get('entity.manager'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'user_export_confirm';
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Export users');
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('entity.user.collection');
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this
      ->t('Export users');
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    // Retrieve the data to be exported from the temp store.
    $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 not selected all items, show list of selected items,
    // else show quantity of all items.
    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;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $export_params = $form_state
      ->get('export_params');
    if ($form_state
      ->getValue('confirm')) {

      // Define the batch.
      $batch = [
        'title' => t('Exporting users'),
        'operations' => [],
        'finished' => [
          '\\Drupal\\social_user_export\\ExportUser',
          'finishedCallback',
        ],
      ];

      // If selected all users, add single massive operation,
      // else add one operation for each user.
      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');
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
    return $this
      ->t('Users will be exported to CSV file');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfirmFormBase::getCancelText public function Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface::getCancelText 1
ConfirmFormBase::getFormName public function Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface::getFormName
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
ExportUserConfirm::$entityManager protected property The entity manager.
ExportUserConfirm::$tempStoreFactory protected property The temp store factory.
ExportUserConfirm::$userStorage protected property The user storage.
ExportUserConfirm::buildForm public function Form constructor. Overrides ConfirmFormBase::buildForm
ExportUserConfirm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ExportUserConfirm::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
ExportUserConfirm::getConfirmText public function Returns a caption for the button that confirms the action. Overrides ConfirmFormBase::getConfirmText
ExportUserConfirm::getDescription public function Returns additional text to display as a description. Overrides ConfirmFormBase::getDescription
ExportUserConfirm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ExportUserConfirm::getQuestion public function Returns the question to ask the user. Overrides ConfirmFormInterface::getQuestion
ExportUserConfirm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ExportUserConfirm::__construct public function Constructs a new ExportUserConfirm.
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.