Problem/Motivation

When I `grep -rl -- \\Drupal:: .` from the module root (and ignore tests and procedural code) I get:

./src/ParagraphAccessControlHandler.php
./src/Entity/ParagraphsType.php
./src/Entity/Paragraph.php
./src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
./src/Plugin/Field/FieldWidget/ParagraphsWidget.php
./src/Plugin/Field/FieldFormatter/ParagraphsSummaryFormatter.php
./modules/paragraphs_library/src/Controller/LibraryItemController.php
./modules/paragraphs_library/src/Entity/LibraryItem.php

As stated in https://www.drupal.org/docs/8/api/services-and-dependency-injection/serv... we really ought to use Dependency Injection.

Proposed resolution

Update the code to use Dependency Injection.

Remaining tasks

Do we want this to be a META issue and break the task into smaller pieces?
Will we want or need to create additional tests?

User interface changes

None

API changes

None

Data model changes

None

Release notes snippet

N/A

Issue fork paragraphs-3091424

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

vegantriathlete created an issue. See original summary.

vegantriathlete’s picture

Issue summary: View changes

Corrected typo in issue summary.

saphemmy’s picture

Assigned: Unassigned » saphemmy
ruturaj chaubey’s picture

Are you still working on this issue?
If not, I would like to take up this issue.

saphemmy’s picture

Assigned: saphemmy » Unassigned
ruturaj chaubey’s picture

Assigned: Unassigned » ruturaj chaubey
chakkche’s picture

ruturaj chaubey’s picture

Assigned: ruturaj chaubey » Unassigned
chakkche’s picture

Assigned: Unassigned » chakkche

I will work on this.

chakkche’s picture

Assigned: chakkche » Unassigned
Category: Task » Bug report
Status: Active » Needs review
StatusFileSize
new96.46 KB

Fixed all the drupal calls and coding standards in their respective files

ruturaj chaubey’s picture

Assigned: Unassigned » ruturaj chaubey

Will review this.

ruturaj chaubey’s picture

Assigned: ruturaj chaubey » Unassigned
Status: Needs review » Needs work

While testing this patch, this following error occurred.

[php7:error] [pid 1335] [client ::1:51754] PHP Fatal error:  Declaration of Drupal\\paragraphs\\Plugin\\Field\\FieldWidget\\ParagraphsWidget::create(Symfony\\Component\\DependencyInjection\\ContainerInterface $container) must be compatible with Drupal\\Core\\Field\\WidgetBase::create(Symfony\\Component\\DependencyInjection\\ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) in /var/www/html/drupalDemo/web/modules/contrib/paragraphs/src/Plugin/Field/FieldWidget/ParagraphsWidget.php on line 273, referer: http://localhost/drupalDemo/web/admin/structure/types/manage/test/fields/add-field

The create method of the ParagraphsWidget Class is not compatible with the core's WidgetBase Class.

Steps to Reproduce

1. Create a paragraph type, by going to admin/structure/paragraphs_type .
2. Add a text or any field to the newly created paragraph type.
3. After the paragraph type is created, Add the paragraph field to a content type.
4. Then, while accessing the field widget of the paragraph type, a HTTP 500 response is displayed.

Also, I think the dependency injection in the ParagraphsWidget is not handled properly.

julien_g’s picture

Patch is outdated.

I have run the grep command. Here are the new files with \\Drupal::

./src/ParagraphAccessControlHandler.php
./src/MigrationPluginsAlterer.php
./src/Plugin/migrate/D7ParagraphsItemDeriver.php
./src/Plugin/migrate/D7FieldCollectionItemDeriver.php
./src/Plugin/Field/FieldFormatter/ParagraphsSummaryFormatter.php
./src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
./src/Plugin/Field/FieldWidget/ParagraphsWidget.php
./src/Entity/ParagraphsType.php
./src/Entity/Paragraph.php
./modules/paragraphs_library/src/Entity/LibraryItem.php

Patch in progress.

julien_g’s picture

Status: Needs work » Needs review
StatusFileSize
new41.84 KB

Here is the patch.

Some files stil contains \Drupal:: calls :

  • ./src/Plugin/Field/FieldFormatter/ParagraphsSummaryFormatter.php : Service call in static functions
  • ./src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php : Service call in static functions
  • ./src/Plugin/Field/FieldWidget/ParagraphsWidget.php : Service call in static functions
  • ./src/Entity/ParagraphsType.php : Not possible to user container for Dependency Injection on Entities
  • ./src/Entity/Paragraph.php : Not possible to user container for Dependency Injection on Entities
  • ./modules/paragraphs_library/src/Entity/LibraryItem.php : Not possible to user container for Dependency Injection on Entities
