1
votes

So I have a pretty specific situation where I have users who have X (unlimited) nodes authored by them... yet they are only allowed to have 6 nodes Published at a time.

I googled around for a while and found http://www.badzilla.co.uk/Drupal-7--Node-Limit-Publish-Module , which works GREAT, except this limits the TOTAL number of published nodes of a particular content type.

I need this exact functionality, except w/the additional limitation on a per-user basis... So that each user can only have 6 total nodes Published at once, rather than the entire Drupal Site only having X total of a particular content type published at once.

Hopefully that makes sense... regardless, the code/module from the URL above is exactly working great, just I need to cater it to check on a per-user basis! I'm not great with module coding or anything, so if someone is able to handhold a little on how to alter the module code on that site, that'd be great! TIA

2
my first instinct is make a rule that checks on publish that checks how many nodes have that user as author...pretty new to drupal, thoughKen

2 Answers

2
votes

For anyone who runs across my same issue, of needing to limit number of PUBLISHED nodes of a particular content type, a friend of mine altered the above linked Badzilla module, and tweaked it to be the following. Thanks to Badzilla for providing the basis of the module, and my buddy for tweaking it to check on a user-basis rather than site-wide # of published nodes.

    <?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 www.badzilla.co.uk @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) {
    global $user;

    // 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)
                        ->condition('uid', $user->uid);
        if (isset($node->nid))
            $query->condition('nid', $node->nid, '!=');
        $count = $query->countQuery()
            ->execute()
            ->fetchField();
        if ($count >= $limit) {
            $node->status = 0;
            // use %type for dynamic node type
            drupal_set_message(t('Sorry, the maximum of this node are active already. You must first disable another!', array('%type' => $node->type)), 'warning');
        }
    }
}
0
votes

Node Limit is the one you're after...

The Node Limit module allows administrators to restrict the number of nodes of a specific type that roles or users may create. For example, if a site has an "Advertiser" role that can create "advertisement" nodes, then the node limit administrator can restrict all users in that role to a specific number of nodes. He may also restrict users on a per-user basis.