Custom Drupal 7 Rules Conditions and Actions

Example Drupal Rules actions and conditions as a module –
Custom rules

Custom Rules Actions

Using hook_rules_action_info()

This example provides two new actions:

  • Delete field_collection_item
  • Clear Drupal messages by type

References:

[pastacode lang=”php” manual=”%3C%3Fphp%0A%2F**%0A%20*%20Implements%20hook_rules_action_info().%0A%20*%0A%20*%20http%3A%2F%2Fwww.drupalcontrib.org%2Fapi%2Fdrupal%2Fcontributions!rules!rules.api.php%2Ffunction%2Fhook_rules_action_info%2F7%0A%20*%2F%0Afunction%20custom_rules_rules_action_info()%20%7B%0A%09return%20array(%0A%09%09%2F%2F%20use%20the%20field_collection_item%20entity%20to%20delete%20a%20field_collection_item%20correctly%0A%09%09’custom_rules_delete_field_collection_item’%20%3D%3E%20array(%0A%09%09%09’label’%20%3D%3E%20t(‘Delete%20field_collection_item%20and%20hostEntity%20reference%20(Custom%20Rules)’)%2C%0A%09%09%09’group’%20%3D%3E%20t(‘Custom%20Rules’)%2C%0A%09%09%09’parameter’%20%3D%3E%20array(%0A%09%09%09%09’field_collection_item’%20%3D%3E%20array(%0A%09%09%09%09%09’type’%20%3D%3E%20’field_collection_item’%2C%0A%09%09%09%09%09’label’%20%3D%3E%20t(‘Field%20collection%20item%20remove’)%2C%0A%09%09%09%09%09’description’%20%3D%3E%20t(‘Field%20collection%20item%20and%20hostEntity%20reference%20will%20be%20deleted.’)%2C%0A%09%09%09%09)%2C%0A%09%09%09)%2C%0A%09%09)%2C%0A%09%09%2F%2F%20set%20an%20node%20to%20private%20and%20immediately%20save%20it%20to%20ensure%20its%20privacy%0A%09%09’custom_rules_drupal_get_messages’%20%3D%3E%20array(%0A%09%09%09’label’%20%3D%3E%20t(‘Clear%20messages%20with%20drupal_get_messages()%20(Custom%20Rules)’)%2C%0A%09%09%09’group’%20%3D%3E%20t(‘Custom%20Rules’)%2C%0A%09%09%09’parameter’%20%3D%3E%20array(%0A%09%09%09%09’type_to_remove’%20%3D%3E%20array(%0A%09%09%09%09%09’type’%20%3D%3E%20’text’%2C%0A%09%09%09%09%09’label’%20%3D%3E%20t(‘Message%20type’)%2C%0A%09%09%09%09%09’description’%20%3D%3E%20t(‘Provide%20a%20message%20type%20to%20be%20removed%3A%20(%20status%2C%20error%2C%20warning%2C%20etc%20).%20Use%20%22any%22%20for%20typeless%20removal.%20Use%20%22all%22%20for%20clearing%20all%20messages.’)%2C%0A%09%09%09%09)%2C%0A%09%09%09%09’message_to_remove’%20%3D%3E%20array(%0A%09%09%09%09%09’type’%20%3D%3E%20’text’%2C%0A%09%09%09%09%09’label’%20%3D%3E%20t(‘Message%20to%20remove’)%2C%0A%09%09%09%09%09’description’%20%3D%3E%20t(‘Provide%20the%20message%20string%20to%20be%20removed.’)%2C%0A%09%09%09%09)%2C%0A%09%09%09)%2C%0A%09%09)%2C%0A%09)%3B%0A%7D%0A%2F**%0A%20*%20Simple%20helper%20action%20to%20really%20delete%20field_collection_item%0A%20*%20The%20normal%20Delete%20entity%20rule%20does%20not%20remove%20the%20hostEntity%20Reference%0A%20*%0A%20*%20%40param%20%24field_collection_item%0A%20*%2F%0Afunction%20custom_rules_delete_field_collection_item(%20%24field_collection_item%20)%7B%0A%09%24field_collection_item-%3Edelete()%3B%0A%7D%0A%2F**%0A%20*%20Search%20%24_SESSION%5Bmessages%5D%20queue%20for%20a%20specific%20message%20to%20remove%0A%20*%0A%20*%20%40param%20%24type_to_remove%0A%20*%20%40param%20%24message_to_remove%0A%20*%2F%0Afunction%20custom_rules_drupal_get_messages(%20%24type_to_remove%2C%20%24message_to_remove%20)%7B%0A%09%24index%20%3D%20FALSE%3B%0A%09%24found_type%20%3D%20FALSE%3B%0A%09%2F%2F%20remove%20all%20messages%0A%09if%20(%20%24type_to_remove%20%3D%3D%20%22all%22%20)%7B%0A%09%09drupal_get_messages()%3B%0A%09%7D%0A%09%2F%2F%20search%20all%20messages%20for%20the%20string%0A%09else%20if%20(%20%24type_to_remove%20%3D%3D%20%22any%22%20)%20%7B%0A%09%09%24messages%20%3D%20drupal_get_messages(%20NULL%2C%20false%20)%3B%0A%09%09foreach%20(%24messages%20as%20%24message_type%20%3D%3E%20%24type_messages)%7B%0A%09%09%09%24index%20%3D%20array_search(%20%24message_to_remove%2C%20%24type_messages%20)%3B%0A%09%09%09if%20(%20%24index%20!%3D%3D%20FALSE%20)%7B%0A%09%09%09%09%24found_type%20%3D%20%24message_type%3B%0A%09%09%09%09break%3B%0A%09%09%09%7D%0A%09%09%7D%0A%09%7D%0A%09%2F%2F%20look%20for%20messages%20of%20a%20specific%20type%0A%09else%20%7B%0A%09%09%24messages%20%3D%20drupal_get_messages(%20%24type_to_remove%2C%20false%20)%3B%0A%09%09if%20(%20isset(%20%24messages%5B%20%24type_to_remove%20%5D%20)%20)%20%7B%0A%09%09%09%24index%20%3D%20array_search(%20%24message_to_remove%2C%20%24messages%5B%20%24type_to_remove%20%5D%20)%3B%0A%09%09%7D%0A%09%09if%20(%20%24index%20!%3D%3D%20FALSE%20)%20%7B%0A%09%09%09%24found_type%20%3D%20%24type_to_remove%3B%0A%09%09%7D%0A%09%7D%0A%09%2F%2F%20if%20found%2C%20remove%20the%20message%20from%20%24_SESSION%5Bmessages%5D%20queue%0A%09if%20(%20%24index%20!%3D%3D%20FALSE%20%26%26%0A%09%20%20%20%20%20%24found_type%20!%3D%3D%20FALSE%20%26%26%0A%09%20%20%20%20%20isset(%20%24_SESSION%5B’messages’%5D%5B%20%24found_type%20%5D%5B%20%24index%20%5D%20)%20)%0A%09%7B%0A%09%09unset(%20%24_SESSION%5B’messages’%5D%5B%20%24found_type%20%5D%5B%20%24index%20%5D%20)%3B%0A%09%09%2F%2F%20remove%20the%20entire%20type%20if%20now%20empty%0A%09%09if%20(%20empty(%20%24_SESSION%5B’messages’%5D%5B%20%24found_type%20%5D%20)%20)%7B%0A%09%09%09unset(%20%24_SESSION%5B’messages’%5D%5B%20%24found_type%20%5D%20)%3B%0A%09%09%7D%0A%09%7D%0A%7D” message=”custom_rules.actions.inc” highlight=”” provider=”manual”/]

