[ 'label' => t('Project was referenced in other entity'), 'description' => t('After the project was added into "Related Projects" field value on some entity (node, taxonomy, user, etc)'), 'group' => t('MKBH: Project'), 'variables' => [ 'project' => [ 'type' => 'node', 'label' => t('Project node that was added into field value.'), ], 'article' => [ 'type' => 'node', 'label' => t('Article which referenced a project.'), ], ], ], ]; return $items; } /** * Implements hook_rules_action_info(). */ function mkbh_article_page_rules_action_info() { $items = [ 'mkbh_send_mail_to_project_subscribers' => [ 'label' => t('Send mail to all project subscribers'), 'group' => t('MKBH: Project'), 'parameter' => [ 'project' => [ 'type' => 'node', 'label' => t('Project node that was added into field value.'), ], 'subject' => [ 'type' => 'text', 'label' => t('Subject'), 'description' => t("The mail's subject."), 'translatable' => TRUE, ], 'message' => [ 'type' => 'text', 'label' => t('Message'), 'description' => t("The mail's message body."), 'translatable' => TRUE, ], 'from' => [ 'type' => 'text', 'label' => t('From'), 'description' => t("The mail's from address. Leave it empty to use the site-wide configured address."), 'optional' => TRUE, ], 'language' => [ 'type' => 'token', 'label' => t('Language'), 'description' => t('If specified, the language used for getting the mail message and subject.'), 'options list' => 'entity_metadata_language_list', 'optional' => TRUE, 'default value' => LANGUAGE_NONE, 'default mode' => 'selector', ], ], 'base' => 'mkbh_send_mail_to_project_subscribers', ], ]; return $items; } /** * Action Implementation: Send mail. */ function mkbh_send_mail_to_project_subscribers($project, $subject, $body, $from = NULL, $langcode, $settings, RulesState $state, RulesPlugin $element) { $projectSubscribers = (array)db_query("SELECT mps.* FROM {mkbh_project_subscribers} mps WHERE mps.project_id = :nid", [ ':nid' => $project->nid, ])->fetchAll(); foreach ($projectSubscribers as $projectSubscriber) { if ($recipient = user_load($projectSubscriber->user_id)) { $to = $recipient->mail; $from = !empty($from) ? str_replace(["\r", "\n"], '', $from) : NULL; $params = [ 'subject' => $subject, 'message' => $body, 'langcode' => $langcode, ]; // Set a unique key for this mail. $name = isset($element->root()->name) ? $element->root()->name : 'unnamed'; $key = 'rules_action_mail_' . $name . '_' . $element->elementId(); $languages = language_list(); $language = isset($languages[$langcode]) ? $languages[$langcode] : language_default(); $message = drupal_mail('mkbh_article_page', $key, $to, $language, $params, $from); if ($message['result']) { watchdog('rules', 'Successfully sent email to %recipient', ['%recipient' => $to]); } } } }