Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nripeshtrivedi’s picture

Hi,

Please find the patch attached for this issue.Any comments or suggestions are appreciated.

nripeshtrivedi’s picture

Status: Active » Needs review
herved’s picture

My first review on D8 ;)

It looks good nripeshtrivedi

+++ b/scheduler.module
@@ -1,5 +1,7 @@
+use \Drupal\node\Entity\Node;
+

This is not needed though ;)

nripeshtrivedi’s picture

Hi,

Going by https://www.drupal.org/node/2266845, it says it does. How could we use an entity class without that

legovaer’s picture

Status: Needs review » Needs work

I 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.

/**
  * @file
  * Scheduler module test case file.
@@ -131,7 +133,7 @@
     $this->assertRaw(t('@type %title has been updated.', array('@type' => t('Basic page'), '%title' => check_plain($edit['title']))), 'The node is saved successfully when the publication date is in the past and the "publish" behavior is chosen.');

     // Reload the changed node and check that it is published.
-    $node = node_load($node->nid, NULL, TRUE);
+    $node = Node::load($node->nid, NULL, TRUE);

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.

Swarnendu-Dutta’s picture

Wouldnt it be better to use entity_load here ??

Swarnendu-Dutta’s picture

Swarnendu-Dutta’s picture

Status: Needs work » Needs review
herved’s picture

FileSize
37.42 KB

Hello,

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é

pfrenssen’s picture

Priority: Normal » Major
Status: Needs review » Needs work
Issue tags: +Needs reroll

This 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.

herved’s picture

Thanks. 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é

herved’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
pfrenssen’s picture

  1. +++ b/scheduler.api.php
    @@ -8,24 +8,20 @@
    +function hook_scheduler_allow_publishing(NodeInterface $node) {
       // Prevent publication of nodes that do not have the 'Approved for publication
    ...
     
    

    Also 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.

  2. +++ b/scheduler.api.php
    @@ -8,24 +8,20 @@
    +  $allowed = !empty($node->field_approved->value);
     
    

    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!

  3. +++ b/scheduler.cron.inc
    @@ -39,9 +41,8 @@ function _scheduler_publish() {
    -  foreach ($nids as $nid) {
    -    $node = node_load($nid);
    -
    +  $nodes = Node::loadMultiple($nids);
    +  foreach ($nodes as $nid => $node) {
    

    Excellent, wasn't even aware that we have this performance hit here. This should be backported to D7.

  4. +++ b/scheduler.module
    @@ -388,43 +390,27 @@ function _scheduler_strptime($date, $format) {
    -    $row = array();
    -    // @todo This seems unneeded and is confusing. It is not certain that this
    -    //   node is either published or unpublished, probably it isn't or the
    -    //   'publish_on' property wouldn't be set in the first place. Remove this
    -    //   for the D8 version.
    -    $row['published'] = $record->publish_on ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $record->publish_on) : NULL;
    -    $row['unpublished'] = $record->unpublish_on ? date(variable_get('date_format_long', 'l, F j, Y - H:i'), $record->unpublish_on) : NULL;
    -    // Add duplicates of the scheduling properties on $node->scheduler for
    -    // backwards compatibility with the D5 and D6 versions of Scheduler. Please
    -    // do not rely on these properties in new code, access them directly on
    -    // $node->publish_on and $node->unpublish_on instead.
    -    // @todo Remove this for the D8 version.
    -    $row['publish_on'] = $record->publish_on;
    -    $row['unpublish_on'] = $record->unpublish_on;
    -    $nodes[$nid]->scheduler = $row;
    

    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.

  • pfrenssen committed 17bb51e on 8.x-1.x authored by herved
    Issue #2418533 by herved: Leverage NodeInterface in...
  • pfrenssen committed 45446bf on 8.x-1.x authored by herved
    Issue #2418533 by herved: Convert hook_node_*() to hook_ENTITY_TYPE_*().
    
  • pfrenssen committed 4ec514d on 8.x-1.x authored by herved
    Issue #2418533 by herved: Update API documentation to use NodeInterface.
    
  • pfrenssen committed 739f8eb on 8.x-1.x authored by herved
    Issue #2418533 by herved: Leverage NodeInterface when publishing and...
pfrenssen’s picture

OK 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!

  • pfrenssen committed 0d8bc70 on authored by herved
    Issue #2418533 by herved: Convert node hooks to entity based hooks.
    
  • pfrenssen committed 2f3775e on authored by herved
    Issue #2418533 by herved: Leverage the new Node API.
    
  • pfrenssen committed 420c516 on authored by herved
    Issue #2418533 by herved: Remove obsolete code.
    
  • pfrenssen committed 76a24ef on authored by herved
    Issue #2418533 by herved: Use the new placeholder format to trigger...
  • pfrenssen committed afa2968 on authored by herved
    Issue #2418533 by herved: Replace $node->scheduler with $node->...
  • pfrenssen committed b324b94 on authored by herved
    Issue #2418533 by herved: Update documentation.
    
  • pfrenssen committed c3a5c71 on authored by herved
    Issue #2418533 by herved: Type hint all nodes with NodeInterface.
    
pfrenssen’s picture

Priority: Major » Normal
FileSize
3.81 KB

I 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.

herved’s picture

Hi,

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:

    \Drupal::entityManager()->getStorage('node')->resetCache(array($nid));
    $node = Node::load($nid);
    

OR (used in tests - we can retrieve the EntityManager from a TestBase class):

    $node_storage = $this->container->get('entity.manager')->getStorage('node');
    $node_storage->resetCache(array($nid));
    $node = $node_storage->load($nid);
    

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é

pfrenssen’s picture

Sounds good, we can finish the node_load() conversion here now, and then tackle the tests.

  • pfrenssen committed aba8cf1 on 8.x-1.x authored by herved
    Issue #2418533 by herved, pfrenssen, Swarnendu-Dutta, nripeshtrivedi,...
pfrenssen’s picture

Status: Needs review » Fixed

Excellent stuff, thanks everyone!

Status: Fixed » Closed (fixed)

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