Updated: Comment #30

Problem/Motivation

Want to have a specific type hint so that IDE's can autocomplete on the methods. Change was triggered by the way translations are handled.

Proposed resolution

Applied patch to current code base with no errors.

Remaining tasks

Needs code review.

User interface changes

None.

API changes

None.

Original report by @Berdir

Comments

bomoko’s picture

Assigned: Unassigned » bomoko

Assigning to myself

bomoko’s picture

I wanted to check, should I be searching for the phrase "EntityInterface $node" across the _entire_ code base? Including tests?

Further, I was wondering if there were any other discussion threads on DO that give the background to this, so I can understand the context of the task slightly better.

berdir’s picture

Basically yes, but it might make sense to start somewhere, e.g. node.module. and first change those and check the test results.

The reason is that we want to have a specific type hint so that IDE's can autocomplete on the methods. We already had them, but had to change it due to do something that since has been changed again, the way translations are handled. See #1391694: Use type-hinting for entity-parameters for one related issue.

bomoko’s picture

Status: Active » Needs review
StatusFileSize
new29.77 KB

Okay here is a first shot patch - as suggested by Berdir, I've taken a swing at node.module.

There are a few places where there are still EntityInterfaces in the node module (mainly where the tests were failing) - I'll trace through the code and work out if and whether these need to be changed to NodeInterface's too - any pointers in expunging these (whether they even need to be killed, for instance) would be greatly appreciated as I'm not yet comfortable with the core source (which is, of course, why I'm trying to do these novice tasks).

berdir’s picture

