Drupal 7 - Node Limit Publish Module

Parent Feed: 

This module adds a configurable limit to how many nodes of any content type can be published at any one time. This is useful in circumstances such as when a website has created content types of polls or competitions or quizzes but only one of each type can be live and published at any one time. Of course the module allows limits other than 1 in case there is such a circumstance. The default is 0 which means unlimited. A web author may create content and exceed the limits providing the content remains unpublished. This allows for content to be created in advance of publication.

If a web author attempts to publish some content which would exceed the limit, the content is saved as unpublished.

The admin should go to admin/config/content/node_limit_publish to set the limits for each content type known to the system.

node_limit_publish.info

name = Node Limit Publish
description = Limit the number of published nodes per type
core = 7.x
configure = admin/config/content/node_limit_publish

node_limit_publish.module

<?php
/*
* File         : node_limit_publish.module
* Title        : Limits the number of concurrently published node types dependent upon admin configurable limits
* Sponsor      : Hangar Seven Digital
* Author       : Badzilla <a href="http://www.badzilla.co.uk" title="www.badzilla.co.uk">www.badzilla.co.uk</a> @badzillacouk
*
* This work is copyright Badzilla under the GPL licence terms and conditions
*
*/

/**
* Implementation of hook_menu().
*
*/

function node_limit_publish_menu() {
   
   
$items = array(); $items['admin/config/content/node_limit_publish'] = array(
       
'title' => 'Limit Number of Published Nodes per Node Type',
       
'description' => t('Zero represents an unlimited amount of published nodes'),
       
'page callback' => 'drupal_get_form',
       
'page arguments' => array('node_limit_publish_admin_settings'),
       
'access arguments' => array('administer node_limit_publish'),
       
'type' => MENU_NORMAL_ITEM,
    );
   
    return
$items;
}

function

node_limit_publish_admin_settings() {
   
   
$form = array();

    if (

is_array($types = node_type_get_types())) { $form['title'] = array(
           
'#markup' => t('Zero represents an unlimited amount of published nodes'),
        );

        foreach(

$types as $key => $value)
           
$form['node_limit_publish_'.$key] = array(
               
'#type' => 'textfield',
               
'#description' => $key,
               
'#size' => 4,
               
'#maxlength' => 10,
               
'#element_validate' => array('node_limit_publish_is_numeric'),
               
'#default_value' => variable_get('node_limit_publish_'.$key, 0),               
            );
    }

    return

system_settings_form($form);
}

function

node_limit_publish_is_numeric($element, &$form_state, $form) {
   
    if (!
is_numeric($element['#value']))
       
form_error($element, t('This field must be numeric'));
}
/**
* Implementation of hook_presave().
*
*/
function node_limit_publish_node_presave($node) {
   
   
// Get the limit on this type
   
if (($limit = variable_get('node_limit_publish_'.$node->type, 0)) and $node->status == 1) {
       
// now check whether we have reached our maximum
       
$query = db_select('node')
            ->
condition('type', $node->type)
            ->
condition('status', 1);
        if (isset(
$node->nid))
           
$query->condition('nid', $node->nid, '!=');
       
$count = $query->countQuery()
            ->
execute()
            ->
fetchField();

        if (

$count >= $limit) {
           
$node->status = 0;
           
drupal_set_message(t('Sorry, the maximum amount of published content for %type has already been reached.', array('%type' => $node->type)), 'warning');
        }
    }
}
?>

Attachment Size
node_limit_publish_02.tar.gz 1.41 KB

Author: 
RSS Tags: 
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web