You are here

public function AutobanFormBase::newDefaultId in Automatic IP ban (Autoban) 8

Generate default ID for new item.

Return value

string Default ID.

1 call to AutobanFormBase::newDefaultId()
AutobanFormBase::buildForm in src/Form/AutobanFormBase.php
Overrides Drupal\Core\Entity\EntityFormController::form().

File

src/Form/AutobanFormBase.php, line 304
Class AutobanFormBase.

Class

AutobanFormBase
Autoban base form for add and edit.

Namespace

Drupal\autoban\Form

Code

public function newDefaultId() {
  $query = $this->entityTypeManager
    ->getStorage('autoban')
    ->getQuery();
  $query
    ->condition('rule_type', AutobanUtils::AUTOBAN_RULE_AUTO, '=');
  $cnt_auto = $query
    ->count()
    ->execute();
  $query = $this->entityTypeManager
    ->getStorage('autoban')
    ->getQuery();
  $cnt_total = $query
    ->count()
    ->execute();
  $next = $cnt_total - $cnt_auto + 1;
  $entity_id = "rule{$next}";
  $result = $query
    ->condition('id', $entity_id)
    ->execute();
  $exists = (bool) $result;
  return $exists ? '' : $entity_id;
}