You are here

public function MobileNumberItem::isUnique in Mobile Number 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/Field/FieldType/MobileNumberItem.php \Drupal\mobile_number\Plugin\Field\FieldType\MobileNumberItem::isUnique()

Is mobile number unique within the entity/field. Will check verified numbers, only if specificed.

Parameters

int $unique_type: Unique type [MOBILE_NUMBER_UNIQUE_YES|MOBILE_NUMBER_UNIQUE_YES_VERIEID].

Return value

bool|null TRUE for is unique, false otherwise. Null if mobile number is not valid.

File

src/Plugin/Field/FieldType/MobileNumberItem.php, line 499

Class

MobileNumberItem
Plugin implementation of the 'mobile_number' field type.

Namespace

Drupal\mobile_number\Plugin\Field\FieldType

Code

public function isUnique($unique_type = MobileNumberUtilInterface::MOBILE_NUMBER_UNIQUE_YES) {

  /** @var \Drupal\mobile_number\MobileNumberUtilInterface $util */
  $util = \Drupal::service('mobile_number.util');
  $entity = $this
    ->getEntity();
  $field_name = $this
    ->getFieldDefinition()
    ->getName();
  if (!($mobile_number = $this
    ->getMobileNumber())) {
    return NULL;
  }
  $entity_type_id = $entity
    ->getEntityTypeId();
  $id_key = $entity
    ->getEntityType()
    ->getKey('id');
  $query = \Drupal::entityQuery($entity_type_id)
    ->condition($id_key, (int) $entity
    ->id(), '<>')
    ->condition($field_name, $util
    ->getCallableNumber($mobile_number))
    ->range(0, 1)
    ->count();
  if ($unique_type == MobileNumberUtilInterface::MOBILE_NUMBER_UNIQUE_YES_VERIFIED) {
    $query
      ->condition("{$field_name}.verified", "1");
    if ($this
      ->isVerified()) {
      $result = !(bool) $query
        ->execute();
    }
    else {
      $result = TRUE;
    }
  }
  else {
    $result = !(bool) $query
      ->execute();
  }
  return $result;
}