function FieldStorageConfig::getCardinality

Same name in other branches
  1. 8.9.x core/modules/field/src/Entity/FieldStorageConfig.php \Drupal\field\Entity\FieldStorageConfig::getCardinality()
  2. 10 core/modules/field/src/Entity/FieldStorageConfig.php \Drupal\field\Entity\FieldStorageConfig::getCardinality()
  3. 11.x core/modules/field/src/Entity/FieldStorageConfig.php \Drupal\field\Entity\FieldStorageConfig::getCardinality()

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;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.