function EntityReferenceItem::fieldSettingsSummary

Same name and namespace in other branches
  1. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem::fieldSettingsSummary()

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php, line 130

Class

EntityReferenceItem
Defines the 'entity_reference' entity field type.

Namespace

Drupal\Core\Field\Plugin\Field\FieldType

Code

public static function fieldSettingsSummary(FieldDefinitionInterface $field_definition) : array {
    $summary = parent::fieldSettingsSummary($field_definition);
    $target_type = $field_definition->getFieldStorageDefinition()
        ->getSetting('target_type');
    $handler_settings = $field_definition->getSetting('handler_settings');
    if (!isset($handler_settings['target_bundles'])) {
        return $summary;
    }
    
    /** @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_bundle_information */
    $entity_bundle_information = \Drupal::service('entity_type.bundle.info');
    $bundle_info = $entity_bundle_information->getBundleInfo($target_type);
    $bundles = array_map(fn($bundle) => $bundle_info[$bundle]['label'], $handler_settings['target_bundles']);
    $bundle_label = \Drupal::entityTypeManager()->getDefinition($target_type)
        ->getBundleLabel();
    if (!empty($bundles)) {
        $summary[] = new FormattableMarkup('@bundle: @entity_type', [
            '@bundle' => $bundle_label ?: new TranslatableMarkup('Bundle'),
            '@entity_type' => implode(', ', $bundles),
        ]);
    }
    return $summary;
}

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