You are here

rules_token.module in Rules Token 8

Same filename and directory in other branches
  1. 2.x rules_token.module
  2. 1.x rules_token.module

Contains rules_token.module.

File

rules_token.module
View source
<?php

/**
 * @file
 * Contains rules_token.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Adding the links for tokens selection and removing of unnecessary buttons from the forms.
 *
 * Implements hook_form_FORM_ID_alter().
 */
function rules_token_form_rules_expression_edit_alter(&$form, FormStateInterface $form_state, $form_id) {

  // Altering the the forms of conditions.
  if (isset($form_state
    ->getStorage()['condition_id'])) {
    $condition_id = $form_state
      ->getStorage()['condition_id'];
    switch ($condition_id) {
      case 'rules_token_compare_data_with_token':

        // Altering the form of 'Compare Data with Token' condition.
        // Add link for tokens selection.
        $form['context_definitions']['token']['token_tree'] = [
          '#theme' => 'token_tree_link',
          '#token_types' => 'all',
          '#show_restricted' => TRUE,
          '#show_nested' => FALSE,
          '#weight' => 100,
        ];
        break;
      case 'rules_token_compare_token_with_token':

        // Altering the form of 'Compare Token with Token' condition.
        // Add link for tokens selection.
        $form['context_definitions']['token_1']['token_tree'] = [
          '#theme' => 'token_tree_link',
          '#token_types' => 'all',
          '#show_restricted' => TRUE,
          '#show_nested' => FALSE,
          '#weight' => 100,
        ];
        $form['context_definitions']['token_2']['token_tree'] = [
          '#theme' => 'token_tree_link',
          '#token_types' => 'all',
          '#show_restricted' => TRUE,
          '#show_nested' => FALSE,
          '#weight' => 100,
        ];
    }
    return;
  }

  // Altering the form of 'Get token value' action.
  if (isset($form_state
    ->getStorage()['action_id'])) {
    if ($form_state
      ->getStorage()['action_id'] == 'rules_token_get_token_value') {

      // Add link for tokens selection.
      $form['context_definitions']['token']['token_tree'] = [
        '#theme' => 'token_tree_link',
        '#token_types' => 'all',
        '#show_restricted' => TRUE,
        '#show_nested' => FALSE,
        '#weight' => 100,
      ];
    }
  }
}

/**
 * Implements hook_help().
 */
function rules_token_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the rules_token module.
    case 'help.page.rules_token':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= t('The module enables to use in Rules tokens provided by the following modules:');
      $output .= '</br>';
      $output .= "<a href='https://www.drupal.org/project/token'>'Token'</a>";
      $output .= '</br>';
      $output .= "<a href='https://www.drupal.org/project/token_custom'>'Custom Tokens'</a>";
      $output .= '</br>';
      $output .= "<a href='https://www.drupal.org/project/token_custom_plus'>'Custom Tokens Plus'</a>";
      $output .= '</br>';
      $output .= t('It allows getting values of any Drupal data and use them in Rules.');
      $output .= '</br>';
      $output .= t('You can get the current date, site URL, webform submissions and so on.');
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= t("The module provides one action, named 'Get token value'.");
      $output .= '</br>';
      $output .= t("And it provides two conditions, named 'Compare Data with Token' and 'Compare Token with Token'.");
      $output .= '</br>';
      $output .= t('They are easy to use.');
      $output .= '</br>';
      $output .= t('You just need to remember that there are two types of tokens:');
      $output .= '</br>';
      $output .= '<ol>';
      $output .= '<li>';
      $output .= t('Tokens that related with a context of a current rule event.');
      $output .= '</br>';
      $output .= t('The examples of such tokens are:');
      $output .= '</br>';
      $output .= t('[node:],');
      $output .= '</br>';
      $output .= t('[user:],');
      $output .= '</br>';
      $output .= t('[webform_submission:]');
      $output .= '</br>';
      $output .= '</br>';
      $output .= t('For such tokens you need to specify the entity to which they are intended.');
      $output .= '</br>';
      $output .= t("The field named 'ENTITY OF TOKEN' provided for that.");
      $output .= '</br>';
      $output .= t("You have to switch to the 'Data selection mode'");
      $output .= '</br>';
      $output .= t("(click on the 'Switch to data selection' button).");
      $output .= '</br>';
      $output .= t("And then select the relevant entity using the selector.");
      $output .= '</br>';
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t('Global tokens that not related with a context.');
      $output .= '</br>';
      $output .= t('The examples of such tokens are:');
      $output .= '</br>';
      $output .= t('[date:],');
      $output .= '</br>';
      $output .= t('[url:],');
      $output .= '</br>';
      $output .= t('[random:]');
      $output .= '</br>';
      $output .= '</br>';
      $output .= t("For those tokens you don't have to specify any entity.");
      $output .= '</br>';
      $output .= t("Therefore, you should leave the 'ENTITY OF TOKEN' field empty.");
      $output .= '</li>';
      $output .= '</ol>';
      $output .= '<dl>';
      $output .= '<dt><h4>' . t("'Get token value' action") . '</h4></dt>';
      $output .= '<dd>';
      $output .= t("Let's say you want to get the value of a webform field after a webform submitted.");
      $output .= '</br>';
      $output .= t("For instance, the name of the webform is 'Contact' and the name of the field is 'Message'.");
      $output .= '</br>';
      $output .= t('To achieve the goal to do the following:');
      $output .= '</br>';
      $output .= '<ol>';
      $output .= '<li>';
      $output .= t("Install the 'Webform' module.");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t("Create the new rule to react on 'After saving a new webform submission' event.");
      $output .= '</br>';
      $output .= t("You will find it under 'Webform submission' section of the event list.");
      $output .= '</br>';
      $output .= t("(be warned, that the 'Rules Webform' module can hide this event).");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t("Add the 'Get token value' action");
      $output .= '</br>';
      $output .= t("(you will find it under the 'Data' section of the action list).");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t("Click inside the 'TOKEN' field to specify the place for the token insertion.");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t("Then click on the 'Browse available tokens.' link");
      $output .= '</br>';
      $output .= t("(you will find it under the 'TOKEN' field).");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t('Wait to see the window with the available tokens list.');
      $output .= '</br>';
      $output .= t('Find the [webform_submission:values:?] token from the appeared window.');
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t('Click on this token.');
      $output .= '</br>';
      $output .= t("After that, this token will be inserted into the 'TOKEN' field.");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t('Replace the question mark in the token with the webform field key.');
      $output .= '</br>';
      $output .= t('As a result our token will change to: [webform_submission:values:message]');
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t("Type the token entity into the 'ENTITY OF TOKEN' field.");
      $output .= '</br>';
      $output .= t("To do this click on the 'Switch to data selection' button");
      $output .= '</br>';
      $output .= t("(you will find it under the 'ENTITY OF TOKEN' field).");
      $output .= '</br>';
      $output .= t("Then select the 'webform_submission' variable from the selector.");
      $output .= '</br>';
      $output .= t("After that the 'ENTITY OF TOKEN' field will contain the following value:");
      $output .= '</br>';
      $output .= t('webform_submission');
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t("Click on the 'Save' button and save the rule.");
      $output .= '</br>';
      $output .= '</br>';
      $output .= '</li>';
      $output .= '</ol>';
      $output .= t("After that, the new 'token_value' variable will be accessible in the data selector.");
      $output .= '</br>';
      $output .= t('When the rule run, this variable will contain the value of our token.');
      $output .= '</br>';
      $output .= t('And you are free to use it in others actions of the rule.');
      $output .= '</br>';
      $output .= '</br>';
      $output .= t('If you need to get the current date or site URL, then select the tokens,');
      $output .= '</br>';
      $output .= t('for instance, [date:html_date] and [site:url-brief]');
      $output .= '</br>';
      $output .= t("and leave the 'ENTITY OF TOKEN' field empty.");
      $output .= '</br>';
      $output .= '</dd>';
      $output .= '<dt><h4>' . t("'Compare Data with Token' condition") . '</h4></dt>';
      $output .= '<dd>';
      $output .= t('For example, you want to compare the current date with a some value.');
      $output .= '</br>';
      $output .= t('For that you should to do the following:');
      $output .= '</br>';
      $output .= '<ol>';
      $output .= '<li>';
      $output .= t("Add the 'Compare Data with Token' condition");
      $output .= '</br>';
      $output .= t("(you will find it under the 'Data' section of the condition list).");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t("Input the value to be compared into 'Data' field.");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t("Click on the 'Browse available tokens.' link");
      $output .= '</br>';
      $output .= t("(the link is located under the 'TOKEN' field).");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t('Select the appropriate token, for instance, [date:html_date]');
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t("Because we used the global token, leave the 'ENTITY OF TOKEN' field empty.");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '<li>';
      $output .= t("Click on the 'Save' button.");
      $output .= '</br>';
      $output .= '</li>';
      $output .= '</ol>';
      $output .= '</dd>';
      $output .= '<dt><h4>' . t("'Compare Token with Token' condition") . '</h4></dt>';
      $output .= '<dd>';
      $output .= t('Use this condition if you need to compare the values of two tokens.');
      $output .= '</dd>';
      $output .= '</dl>';
      return $output;
    default:
  }
}

Functions

Namesort descending Description
rules_token_form_rules_expression_edit_alter Adding the links for tokens selection and removing of unnecessary buttons from the forms.
rules_token_help Implements hook_help().