You are here

class ExportMultipleForm in Bibliography & Citation 2.0.x

Same name and namespace in other branches
  1. 8 modules/bibcite_export/src/Form/ExportMultipleForm.php \Drupal\bibcite_export\Form\ExportMultipleForm

Export multiple reference entities.

Hierarchy

Expanded class hierarchy of ExportMultipleForm

1 string reference to 'ExportMultipleForm'
bibcite_export.routing.yml in modules/bibcite_export/bibcite_export.routing.yml
modules/bibcite_export/bibcite_export.routing.yml

File

modules/bibcite_export/src/Form/ExportMultipleForm.php, line 16

Namespace

Drupal\bibcite_export\Form
View source
class ExportMultipleForm extends ConfirmFormBase {

  /**
   * The array of entities to export.
   *
   * @var array
   */
  protected $entityInfo = [];

  /**
   * The tempstore object.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStore
   */
  protected $tempStore;

  /**
   * The current user object.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * Bibcite format manager service.
   *
   * @var \Drupal\bibcite\Plugin\BibciteFormatManagerInterface
   */
  protected $formatManager;

  /**
   * Construct new ExportMultipleForm object.
   *
   * @param \Drupal\Core\TempStore\PrivateTempStoreFactory $temp_store_factory
   *   The tempstore factory.
   * @param \Drupal\bibcite\Plugin\BibciteFormatManagerInterface $format_manager
   *   The bibcite format manager.
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user object.
   */
  public function __construct(PrivateTempStoreFactory $temp_store_factory, BibciteFormatManagerInterface $format_manager, AccountInterface $current_user) {
    $this->tempStore = $temp_store_factory
      ->get('bibcite_export_multiple');
    $this->formatManager = $format_manager;
    $this->currentUser = $current_user;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('tempstore.private'), $container
      ->get('plugin.manager.bibcite_format'), $container
      ->get('current_user'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this
      ->t('Select the format to export these references.');
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $this->entityInfo = $this->tempStore
      ->get($this->currentUser
      ->id());
    $form['entities'] = [
      '#theme' => 'item_list',
      '#items' => $this->entityInfo,
    ];
    $form['format'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Export format'),
      '#options' => array_map(function ($format) {
        return $format['label'];
      }, $this->formatManager
        ->getExportDefinitions()),
      '#required' => TRUE,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $format = $this->formatManager
      ->createInstance($form_state
      ->getValue('format'));
    $entity_type = 'bibcite_reference';
    $ids = array_keys($this->entityInfo);
    $chunks = array_chunk($ids, 100);
    $operations = [];
    foreach ($chunks as $chunk) {
      $operations[] = [
        'bibcite_export_batch_list',
        [
          $chunk,
          $entity_type,
          $format,
        ],
      ];
    }
    $batch = [
      'title' => t('Export references'),
      'operations' => $operations,
      'file' => drupal_get_path('module', 'bibcite_export') . '/bibcite_export.batch.inc',
      'finished' => 'bibcite_export_batch_finished',
    ];
    batch_set($batch);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfirmFormBase::getCancelText public function Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface::getCancelText 2
ConfirmFormBase::getFormName public function Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface::getFormName
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
ExportMultipleForm::$currentUser protected property The current user object.
ExportMultipleForm::$entityInfo protected property The array of entities to export.
ExportMultipleForm::$formatManager protected property Bibcite format manager service.
ExportMultipleForm::$tempStore protected property The tempstore object.
ExportMultipleForm::buildForm public function Form constructor. Overrides ConfirmFormBase::buildForm
ExportMultipleForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ExportMultipleForm::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
ExportMultipleForm::getConfirmText public function Returns a caption for the button that confirms the action. Overrides ConfirmFormBase::getConfirmText
ExportMultipleForm::getDescription public function Returns additional text to display as a description. Overrides ConfirmFormBase::getDescription
ExportMultipleForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ExportMultipleForm::getQuestion public function Returns the question to ask the user. Overrides ConfirmFormInterface::getQuestion
ExportMultipleForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ExportMultipleForm::__construct public function Construct new ExportMultipleForm object.
FormBase::$configFactory protected property The config factory. 3
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. 3
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.
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 72
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. 27
MessengerTrait::messenger public function Gets the messenger. 27
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. 4
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.