Commit e00891aa authored by Tim Rohaly's avatar Tim Rohaly Committed by Tim Rohaly
Browse files

Issue #3255038 by TR, jonathan1055: Dependency injection in options providers

parent 4973c905
Loading
Loading
Loading
Loading
+44 −3
Changes for src/TypedData/Options/EntityBundleOptions.php: 44 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -2,8 +2,12 @@

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;

/**
 * Options provider for entity bundles.
@@ -11,7 +15,44 @@ use Drupal\Core\Session\AccountInterface;
 * The returned top-level array is keyed on the bundle label, with nested arrays
 * keyed on the bundle machine name.
 */
class EntityBundleOptions extends OptionsProviderBase {
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}
@@ -20,7 +61,7 @@ class EntityBundleOptions extends OptionsProviderBase {
    $options = [];

    // Load all the entity types.
    $entity_types = \Drupal::service('entity_type.manager')->getDefinitions();
    $entity_types = $this->entityTypeManager->getDefinitions();

    foreach ($entity_types as $entity_type) {
      if (!$entity_type instanceof ContentEntityTypeInterface) {
@@ -28,7 +69,7 @@ class EntityBundleOptions extends OptionsProviderBase {
      }

      // Get the bundles for this entity type.
      $bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($entity_type->id());
      $bundles = $this->entityBundleInfo->getBundleInfo($entity_type->id());

      // Transform the $bundles array into a form suitable for select options.
      array_walk($bundles, function (&$value, $key) {
+31 −2
Changes for src/TypedData/Options/EntityTypeOptions.php: 31 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,13 +2,42 @@

namespace Drupal\rules\TypedData\Options;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Options provider to list all entity types.
 */
class EntityTypeOptions extends OptionsProviderBase {
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}
@@ -17,7 +46,7 @@ class EntityTypeOptions extends OptionsProviderBase {
    $options = [];

    // Load all the entity types.
    $entity_types = \Drupal::service('entity_type.manager')->getDefinitions();
    $entity_types = $this->entityTypeManager->getDefinitions();
    foreach ($entity_types as $entity_type) {
      if (!$entity_type instanceof ContentEntityTypeInterface) {
        continue;
+31 −2
Changes for src/TypedData/Options/FieldListOptions.php: 31 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,12 +2,41 @@

namespace Drupal\rules\TypedData\Options;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Options provider to return all fields in the system.
 */
class FieldListOptions extends OptionsProviderBase {
class FieldListOptions extends OptionsProviderBase implements ContainerInjectionInterface {

  /**
   * The entity field manager service.
   *
   * @var \Drupal\Core\Entity\EntityFieldManagerInterface
   */
  protected $entityFieldManager;

  /**
   * Constructs a FieldListOptions object.
   *
   * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
   *   The entity field manager service.
   */
  public function __construct(EntityFieldManagerInterface $entity_field_manager) {
    $this->entityFieldManager = $entity_field_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('entity_field.manager')
    );
  }

  /**
   * {@inheritdoc}
@@ -16,7 +45,7 @@ class FieldListOptions extends OptionsProviderBase {
    $options = [];

    // Load all the fields in the system.
    $fields = \Drupal::service('entity_field.manager')->getFieldMap();
    $fields = $this->entityFieldManager->getFieldMap();

    // Add each field to our options array.
    foreach ($fields as $entity_fields) {
+36 −4
Changes for src/TypedData/Options/LanguageOptions.php: 36 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -2,25 +2,57 @@

namespace Drupal\rules\TypedData\Options;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Options provider to list all languages enabled on the site.
 */
class LanguageOptions extends OptionsProviderBase {
class LanguageOptions extends OptionsProviderBase implements ContainerInjectionInterface {

  /**
   * The language_manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * Constructs a LanguageOptions object.
   *
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language_manager service.
   */
  public function __construct(LanguageManagerInterface $language_manager) {
    $this->languageManager = $language_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('language_manager')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getPossibleOptions(AccountInterface $account = NULL) {
    $languageManager = \Drupal::service('language_manager');
    $languages = $languageManager->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
    $default = $languageManager->getDefaultLanguage()->getId();
    $languages = $this->languageManager->getLanguages(LanguageInterface::STATE_CONFIGURABLE);
    $default = $this->languageManager->getDefaultLanguage()->getId();
    $options = [LanguageInterface::LANGCODE_NOT_SPECIFIED => $this->t('Not specified')];
    foreach ($languages as $langcode => $language) {
      $options[$langcode] = $language->getName() . ($langcode == $default ? ' - default' : '') . ' (' . $langcode . ')';
    }

    // Sort the result by value for ease of locating and selecting.
    asort($options);

    return $options;
  }

+31 −2
Changes for src/TypedData/Options/NodeTypeOptions.php: 31 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -2,12 +2,41 @@

namespace Drupal\rules\TypedData\Options;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Options provider to list all node types.
 */
class NodeTypeOptions extends OptionsProviderBase {
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}
@@ -16,7 +45,7 @@ class NodeTypeOptions extends OptionsProviderBase {
    $options = [];

    // Load all the node types.
    $node_types = \Drupal::service('entity_type.manager')->getStorage('node_type')->loadMultiple();
    $node_types = $this->entityTypeManager->getStorage('node_type')->loadMultiple();

    foreach ($node_types as $node_type) {
      $options[$node_type->id()] = $node_type->label();
Loading