protected function NodeFormAlter::getGroups in Group Content Menu 8
Get a node's groups for the edit form.
Parameters
\Drupal\Core\Form\FormStateInterface $form_state: Form state object from hook_form_node_form_alter().
\Drupal\node\NodeInterface $node: The node object being edited.
Return value
array|\Drupal\group\Entity\GroupInterface[] An empty array or an array of Groups.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
1 call to NodeFormAlter::getGroups()
- NodeFormAlter::alter in src/
NodeFormAlter.php - Alter node forms to add GroupContentMenu options where appropriate.
File
- src/
NodeFormAlter.php, line 231
Class
- NodeFormAlter
- Helper class to handle altering node forms.
Namespace
Drupal\group_content_menuCode
protected function getGroups(FormStateInterface $form_state, NodeInterface $node) : array {
$groups = [];
if ($group_id = $form_state
->get('group')) {
// If group is set in $form_state, we're currently creating a new node in
// a group so the node will have only one group.
$groups[] = $group_id;
}
elseif (!$node
->isNew()) {
// We're on a node's edit form. A node can be added to any number of
// groups so we must load all groups for the node.
$group_contents = $this->entityTypeManager
->getStorage('group_content')
->loadByEntity($node);
$group_ids = array_map(static function (GroupContent $group_content) {
return $group_content
->getGroup()
->id();
}, $group_contents);
$groups = $this->entityTypeManager
->getStorage('group')
->loadMultiple($group_ids);
}
return $groups;
}