class NodeTypeOptions

Options provider to list all node types.

Hierarchy

Expanded class hierarchy of NodeTypeOptions

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

File

src/TypedData/Options/NodeTypeOptions.php, line 13

Namespace

Drupal\rules\TypedData\Options
View source
class NodeTypeOptions extends OptionsProviderBase implements ContainerInjectionInterface {
    
    /**
     * The entity type manager service.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Constructs a NodeTypeOptions 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 node types.
        $node_types = $this->entityTypeManager
            ->getStorage('node_type')
            ->loadMultiple();
        foreach ($node_types as $node_type) {
            $options[$node_type->id()] = $node_type->label();
            // If the id differs from the label add the id in brackets for clarity.
            if (strtolower(str_replace('_', ' ', $node_type->id())) != strtolower($node_type->label())) {
                $options[$node_type->id()] .= ' (' . $node_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
NodeTypeOptions::$entityTypeManager protected property The entity type manager service.
NodeTypeOptions::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
NodeTypeOptions::getPossibleOptions public function Returns an array of possible values with labels for display. Overrides OptionsProviderInterface::getPossibleOptions
NodeTypeOptions::__construct public function Constructs a NodeTypeOptions 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.