You are here

class RemoveGatherContentLocalData in GatherContent 8.4

Class RemoveGatherContentLocalData.

@package Drupal\gathercontent\Form

Hierarchy

Expanded class hierarchy of RemoveGatherContentLocalData

1 string reference to 'RemoveGatherContentLocalData'
gathercontent_ui.routing.yml in gathercontent_ui/gathercontent_ui.routing.yml
gathercontent_ui/gathercontent_ui.routing.yml

File

gathercontent_ui/src/Form/RemoveGatherContentLocalData.php, line 13

Namespace

Drupal\gathercontent_ui\Form
View source
class RemoveGatherContentLocalData extends FormBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    drupal_set_message($this
      ->t('This operation is irreversible and should be done before module uninstall!'), 'warning');
    $form['remove_message'] = [
      '#type' => 'markup',
      '#markup' => $this
        ->t('This form removes the reference Gather Content IDs from your local Drupal site. The nodes keeps on your site.'),
    ];
    $form['actions']['#type'] = 'actions';
    $form['actions']['delete'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Delete data'),
      '#button_type' => 'primary',
      '#return_value' => 'submit',
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $batch = [
      'title' => $this
        ->t('Deleting...'),
      'operations' => [
        [
          static::class . '::deleteAllEntity',
          [
            'gathercontent_operation_item',
          ],
        ],
        [
          static::class . '::deleteAllEntity',
          [
            'gathercontent_operation',
          ],
        ],
        [
          static::class . '::cleanUpGcData',
          [
            'node',
          ],
        ],
        [
          static::class . '::cleanUpGcData',
          [
            'file',
          ],
        ],
      ],
    ];
    batch_set($batch);
    $form_state
      ->setRedirect('system.modules_uninstall');
    drupal_set_message($this
      ->t('Now you can try to uninstall the Gather Content module.'));
  }

  /**
   * Delete gathercontent_operation and gathercontent_operation_item entities.
   *
   * @param string $entity_type_id
   *   Entity type ID of which we want to delete entities.
   * @param array $context
   *   Array of context.
   */
  public static function deleteAllEntity($entity_type_id, array &$context) {
    $entity_type_manager = \Drupal::entityTypeManager();
    if (empty($context['sandbox'])) {
      $context['sandbox']['num_of_deleted_items'] = 0;
      $context['sandbox']['current_id'] = 0;
      $context['sandbox']['total_count'] = \Drupal::entityQuery($entity_type_id)
        ->count()
        ->execute();
    }
    $steps = 50;
    $entity_ids = \Drupal::entityQuery($entity_type_id)
      ->condition($entity_type_manager
      ->getDefinition($entity_type_id)
      ->getKey('id'), $context['sandbox']['current_id'], '>')
      ->sort($entity_type_manager
      ->getDefinition($entity_type_id)
      ->getKey('id'))
      ->range(0, $steps)
      ->execute();
    foreach ($entity_ids as $entity_id) {
      $context['sandbox']['num_of_deleted_items']++;
      $context['sandbox']['current_id'] = $entity_id;
      $entity_type_manager
        ->getStorage($entity_type_id)
        ->load($entity_id)
        ->delete();
    }
    if ($context['sandbox']['num_of_deleted_items'] != $context['sandbox']['total_count']) {
      $context['finished'] = $context['sandbox']['num_of_deleted_items'] / $context['sandbox']['total_count'];
    }
  }

  /**
   * Clean GC IDs from node and file entity.
   *
   * @param string $entity_type_id
   *   Entity type ID of which we want to clean entities.
   * @param array $context
   *   Array of context.
   */
  public static function cleanUpGcData($entity_type_id, array &$context) {
    if (empty($context['sandbox'])) {
      $context['sandbox']['num_of_precessed_items'] = 0;
      $context['sandbox']['current_id'] = 0;
      $context['sandbox']['total_count'] = \Drupal::entityQuery($entity_type_id)
        ->condition('gc_id', NULL, 'IS NOT NULL')
        ->count()
        ->execute();
    }
    $limit = 50;
    $id_key = \Drupal::entityTypeManager()
      ->getDefinition($entity_type_id)
      ->getKey('id');
    $entity_ids = \Drupal::entityQuery($entity_type_id)
      ->condition($id_key, $context['sandbox']['current_id'], '>')
      ->condition('gc_id', NULL, 'IS NOT NULL')
      ->sort($id_key)
      ->range(0, $limit)
      ->execute();
    foreach ($entity_ids as $entity_id) {
      $context['sandbox']['num_of_precessed_items']++;
      $context['sandbox']['current_id'] = $entity_id;

      /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
      $entity = \Drupal::entityTypeManager()
        ->getStorage($entity_type_id)
        ->load($entity_id);
      $entity
        ->set('gc_id', NULL);
      if ($entity
        ->hasField('gc_mapping_id')) {
        $entity
          ->set('gc_mapping_id', NULL);
      }
      $entity
        ->save();
    }
    if ($context['sandbox']['num_of_precessed_items'] != $context['sandbox']['total_count']) {
      $context['finished'] = $context['sandbox']['num_of_precessed_items'] / $context['sandbox']['total_count'];
    }
  }

}

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
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.
RemoveGatherContentLocalData::buildForm public function Form constructor. Overrides FormInterface::buildForm
RemoveGatherContentLocalData::cleanUpGcData public static function Clean GC IDs from node and file entity.
RemoveGatherContentLocalData::deleteAllEntity public static function Delete gathercontent_operation and gathercontent_operation_item entities.
RemoveGatherContentLocalData::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
RemoveGatherContentLocalData::submitForm public function Form submission handler. Overrides FormInterface::submitForm
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.