You are here

public function HostForm::buildEntity in http:BL 8

Builds an updated entity object based upon the submitted form values.

For building the updated entity object the form's entity is cloned and the submitted form values are copied to entity properties. The form's entity remains unchanged.

Parameters

array $form: A nested array form elements comprising the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

\Drupal\Core\Entity\EntityInterface An updated copy of the form's entity object.

Overrides ContentEntityForm::buildEntity

See also

\Drupal\Core\Entity\EntityFormInterface::getEntity()

File

src/Form/HostForm.php, line 139

Class

HostForm
Form controller for the host entity Add/Edit form.

Namespace

Drupal\httpbl\Form

Code

public function buildEntity(array $form, FormStateInterface $form_state) {

  /* @var $host \Drupal\httpbl\Entity\Host */
  $host = parent::buildEntity($form, $form_state);
  $values = $form_state
    ->getValues();

  // Get the current route so we can check whether we are adding or editing.
  $route = \Drupal::service('current_route_match')
    ->getCurrentRouteMatch()
    ->getRouteName();

  // If adding a host and the host already exists, don't allow duplicates.
  if ($route == 'httpbl.host_add' && HostQuery::getHostsByIp($values['host_ip'])) {
    $form_state
      ->setErrorByName('duplicate_host_attempt', $this
      ->t('Host @ip already exists!', array(
      '@ip' => $values['host_ip'],
    )));
  }
  $safeOffset = \Drupal::state()
    ->get('httpbl.safe_offset');
  $greyOffset = \Drupal::state()
    ->get('httpbl.greylist_offset');
  $blackOffset = \Drupal::state()
    ->get('httpbl.blacklist_offset');
  $safeExpiry = $safeOffset + \Drupal::time()
    ->getRequestTime();
  $greyExpiry = $greyOffset + \Drupal::time()
    ->getRequestTime();
  $blackExpiry = $blackOffset + \Drupal::time()
    ->getRequestTime();
  switch ($values['host_status']) {
    case '0':
      $host
        ->setExpiry($safeExpiry);
      break;
    case '1':
      $host
        ->setExpiry($blackExpiry);
      break;
    case '2':
      $host
        ->setExpiry($greyExpiry);
      break;
  }

  //$host->setSource(HTTPBL_ADMIN_SOURCE);
  return $host;
}