class EntityTypeOptions

Options provider to list all entity types.

Hierarchy

Expanded class hierarchy of EntityTypeOptions

1 file declares its use of EntityTypeOptions
OptionsProviderTest.php in tests/src/Functional/OptionsProvider/OptionsProviderTest.php

File

src/TypedData/Options/EntityTypeOptions.php, line 14

Namespace

Drupal\rules\TypedData\Options
View source
class EntityTypeOptions extends OptionsProviderBase implements ContainerInjectionInterface {
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Constructs a EntityTypeOptions object.
     *
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager service.
     */
    public function __construct(EntityTypeManagerInterface $entity_type_manager) {
        $this->entityTypeManager = $entity_type_manager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('entity_type.manager'));
    }
    
    /**
     * {@inheritdoc}
     */
    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;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
EntityTypeOptions::$entityTypeManager protected property The entity type manager service.
EntityTypeOptions::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityTypeOptions::getPossibleOptions public function Returns an array of possible values with labels for display. Overrides OptionsProviderInterface::getPossibleOptions
EntityTypeOptions::__construct public function Constructs a EntityTypeOptions object.
OptionsProviderBase::getPossibleValues public function Returns an array of possible values. Overrides OptionsProviderInterface::getPossibleValues
OptionsProviderBase::getSettableOptions public function Returns an array of settable values with labels for display. Overrides OptionsProviderInterface::getSettableOptions
OptionsProviderBase::getSettableValues public function Returns an array of settable values. Overrides OptionsProviderInterface::getSettableValues
StringTranslationTrait::$stringTranslation protected property The string translation service. 3
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.