mutual_credit-5.0.x-dev/modules/signatures/src/Plugin/Block/Pending.php

modules/signatures/src/Plugin/Block/Pending.php
<?php

namespace Drupal\mcapi_signatures\Plugin\Block;

use Drupal\mcapi\Plugin\Block\WalletContextBlockBase;
use Drupal\user\Entity\User;
use Drupal\user\UserInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Render\Markup;

/**
 * Provides a block for switching users.
 *
 * @Block(
 *   id = "mcapi_pending",
 *   admin_label = @Translation("Pending transactions"),
 *   category = "Community Accounting"
 * )
 */
class Pending extends WalletContextBlockBase implements ContainerFactoryPluginInterface {

  /**
   * {@inheritdoc}
   */
  public function label() {
    if ($this->configuration['holder_source'] == static::HOLDER_CURRENT_USER) {
      $label = $this->t('Pending transactions');
    }
    else {
      // Only the settings page should have no holder.
      $label = $this->t('Pending transactions for @name', ['@name' => $this->getWalletOwner()->getDisplayName()]);
    }
    return $label;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'empty_message' => $this->t('No pending transactions.'),
      'others' => TRUE
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, $form_state) {
    $form = parent::blockForm($form, $form_state);
    $form['empty_message'] = [
      '#title' => $this->t('Empty message'),
      '#description' => $this->t('leave blank to hide the block when there are no pending transactions'),
      '#type' => 'textfield',
      '#default_value' => $this->configuration['empty_message'],
      '#placeholder' => $this->t('No pending transactions.')
    ];
    $form['others'] = [
      '#title' => $this->t('Also show my transactions waiting for others to sign'),
      '#type' => 'checkbox',
      '#default_value' => $this->configuration['others']
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function build(): array {
    $renderable = [];
    $uid = $this->getWalletOwner()->id();
    $serials = signature_needed_of_me($uid);
    if ($this->configuration['others']) {
      $serials = array_merge($serials, signature_needed_of_other($uid));
    }
    if ($serials) {
      $renderable = mcapi_render_sentences($serials, TRUE);
    }
    elseif ($this->configuration['empty_message']) {
      // @todo put an optional empty message
      $renderable[] = [
        '#markup' => Markup::create($this->configuration['empty_message']),
        '#weight' => -1
      ];
    }
    return $renderable += parent::build();
  }


  /**
   * Get the owner i.e. the user responsible for the wallet held
   */
  private function getWalletOwner() : User {
    if ($this->holder instanceOf UserInterface) {
      $owner = $this->holder;
    }
    else {
      $owner = $this->holder->getOwner();
    }
    return $owner;
  }

}

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

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