You are here

function maestro_tokens in Maestro 8.2

Same name and namespace in other branches
  1. 3.x maestro.module \maestro_tokens()

Implements hook_tokens().

File

./maestro.module, line 599
Provides glue logic, hook implementation and core set process variable functions.

Code

function maestro_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
  $token_service = \Drupal::token();
  $url_options = [
    'absolute' => TRUE,
  ];
  if (isset($options['langcode'])) {
    $url_options['language'] = \Drupal::languageManager()
      ->getLanguage($options['langcode']);
    $langcode = $options['langcode'];
  }
  else {
    $langcode = NULL;
  }
  $replacements = [];
  if ($type == 'maestro' && !empty($data['maestro'])) {
    $task = $data['maestro']['task'];
    $queueID = $data['maestro']['queueID'];
    $processID = MaestroEngine::getProcessIdFromQueueId($queueID);
    foreach ($tokens as $name => $original) {
      switch ($name) {
        case 'task-url':
          $url = TaskHandler::getHandlerURL($queueID);
          $url = ltrim($url, '/');
          $replacements[$original] = $url;
          break;
        case 'task-id':
          $replacements[$original] = $task['id'];
          break;
        case 'task-name':
          $replacements[$original] = $task['label'];
          break;
        case 'template-name':
          $templateMachineName = MaestroEngine::getTemplateIdFromProcessId($processID);
          $template = MaestroEngine::getTemplate($templateMachineName);
          $replacements[$original] = $template->label;
          break;
        case 'queueid':
          $replacements[$original] = $queueID;
          break;
        case 'assigned-user-names':
          $replace = '';
          $assignments = MaestroEngine::getAssignedNamesOfQueueItem($queueID, TRUE);
          foreach ($assignments as $name => $arr) {
            if ($arr['assign_type'] == 'user') {
              if ($replace != '') {
                $replace .= ', ';
              }
              $replace .= $name;
            }
          }
          $replacements[$original] = $replace;
          break;
        case 'assigned-user-emails':
          $replace = '';
          $assignments = MaestroEngine::getAssignedNamesOfQueueItem($queueID, TRUE);
          foreach ($assignments as $name => $arr) {
            if ($arr['assign_type'] == 'user') {
              $usr = user_load_by_name($name);
              if ($replace != '') {
                $replace .= ', ';
              }
              $replace .= $usr->mail
                ->getString();
            }
          }
          $replacements[$original] = $replace;
          break;
        case 'assigned-roles':
          $replace = '';
          $assignments = MaestroEngine::getAssignedNamesOfQueueItem($queueID, TRUE);
          foreach ($assignments as $name => $arr) {
            if ($arr['assign_type'] == 'role') {
              if ($replace != '') {
                $replace .= ', ';
              }
              $replace .= $name;
            }
          }
          $replacements[$original] = $replace;
          break;

        // We will use this section to detect the 'entity-identifier' token which uses a few dynamic parameters
        // that associate the entity identifier unique ID and the field you wish to pull its value for display.
        default:
          $replace = '';
          $token_parts = explode(':', $name);
          if (count($token_parts) > 1) {

            // We know there's a multi-part token.  [0] should be 'entity-identifier'.  if so, continue.
            if ($token_parts[0] == 'entity-identifier') {

              // let's be sure there's enough parts here.
              if (count($token_parts) == 3) {

                // Part [1] will be the unique ID and [2] will be the field to pull the value out of the entity.
                $uniqueIdentifier = MaestroEngine::getEntityIdentiferFieldsByUniqueID($processID, $token_parts[1]);

                /* Drupal 9 update - entity_load is deprecated so we will now have to assume entity type node */

                // $entity = entity_load($uniqueIdentifier[$token_parts[1]]['entity_type'], $uniqueIdentifier[$token_parts[1]]['entity_id']);
                $entity = Node::load($uniqueIdentifier[$token_parts[1]]['entity_id']);
                if ($entity) {

                  // We have a match.
                  // we can support 2 different types of entities our of the box here.  Content types and webforms.
                  // if we don't have one of those types of entities, we'll just provide a hook so you can
                  // take care of the fetching.  We'll expand these in-code as required over time.
                  switch ($entity
                    ->getEntityTypeId()) {
                    case 'webform_submission':
                      if (\Drupal::moduleHandler()
                        ->moduleExists('maestro_webform')) {
                        $webform_submission = WebformSubmission::load($entity
                          ->id());
                        $replace = $webform_submission
                          ->getElementData($token_parts[2]);
                      }
                      break;
                    case 'node':
                      $field_ref = $entity->{$token_parts[2]};
                      $replace = $field_ref
                        ->getValue();
                      if (is_array($replace)) {

                        // Bail out once we know we have a value.
                        $replace = current($replace)['value'];
                      }
                      break;
                  }

                  // let's pass off the fetching of the data to any hooks that wish to do any work on the entity type.
                  \Drupal::moduleHandler()
                    ->invokeAll('maestro_get_entity_token_value', [
                    $entity,
                    $name,
                    $original,
                    $token_parts,
                    &$replacements,
                  ]);
                }
              }
            }
          }
          $replacements[$original] = $replace;
          break;
      }
    }
  }
  return $replacements;
}