Created on 18 October 2024, about 1 month ago

Problem/Motivation

Hello
I have couple question about the integration with ECA.
1. How to get the results of the request? I don't see any action inside ui did i miss something ? Do i need to read directly inside private temp store ?
2. How to use ECA token.
As I can see and understand the token system is not working currently.
I just need to tokenize some parameter inside a pre configured request.
I manage it by replacing the extend ActionBase core by the ECA. But I think this is a problem for your module to have depency on ECA, so do we need to use a new plugin with providers to make the depenedency, or do we need sub module, or something else ?


namespace Drupal\http_client_manager\Plugin\Action;

use Drupal\Core\Access\AccessResult;
use Drupal\eca\Plugin\Action\ActionBase;
use Drupal\Core\Config\Entity\ConfigEntityStorageInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\TempStore\PrivateTempStore;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Wrapper action for all pre-configured http requests.
 *
 * @Action(
 *   id = "http_client_manager_preconfigured_request",
 *   provider = "eca",
 *   deriver =
 *   "\Drupal\http_client_manager\Plugin\Action\PreConfiguredRequestDeriver",
 *   nodocs = true
 * )
 */
class PreConfiguredRequest extends ActionBase {

  /**
   * The entity storage interface for http config request config entities.
   *
   * @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
   */
  protected ConfigEntityStorageInterface $entityStorage;

  /**
   * The private temporary key value store.
   *
   * @var \Drupal\Core\TempStore\PrivateTempStore
   */
  protected PrivateTempStore $privateTempStore;

  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): static {
    $instance = new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('entity_type.manager'),
      $container->get('eca.token_services'),
      $container->get('current_user'),
      $container->get('datetime.time'),
      $container->get('eca.state'),
      $container->get('logger.channel.eca')
    );
    $instance->entityStorage = $container->get('entity_type.manager')
      ->getStorage('http_config_request');
    $instance->privateTempStore = $container->get('tempstore.private')
      ->get('http_client_manager');

    return $instance;
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $access = AccessResult::allowed();
    return $return_as_object ? $access : $access->isAllowed();
  }

  /**
   * {@inheritdoc}
   */
  public function execute($object = NULL): void {
    [, $entityId] = explode(':', $this->getPluginId());
    /** @var \Drupal\http_client_manager\Entity\HttpConfigRequestInterface $request */
    $request = $this->entityStorage->load($entityId);
    $parameters = $request->getParameters();
    $parameters = $this->tokenizeParameters($parameters);
    $request->set('parameters', $parameters);
    $result = $request->execute();
    $this->privateTempStore->set(Command::KEY_LAST_RESULT, $result->toArray());
  }

  /**
   * Apply token to parameters.
   *
   * @param array $parameters
   *
   * @return array
   */
  private function tokenizeParameters(array $parameters) {
    foreach ($parameters as $key_param => $parameter) {
      if (is_array($parameter)) {
        $parameters[$key_param] = $this->tokenizeParameters($parameter);
        continue;
      }
      $parameters[$key_param] = $this->tokenService->replaceClear($parameter);
    }
    return $parameters;
  }

}
💬 Support request
Status

Active

Version

10.0

Component

Code

Created by

🇫🇷France musa.thomas France 🇫🇷

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024