class RemoveGatherContentLocalData in GatherContent 8.4
Class RemoveGatherContentLocalData.
@package Drupal\gathercontent\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\gathercontent_ui\Form\RemoveGatherContentLocalData
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\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 1 |
FormBase:: |
protected | property | The request stack. | 1 |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 1 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
87 |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
62 |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
RemoveGatherContentLocalData:: |
public | function |
Form constructor. Overrides FormInterface:: |
|
RemoveGatherContentLocalData:: |
public static | function | Clean GC IDs from node and file entity. | |
RemoveGatherContentLocalData:: |
public static | function | Delete gathercontent_operation and gathercontent_operation_item entities. | |
RemoveGatherContentLocalData:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
RemoveGatherContentLocalData:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |