Namespace
Drupal\rules\TypedData\Options
File
-
src/TypedData/Options/EntityBundleOptions.php
View source
<?php
namespace Drupal\rules\TypedData\Options;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class EntityBundleOptions extends OptionsProviderBase implements ContainerInjectionInterface {
protected $entityTypeManager;
protected $entityBundleInfo;
public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_bundle_info) {
$this->entityTypeManager = $entity_type_manager;
$this->entityBundleInfo = $entity_bundle_info;
}
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager'), $container->get('entity_type.bundle.info'));
}
public function getPossibleOptions(AccountInterface $account = NULL) {
$options = [];
$entity_types = $this->entityTypeManager
->getDefinitions();
foreach ($entity_types as $entity_type) {
if (!$entity_type instanceof ContentEntityTypeInterface) {
continue;
}
$bundles = $this->entityBundleInfo
->getBundleInfo($entity_type->id());
array_walk($bundles, function (&$value, $key) {
$value = (string) $value['label'];
if (strtolower(str_replace('_', ' ', $key)) != strtolower($value)) {
$value .= ' (' . $key . ')';
}
});
$options[(string) $entity_type->getLabel()] = $bundles;
}
ksort($options);
return $options;
}
}
Classes