SocialProfileFieldsBatch.php in Open Social 8.8
Same filename and directory in other branches
- 8.9 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
- 8.2 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
- 8.3 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
- 8.4 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
- 8.5 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
- 8.6 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
- 8.7 modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
- 10.3.x modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
- 10.0.x modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
- 10.1.x modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
- 10.2.x modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.php
Namespace
Drupal\social_profile_fieldsFile
modules/social_features/social_profile/modules/social_profile_fields/src/SocialProfileFieldsBatch.phpView source
<?php
namespace Drupal\social_profile_fields;
use Drupal\Core\Cache\Cache;
use Drupal\profile\Entity\Profile;
use Drupal\search_api\Entity\Index;
/**
* Class SocialProfileFieldsBatch.
*
* Empty profile fields in batch.
*
* @package Drupal\social_profile_fields
*/
class SocialProfileFieldsBatch {
/**
* Perform the flush.
*
* @param array $pids
* Profile id's.
* @param array $fields
* An array of fields to empty.
* @param array $context
* The context of the flush.
*/
public static function performFlush(array $pids, array $fields, array &$context) {
$message = 'Flushing profile data...';
$results = [];
foreach ($pids as $pid) {
$profile = Profile::load($pid);
foreach ($fields as $field_name) {
// Check if the field exists.
if ($profile
->hasField($field_name)) {
// Empty the field.
$profile
->set($field_name, '');
}
}
// Save the profile.
$results[] = $profile
->save();
// Oh and also clear the profile cache while we're at it.
Cache::invalidateTags([
'profile:' . $profile
->id(),
]);
}
$context['message'] = $message;
$context['results'] = $results;
}
/**
* Message when done.
*
* @param bool $success
* If the operation was a success.
* @param array $results
* The amount of items done.
* @param string $operations
* The operation performed.
*/
public static function performFlushFinishedCallback($success, array $results, $operations) {
// The 'success' parameter means no fatal PHP errors were detected. All
// other error management should be handled using 'results'.
if ($success) {
$message = \Drupal::translation()
->formatPlural(count($results), 'One profile flushed.', '@count profiles flushed.');
$indexes = Index::loadMultiple([
'social_all',
'social_users',
]);
/** @var \Drupal\search_api\Entity\Index $index */
foreach ($indexes as $index) {
// If the search index is on and items are not indexed immediately, the
// index also needs to be flushed and re-indexed.
if ($index !== NULL && $index
->status() && !$index
->getOption('index_directly')) {
$index
->clear();
$index
->reindex();
}
}
}
else {
$message = t('Whoops... something went wrong!');
}
drupal_set_message($message);
}
}
Classes
Name | Description |
---|---|
SocialProfileFieldsBatch | Class SocialProfileFieldsBatch. |