Use dependency injection in entity query

Created on 1 May 2018, about 6 years ago
Updated 14 July 2023, 12 months ago

Problem/Motivation

Coming from a comment from @plach in #2784921-146: Add Workspaces experimental module → , we need to be able to use Dependency Injection in the entity query code.

For example, \Drupal\Core\Entity\Query\Sql\Tables::__construct() currently sets the entity type manager using \Drupal::entityTypeManager().

  /**
   * @param \Drupal\Core\Database\Query\SelectInterface $sql_query
   *   The SQL query.
   */
  public function __construct(SelectInterface $sql_query) {
    $this->sqlQuery = $sql_query;
    $this->entityTypeManager = \Drupal::entityTypeManager();
    $this->entityFieldManager = \Drupal::service('entity_field.manager');
  }
📌 Task
Status

Needs work

Version

11.0 🔥

Component
Entity  →

Last updated 24 minutes ago

  • Maintained by
  • 🇬🇧United Kingdom @catch
  • 🇨🇭Switzerland @Berdir
  • 🇩🇪Germany @hchonov
Created by

🇷🇴Romania amateescu

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇮🇳India himanshu_jhaloya Mandsaur

    using this way can do
    basic/basic.services.yml

    services:
    basic.basic_hero_articles:
    class: Drupal\basic\BasicArticleService

    basic.basic_recipes:
    class: Drupal\basic\BasicRecipeService
    arguments: ['@entity.manager']

    ...

    lightning-8/modules/custom/basic/src/BasicRecipeService.php

    <?php

    namespace Drupal\basic;

    use Drupal\Core\Entity\EntityManager;

    /**
    * Our basic recipes service
    */
    class BasicRecipeService {

    private $entityQuery;
    private $entityManager;

    public function __construct(EntityManager $entityManager) {
    $this->entityManager = $entityManager;
    }

    /**
    * Methood for getting the recipes
    */
    public function getRecipes() {
    $recipesNids = $this->entityQuery->get('node')->condition('type', 'recipe')->execute();
    return $this->entityManager->getStorage('node')->loadMultiple($recipesNids);
    }
    }

  • Issue was unassigned.
  • Status changed to Needs review over 1 year ago
  • Status changed to Needs work over 1 year ago
  • 🇺🇸United States smustgrave

    Not sure what's there to review in #14? Are you asking how dependency injection works?

    Adding a new service or updating one will require a change record just FYI.

  • Assigned to roshni27
  • 🇮🇳India roshni27

    Drupal core version 11.x.
    I am working on it.

  • Issue was unassigned.
  • Status changed to Needs review 12 months ago
  • last update 12 months ago
    Custom Commands Failed
  • 🇮🇳India roshni27

    Please Review the patch.

  • Status changed to Needs work 12 months ago
  • 🇺🇸United States smustgrave

    Will need a trigger_error for the new parameter.

    Also please don't assign tickets to yourself unless a maintainer. A simple comment is enough.

  • 🇮🇹Italy apaderno Brescia, 🇮🇹
    +  /**
    +   * Creates an instance of this class.
    +   *
    +   * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
    +   *   The container to resolve services.
    +   *
    +   * @return static
    +   *   The instance of this class.
    +   */
    +  public static function create(ContainerInterface $container) {
    

    The documentation comment for methods defined in an interface does not need to give that information, since it would repeat the documentation comment present in the interface.

       /**
        * Add the next entity base table.
        *
    -   * For example, when building the SQL query for
    +   * For example, when building the SQL query for.
        * @code
        * condition('uid.entity.name', 'foo', 'CONTAINS')
        * @endcode

    Since that comment is changed, it would be better to change it as:

      /**
       * Add the next entity base table.
       *
       * For example, this adds the users table.
       * @code
       * condition('uid.entity.name', 'foo', 'CONTAINS')
       * @endcode
       *
       * @param \Drupal\Core\Entity\EntityType $entity_type
       *   The entity type being joined, in the above example, User.
       * @param string $table
       *   This is the table being joined, in the above example, {users}.
       * @param string $sql_column
       *   This is the SQL column in the existing table being joined to.
       * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $field_storage
       *   The field storage definition for the field referencing this column.
       *
       * @return string
       *   The alias of the next entity table joined in.
       */
    
Production build 0.69.0 2024