0

I'm using the Scheduler module to schedule when certain nodes get published&unpublished. Now, they are set to be "Promoted to the front page" and even after the Scheduler unpublishes the content, it stays promoted to the front page where it still appears and leads to a dead link.

How can I automatically set the content to also get un-promoted from the front page when it gets unpublished?

I also submitted a feature support request here: https://www.drupal.org/node/2672500 - and I'll update this post with comments from that post on drupal.org to keep the two threads in sync!

EDIT 1 My front page is the default front page that comes with Drupal 7. I understand, that if I had my own view, I could select what kind of content gets displayed. But how can I do this with the Drupal 7 news feed?

EDIT 2 With mchar's suggestion, I came up with follwoing patch:

--- scheduler_orig.module   2016-02-20 12:52:02.002658565 -0800
+++ scheduler.module    2016-02-20 12:50:57.174657579 -0800
@@ -20,6 +20,20 @@
 define('SCHEDULER_DATE_LETTERS', 'djmnFMyY');
 define('SCHEDULER_TIME_LETTERS', 'hHgGisaA');

+/** [inserted by Ron 02/20/2016] -  to automatically unpromote a node as it gets unpublished**/
+/**
+ * Implements hook_node_presave().
+*/ 
+function my_module_node_presave($node) {
+  $was_published = !empty($node->original->unpublish_on);
+  $now_is_unpublished = empty($node->unpublish_on);
+
+  if (!$node->is_new && $was_published && $now_is_unpublished) {
+    $node->promote = 0;
+  }
+}
+/** [/inserted by Ron 02/20/2016] **/
+
 /**
  * Implements hook_permission().
  */
  • 1
    It depends on the way that you have developed your front page. Is it a view, a panel, a custom page, something else ? – mchar Feb 20 '16 at 19:05
  • @mchar please see EDIT 1 above – stdcerr Feb 20 '16 at 19:08
1

As far as I know scheduler module is not responsible for promoting or not nodes to the frontpage, so you have to go with a custom solution.

Try to implement hook_node_presave as follows:

/**
 * Implements hook_node_presave().
*/ 
function my_module_node_presave($node) {
  $was_published = !empty($node->original->unpublish_on);
  $now_is_unpublished = empty($node->unpublish_on);

  if (!$node->is_new && $was_published && $now_is_unpublished) {
    $node->promote = 0;
  }
}

Hope that helps!

  • Cool! Do I understand right, that this hook will go into scheduler.module along with where the other hooks are and it will get invoked (by cron) when the nodegets unpublished? – stdcerr Feb 20 '16 at 20:49
  • 1
    Well, you have to create your own module and place that code in your module's .module file. Don't ever try to hack contrib modules or the core, for many reasons! Yes, the hook_node_presave will fire whenever the cron runs. Here is a guide to create your first module, don't be scared, it's really easy. – mchar Feb 20 '16 at 21:18
  • Cool yeah! But just following that tutorial didn't help, I had to create a .module file with an opening <?php tag. Otherwise it wouldn't show up in the modules list... hmm. – stdcerr Feb 20 '16 at 21:35
  • Create a my_module.info file and add these lines: name = My custom module description = Description of my custom module core = 7.x. Then create a my_module.module file and add: <?php //your code. Those files files must placed in sites/all/modules/custom. Enable your module and clear caches. – mchar Feb 20 '16 at 22:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.