You are here

class ImportPartialForm in Tome 8

Contains a form for performing a partial import.

@internal

Hierarchy

Expanded class hierarchy of ImportPartialForm

1 string reference to 'ImportPartialForm'
tome_sync.routing.yml in modules/tome_sync/tome_sync.routing.yml
modules/tome_sync/tome_sync.routing.yml

File

modules/tome_sync/src/Form/ImportPartialForm.php, line 24

Namespace

Drupal\tome_sync\Form
View source
class ImportPartialForm extends FormBase {

  /**
   * The importer.
   *
   * @var \Drupal\tome_sync\ImporterInterface
   */
  protected $importer;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The content hasher.
   *
   * @var \Drupal\tome_sync\ContentHasherInterface
   */
  protected $contentHasher;

  /**
   * The state system.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;

  /**
   * The sync configuration object.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $syncStorage;

  /**
   * The active configuration object.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $activeStorage;

  /**
   * Constructs an ImportPartialForm instance.
   *
   * @param \Drupal\tome_sync\ImporterInterface $importer
   *   The importer.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\tome_sync\ContentHasherInterface $content_hasher
   *   The content hasher.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state system.
   * @param \Drupal\Core\Config\StorageInterface $sync_storage
   *   The source storage.
   * @param \Drupal\Core\Config\StorageInterface $active_storage
   *   The target storage.
   */
  public function __construct(ImporterInterface $importer, EntityTypeManagerInterface $entity_type_manager, ContentHasherInterface $content_hasher, StateInterface $state, StorageInterface $sync_storage, StorageInterface $active_storage) {
    $this->importer = $importer;
    $this->entityTypeManager = $entity_type_manager;
    $this->contentHasher = $content_hasher;
    $this->state = $state;
    $this->syncStorage = $sync_storage;
    $this->activeStorage = $active_storage;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('tome_sync.importer'), $container
      ->get('entity_type.manager'), $container
      ->get('tome_sync.content_hasher'), $container
      ->get('state'), $container
      ->get('config.storage.sync'), $container
      ->get('config.storage'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    if ($error = $this
      ->getInitialError()) {
      $form['error']['#markup'] = '<p>' . $error . '</p>';
      return $form;
    }
    $form['description'] = [
      '#markup' => '<p>' . $this
        ->t("Submitting this form will import all changed Tome content and files. Your local site's content and files will be deleted or updated.") . '</p>',
    ];
    $form['content_status'] = [
      '#type' => 'container',
    ];
    $change_list = $this->contentHasher
      ->getChangelist();
    if (empty($change_list['modified']) && empty($change_list['deleted']) && empty($change_list['added'])) {
      $form['content_status']['status'] = [
        '#markup' => '<p>' . $this
          ->t('No content has been changed or deleted. There may be file changes to import.') . '</p>',
      ];
    }
    if (!empty($change_list['modified'])) {
      $form['content_status']['modified'] = [
        '#title' => $this
          ->t('Modified'),
        '#theme' => 'item_list',
        '#items' => $change_list['modified'],
      ];
    }
    if (!empty($change_list['added'])) {
      $form['content_status']['added'] = [
        '#title' => $this
          ->t('Added'),
        '#theme' => 'item_list',
        '#items' => $change_list['added'],
      ];
    }
    if (!empty($change_list['deleted'])) {
      $form['content_status']['deleted'] = [
        '#title' => $this
          ->t('Deleted'),
        '#theme' => 'item_list',
        '#items' => $change_list['deleted'],
      ];
    }
    if ($this->state
      ->get(ImporterInterface::STATE_KEY_IMPORTING, FALSE)) {
      $form['warning'] = [
        '#markup' => '<p>' . $this
          ->t('<strong>Warning</strong>: Another user may be running an import, proceed only if the last import failed unexpectedly') . '</p>',
      ];
    }
    $form['actions'] = [
      '#type' => 'actions',
    ];
    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Submit'),
    ];
    return $form;
  }

  /**
   * Determines if there is an initial error that should prevent an import.
   *
   * @return \Drupal\Core\StringTranslation\TranslatableMarkup|string
   *   An error message, if available.
   */
  protected function getInitialError() {
    if (!$this->contentHasher
      ->hashesExist()) {
      return $this
        ->t('No content hashes exist to perform a partial import. Please run a full Tome install and export (i.e. "drush tome:install && drush tome:export"), which will ensure hashes exist in the database and filesystem.');
    }

    // Inform the user if a config import needs to be made. This has to be done
    // before a content import in case bundles or fields change.
    // @todo Remove temporary third argument when 8.6.x EOLs.
    $storage_comparer = new StorageComparer($this->syncStorage, $this->activeStorage, \Drupal::service('config.manager'));
    if (!empty($this->syncStorage
      ->listAll()) && $storage_comparer
      ->createChangelist()
      ->hasChanges()) {
      $message = $this
        ->t('There are configuration changes that need importing.');
      $url = Url::fromRoute('config.sync', [], [
        'query' => [
          'destination' => Url::fromRoute('tome_sync.import_partial')
            ->toString(),
        ],
      ]);

      // The config module may not be enabled.
      if ($url
        ->access()) {
        $message .= ' ' . $this
          ->t('Please use the <a href=":url">Config Synchronize form</a>, then return here to import content and files.', [
          ':url' => $url
            ->toString(),
        ]);
      }
      else {
        $message .= ' ' . $this
          ->t('Please run a partial config import (i.e. "drush config:import --partial"), then return here to import content and files.');
      }
      return $message;
    }

    // Prevent the current user from being deleted.
    $current_user = $this
      ->currentUser();
    if ($current_user instanceof EntityInterface) {
      $user_name = TomeSyncHelper::getContentName($current_user);
      if (in_array($user_name, $this->contentHasher
        ->getChangelist()['deleted'], TRUE)) {
        return $this
          ->t('This import would delete the current user and cannot be performed using the user interface.');
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->state
      ->set(ImporterInterface::STATE_KEY_IMPORTING, TRUE);
    $batch_builder = (new BatchBuilder())
      ->setTitle($this
      ->t('Importing changes...'))
      ->setFinishCallback([
      $this,
      'finishCallback',
    ]);
    $change_list = $this->contentHasher
      ->getChangelist();
    foreach ($change_list['deleted'] as $name) {
      $batch_builder
        ->addOperation([
        $this,
        'deleteContent',
      ], [
        $name,
      ]);
    }
    $chunked_names = $this->importer
      ->getChunkedNames();
    foreach ($chunked_names as $i => $chunk) {
      foreach ($chunk as $j => $name) {
        if (in_array($name, $change_list['modified'], TRUE) || in_array($name, $change_list['added'], TRUE)) {
          $batch_builder
            ->addOperation([
            $this,
            'importContent',
          ], [
            $name,
          ]);
        }
      }
    }
    $batch_builder
      ->addOperation([
      $this,
      'importFiles',
    ]);
    batch_set($batch_builder
      ->toArray());
  }

  /**
   * Batch callback to delete content or a content translation.
   *
   * @param string $name
   *   A content name.
   */
  public function deleteContent($name) {
    list($entity_type_id, $uuid, $langcode) = TomeSyncHelper::getPartsFromContentName($name);
    $results = $this->entityTypeManager
      ->getStorage($entity_type_id)
      ->loadByProperties([
      'uuid' => $uuid,
    ]);
    if (count($results) === 1) {
      $entity = reset($results);
      if ($langcode) {
        if ($translation = $entity
          ->getTranslation($langcode)) {
          $entity
            ->removeTranslation($langcode);
          $entity
            ->save();
        }
      }
      else {
        $entity
          ->delete();
      }
    }
  }

  /**
   * Batch callback to import content or add a content translation.
   *
   * @param string $name
   *   A content name.
   */
  public function importContent($name) {
    list($entity_type_id, $uuid, $langcode) = TomeSyncHelper::getPartsFromContentName($name);
    $this->importer
      ->importContent($entity_type_id, $uuid, $langcode);
  }

  /**
   * Batch callback to import files.
   */
  public function importFiles() {
    $this->importer
      ->importFiles();
  }

  /**
   * Batch finished callback.
   *
   * @param bool $success
   *   Whether or not the batch was successful.
   * @param mixed $results
   *   Batch results set with context.
   */
  public function finishCallback($success, $results) {
    $this->state
      ->set(ImporterInterface::STATE_KEY_IMPORTING, FALSE);
    if (!$success) {
      $this
        ->messenger()
        ->addError($this
        ->t('Import failed - consult the error log for more details.'));
      return;
    }
    $this
      ->messenger()
      ->addStatus($this
      ->t('Import complete!'));
  }

}

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::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
ImportPartialForm::$activeStorage protected property The active configuration object.
ImportPartialForm::$contentHasher protected property The content hasher.
ImportPartialForm::$entityTypeManager protected property The entity type manager.
ImportPartialForm::$importer protected property The importer.
ImportPartialForm::$state protected property The state system.
ImportPartialForm::$syncStorage protected property The sync configuration object.
ImportPartialForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
ImportPartialForm::create public static function Instantiates a new instance of this class. Overrides FormBase::create
ImportPartialForm::deleteContent public function Batch callback to delete content or a content translation.
ImportPartialForm::finishCallback public function Batch finished callback.
ImportPartialForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ImportPartialForm::getInitialError protected function Determines if there is an initial error that should prevent an import.
ImportPartialForm::importContent public function Batch callback to import content or add a content translation.
ImportPartialForm::importFiles public function Batch callback to import files.
ImportPartialForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ImportPartialForm::__construct public function Constructs an ImportPartialForm instance.
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.