2 added 158 characters in body
source | link

IfYes, you setcan limit a custom field to single value by defining a cardinality in the annotations of the field type.

I don't know if you can find this in the docs, for examplebut you can use the core comment field as example:

/**
 * Plugin implementation of the 'comment' field type.
 *
 * @FieldType(
 *   id = "comment",
 *   label = @Translation("Comments"),
 *   description = @Translation("This field manages configuration and presentation of comments on an entity."),
 *   list_class = "\Drupal\comment\CommentFieldItemList",
 *   default_widget = "comment_default",
 *   default_formatter = "comment_default",
 *   cardinality = 1,
 * )
 */
class CommentItem extends FieldItemBase implements CommentItemInterface {

When addingyou add a comment field, to a content type the cardinality form element is hidden and instead this message is displayed:

Allowed number of values

This field cardinality is set to 1 and cannot be configured.

If you set a cardinality in the field type, for example the comment field:

/**
 * Plugin implementation of the 'comment' field type.
 *
 * @FieldType(
 *   id = "comment",
 *   label = @Translation("Comments"),
 *   description = @Translation("This field manages configuration and presentation of comments on an entity."),
 *   list_class = "\Drupal\comment\CommentFieldItemList",
 *   default_widget = "comment_default",
 *   default_formatter = "comment_default",
 *   cardinality = 1,
 * )
 */
class CommentItem extends FieldItemBase implements CommentItemInterface {

When adding a field, the cardinality form element is hidden and instead this message is displayed:

Allowed number of values

This field cardinality is set to 1 and cannot be configured.

Yes, you can limit a custom field to single value by defining a cardinality in the annotations of the field type.

I don't know if you can find this in the docs, but you can use the core comment field as example:

/**
 * Plugin implementation of the 'comment' field type.
 *
 * @FieldType(
 *   id = "comment",
 *   label = @Translation("Comments"),
 *   description = @Translation("This field manages configuration and presentation of comments on an entity."),
 *   list_class = "\Drupal\comment\CommentFieldItemList",
 *   default_widget = "comment_default",
 *   default_formatter = "comment_default",
 *   cardinality = 1,
 * )
 */
class CommentItem extends FieldItemBase implements CommentItemInterface {

When you add a comment field to a content type the cardinality form element is hidden and instead this message is displayed:

Allowed number of values

This field cardinality is set to 1 and cannot be configured.

1
source | link

If you set a cardinality in the field type, for example the comment field:

/**
 * Plugin implementation of the 'comment' field type.
 *
 * @FieldType(
 *   id = "comment",
 *   label = @Translation("Comments"),
 *   description = @Translation("This field manages configuration and presentation of comments on an entity."),
 *   list_class = "\Drupal\comment\CommentFieldItemList",
 *   default_widget = "comment_default",
 *   default_formatter = "comment_default",
 *   cardinality = 1,
 * )
 */
class CommentItem extends FieldItemBase implements CommentItemInterface {

When adding a field, the cardinality form element is hidden and instead this message is displayed:

Allowed number of values

This field cardinality is set to 1 and cannot be configured.