You are here

function actions_synchronize in Drupal 7

Same name and namespace in other branches
  1. 6 includes/actions.inc \actions_synchronize()

Synchronizes actions that are provided by modules in hook_action_info().

Actions provided by modules in hook_action_info() implementations are synchronized with actions that are stored in the actions database table. This is necessary so that actions that do not require configuration can receive action IDs.

Parameters

$delete_orphans: If TRUE, any actions that exist in the database but are no longer found in the code (for example, because the module that provides them has been disabled) will be deleted.

4 calls to actions_synchronize()
drupal_flush_all_caches in includes/common.inc
Flushes all cached data on the site.
system_actions_manage in modules/system/system.admin.inc
Menu callback; Displays an overview of available and configured actions.
system_actions_remove_orphans in modules/system/system.admin.inc
Remove actions that are in the database but not supported by any enabled module.
trigger_install in modules/trigger/trigger.install
Implements hook_install().

File

includes/actions.inc, line 274
This is the actions engine for executing stored actions.

Code

function actions_synchronize($delete_orphans = FALSE) {
  $actions_in_code = actions_list(TRUE);
  $actions_in_db = db_query("SELECT aid, callback, label FROM {actions} WHERE parameters = ''")
    ->fetchAllAssoc('callback', PDO::FETCH_ASSOC);

  // Go through all the actions provided by modules.
  foreach ($actions_in_code as $callback => $array) {

    // Ignore configurable actions since their instances get put in when the
    // user adds the action.
    if (!$array['configurable']) {

      // If we already have an action ID for this action, no need to assign aid.
      if (isset($actions_in_db[$callback])) {
        unset($actions_in_db[$callback]);
      }
      else {

        // This is a new singleton that we don't have an aid for; assign one.
        db_insert('actions')
          ->fields(array(
          'aid' => $callback,
          'type' => $array['type'],
          'callback' => $callback,
          'parameters' => '',
          'label' => $array['label'],
        ))
          ->execute();
        watchdog('actions', "Action '%action' added.", array(
          '%action' => $array['label'],
        ));
      }
    }
  }

  // Any actions that we have left in $actions_in_db are orphaned.
  if ($actions_in_db) {
    $orphaned = array_keys($actions_in_db);
    if ($delete_orphans) {
      $actions = db_query('SELECT aid, label FROM {actions} WHERE callback IN (:orphaned)', array(
        ':orphaned' => $orphaned,
      ))
        ->fetchAll();
      foreach ($actions as $action) {
        actions_delete($action->aid);
        watchdog('actions', "Removed orphaned action '%action' from database.", array(
          '%action' => $action->label,
        ));
      }
    }
    else {
      $link = l(t('Remove orphaned actions'), 'admin/config/system/actions/orphan');
      $count = count($actions_in_db);
      $orphans = implode(', ', $orphaned);
      watchdog('actions', '@count orphaned actions (%orphans) exist in the actions table. !link', array(
        '@count' => $count,
        '%orphans' => $orphans,
        '!link' => $link,
      ), WATCHDOG_INFO);
    }
  }
}