public function EventTypeForm::save in RNG - Events and Registrations 3.x
Same name and namespace in other branches
- 8.2 src/Form/EventTypeForm.php \Drupal\rng\Form\EventTypeForm::save()
- 8 src/Form/EventTypeForm.php \Drupal\rng\Form\EventTypeForm::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/
Form/ EventTypeForm.php, line 367
Class
- EventTypeForm
- Form controller for event config entities.
Namespace
Drupal\rng\FormCode
public function save(array $form, FormStateInterface $form_state) {
/** @var \Drupal\rng\Entity\EventTypeInterface $event_type */
$event_type = $this
->getEntity();
if ($event_type
->isNew()) {
if ($this->moduleHandler
->moduleExists('node') && $form_state
->getValue('entity_type') == 'node') {
$node_type = $this
->createContentType('Event');
$t_args = [
'%label' => $node_type
->label(),
':url' => $node_type
->toUrl()
->toString(),
];
$this
->messenger()
->addMessage(t('The content type <a href=":url">%label</a> has been added.', $t_args));
$event_type
->setEventEntityTypeId($node_type
->getEntityType()
->getBundleOf());
$event_type
->setEventBundle($node_type
->id());
}
else {
$bundle = explode('.', $form_state
->getValue('bundle'));
$event_type
->setEventEntityTypeId($bundle[0]);
$event_type
->setEventBundle($bundle[1]);
}
}
foreach ($form_state
->getValue([
'registrants',
'registrants',
]) as $row_key => $row) {
[
$entity_type,
$bundle,
] = explode(':', $row_key);
$event_type
->setIdentityTypeCreate($entity_type, $bundle, !empty($row['create']));
$event_type
->setIdentityTypeReference($entity_type, $bundle, !empty($row['existing']));
$event_type
->setIdentityTypeEntityFormMode($entity_type, $bundle, $row['entity_form_mode']);
}
$event_type
->setDefaultRegistrantType($form_state
->getValue([
'registrants',
'registrant_type',
]));
// Set to the access operation for event.
$op = $form_state
->getValue('mirror_update') ? 'update' : '';
$event_type
->setEventManageOperation($op)
->setAllowAnonRegistrants($form_state
->getValue('allow_anon_registrants'))
->setAutoSyncRegistrants($form_state
->getValue('auto_sync_registrants'))
->setAutoAttachUsers($form_state
->getValue('auto_attach_users'))
->setRegistrantEmailField($form_state
->getValue('registrant_email_field'))
->setEventStartDateField($form_state
->getValue('event_date_field_start'))
->setEventEndDateField($form_state
->getValue('event_date_field_end'));
$status = $event_type
->save();
// Create default rules.
if ($status == SAVED_NEW) {
$this
->createDefaultRules($event_type
->getEventEntityTypeId(), $event_type
->getEventBundle());
}
$message = $status == SAVED_UPDATED ? '%label event type updated.' : '%label event type added.';
$url = $event_type
->toUrl();
$t_args = [
'%label' => $event_type
->id(),
'link' => Link::fromTextAndUrl(t('Edit'), $url),
];
$this
->messenger()
->addMessage($this
->t($message, $t_args));
$this
->logger('rng')
->notice($message, $t_args);
}