public function GroupTypeForm::save in Group 8
Same name and namespace in other branches
- 2.0.x src/Entity/Form/GroupTypeForm.php \Drupal\group\Entity\Form\GroupTypeForm::save()
Form submission handler for the 'save' action.
Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.
Overrides EntityForm::save
File
- src/
Entity/ Form/ GroupTypeForm.php, line 197
Class
- GroupTypeForm
- Form controller for group type forms.
Namespace
Drupal\group\Entity\FormCode
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\group\Entity\GroupTypeInterface $type */
$type = $this->entity;
// Trim any whitespace off the label.
$type
->set('label', trim($type
->label()));
// Clean up the creator role IDs as it comes from a checkboxes element.
if ($creator_roles = $type
->getCreatorRoleIds()) {
$type
->set('creator_roles', array_values(array_filter($creator_roles)));
}
$status = $type
->save();
$t_args = [
'%label' => $type
->label(),
];
// Update title field definition.
$fields = $this->entityFieldManager
->getFieldDefinitions('group', $type
->id());
$title_field = $fields['label'];
$title_label = $form_state
->getValue('title_label');
if ($title_field
->getLabel() !== $title_label) {
$title_field
->getConfig($type
->id())
->setLabel($title_label)
->save();
}
if ($status == SAVED_UPDATED) {
$this
->messenger()
->addStatus($this
->t('The group type %label has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) {
$this
->messenger()
->addStatus($this
->t('The group type %label has been added. You may now configure which roles a group creator will receive by editing the group type.', $t_args));
$context = array_merge($t_args, [
'link' => $type
->toLink($this
->t('View'), 'collection')
->toString(),
]);
$this
->logger('group')
->notice('Added group type %label.', $context);
// Optionally create a default admin role.
if ($form_state
->getValue('add_admin_role')) {
$storage = $this->entityTypeManager
->getStorage('group_role');
/** @var \Drupal\group\Entity\GroupRoleInterface $group_role */
$group_role = $storage
->create([
'id' => $type
->id() . '-admin',
'label' => $this
->t('Admin'),
'weight' => 100,
'group_type' => $type
->id(),
]);
$group_role
->grantAllPermissions()
->save();
// Optionally auto-assign the admin role to group creators.
if ($form_state
->getValue('assign_admin_role')) {
$type
->set('creator_roles', [
$type
->id() . '-admin',
])
->save();
}
}
}
$form_state
->setRedirectUrl($type
->toUrl('collection'));
}