You are here

class LaunchExportForm in Content Synchronizer 8

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

Launch Export Form.

Hierarchy

Expanded class hierarchy of LaunchExportForm

File

src/Form/LaunchExportForm.php, line 19

Namespace

Drupal\content_synchronizer\Form
View source
class LaunchExportForm extends FormBase {

  /**
   * The export entity.
   *
   * @var \Drupal\content_synchronizer\Entity\ExportEntity
   */
  protected $export;
  protected $currentUrl;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'content_synchronizer.export.launch';
  }

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

    /** @var \Drupal\content_synchronizer\Entity\ImportEntity $import */
    $this->export = $form_state
      ->getBuildInfo()['export'];

    // Entity list :
    $this
      ->initRootEntitiesList($form);
    if (array_key_exists('entities_list', $form)) {
      $form['launch'] = [
        '#type' => 'submit',
        '#value' => t('Launch export'),
        '#button_type' => 'primary',
      ];
      $form['deleteEntities'] = [
        '#type' => 'submit',
        '#value' => t('Remove selected entities from export list'),
        '#submit' => [
          '::removeSelectedEntities',
        ],
      ];
    }
    return $form;
  }

  /**
   * Remove selected entities of the import.
   */
  public function removeSelectedEntities(array &$form, FormStateInterface $form_state) {
    $entitiesToRemove = $form_state
      ->getUserInput()['entities_to_export'];
    $entitiesToRemove = array_intersect_key($this->export
      ->getEntitiesList(), array_flip($entitiesToRemove));
    foreach ($entitiesToRemove as $entity) {
      $this->export
        ->removeEntity($entity);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $entitiesToExport = $form_state
      ->getUserInput()['entities_to_export'];
    $entitiesToExport = array_intersect_key($this->export
      ->getEntitiesList(), array_flip($entitiesToExport));
    if (!empty($entitiesToExport)) {
      $this->currentUrl = \Drupal::request()
        ->getRequestUri();
      $writer = new ExportEntityWriter();
      $writer
        ->initFromId($this->export
        ->label());
      $batchExportProcessor = new BatchExportProcessor($writer);
      $batchExportProcessor
        ->exportEntities($entitiesToExport, [
        $this,
        'onBatchEnd',
      ]);
    }
  }

  /**
   * Callback after batch.
   */
  public function onBatchEnd($archiveUri) {
    $redirectUrl = $this->currentUrl;
    \Drupal::service(ArchiveDownloader::SERVICE_NAME)
      ->redirectWithArchivePath($redirectUrl, $archiveUri);
  }

  /**
   * Init Root entities lists for display.
   */
  protected function initRootEntitiesList(array &$form) {
    $rootEntities = $this
      ->getRootsEntities();
    if (!empty($rootEntities)) {
      $build = [
        '#theme' => 'entities_list_table',
        '#entities' => $rootEntities,
        '#status_or_bundle' => 'bundle',
        '#checkbox_name' => 'entities_to_export[]',
        '#title' => t('Entities to export'),
        '#attached' => [
          'library' => [
            'content_synchronizer/list',
          ],
        ],
      ];
      $form['entities_list'] = $build;
    }
  }

  /**
   * Return the roots entities.
   */
  public function getRootsEntities() {
    $data = [];

    /** @var \Drupal\Core\Entity\Entity $entity */
    foreach ($this->export
      ->getEntitiesList() as $key => $entity) {
      $data[$key] = [
        "entity_type_id" => $entity
          ->getEntityTypeId(),
        "entity_id" => $entity
          ->id(),
        "status" => $entity
          ->bundle(),
        "label" => ExportEntityWriter::getEntityLabel($entity),
        'edit_url' => Url::fromRoute('entity.' . $entity
          ->getEntityTypeId() . '.edit_form', [
          $entity
            ->getEntityTypeId() => $entity
            ->id(),
        ]),
        'view_url' => $entity
          ->toUrl(),
      ];
    }
    return $data;
  }

  /**
   * Add a donwload hidden iframe.
   *
   * @param array $form
   *   The build form array.
   */
  protected function addDownloadIframe(array &$form) {
    if ($archiveUri = \Drupal::request()
      ->get(EntityExportFormBuilder::ARCHIVE_PARAMS)) {
      if (file_exists($archiveUri)) {
        $form['archive'] = [
          '#type' => 'html_tag',
          '#tag' => 'iframe',
          '#attributes' => [
            'style' => [
              'display:none',
            ],
            'src' => file_create_url($archiveUri),
          ],
        ];
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
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
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::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
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
LaunchExportForm::$currentUrl protected property
LaunchExportForm::$export protected property The export entity.
LaunchExportForm::addDownloadIframe protected function Add a donwload hidden iframe.
LaunchExportForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
LaunchExportForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LaunchExportForm::getRootsEntities public function Return the roots entities.
LaunchExportForm::initRootEntitiesList protected function Init Root entities lists for display.
LaunchExportForm::onBatchEnd public function Callback after batch.
LaunchExportForm::removeSelectedEntities public function Remove selected entities of the import.
LaunchExportForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
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.