Custom Rules Conditions

Using hook_rules_condition_info()

This example provides four new Drupal Rules conditions:

  • Is cron running
  • Is during password reset
  • Is destination set in URL
  • Is during OAuth

References:

[pastacode lang=”php” manual=”%3C%3Fphp%0A%2F**%0A%20*%20Implements%20hook_rules_condition_info().%0A%20*%0A%20*%20http%3A%2F%2Fwww.drupalcontrib.org%2Fapi%2Fdrupal%2Fcontributions!rules!rules.api.php%2Ffunction%2Fhook_rules_condition_info%2F7%0A%20*%2F%0Afunction%20custom_rules_rules_condition_info()%20%7B%0A%09return%20array(%0A%09%09’custom_rules_is_cron_running’%20%3D%3E%20array(%0A%09%09%09’label’%20%3D%3E%20t(‘Is%20cron%20running’)%2C%0A%09%09%09’group’%20%3D%3E%20t(‘Custom%20Rules’)%2C%0A%09%09)%2C%0A%09%09’custom_rules_is_during_password_reset’%20%3D%3E%20array(%0A%09%09%09’label’%20%3D%3E%20t(‘Is%20during%20password%20reset’)%2C%0A%09%09%09’group’%20%3D%3E%20t(‘Custom%20Rules’)%2C%0A%09%09)%2C%0A%09%09’custom_rules_is_destination_set’%20%3D%3E%20array(%0A%09%09%09’label’%20%3D%3E%20t(‘Is%20destination%20set%20in%20URL’)%2C%0A%09%09%09’group’%20%3D%3E%20t(‘Custom%20Rules’)%2C%0A%09%09)%2C%0A%09%09’custom_rules_is_during_oauth’%20%3D%3E%20array(%0A%09%09%09’label’%20%3D%3E%20t(‘Is%20during%20OAuth’)%2C%0A%09%09%09’group’%20%3D%3E%20t(‘Custom%20Rules’)%2C%0A%09%09)%2C%0A%09)%3B%0A%7D%0A%2F**%0A%20*%20Condition%20-%20is%20cron%20running%3F%0A%20*%0A%20*%20%40return%20bool%0A%20*%2F%0Afunction%20custom_rules_is_cron_running()%7B%0A%09%24cron_expires%20%3D%20db_query(%22SELECT%20%60expire%60%20FROM%20%7Bsemaphore%7D%20WHERE%20%60name%60%20%3D%20’cron’%20LIMIT%201%22)-%3EfetchField()%3B%0A%09if%20(%20%24cron_expires%20%26%26%20%24cron_expires%20%3E%20microtime(TRUE)%20)%20%7B%0A%09%09return%20TRUE%3B%0A%09%7D%0A%09return%20FALSE%3B%0A%7D%0A%2F**%0A%20*%20Condition%20-%20is%20a%20user%20password%20currently%20being%20reset%3F%0A%20*%0A%20*%20%40return%20bool%0A%20*%2F%0Afunction%20custom_rules_is_during_password_reset()%7B%0A%09return%20(arg(0)%20%3D%3D%20’user’%20%26%26%20arg(1)%20%3D%3D%20’reset’)%3B%0A%7D%0A%2F**%0A%20*%20Condition%20-%20destination%20set%20in%20url%20query%0A%20*%0A%20*%20%40return%20bool%0A%20*%2F%0Afunction%20custom_rules_is_destination_set()%7B%0A%09return%20isset(%20%24_GET%5B’destination’%5D%20)%3B%0A%7D%0A%2F**%0A%20*%20Condition%20-%20Check%20if%20user%20is%20being%20processed%20through%20oauth%0A%20*%0A%20*%20%40return%20bool%0A%20*%2F%0Afunction%20custom_rules_is_during_oauth()%7B%0A%09return%20(%0A%09%09%2F%2F%20on%20oauth%20route%20itself%0A%09%09arg(0)%20%3D%3D%20’oauth2’%20%7C%7C%0A%09%09%2F%2F%20or%20logging%20in%20using%20the%20form%0A%09%09(%20isset(%20%24_GET%5B’destination’%5D%20)%20%26%26%0A%09%09%20%20stripos(%20%24_GET%5B’destination’%5D%2C%20’oauth2’%20)%20!%3D%3D%20FALSE%20)%0A%09)%3B%0A%7D” message=”custom_rules.conditions.inc” highlight=”” provider=”manual”/]

0 Thoughts

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *