You are here

protected function EntityReferenceBrowserWidget::getCardinalityMessage in Entity Browser 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php \Drupal\entity_browser\Plugin\Field\FieldWidget\EntityReferenceBrowserWidget::getCardinalityMessage()

Generates a message informing the user how many more items they can choose.

Parameters

array|int $selected: The current selections, or how many items are selected.

Return value

string A message informing the user who many more items they can select.

1 call to EntityReferenceBrowserWidget::getCardinalityMessage()
EntityReferenceBrowserWidget::displayCurrentSelection in src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php
Builds the render array for displaying the current results.

File

src/Plugin/Field/FieldWidget/EntityReferenceBrowserWidget.php, line 688

Class

EntityReferenceBrowserWidget
Plugin implementation of the 'entity_reference' widget for entity browser.

Namespace

Drupal\entity_browser\Plugin\Field\FieldWidget

Code

protected function getCardinalityMessage($selected) {
  $message = NULL;
  $storage = $this->fieldDefinition
    ->getFieldStorageDefinition();
  $cardinality = $storage
    ->getCardinality();
  $target_type = $storage
    ->getSetting('target_type');
  $target_type = $this->entityTypeManager
    ->getDefinition($target_type);
  if (is_array($selected)) {
    $selected = count($selected);
  }
  if ($cardinality === 1 && $selected === 0) {
    $message = $this
      ->t('You can select one @entity_type.', [
      '@entity_type' => $target_type
        ->getSingularLabel(),
    ]);
  }
  elseif ($cardinality >= $selected) {
    $message = $this
      ->t('You can select up to @maximum @entity_type (@remaining left).', [
      '@maximum' => $cardinality,
      '@entity_type' => $target_type
        ->getPluralLabel(),
      '@remaining' => $cardinality - $selected,
    ]);
  }
  return (string) $message;
}