function social_group_update_8301 in Open Social 8.9
Same name and namespace in other branches
- 8.3 modules/social_features/social_group/social_group.install \social_group_update_8301()
- 8.4 modules/social_features/social_group/social_group.install \social_group_update_8301()
- 8.5 modules/social_features/social_group/social_group.install \social_group_update_8301()
- 8.6 modules/social_features/social_group/social_group.install \social_group_update_8301()
- 8.7 modules/social_features/social_group/social_group.install \social_group_update_8301()
- 8.8 modules/social_features/social_group/social_group.install \social_group_update_8301()
- 10.3.x modules/social_features/social_group/social_group.install \social_group_update_8301()
- 10.0.x modules/social_features/social_group/social_group.install \social_group_update_8301()
- 10.1.x modules/social_features/social_group/social_group.install \social_group_update_8301()
- 10.2.x modules/social_features/social_group/social_group.install \social_group_update_8301()
NOTE: Contains possible data alteration!
Change the visibility of all posts placed in an open group, which have visibility public, to community. See: https://www.drupal.org/project/social/issues/2992332#comment-12790905.
File
- modules/
social_features/ social_group/ social_group.install, line 413 - Install, update and uninstall functions for the social_group module.
Code
function social_group_update_8301(&$sandbox) {
if (!isset($sandbox['progress'])) {
$sandbox['progress'] = 0;
$sandbox['items'] = [];
$sandbox['max'] = 0;
// First grab all the post IDs that need to change.
$connection = Database::getConnection();
$sth = $connection
->select('post__field_visibility', 'pv');
$sth
->fields('pv', [
'entity_id',
]);
$sth
->join('post_field_data', 'pd', 'pd.id = pv.entity_id');
$sth
->join('post__field_recipient_group', 'pg', 'pv.entity_id = pg.entity_id');
$sth
->join('groups', 'g', 'pg.field_recipient_group_target_id = g.id');
$sth
->condition('pv.field_visibility_value', '1', '=');
$sth
->condition('g.type', 'open_group', '=');
// Timestamp is from the moment the commit landed in 8.x-3.x see:
// https://cgit.drupalcode.org/social/commit/?id=3e465bb1ad927712e22469c193b6e9547ba1c081
$sth
->condition('pd.created', '1534118400', '>');
$data = $sth
->execute();
$sandbox['items']['post_ids'] = $data
->fetchCol();
$sandbox['max'] = count($sandbox['items']['post_ids']);
}
if ($sandbox['items']['post_ids']) {
$pid = array_shift($sandbox['items']['post_ids']);
// Load all the entities and re-save them with the correct visibility.
$post = Post::load($pid);
/** @var \Drupal\social_post\Entity\Post $post */
$post
->set('field_visibility', '0');
$post
->save();
}
$sandbox['progress']++;
$sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}