Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Replace all instances of node_load()
with Node::load()
.
Change record: EntityInterface::load(), loadMultiple() and create() added to load and create new entities.
Comment | File | Size | Author |
---|---|---|---|
#18 | D8_NodeLoad_2418533_18.patch | 4.82 KB | herved |
#17 | D8_NodeLoad_2418533_17.patch | 3.81 KB | pfrenssen |
Comments
Comment #1
nripeshtrivedi CreditAttribution: nripeshtrivedi commentedHi,
Please find the patch attached for this issue.Any comments or suggestions are appreciated.
Comment #2
nripeshtrivedi CreditAttribution: nripeshtrivedi commentedComment #3
herved CreditAttribution: herved commentedMy first review on D8 ;)
It looks good nripeshtrivedi
This is not needed though ;)
Comment #4
nripeshtrivedi CreditAttribution: nripeshtrivedi commentedHi,
Going by https://www.drupal.org/node/2266845, it says it does. How could we use an entity class without that
Comment #5
legovaerI disagree with #3. It's necessary to add "use Drupal\node\Entity\Node;" in order to use the Node class. After a review of the patch, I noticed that there is still some work to do.
Too many parameters added when using ::load
In the code below, you're calling Node::load($node->nid, NULL, TRUE). The load method accepts only one parameter: nid. You're providing these additional parameters in every Node::load call in both scheduler.test and scheduler_api.test.
Saving a node.
Changing a node is done by calling the save() method of the Node class. The following code;
$node->save($node);
Needs to be replaced with;
$node->save();
Note: I'm not 100% sure wether this in scope in this issue.
Changing the value of a node.
In your patch, you're changing the value of a field of a node as following;
$node->field_scheduler_test_approved[$node->language][0]['value'] = TRUE;
However, in Drupal 8, this should be done something like this;
$node->set('field_scheduler_test_approved' , TRUE);
For more information, check class Node.
Another example that needs to be refactored;
$node->{$action . '_on'} = strtotime('-1 day');
Note: I'm not 100% sure wether this in scope in this issue.
Comment #6
Swarnendu-Dutta CreditAttribution: Swarnendu-Dutta commentedWouldnt it be better to use entity_load here ??
Comment #7
Swarnendu-Dutta CreditAttribution: Swarnendu-Dutta commentedComment #8
Swarnendu-Dutta CreditAttribution: Swarnendu-Dutta commentedComment #9
herved CreditAttribution: herved commentedHello,
Thank you legovaer for the review ;)
I created a new patch fixing the following:
- Fix node_load() to Node::load()
- Fix too many parameters added when using Node::load
- Use Node::loadMultiple() instead of Node::load() when possible
- Fix doc and type when passing $node objects as parameters in functions
- Fix node accessors and setters - Change to $node->id(), $node->getType(), $node->set...
$node->save($node) was fixed in #2418535: Replace node_save() with $node->save()
Also I didn't change how we access or set the publish_on or unpublish_on properties... I think it's ok like this those properties are injected during scheduler_node_load(). WHat do you think?
@Swarnendu-Dutta: We know we have nodes here so I think the best is to stick with Node::load(). Maybe later on we could change the code so scheduler could apply on entities and not only nodes but this is a big change to be discussed I guess...
Let me know,
Hervé
Comment #10
pfrenssenThis is amazing work! At first glance it looks very good. I will review this in detail as soon as I can.
In the meanwhile, this will need to be rerolled since #2423407: Replace abbreviated $n variable name with $node is in, and that is also touching these lines.
I will raise the priority on this, because of the size of this patch this should get in ASAP so that we do not end up having to reroll this many times.
Comment #11
herved CreditAttribution: herved commentedThanks. Sure here it is :)
I might have slighly changed the scope of this issue with such a big patch... sorry for that and for the conflicts.
I hope it won't affect other issues that much.
Let me know,
Hervé
Comment #12
herved CreditAttribution: herved commentedComment #13
pfrenssenAlso add the use statement fir NodeInterface to the API documentation. This code will never be executed, but people might still want to consult the class on their IDE.
Interesting, at first I thought this wouldn't work in D8 but it seems to do. The 'value' property is not a part of
FieldItemBase
but this could be a single value field with a 'value' column. Nice one!Excellent, wasn't even aware that we have this performance hit here. This should be backported to D7.
Thanks for taking care of these cleanups.
I am half way through the review. I have about 15 minutes left. I will already make some atomic commits of parts of the patch and upload the remainder to review at a later point.
Comment #15
pfrenssenOK I have split off some parts of the patch into a series of atomic commits. Here is the remainder of the patch.
I will continue this later this week. Thanks a lot so far, great work!
Comment #17
pfrenssenI have reviewed and committed the all changes that were done in addition to the original scope of the patch. Lowering priority to "normal" again. Attaching remaining patch.
Comment #18
herved CreditAttribution: herved commentedHi,
Thanks a lot pfrenssen. Great job on the split up too ;)
I'm rerolling the remaining patch to take into account the 3rd parameter of node_load() that I missed.
D7:
$node = node_load($nid, NULL, TRUE);
D8:
Used in module files:
OR (used in tests - we can retrieve the EntityManager from a TestBase class):
So I used the 2nd notation but for this to work we would need to finish: #2418543: Relocate automated tests in PSR-4 namespaces
Having the tests working would be nice so I'll probably spend some time on it this week then.
Hervé
Comment #19
pfrenssenSounds good, we can finish the node_load() conversion here now, and then tackle the tests.
Comment #21
pfrenssenExcellent stuff, thanks everyone!