You are here

function social_group_save_group_from_node in Open Social 8

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

Form submit to save the group from a node form.

Parameters

array $form: Node add or node edit form.

\Drupal\Core\Form\FormStateInterface $form_state: Form state interface.

1 string reference to 'social_group_save_group_from_node'
social_group_form_node_form_alter in modules/social_features/social_group/social_group.module
Implements hook_form_FORM_ID_alter().

File

modules/social_features/social_group/social_group.module, line 1190
The Social group module.

Code

function social_group_save_group_from_node(array $form, FormStateInterface $form_state) {

  /* @var \Drupal\node\Entity\Node $node */
  $node = $form_state
    ->getFormObject()
    ->getEntity();
  $original_groups = [];
  $groups_to_add = [];
  $groups_to_remove = [];
  foreach ($form_state
    ->getValue('groups') as $new_group_key => $new_group) {
    $groups_to_add[$new_group['target_id']] = $new_group['target_id'];
  }

  // The node already exist so lets also change the logic accordingly.
  if ($form['#form_id'] === 'node_' . $node
    ->bundle() . '_edit_form') {
    $original_groups = $form['groups']['widget']['#default_value'];
    foreach ($original_groups as $original_group_key => $original_group) {
      if (!in_array($original_group, $groups_to_add)) {
        $groups_to_remove[$original_group] = $original_group;
      }
      else {
        unset($groups_to_add[$original_group]);
      }
    }
  }

  // Now make sure the relevant GroupContent is removed or added.
  $setGroupsForNodeService = \Drupal::service('social_group.set_groups_for_node_service');
  $setGroupsForNodeService
    ->setGroupsForNode($node, $groups_to_remove, $groups_to_add, $original_groups);
}