+++ b/core/modules/node/tests/modules/node_test/node_test.module
@@ -159,7 +158,7 @@ function node_test_node_update(EntityInterface $node) {
-function node_test_entity_view_mode_alter(&$view_mode, Drupal\Core\Entity\EntityInterface $entity, $context) {
+function node_test_entity_view_mode_alter(&$view_mode, Drupal\node\NodeInterface $entity, $context) {

This is an $entity, not a node, so this one shouldn't be replaced.

This looks great already. This overlaps with a few big issues (page callback conversions, node BC removal, converting search to plugins) that will remove or touch some of those functions. Those are currently more important to get in, so this will require a re-roll after they are.

You might want to follow #1939994: Complete conversion of nodes to the new Entity Field API, #2003482: Convert hook_search_info to plugin system and the not yet committed, node related issues in #1971384: [META] Convert page callbacks to controllers and wait with a re-roll until they landed.

Status: Needs review » Needs work
bomoko’s picture

okay cool - following them. Should I leave the ticket assigned to myself while we wait? (not quite sure about the protocol for this)

bomoko’s picture

Assigned: bomoko » Unassigned

unassigning from myself for now

xjm’s picture

Component: node.module » node system
Issue summary: View changes

(Merging "node system" and "node.module" components for 8.x; disregard.)

berdir’s picture

Issue tags: +Needs reroll

The blockers are through now, would be nice to finish this :)

jdillick’s picture

Assigned: Unassigned » jdillick
jdillick’s picture

Issue summary: View changes
StatusFileSize
new40.16 KB
jdillick’s picture

Issue summary: View changes

Rerolled this patch as part of core mentoring. This patch does not deal with books, comments, forum, history, menu, taxonomy, tracker, devel implicit use of EntityInterface for apparent Node objects.

I'll probably reroll once more to include these things after test run for this patch.

Point of discussion though. If there is not loss of functionality with the current use of the more generic interface, why would we want to enforce the more specific interface? Regardless of the intended use of the node api, why not allow other objects conforming to the EntityInterface to work with the node api?

I would think it is generally a good thing to air on the side of using the least specific interface necessary to enforce the method calls used in the implementation. In the interest of not bike-shedding on this, I'm going to assume there is a good reason to proceed that isn't obvious to me.

John

berdir’s picture

Status: Needs work » Needs review

Thanks! (remember to set issues to needs review when you upload a patch)

node hooks work with node entities. Passing anything else to them is not valid. So the type hint should be on NodeInterface. Otherwise, modules would have to add an if ($node instanceof NodeInterface) to their code to be able to rely on methods provided by that interface. There are separate hook_entity_* hooks that are invoked for all entity types, the point of hook_node_* (hook_$ENTITY_TYPE_) is that they only receive entities of a specific type.

Status: Needs review » Needs work

The last submitted patch, 12: 2067345-entityinterface-to-nodeinterface.patch, failed testing.

jdillick’s picture

Issue tags: +DrupalWorkParty
jdillick’s picture

StatusFileSize
new40.17 KB
jdillick’s picture

StatusFileSize
new39.39 KB
berdir’s picture

Status: Needs work » Needs review

Status: Needs review » Needs work

The last submitted patch, 18: 2067345-entityinterface-to-nodeinterface.patch, failed testing.

berdir’s picture

  1. +++ b/core/modules/node/lib/Drupal/node/NodeAccessController.php
    @@ -87,7 +87,7 @@ public function createAccess($entity_bundle = NULL, AccountInterface $account =
        * {@inheritdoc}
        */
    -  protected function checkAccess(EntityInterface $node, $operation, $langcode, AccountInterface $account) {
    +  protected function checkAccess(NodeInterface $node, $operation, $langcode, AccountInterface $account) {
    

    You can't change it here, becauseEntityInterface is enforced by the parent class.

  2. +++ b/core/modules/node/lib/Drupal/node/NodeListController.php
    @@ -93,40 +92,40 @@ public function buildHeader() {
        * {@inheritdoc}
        */
    -  public function buildRow(EntityInterface $entity) {
    +  public function buildRow(NodeInterface $node) {
    

    Same here, everywhere where it says "@ineheritdoc" means that it's enforced by a parent class or interface to be EntityInterface.

  3. +++ b/core/modules/node/lib/Drupal/node/NodeTranslationController.php
    @@ -18,8 +17,8 @@ class NodeTranslationController extends ContentTranslationController {
       /**
        * Overrides ContentTranslationController::entityFormAlter().
        */
    -  public function entityFormAlter(array &$form, array &$form_state, EntityInterface $entity) {
    -    parent::entityFormAlter($form, $form_state, $entity);
    +  public function entityFormAlter(array &$form, array &$form_state, NodeInterface $node) {
    +    parent::entityFormAlter($form, $form_state, $node);
    

    Or an outdated Overrides/Implements docblock in a class method.

jdillick’s picture

StatusFileSize
new31.8 KB

Ok, thanks... I learned something, was under mistaken idea that you could change the fingerprint if you were overriding a class. I knew you couldn't override the fingerprint if implementing an interface, but I didn't know it enforced the interface in the parent class too.

Here I've rerolled that patch to remove those attempts.

jdillick’s picture

Issue tags: +Needs reviewed
jdillick’s picture

Do we want to fix those old docblocks, or would that be better for another ticket?

yesct’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll, -Needs reviewed +Needs issue summary update

@jdillick to trigger the testbot, instead of a review tag. it's under the field "Status". No worries, you will get the hang of it. :)

taking off the Needs reroll tag since this new patch does apply ok to the current 8.x head.

Unless someone else has a good reason, I would suggest only changing just the docs (@param, ...) that you *have* to to keep the code accurate, and open another issue to do a clean up of old docs. If you open another issue, please link it to this one. :)

Thanks! And ask any questions you have. :)

oh, adding tag: Needs issue summary update. This issue doesn't have a summary. :) Here are instructions: https://drupal.org/contributor-tasks/write-issue-summary (I hightly recommend installing dreditor in your browser, it has a nice issue summary button to insert the issue summary template)

ps. In case you are curious, or want to provide a new contributor opinion, #2013222: Add "Issue tasks" to project issues and correlate tasks with handbook documentation is where we are discussing a new field for "needs" tasks.

Status: Needs review » Needs work

The last submitted patch, 22: 2067345-entityinterface-to-nodeinterface.patch, failed testing.

xano’s picture

Status: Needs work » Needs review
StatusFileSize
new28.97 KB

Re-rolling, plus reverting some incorrect changes to node type code.

Status: Needs review » Needs work

The last submitted patch, 27: drupal_2067345_27.patch, failed testing.

xano’s picture

Status: Needs work » Needs review
StatusFileSize
new29.31 KB
new627 bytes
berdir’s picture

Status: Needs review » Reviewed & tested by the community

Finding weird things in the documentation but that's not the problem of this issue. Thanks, looks good!

  1. +++ b/core/modules/node/node.api.php
    @@ -546,7 +545,7 @@ function hook_node_load($nodes) {
      * @link node_access Node access rights @endlink for a full explanation.
      *
    - * @param \Drupal\Core\Entity\EntityInterface|string $node
    + * @param \Drupal\node\NodeInterface|string $node
      *   Either a node entity or the machine name of the content type on which to
      *   perform the access check.
    

    This isn't true anymore I think, but let's not touch that there.

  2. +++ b/core/modules/node/node.api.php
    @@ -766,7 +765,7 @@ function hook_node_validate(\Drupal\Core\Entity\EntityInterface $node, $form, &$
      *
      * @ingroup node_api_hooks
      */
    -function hook_node_submit(\Drupal\Core\Entity\EntityInterface $node, $form, &$form_state) {
    +function hook_node_submit(\Drupal\node\NodeInterface $node, $form, &$form_state) {
       // Decompose the selected menu parent option into 'menu_name' and 'plid', if
       // the form used the default parent selection widget.
       if (!empty($form_state['values']['menu']['parent'])) {
    

    Didn't we kill that stuff? :) Again, not our problem here.

WarrenK’s picture

Issue summary: View changes
alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

Needs a reroll

error: patch failed: core/modules/node/node.module:217
error: core/modules/node/node.module: patch does not apply
internetdevels’s picture

internetdevels’s picture

Status: Needs work » Needs review

changed to "need review"

berdir’s picture

Status: Needs review » Needs work
+++ b/core/modules/node/node.module
@@ -217,13 +218,13 @@ function node_entity_form_display_alter(EntityFormDisplayInterface $form_display
  * Entity URI callback.
  *
- * @param \Drupal\Core\Entity\EntityInterface $node
+ * @param \Drupal\Core\Entity\NodeInterface $node
  *   A node entity.
  *

This doesn't exist.

Also not 100% if we should change this, if this would be a default method on the entity class, we wouldn't be allowed to do so.

Otherwise the re-roll looks fine.

xano’s picture

Status: Needs work » Needs review
StatusFileSize
new29.2 KB
new450 bytes
andypost’s picture

Related issue #2030191: Clean-up api examples of node module just about to clean-up node.api.php

berdir’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: -Needs issue summary update, -Needs reroll

Thanks.

alexpott’s picture

Status: Reviewed & tested by the community » Needs work
Issue tags: +Needs reroll

drupal_2067345_36.patch no longer applies.

error: patch failed: core/modules/node/lib/Drupal/node/NodeAccessController.php:88
error: core/modules/node/lib/Drupal/node/NodeAccessController.php: patch does not apply

xano’s picture

Status: Needs work » Reviewed & tested by the community
Issue tags: -Needs reroll
StatusFileSize
new29.45 KB

Re-roll because of NodeAccessController.

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 40: drupal_2067345_40.patch, failed testing.

xano’s picture

Status: Needs work » Needs review
StatusFileSize
new29.63 KB
new400 bytes
andypost’s picture

Status: Needs review » Reviewed & tested by the community

back to rtbc

webchick’s picture

Status: Reviewed & tested by the community » Fixed
Issue tags: +Will cause commit conflicts

Yayyyy this patch makes me so happyyyyyy! :D

It touches a bunch of stuff though, in sensitive areas where we have a lot of criticals/majors at the moment.

I'm going to tentatively commit this, but if it breaks something more important, we'll need to roll this back and schedule it just after the next alpha instead.

Committed and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

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