Drupal rules module provide by default events,conditions and action. But we want to create a custom components for additional functionality. Here I will tell you how to create Condition and Action.
Let's create a custom module and name the module as "hsk_rule".
hsk_rule.info
name = Hsk Rule
description = Hsk Custom Rules
core = 7.x
package = Hsk
hsk_rule.module
<?php
/**
* implementation of hook_rules_condition_info()
*/
function hsk_rule_rules_condition_info() {
return array(
'hsk_rule_condition_competition_start_type' => array(
'label' => t('Custom condition for node publish'),
'group' => 'Custom Condition in Rule' ,
'module' => 'hsk_rule',
),
);
}
/**
* Condition competition_start_condition_competition_start_type
*/
function hsk_rule_condition_competition_start_type($type) {
$result = db_query('SELECT nid FROM node WHERE type = :type', array(':type' => 'competition_start'));
foreach ($result as $rec) {
$nodedata = node_load($rec->nid);
$com_start =$nodedata->field_comp_start_date['und'][0]['countdown_timer'];
$c= format_date(time(), 'custom', 'd-m-Y H:i:s');
$current_date=strtotime($c);
if($com_start <= $current_date) {
return true;
}
else {
return false;
}
}
}
/**
* Implementation of hook_rules_action_info().
*/
function hsk_rule_rules_action_info() {
return array(
'hsk_rule_action_change_node_status' => array(
'label' => t('Change the node status'),
'module' => 'hsk_rule',
'group' => 'Custom Action in Rule' ,
'variables' => array(
'competition_start' => array('type' => 'node', 'label' => t('The competition_start node.')),
),
),
);
}
function hsk_rule_action_change_node_status() {
$result = db_query('SELECT nid FROM node WHERE type = :type', array(':type' => 'competition'));
foreach ($result as $rec) {
$nodedata = node_load($rec->nid);
$result1 = db_query('SELECT nid FROM node WHERE type = :type', array(':type' => 'competition_start'));
foreach ($result1 as $com_st) {
if(isset($nodedata->field_competition_reference['und'][0]['nid'])==$com_st->nid) {
$nodedata->status =1;
node_submit($nodedata);
node_save($nodedata);
}
}
}
}
Let's create a custom module and name the module as "hsk_rule".
hsk_rule.info
name = Hsk Rule
description = Hsk Custom Rules
core = 7.x
package = Hsk
hsk_rule.module
<?php
/**
* implementation of hook_rules_condition_info()
*/
function hsk_rule_rules_condition_info() {
return array(
'hsk_rule_condition_competition_start_type' => array(
'label' => t('Custom condition for node publish'),
'group' => 'Custom Condition in Rule' ,
'module' => 'hsk_rule',
),
);
}
/**
* Condition competition_start_condition_competition_start_type
*/
function hsk_rule_condition_competition_start_type($type) {
$result = db_query('SELECT nid FROM node WHERE type = :type', array(':type' => 'competition_start'));
foreach ($result as $rec) {
$nodedata = node_load($rec->nid);
$com_start =$nodedata->field_comp_start_date['und'][0]['countdown_timer'];
$c= format_date(time(), 'custom', 'd-m-Y H:i:s');
$current_date=strtotime($c);
if($com_start <= $current_date) {
return true;
}
else {
return false;
}
}
}
/**
* Implementation of hook_rules_action_info().
*/
function hsk_rule_rules_action_info() {
return array(
'hsk_rule_action_change_node_status' => array(
'label' => t('Change the node status'),
'module' => 'hsk_rule',
'group' => 'Custom Action in Rule' ,
'variables' => array(
'competition_start' => array('type' => 'node', 'label' => t('The competition_start node.')),
),
),
);
}
function hsk_rule_action_change_node_status() {
$result = db_query('SELECT nid FROM node WHERE type = :type', array(':type' => 'competition'));
foreach ($result as $rec) {
$nodedata = node_load($rec->nid);
$result1 = db_query('SELECT nid FROM node WHERE type = :type', array(':type' => 'competition_start'));
foreach ($result1 as $com_st) {
if(isset($nodedata->field_competition_reference['und'][0]['nid'])==$com_st->nid) {
$nodedata->status =1;
node_submit($nodedata);
node_save($nodedata);
}
}
}
}
Comments
Post a Comment