Declare custom data format for rules

Public

Get raw version
php
  1. /**
  2.  * hook_rules_data_info()
  3.  * in mymodule.rules.inc file
  4.  */
  5. function MYMODULENAME_rules_data_info() {
  6. $return = array(
  7. 'checkboxes' => array(
  8. 'label' => t('checkboxes'),
  9. 'ui class' => 'RulesDataUICheckboxes',
  10. 'token type' => 'rules_text',
  11. )
  12. );
  13. return $return;
  14. }
  15.  
  16. /**
  17.  *
  18.  * in mymodule.rules.inc file
  19.  */
  20. class RulesDataUICheckboxes extends RulesDataUI implements RulesDataDirectInputFormInterface {
  21.  
  22. public static function inputForm($name, $info, $settings, RulesPlugin $element) {
  23. if (!empty($info['options list'])) {
  24. // Make sure the .rules.inc of the providing module is included as the
  25. // options list callback may reside there.
  26. $element->call('loadBasicInclude');
  27. $form[$name] = array(
  28. '#type' => 'checkboxes',
  29. '#options' => call_user_func($info['options list'], $element, $name),
  30. );
  31. }
  32. else {
  33. $form[$name] = array(
  34. '#type' => 'textarea',
  35. );
  36. RulesDataInputEvaluator::attachForm($form, $settings, $info, $element->availableVariables());
  37. }
  38. return $form;
  39. }
  40. public static function render($value) {
  41. return array(
  42. 'content' => array('#markup' => check_plain($value)),
  43. '#attributes' => array('class' => array('rules-parameter-text')),
  44. );
  45. }
  46. }
  47.  
  48. Then in hook_rules_action_info(), you can use checkboxes type
  49.  
  50. /**
  51.  * Implements hook_rules_action_info().
  52.  * in .module file
  53.  */
  54. function MYMOUDLENAME_rules_rules_action_info() {
  55. return array(
  56. // Action: Add a given node to the given nodequeue.
  57. 'queue' => array(
  58. 'type' => 'checkboxes',
  59. 'label' => t('Queue'),
  60. 'description' => t('The queue where the node should be added to.'),
  61. 'options list' => 'nodequeue_rules_get_queue_names',
  62. ),
  63. ),
  64. );
  65. }