5

I need to have a functionality that allows users to create an article and choose for how long the user wants it to be published (for example 7 days or 2 weeks). After this time the node will automatically move to archive.

How can I achieve this in Drupal 7? I know that there were some modules (Node expire?) in Drupal 6, but I am not sure about Drupal 7. Any advice?

0

3 Answers 3

8

I think you can easily archive your goal with a bunch of module.

I guess with Rules that gonna help you to make some actions when the article will be unpublished. Flag module will help you to set a flag 'Archived article' and finally Scheduler that let your user to choose when the article will be archived.

2
  • Is it possible for the node creator to choose when creating the node for how long it is going to be 'available' before the post is moved to archieve with this method?
    – Vonder
    Oct 17, 2011 at 11:50
  • Yes, Scheduler allows it. From publish date to unpublish date.
    – yvan
    Oct 17, 2011 at 12:08
3

I went round the houses with this and ended up using a HOOK_cron function in a custom module. Here it is - hope it helps someone:

function mymodule_cron() {

// we only want to deal with messages (a content type)
$message_type = "message";

// grab all messages
$message_nodes = node_load_multiple(array(), array('type' => $message_type));
// set up the timestamp for the expiry
$message_search_date = strtotime("- 30 days"); // UNIX timestamp for 30 days ago

// now loop through messages, & if they are old, expire them
  foreach($message_nodes as $m) {

    $obj = entity_metadata_wrapper('node', $m);

    // when was it last updated, & is it still published?
    $last_update = $obj->changed->value();
    $published = $obj->status->value();

    // if it's still published & it's not recent, then unpublish it
    if(($message_search_date > $last_update) && $published<>0) {
        $obj->status = 0;
        $obj->save();
    }
  }
}
1

These days there is a 7.x release of the Node Expire module, however you can also use the Rules module, as further explained below.

Part 1 - Unpublish the node

Use the Rules module together with Rules Scheduler (a sub-module of it), as explained in the steps below. These steps are based on the assumption that your content type is article (adapt to whatever else fits).

Step 1: Create a field to store the expiration date

Add a field to the article content type (to store the "expiration" date), with these specs:

  • Machine name (say) field_expiration_date.
  • Use "date" as field type, for which the date module (and data API) needs to be enabled first.
  • Allow only 1 date.

Step 2: Create a Rules Component

Here is the Rules Component to be created (in Rules export format, just import it in your own environment via copy-paste):

{ "rules_unpublish_a_node" : {
    "LABEL" : "Unpublish a node",
    "PLUGIN" : "rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "USES VARIABLES" : { "node_to_be_unpublished" : { "label" : "Node to be unpublished", "type" : "node" } },
    "DO" : [ { "node_unpublish" : { "node" : [ "node-to-be-unpublished" ] } } ]
  }
}

Step 3: Create a Rule using the Rules Component

Here is the Rule to be created, in Rules export format (just import it in your own environment via copy-paste, then "enable" the rule also):

{ "rules_schedule_the_unpublish_of_a_node_as_requested_by_author" : {
    "LABEL" : "Schedule the unpublish of a node as requested by author",
    "PLUGIN" : "reaction rule",
    "ACTIVE" : false,
    "OWNER" : "rules",
    "REQUIRES" : [ "rules", "rules_scheduler" ],
    "ON" : { "node_insert--article" : { "bundle" : "article" } },
    "IF" : [
      { "entity_has_field" : { "entity" : [ "node" ], "field" : "field_expiration_date" } }
    ],
    "DO" : [
      { "schedule" : {
          "component" : "rules_unpublish_a_node",
          "date" : [ "node:field-expiration-date" ],
          "identifier" : "Unpublish-node-[node:nid]",
          "param_node_to_be_unpublished" : [ "node" ]
        }
      }
    ]
  }
}

Note that the above rule refers to the Rules Component from the previous step. Make sure to only perform the import of this rule only after you completed step 1, if not the import will fail ... Should you want to use another machine name, then change field_expiration_date in the rule above to what you prefer, prior to importing this rule.

Be aware: the above rule is only triggered once, i.e. when the node (of type article) is first created (as in the question). It will right away schedule the unpublish of the node (as requested in the expiration date). If the node gets edited later on, and/or the expiration date is changed afterwards: too bad, that won't change anything to when the scheduled unpublish is going to happen. Also, from the question it isn't really clear if the expiration is optional or mandatory, so if needed make the expiration date a mandatory field.

Part 2 - Move to archive

Your question doesn't really explain what you mean with "move to archive". Maybe just "unpublish" is already sufficient to consider it as archived right away. If not, you could use the Flag module. It would be straight forward to also flag the node via an additional Rules Action that you add to the above Rules Component.

And with those "flagged" nodes, you can then select from various approaches to actually implement such move to archive. Just to mention a few:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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