tmaiochi’s picture

Assigned: Unassigned » tmaiochi

I'll review this!

tmaiochi’s picture

Assigned: tmaiochi » Unassigned
Status: Needs review » Needs work

The patch #14 I found a few standard coding problems in the added code and, when I ran the tests, I got a lot of errors that weren't there before the patch, so I'm moving to needs work

julien_g’s picture

Status: Needs work » Needs review

Hi @tmaiochi, can you give me more details .
When I run phpcs checks I have the following results :

  • bash-5.1# ../vendor/bin/phpcs --standard=Drupal modules/contrib/paragraphs/src/ParagraphAccessControlHandler.php
  • bash-5.1# ../vendor/bin/phpcs --standard=Drupal modules/contrib/paragraphs/src/Plugin/migrate/D7*

for files that have issues on phpcs :
Before patch :

FILE: /var/www/web/modules/contrib/paragraphs/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
--------------------------------------------------------------------------------------------------------
FOUND 113 ERRORS AND 15 WARNINGS AFFECTING 122 LINES
--------------------------------------------------------------------------------------------------------
   84 | ERROR   | [x] Short array syntax must be used to define arrays
FILE: /var/www/web/modules/contrib/paragraphs/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
--------------------------------------------------------------------------------------------------------
FOUND 88 ERRORS AND 20 WARNINGS AFFECTING 102 LINES
--------------------------------------------------------------------------------------------------------
  112 | ERROR   | [x] Expected 1 space after IF keyword; 0 found

After patch :

FILE: /var/www/web/modules/contrib/paragraphs/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php
--------------------------------------------------------------------------------------------------------
FOUND 113 ERRORS AND 15 WARNINGS AFFECTING 122 LINES
--------------------------------------------------------------------------------------------------------
  214 | ERROR   | [x] Short array syntax must be used to define arrays
FILE: /var/www/web/modules/contrib/paragraphs/src/Plugin/Field/FieldWidget/ParagraphsWidget.php
--------------------------------------------------------------------------------------------------------
FOUND 88 ERRORS AND 20 WARNINGS AFFECTING 102 LINES
--------------------------------------------------------------------------------------------------------
  240 | ERROR   | [x] Expected 1 space after IF keyword; 0 found
tmaiochi’s picture

Of course @julien_g

In reality it's just a phpcs error that accuses the InlineParagraphsWidget.php file:

I ran phpcs --standard=Drupal,DrupalPractice --extensions=php,module,inc,install,test,profile,theme,css,info,txt,md,yml,js src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php

And the only line that there was a change and phpcs gives a message was this:

396 | ERROR | [x] Short array syntax must be used to define arrays

julien_g’s picture

StatusFileSize
new41.88 KB

Thanks @tmaiochi,

here is the patch with phpcs error fixed

anabpv’s picture

Assigned: Unassigned » anabpv

I will review this

anabpv’s picture

Assigned: anabpv » Unassigned
Status: Needs review » Reviewed & tested by the community

Steps performed:
1 - downloaded the module and applied the patch from #19
2 - ran phpcs to properly review the dependency injections
3 - opened an MR and committed the changes from #19

Moving to RTBC.

The last submitted patch, 14: 3091424_4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

The last submitted patch, 14: 3091424_4.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 19: 3091424_19.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

saphemmy’s picture

I do suggest phpcs should be created on another issue if none exists.
My opinion: This issue should address the issue scope which is dependency injections as described by the title.

julien_g’s picture

StatusFileSize
new41.94 KB

Hi,

New patch with failed test fixes.

julien_g’s picture

StatusFileSize
new42.52 KB

Service not found exception on test fixes.

julien_g’s picture

StatusFileSize
new42.57 KB
julien_g’s picture

StatusFileSize
new42.57 KB
julien_g’s picture

StatusFileSize
new42.56 KB

Here is the patch that pass all tests

julien_g’s picture

Status: Needs work » Needs review
lucasbaralm’s picture

Assigned: Unassigned » lucasbaralm

I'm going to review this!

lucasbaralm’s picture

Assigned: lucasbaralm » Unassigned
Status: Needs review » Reviewed & tested by the community

Steps performed:
1 - downloaded the module and applied the patch from #31.
2 - ran phpcs to properly review the dependency injections.

Moving to RTBC.

berdir’s picture

Status: Reviewed & tested by the community » Needs work

Must be a merge request now.

jatingupta40 made their first commit to this issue’s fork.

jatingupta40’s picture

#3091424: Created and rebase the MR with respect to 8.x-1.x.
Moving to Needs Review.

jatingupta40’s picture

Status: Needs work » Needs review