Comments

thalles created an issue. See original summary.

thalles’s picture

Title: Drupal dependency injection » Drupal dependency injection on Controller/ItemListController
Issue summary: View changes
StatusFileSize
new2.05 KB

Follow the patch!

thalles’s picture

Status: Needs work » Needs review
megachriz’s picture

Status: Needs review » Needs work

Nice change. I have a few remarks.

  1. +++ b/src/Controller/ItemListController.php
    @@ -9,12 +9,37 @@ use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
    +  protected $entityQuery;
    ...
    +  protected $dateFormatter;
    ...
    +   * {@inheritdoc}
    

    The contructor parameters and the class properties need to be documented.

  2. +++ b/src/Controller/ItemListController.php
    @@ -56,7 +81,7 @@ class ItemListController extends ControllerBase {
    -      $ago = \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $entity->get('feeds_item')->imported);
    +      $ago = $this->dateFormatter->formatInterval(REQUEST_TIME - $entity->get('feeds_item')->imported);
    

    Now that we are at it, we might as well replace the constant REQUEST_TIME which is deprecated since Drupal 8.3.
    See https://api.drupal.org/api/drupal/core%21includes%21bootstrap.inc/consta...

thalles’s picture

Status: Needs work » Needs review
StatusFileSize
new5.5 KB

Thanks for helping me! :)

Follow the patch!

megachriz’s picture

Status: Needs review » Needs work

It looks like that patch is meant for the Google Analytics module. ;)

thalles’s picture

Status: Needs work » Needs review
StatusFileSize
new2.85 KB

:)
Sorry! I was in the wrong directory at the time of generating the patch.
Follow!

megachriz’s picture

Status: Needs review » Needs work
+++ b/src/Controller/ItemListController.php
@@ -9,12 +9,65 @@ use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
+use Drupal\Core\Entity\Query\QueryFactory;
...
+  /**
+   * Creating entity query objects.
+   *
+   * @var \Drupal\Core\Entity\Query\QueryFactory
+   */
+  protected $entityQuery;
...
+   * @param \Drupal\Core\Entity\Query\QueryFactory $entity_query
+   *   Entity query objects.
...
+  public function __construct(QueryFactory $entity_query, Time $time, DateFormatter $date_formatter) {
+    $this->entityQuery = $entity_query;
...
+      $container->get('entity.query'),

Sorry, I forgot that the entity.query service is also deprecated. See https://www.drupal.org/node/2849874

I think that instead we need to pass the entity type manager here.

thalles’s picture

Status: Needs work » Needs review
StatusFileSize
new2.5 KB

Follow the patch!

megachriz’s picture

I was about to commit this, but I noticed that classes were referenced instead of interfaces. It is a best practice to require interfaces in methods instead of classes if available (a prerequisite is of course that the interface has the methods that you want to call on the object).

Also changed:

  • Sorted the use statements on the top of the file.
  • Removed a blank line in ::create().
  • Swapped the lines in the constructor so $time and $date_formatter are everywhere in the same order.
thalles’s picture

Status: Needs review » Reviewed & tested by the community

Thanks!
Works for me!

  • MegaChriz committed 41ee9e7 on 8.x-3.x authored by thalles
    Issue #3020920 by thalles, MegaChriz: Fixed use dependency injection in...
megachriz’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for checking, @thalles! Committed #10.

thalles’s picture

Muito bom trabalho com você!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.