You are here

function workflow_actions_trigger_info in Workflow 7.2

Same name and namespace in other branches
  1. 7 workflow_actions/workflow_actions.module \workflow_actions_trigger_info()

Implements hook_trigger_info().

Expose each transition as a hook.

1 call to workflow_actions_trigger_info()
workflow_actions_action_info_alter in workflow_actions/workflow_actions.module
Implements hook_drupal_alter().

File

workflow_actions/workflow_actions.module, line 111
Enables actions to be fired upon a Workflow State change.

Code

function workflow_actions_trigger_info() {
  static $pseudohooks = array();
  if ($pseudohooks) {
    return $pseudohooks;
  }

  // If we come from a specific workflow, only show triggers for that workflow.
  // Removed: it is confusing.
  $trigger_page = FALSE;
  $wid = 0;

  /*
   if (drupal_substr($_GET['q'], 0, 32) == 'admin/structure/trigger/workflow') {
     $trigger_page = TRUE;
     $wid = arg(4);
   }
  */
  $workflows = workflow_load_multiple($wid ? array(
    $wid,
  ) : FALSE);

  // Get the mapping for Node API.
  // Build an array of Wid => array of types.
  $workflow_node_type_map = module_exists('workflownode') ? workflow_get_workflow_type_map() : array();
  foreach ($workflow_node_type_map as $type => $wid) {
    $group = 'workflow';
    $type_map[$wid][] = array(
      'entity_type' => 'node',
      'bundle' => $type,
      $group,
    );
  }

  // Get the mapping for Field API.
  $fields = _workflow_info_fields($entity = NULL, $entity_type = '');
  foreach ($fields as $field_name => $field) {
    if ($field['type'] == 'workflow') {
      foreach ($field['bundles'] as $entity_type => $bundles) {
        foreach ($bundles as $bundle) {

          // Add the trigger to the approriate Tab on admin/structure/trigger.
          switch ($entity_type) {
            case 'node':
              $group = $entity_type;
              break;
            case 'taxonomy_term':
              $group = 'taxonomy';
              break;
            default:
              $group = 'workflow';
              break;
          }
          $type_map[$field['settings']['wid']][] = array(
            'entity_type' => $entity_type,
            'bundle' => $bundle,
            'group' => $group,
          );
        }
      }
    }
  }

  // Initialize the Workflow tab on admin/structure/trigger/workflow.
  $pseudohooks['workflow'] = array();

  // Create a trigger for each possible combination.
  foreach ($workflows as $wid => $workflow) {
    $states = $workflow
      ->getStates('CREATION');
    foreach ($workflow
      ->getTransitions() as $config_transition) {
      $state = $states[$config_transition->sid];
      $target_state = $states[$config_transition->target_sid];
      if (!$state || !$target_state || !$state
        ->isActive() || !$target_state
        ->isActive()) {
        continue;
      }

      // Add hook for Node API.
      if (isset($type_map[$wid])) {
        $creation_flag = $state
          ->isCreationState();
        foreach ($type_map[$wid] as $type_bundle) {

          // Add the trigger to the appropriate Tab on admin/structure/trigger.
          $group = isset($type_bundle['group']) ? $type_bundle['group'] : 'workflow';
          $label = t('When @entity_type %bundle moves %workflow from %state to %target_state', array(
            '@entity_type' => $type_bundle['entity_type'],
            '%bundle' => $type_bundle['bundle'],
            '%workflow' => $workflow
              ->label(),
            '%state' => $state
              ->label(),
            '%target_state' => $target_state
              ->label(),
          ));
          $pseudohooks[$group]['workflow-' . $type_bundle['entity_type'] . '-' . $type_bundle['bundle'] . '-' . $config_transition->tid] = array(
            'label' => $label,
            'workflow_creation_state' => $creation_flag,
          );
        }
      }
    }
  }

  // $pseudohooks will not be set if no workflows have been assigned
  // to node types.
  if ($pseudohooks) {
    return $pseudohooks;
  }
  elseif ($trigger_page) {
    drupal_set_message(t('Either no transitions have been set up or this
      workflow has not yet been assigned to a content type. To enable the
      assignment of actions, edit the workflow to assign permissions for roles
      to do transitions. After that is completed, transitions will appear here
      and you will be able to assign actions to them.'));
  }
  else {
    return array();
  }
}