You are here

public function FieldStorageConfig::getCardinality in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/field/src/Entity/FieldStorageConfig.php \Drupal\field\Entity\FieldStorageConfig::getCardinality()

Returns the maximum number of items allowed for the field.

Possible values are positive integers or FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED.

Return value

int The field cardinality.

Overrides FieldStorageDefinitionInterface::getCardinality

1 call to FieldStorageConfig::getCardinality()
FieldStorageConfig::isMultiple in core/modules/field/src/Entity/FieldStorageConfig.php
Returns whether the field can contain multiple items.

File

core/modules/field/src/Entity/FieldStorageConfig.php, line 634

Class

FieldStorageConfig
Defines the Field storage configuration entity.

Namespace

Drupal\field\Entity

Code

public function getCardinality() {

  /** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
  $field_type_manager = \Drupal::service('plugin.manager.field.field_type');
  $definition = $field_type_manager
    ->getDefinition($this
    ->getType());
  $enforced_cardinality = isset($definition['cardinality']) ? (int) $definition['cardinality'] : NULL;

  // Enforced cardinality is a positive integer or -1.
  if ($enforced_cardinality !== NULL && $enforced_cardinality < 1 && $enforced_cardinality !== FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
    throw new FieldException("Invalid enforced cardinality '{$definition['cardinality']}'. Allowed values: a positive integer or -1.");
  }
  return $enforced_cardinality ?: $this->cardinality;
}