function social_group_save_group_from_node in Open Social 8.9
Same name and namespace in other branches
- 8 modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 8.2 modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 8.3 modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 8.4 modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 8.5 modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 8.6 modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 8.7 modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 8.8 modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 10.3.x modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 10.0.x modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 10.1.x modules/social_features/social_group/social_group.module \social_group_save_group_from_node()
- 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 1699 - The Social group module.
Code
function social_group_save_group_from_node(array $form, FormStateInterface $form_state) {
// Check if we are adding a translation or it's the default being editted.
/** @var \Drupal\Core\Entity\EntityForm $form_object */
$form_object = $form_state
->getFormObject();
/** @var \Drupal\social_node\Entity\Node $node */
$node = $form_object
->getEntity();
$is_original_language = (bool) $node
->getFieldValue('default_langcode', 'value');
// Check if the created node is new or updated.
$is_new = NULL !== $form_state
->getValue('is_new') ? $form_state
->getValue('is_new') : FALSE;
$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,
// only if there is already a group that needs to be removed.
if (!empty($form['groups']['widget']['#default_value']) && $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.
// But only when we are on the original language, we don't want to update
// this when translating. The field is disabled for that scenario so no
// need to run this.
if ($is_original_language) {
$setGroupsForNodeService = \Drupal::service('social_group.set_groups_for_node_service');
$setGroupsForNodeService
->setGroupsForNode($node, $groups_to_remove, $groups_to_add, $original_groups, $is_new);
}
}