public function HostForm::save in http:BL 8
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/ HostForm.php, line 177
Class
- HostForm
- Form controller for the host entity Add/Edit form.
Namespace
Drupal\httpbl\FormCode
public function save(array $form, FormStateInterface $form_state) {
$form_state
->setRedirect('entity.host.collection');
$entity = $this
->getEntity();
$previous_source = $entity
->getSource();
$entity
->setSource(HTTPBL_ADMIN_SOURCE);
$entity
->save();
// Get the IP being added or edited.
$ip = $entity->host_ip->value;
// Get the user who added / edited this.
$user = $this
->currentUser();
$user = $user
->getDisplayName();
// Get the storage state (whether we are saving host entities only or also to core Ban_ip for banning blacklisted IPs).
$storage = \Drupal::state()
->get('httpbl.storage');
// Get the current route so we can check whether we are adding or editing.
$route = \Drupal::service('current_route_match')
->getCurrentRouteMatch()
->getRouteName();
// If this is an edit it could be a status change for an IP previously banned.
if ($route == 'entity.host.edit_form' && $entity->host_status->value != HTTPBL_LIST_BLACK && $this->banManager
->isBanned($ip)) {
//Unban this IP.
$this->banManager
->unbanIp($ip);
// Log and message this unbanning.
$this->logTrapper
->trapWarning('A banned @type has been un-banned: @title, by user @user. Previous evaluation source: @source.', array(
'@type' => $this->entity
->bundle(),
'@title' => $this->entity
->label(),
'@user' => $user,
'@source' => $previous_source,
'link' => $entity
->projectLink(),
));
drupal_set_message($this
->t('Previously banned host @ip has been unbanned.', array(
'@ip' => $entity->host_ip->value,
)), 'warning');
}
// @todo, do better separation of add and edit.
// If Blacklisted & Banning...
if ($entity->host_status->value == HTTPBL_LIST_BLACK && $storage == HTTPBL_DB_HH_DRUPAL) {
// Ban the IP!
$this->banManager
->banIp($ip);
// Log this banning.
if ($route == 'httpbl.host_add') {
$this->logTrapper
->trapNotice('@type: added blacklisted and banned @title: by user @user. Source:@source.', array(
'@type' => $this->entity
->bundle(),
'@title' => $this->entity
->label(),
'@user' => $user,
'@source' => $this->entity
->getSource(),
'link' => $entity
->projectLink(),
));
}
else {
$this->logTrapper
->trapNotice('@type: updated blacklisted and banned @title: by user @user. Previous source: @source.', array(
'@type' => $this->entity
->bundle(),
'@title' => $this->entity
->label(),
'@user' => $user,
'@source' => $previous_source,
'link' => $entity
->projectLink(),
));
}
drupal_set_message($this
->t('Host @ip was blacklisted and banned.', array(
'@ip' => $entity->host_ip->value,
)));
}
else {
// Otherwise, log the edit that was made.
$this->logTrapper
->trapNotice('Evaluated @type edited: @title, by user @user. Previous source: @source.', array(
'@type' => $this->entity
->bundle(),
'@title' => $this->entity
->label(),
'@user' => $user,
'@source' => $previous_source,
'link' => $entity
->projectLink(),
));
}
$form_state
->setRedirect('entity.host.collection');
}