You are here

function social_group_update_8302 in Open Social 8.8

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_group/social_group.install \social_group_update_8302()
  2. 8.3 modules/social_features/social_group/social_group.install \social_group_update_8302()
  3. 8.4 modules/social_features/social_group/social_group.install \social_group_update_8302()
  4. 8.5 modules/social_features/social_group/social_group.install \social_group_update_8302()
  5. 8.6 modules/social_features/social_group/social_group.install \social_group_update_8302()
  6. 8.7 modules/social_features/social_group/social_group.install \social_group_update_8302()
  7. 10.3.x modules/social_features/social_group/social_group.install \social_group_update_8302()
  8. 10.0.x modules/social_features/social_group/social_group.install \social_group_update_8302()
  9. 10.1.x modules/social_features/social_group/social_group.install \social_group_update_8302()
  10. 10.2.x modules/social_features/social_group/social_group.install \social_group_update_8302()

NOTE: Contains possible data alteration!

Change the visibility of all nodes 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 457
Install, update and uninstall functions for the social_group module.

Code

function social_group_update_8302(&$sandbox) {
  if (!isset($sandbox['progress'])) {
    $sandbox['progress'] = 0;
    $sandbox['items'] = [];
    $sandbox['max'] = 0;

    // First grab all the node IDs that need to change.
    $connection = Database::getConnection();
    $sth = $connection
      ->select('group_content_field_data', 'gc');
    $sth
      ->fields('gc', [
      'entity_id',
    ]);
    $sth
      ->join('node__field_content_visibility', 'nv', 'gc.entity_id = nv.entity_id');
    $sth
      ->join('node_field_data', 'nd', 'gc.entity_id = nd.nid');
    $sth
      ->condition('gc.type', 'open_group-group_node-%', 'LIKE');
    $sth
      ->condition('nv.field_content_visibility_value', 'public', '=');

    // Timestamp is from the moment the commit landed in 8.x-3.x see:
    // https://cgit.drupalcode.org/social/commit/?id=3e465bb1ad927712e22469c193b6e9547ba1c081
    $sth
      ->condition('nd.created', '1534118400', '>');
    $data = $sth
      ->execute();
    $sandbox['items']['node_ids'] = $data
      ->fetchCol();
    $sandbox['max'] = count($sandbox['items']['node_ids']);
  }
  if ($sandbox['items']['node_ids']) {
    $pid = array_shift($sandbox['items']['node_ids']);

    // Load all the entities and re-save them with the correct visibility.
    $node = Node::load($pid);

    /** @var \Drupal\node\Entity\Node $node */
    $node
      ->set('field_content_visibility', 'community');
    $node
      ->save();
  }
  $sandbox['progress']++;
  $sandbox['#finished'] = empty($sandbox['max']) ? 1 : $sandbox['progress'] / $sandbox['max'];
}