function EntityTypeOptions::getPossibleOptions
Returns an array of possible values with labels for display.
If the optional $account parameter is passed, then the array is filtered to values viewable by the account.
Parameters
\Drupal\Core\Session\AccountInterface $account: (optional) The user account for which to filter the possible options. If omitted, all possible options are returned.
Return value
array An array of possible options for the object that may be used in an Options widget, for example when existing data should be filtered. It may either be a flat array of option labels keyed by values, or a two-dimensional array of option groups (array of flat option arrays, keyed by option group label). Note that labels should NOT be sanitized.
Overrides OptionsProviderInterface::getPossibleOptions
File
-
src/
TypedData/ Options/ EntityTypeOptions.php, line 45
Class
- EntityTypeOptions
- Options provider to list all entity types.
Namespace
Drupal\rules\TypedData\OptionsCode
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;
}
$options[$entity_type->id()] = (string) $entity_type->getLabel();
// If the id differs from the label add the id in brackets for clarity.
if (strtolower(str_replace('_', ' ', $entity_type->id())) != strtolower($entity_type->getLabel())) {
$options[$entity_type->id()] .= ' (' . $entity_type->id() . ')';
}
}
// Sort the result by value for ease of locating and selecting.
asort($options);
return $options;
}