class EntityBundleOptions

Options provider for entity bundles.

The returned top-level array is keyed on the bundle label, with nested arrays keyed on the bundle machine name.

Hierarchy

Expanded class hierarchy of EntityBundleOptions

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

File

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

Namespace

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

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
EntityBundleOptions::$entityBundleInfo protected property The entity type bundle information manager.
EntityBundleOptions::$entityTypeManager protected property The entity type manager service.
EntityBundleOptions::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityBundleOptions::getPossibleOptions public function Returns an array of possible values with labels for display. Overrides OptionsProviderInterface::getPossibleOptions
EntityBundleOptions::__construct public function Constructs a EntityBundleOptions 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.