mutual_credit-5.0.x-dev/src/TransactionOperations.php

src/TransactionOperations.php
<?php

namespace Drupal\mcapi;

use Drupal\mcapi\Entity\TransactionInterface;
use Drupal\system\Entity\Action;
use Drupal\Core\Url;

/**
 * Handle the transaction entity's 'operations' which are in fact system actions
 * with extra config. Most operations lead to a highly configured confirm form,
 * which can be reached though ajax, a modal box or in a new page.
 * View is special because it has no confirm form and indeed no action, but
 * shares access control and page formatting with the other operations. Save is
 * also special, being hidden from the UI.
 *
 * @todo make this a TransactionActionManager service utilising a Collection.
 */
class TransactionOperations {

  /**
   * Get the operations for the given transaction
   *
   * @param TransactionInterface $transaction
   * @param bool $include_view
   *
   * @return array
   *   A renderable array
   *
   * @todo cache these by user and transaction
   */
  public static function get(TransactionInterface $transaction, bool $include_view) {
    $transactionAccessController = \Drupal::entityTypeManager()->getAccessControlHandler('mcapi_transaction');
    $operations = [];
    $actions = static::loadAllActions();
    if (!$include_view) {
      unset($actions[array_search('view', $actions)]);
    }
    foreach ($actions as $action_name) {
      // Access usually depends on transaction state and the current user's relation to the transaction
      if ($transactionAccessController->access($transaction, $action_name)) {
        $action = Action::load("transaction_$action_name");
        $operations[$action_name] = static::prepareOp($action, $transaction->serial->value);
      }
    }
    \Drupal::moduleHandler()->alter('entity_operation', $operations, $transaction);
    uasort($operations, '\Drupal\Component\Utility\SortArray::sortByWeightElement');
    return $operations;
  }

  /**
   *
   * @param Action $action
   * @param int $serial
   *
   * @return array
   *   with keys 'title', 'url', 'weight'
   */
  static function prepareOp(Action $action, $serial) {
    $name = $action->id();
    if ($name == 'transaction_view' or $name == 'transaction_save') {
      $route_name = 'entity.mcapi_transaction.canonical';
    }
    else {
      $route_name = 'entity.mcapi_transaction.'.$name;
    }
    $action_options = $action->getPlugin()->getConfiguration();
    $url = Url::fromRoute(
      $route_name,
      ['mcapi_transaction' => $serial],
      ['attributes' => ['title' => $action_options['tooltip']]]
    );
    return [
      'title' => $action_options['title'],
      'url' => $url,
      'weight' => $action_options['weight']
    ];
  }

  /**
   * Utility function.
   *
   * Loads any of the transaction operation actions.
   *
   * @param string $operation
   *
   * @return Action
   */
  public static function loadOperation($operation) : Action {
    $name = 'transaction_' . $operation;
    if ($action = Action::load($name)) {
      return $action;
    }
    throw new \Exception('Action does not exist: '.$name);
  }

  /**
   * Utility function.
   * Load those special transaction operations which are also actions.
   *
   * @param bool $full
   *   TRUE to load the action objects
   *
   * @return []
   *   The full actions or just names
   */
  public static function loadAllActions(bool $full = FALSE) : array {
    $actions = [];
    $config_names = \Drupal::configFactory()->listAll('system.action.transaction_');
    foreach ($config_names as $action_name) {
      $actions[] = substr($action_name, 26);
    }
    if ($full) {
      foreach ($config_names as $action_name) {
        $action_names[] = substr($action_name, 14);
      }
      $actions = array_combine($actions, Action::loadMultiple($action_names));
    }
    return $actions;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc