You are here

class LaunchExportForm in Content Synchronizer 3.x

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

Launch Export Form.

Hierarchy

Expanded class hierarchy of LaunchExportForm

1 file declares its use of LaunchExportForm
ExportEntityViewBuilder.php in src/Entity/ExportEntityViewBuilder.php

File

src/Form/LaunchExportForm.php, line 18

Namespace

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

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

  /**
   * Current url.
   *
   * @var string
   */
  protected $currentUrl;

  /**
   * LaunchExportForm constructor.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   */
  public function __construct(Request $request) {
    $this->currentUrl = $request
      ->getRequestUri();
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('request_stack')
      ->getCurrentRequest());
  }

  /**
   * {@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' => $this
          ->t('Launch export'),
        '#button_type' => 'primary',
      ];
      $form['deleteEntities'] = [
        '#type' => 'submit',
        '#value' => $this
          ->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)) {
      $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;
    ArchiveDownloader::me()
      ->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' => $this
          ->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 = static::getRequestParam()) {
      if (file_exists($archiveUri)) {
        $form['archive'] = [
          '#type' => 'html_tag',
          '#tag' => 'iframe',
          '#attributes' => [
            'style' => [
              'display:none',
            ],
            'src' => file_create_url($archiveUri),
          ],
        ];
      }
    }
  }

  /**
   * Return request.
   *
   * @return mixed|\Symfony\Component\HttpFoundation\Request
   *   The value of the param.
   */
  public static function getRequestParam() {
    return \Drupal::request()
      ->get(EntityExportFormBuilder::ARCHIVE_PARAMS);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
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
LaunchExportForm::$currentUrl protected property Current url.
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::create public static function Instantiates a new instance of this class. Overrides FormBase::create
LaunchExportForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LaunchExportForm::getRequestParam public static function Return request.
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
LaunchExportForm::__construct public function LaunchExportForm constructor.
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.