You are here

protected function ConfigActionsService::doProcessAction in Config Actions 8

Recursive helper function to process an action

Parameters

array $action: array of action data

array $options: optional array of action options If $options is empty, values are taken from $action data

array $overrides: A set of options that always override any actions or sub-actions.

string $action_id: The id string of the action to be executed. If omitted, execute all actions in the $action array. Nested actions can be separated with a colon, such as "action:subaction"

string $action_path: Recursive current full action path string being examined

array $action_list: Optional array used to return a list of actions keyed by their path. If specified, the actions are NOT executed, then are just listed. Sub-actions are keyed by parent:child

Return value

mixed Returns the data that was processed, or NULL if nothing was processed

2 calls to ConfigActionsService::doProcessAction()
ConfigActionsService::listActions in src/ConfigActionsService.php
Return a list of actions within an actions array
ConfigActionsService::processAction in src/ConfigActionsService.php
Process a single config_actions action

File

src/ConfigActionsService.php, line 269

Class

ConfigActionsService
Base class for config_actions plugins.

Namespace

Drupal\config_actions

Code

protected function doProcessAction(array $action, array $options = [], $overrides = [], $action_id = '', $action_path = '', &$action_list = NULL) {

  // Allow values in the action to override the passed options.
  $result = NULL;

  // If we are in autoExecute mode, skip any actions marked with "auto:false".
  if ($this->auto && isset($action['auto']) && $action['auto'] === FALSE && (!isset($overrides['auto']) || $overrides['auto'] === FALSE)) {
    return $result;
  }
  $options = NestedArray::mergeDeepArray([
    $options,
    $action,
    $overrides,
  ], TRUE);

  // Prune actions from options to reduce memory use
  unset($options[static::ACTION_KEY]);

  // Check to see if we specified an action_id and if it matches current path
  if (empty($action_id) || $action_id == $action_path) {
    if (array_key_exists(static::ACTION_KEY, $action)) {

      // Process list of nested actions.
      foreach ($action[static::ACTION_KEY] as $key => $action_item) {
        $path = empty($action_path) ? $key : $action_path . ':' . $key;
        $options['id'] = $key;

        // Pass an empty action_id to ensure all sub-actions are executed
        $action_item = !empty($action_item) ? $action_item : [];
        $result = $this
          ->doProcessAction($action_item, $options, [], '', $path, $action_list);
      }
    }
    elseif (isset($action_list)) {

      // Just list the action ids, don't execute action.
      $action_list[$action_path] = $action;
    }
    else {

      // Execute a specific action.
      // Use 'default' if no plugin is specified.
      $plugin_id = !empty($action['plugin']) ? $action['plugin'] : (!empty($options['plugin']) ? $options['plugin'] : 'default');

      // Get Plugin instance for this action.

      /** @var \Drupal\config_actions\ConfigActionsPluginInterface $plugin */
      $plugin = $this->pluginManager
        ->createInstance($plugin_id, $options);
      if (!isset($plugin)) {
        throw new \Exception($this
          ->t('Could not find plugin: @name.', [
          '@name' => $plugin_id,
        ]));
      }
      $result = $plugin
        ->execute($action);
    }
  }
  elseif (array_key_exists(static::ACTION_KEY, $action)) {

    // No match, so recurse down into nested actions looking for a match.
    foreach ($action[static::ACTION_KEY] as $key => $action_item) {
      $path = empty($action_path) ? $key : $action_path . ':' . $key;
      $options['id'] = $key;
      $action_item = !empty($action_item) ? $action_item : [];
      if ($result = $this
        ->doProcessAction($action_item, $options, $overrides, $action_id, $path, $action_list)) {

        // Stop looping when we find an action to process, unless we are listing actions
        if (!isset($action_list)) {
          break;
        }
      }
    }
  }
  return $result;
}