You are here

class ExportConfirmForm in Content Synchronizer 8

Same name and namespace in other branches
  1. 8.2 src/Form/ExportConfirmForm.php \Drupal\content_synchronizer\Form\ExportConfirmForm
  2. 3.x src/Form/ExportConfirmForm.php \Drupal\content_synchronizer\Form\ExportConfirmForm

Class ExportConfirmForm.

@package Drupal\content_synchronizer\Form

Hierarchy

Expanded class hierarchy of ExportConfirmForm

1 file declares its use of ExportConfirmForm
ExportAction.php in src/Plugin/Action/ExportAction.php
1 string reference to 'ExportConfirmForm'
content_synchronizer.routing.yml in ./content_synchronizer.routing.yml
content_synchronizer.routing.yml

File

src/Form/ExportConfirmForm.php, line 22

Namespace

Drupal\content_synchronizer\Form
View source
class ExportConfirmForm extends ConfirmFormBase {
  const FORM_ID = 'content_synchronizer.export_confirm';
  const ARCHIVE_PARAMS = 'archive';

  /**
   * The array of nodes to delete.
   *
   * @var string[][]
   */
  protected $nodeInfo = [];

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

  /**
   * The node storage.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $manager;

  /**
   * The export manager.
   *
   * @var \Drupal\content_synchronizer\Service\ExportManager
   */
  protected $exportManager;

  /**
   * Constructs a DeleteMultiple form object.
   *
   * @param \Drupal\user\PrivateTempStoreFactory $temp_store_factory
   *   The tempstore factory.
   * @param \Drupal\Core\Entity\EntityManagerInterface $manager
   *   The entity manager.
   */
  public function __construct(PrivateTempStoreFactory $temp_store_factory, EntityManagerInterface $manager) {
    $this->tempStoreFactory = $temp_store_factory;
    $this->storage = $manager
      ->getStorage('node');
    $this->exportManager = \Drupal::service(ExportManager::SERVICE_NAME);
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return new Url('system.admin_content');
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return static::FORM_ID;
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['content_synchronizer'] = [
      '#type' => 'fieldset',
      '#title' => t('Export'),
    ];
    $form['content_synchronizer']['quick_export'] = [
      '#type' => 'submit',
      '#value' => t('Export entity'),
      '#description' => t('Download the entity .zip file with dependencies'),
      '#button_type' => 'primary',
      '#submit' => [
        [
          $this,
          'onQuickExport',
        ],
      ],
    ];
    $exportsListOptions = $this->exportManager
      ->getExportsListOptions();
    if (!empty($exportsListOptions)) {
      $form['content_synchronizer']['exports_list'] = [
        '#type' => 'checkboxes',
        '#title' => t('Or add the entity to an existing export'),
        '#options' => $exportsListOptions,
      ];
      $form['content_synchronizer']['add_to_export'] = [
        '#type' => 'submit',
        '#value' => t('Add to the choosen export'),
        '#submit' => [
          [
            $this,
            'onAddToExport',
          ],
        ],
      ];
    }
    return $form;
  }

  /**
   * Action on quick export submit action.
   *
   * @param array $form
   *   The form build array.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The form state.
   */
  public function onQuickExport(array &$form, FormStateInterface $formState) {
    $entities = $this
      ->getEntities();
    $writer = new ExportEntityWriter();
    $writer
      ->initFromId(time());
    $batchExportProcessor = new BatchExportProcessor($writer);
    $batchExportProcessor
      ->exportEntities($entities, [
      $this,
      'onBatchEnd',
    ]);
  }

  /**
   * On batch end redirect to the form url.
   *
   * @param string $archiveUri
   *   THe archive to download.
   */
  public function onBatchEnd($archiveUri) {
    $redirectUrl = $this
      ->getTmpStoredData('url');
    \Drupal::service(ArchiveDownloader::SERVICE_NAME)
      ->redirectWithArchivePath($redirectUrl, $archiveUri);
  }

  /**
   * Add entity to an existing entity export.
   *
   * @param array $form
   *   The form build array.
   * @param \Drupal\Core\Form\FormStateInterface $formState
   *   The form state.
   */
  public function onAddToExport(array &$form, FormStateInterface $formState) {
    $exportsList = ExportEntity::loadMultiple($formState
      ->getValue('exports_list'));
    foreach ($this
      ->getEntities() as $entity) {

      /** @var \Drupal\content_synchronizer\Entity\ExportEntity $export */
      foreach ($exportsList as $export) {
        $export
          ->addEntity($entity);
      }
    }
  }

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

  /**
   * Return the list of entities to export.
   *
   * @return array|\Drupal\Core\Entity\EntityInterface[]
   *   The entities.
   */
  protected function getEntities() {
    $entities = [];
    foreach ($this
      ->getTmpStoredData('entities') as $entityTypeId => $entitiesIds) {
      $entities += \Drupal::entityTypeManager()
        ->getStorage($entityTypeId)
        ->loadMultiple($entitiesIds);
    }
    return $entities;
  }

  /**
   * Return the stored element by key.
   *
   * @param string $key
   *   The key.
   *
   * @return mixed
   *   The value.
   */
  protected function getTmpStoredData($key) {
    return $this->tempStoreFactory
      ->get(static::FORM_ID)
      ->get(\Drupal::currentUser()
      ->id())[$key];
  }

}

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::getDescription public function Returns additional text to display as a description. Overrides ConfirmFormInterface::getDescription 11
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
ExportConfirmForm::$exportManager protected property The export manager.
ExportConfirmForm::$manager protected property The node storage.
ExportConfirmForm::$nodeInfo protected property The array of nodes to delete.
ExportConfirmForm::$tempStoreFactory protected property The tempstore factory.
ExportConfirmForm::ARCHIVE_PARAMS constant
ExportConfirmForm::buildForm public function Form constructor. Overrides ConfirmFormBase::buildForm
ExportConfirmForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ExportConfirmForm::FORM_ID constant
ExportConfirmForm::getCancelUrl public function Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface::getCancelUrl
ExportConfirmForm::getConfirmText public function Returns a caption for the button that confirms the action. Overrides ConfirmFormBase::getConfirmText
ExportConfirmForm::getEntities protected function Return the list of entities to export.
ExportConfirmForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ExportConfirmForm::getQuestion public function Returns the question to ask the user. Overrides ConfirmFormInterface::getQuestion
ExportConfirmForm::getTmpStoredData protected function Return the stored element by key.
ExportConfirmForm::onAddToExport public function Add entity to an existing entity export.
ExportConfirmForm::onBatchEnd public function On batch end redirect to the form url.
ExportConfirmForm::onQuickExport public function Action on quick export submit action.
ExportConfirmForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ExportConfirmForm::__construct public function Constructs a DeleteMultiple form object.
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.