My condition parameter's 'optional' and 'default mode' options not having any effects?

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by Arwym on May 26, 2012 at 4:27pm

Coming from a previous problem, I finally decided to just code my own condition, and the condition is working properly so far. Except for one thing, that is. There is one optional parameter I would like to add to my condition; basically, I'd like to let the user specify the content types he/she would like to restrict, if any. So, my condition function is as follow:

<?php
/**
* Implementation of hook_rules_condition_info().
*/
function nodecount_rules_condition_info() {
$conditions = array();
$conditions['nodecount_rules_condition'] = array(
'group' => t('Nodes'),
'label' => t('User has reached their active (published) node quota'),
'parameter' => array(
'node' => array(
'type' => 'node',
'label' => t('Node being created or changed')
),
'quota' => array(
'type' => 'integer',
'label' => t('Specified quota')
),
'c_type' => array(
'type' => array(
'text',
'list<text>',
'token',
'list<token>'
),
'label' => t('Specified content type (optional)'),
'optional' => TRUE,
'allow null' => TRUE,
'default mode' => 'selector'
)
),
'module' => 'nodecount',
);
return
$conditions;
}
?>

The third parameter (c_type) is the one I have problems with. Rules is still forcing me to add values to it, even though I explicitly indicated that it is to be an OPTIONAL parameter. Furthermore, I want the default mode to be selector rather than input, but I'm still seeing input by default. I also tried the allow null option. Neither of those three options seem to have any effect, though. Is the API documentation outdated, or am I doing something wrong?

Here is a screenshot, for reference:

Only local images are allowed.

If you can point me in the right direction, I'd be so grateful.

Thanks!

Categories: , ,

Comments

Problem Solved!

Posted by Arwym on May 27, 2012 at 6:34pm

I am answering my own question by providing my source code here. If you want to use it for your own needs, be my guest. :) It seems to work perfectly. Thoroughly tested.

<?php
// Implementation of hook_rules_condition_info().
function nodequota_rules_condition_info() {
$conditions = array();
$conditions['nodequota_rules_condition'] = array(
'group' => t('Nodes'),
'label' => t('User has reached their active (published) node quota'),
'parameter' => array(
'node' => array(
'type' => 'node',
'label' => t('Node being created or changed')
),
'quota' => array(
'type' => 'integer',
'label' => t('Specified quota')
),
'c_type' => array(
'type' => 'list<text>',
'options list' => 'node_type_get_names',
'label' => t('Specified content type(s) (optional)'),
'optional' => TRUE,
'allow null' => TRUE,
'default value' => null,
'restriction' => 'input'
)
),
'module' => 'nodequota',
);
return
$conditions;
}

//Condition: User has reached their quota for specified node content type(s), if any.
function nodequota_rules_condition($node, $quota, $c_type = null, $settings) {
if (!empty(
$c_type)) {
$type_list = implode(",", $c_type);
$result = db_query('SELECT n.uid FROM {node} n WHERE n.status = :status AND n.type IN(:type)', array(':status' => $node->status, ':type' => $type_list));
} else {
$result = db_query('SELECT n.uid FROM {node} n WHERE n.status = :status', array(':status' => $node->status));
}
if (
$result->rowCount() >= $quota) {
return
true;
} else {
return
false;
}
}
?>

Basically, in my nodequota_rules_condition function, I forgot to declare my optional parameter as null by default. Once I did this, Rules recognized it as optional. :)

If you find a better way to implement this, let me know. I am always open to suggestions and improvement.

Thanks!

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /