mutual_credit-5.0.x-dev/src/Plugin/views/field/WalletStat.php

src/Plugin/views/field/WalletStat.php
<?php

namespace Drupal\mcapi\Plugin\views\field;

use Drupal\mcapi\Element\WorthsView;
use Drupal\mcapi\Entity\Storage\WalletStorage;
use Drupal\views\ResultRow;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Field handler provides current stat for given wallet via Wallet::getStatAll.
 *
 * @ingroup views_field_handlers
 *
 * @ViewsField("wallet_stat")
 *
 * @note for aggregated worths, see src/Plugin/views/query/Sql::getAggregationInfo()
 */
class WalletStat extends FieldPluginBase {

  protected function defineOptions() {
    $options = parent::defineOptions();
    $options['context'] = ['default' => TRUE];
    return $options;
  }

  /**
   * Default options form that provides the label widget that all fields
   * should have.
   */
  public function buildOptionsForm(&$form, FormStateInterface $form_state) {
    parent::buildOptionsForm($form, $form_state);
    $form['context'] = [
      '#type' => 'radios',
      '#title' => $this->t('Worth view context'),
      '#options' => WorthsView::options(),
      '#default_value' => $this->options['context'],
      '#weight' => 10,
    ];
  }
  /**
   * {@inheritdoc}
   */
  public function query() {
    $this->addAdditionalFields();
  }

  /**
   * {@inheritdoc}
   */
  public function render(ResultRow $values) {
    $entity = $this->getEntity($values);
    if ($entity->getEntityTypeId() != 'mcapi_wallet') {
      $entities = WalletStorage::walletsOf($entity, TRUE);
      $wallet = reset($entities);
    }
    else {
      $wallet = $entity;
    }
    if ($wallet) {
      $stat = $this->definition['stat'];
      $vals = $wallet->getStatAll($stat);
      switch ($stat) {
        // Worth value
        case 'volume':
        case 'incoming':
        case 'outgoing':
        case 'balance':
          foreach ($vals as $curr_id => $info) $worths[] = ['curr_id' => $curr_id, 'value' => $info[$stat]];
          return [
            '#type' => 'worths_view',
            '#worths' => $worths,
            '#context' => $this->options['context']
          ];
        // hard to know how to render these. E.g. In Curr1 there were 74 trades, in curr2 there were 21
        case 'trades':
        case 'partners':
          foreach ($vals as $curr_id => $info) {
            $vals[] = $this->t(
              '@currency_name : @val',
              ['@currency_name' => Currency::load($curr_id)->name, '@val' => $info[$stat]]
            );
          }
          return implode('<br />', $vals);
      }
    }
    return 'none';
  }

}

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

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