function EntityBundleOptions::getPossibleOptions

Overrides OptionsProviderInterface::getPossibleOptions

File

src/TypedData/Options/EntityBundleOptions.php, line 60

Class

EntityBundleOptions
Options provider for entity bundles.

Namespace

Drupal\rules\TypedData\Options

Code

public function getPossibleOptions(AccountInterface $account = NULL) {
    $options = [];
    // Load all the entity types.
    $entity_types = $this->entityTypeManager
        ->getDefinitions();
    foreach ($entity_types as $entity_type) {
        if (!$entity_type instanceof ContentEntityTypeInterface) {
            continue;
        }
        // Get the bundles for this entity type.
        $bundles = $this->entityBundleInfo
            ->getBundleInfo($entity_type->id());
        // Transform the $bundles array into a form suitable for select options.
        array_walk($bundles, function (&$value, $key) {
            // Flatten to just the label text.
            $value = (string) $value['label'];
            // If the key differs from the label add the key in brackets.
            if (strtolower(str_replace('_', ' ', $key)) != strtolower($value)) {
                $value .= ' (' . $key . ')';
            }
        });
        $options[(string) $entity_type->getLabel()] = $bundles;
    }
    // Sort the result by key, which is the group name.
    ksort($options);
    return $options;
}