openquestions-1.0.x-dev/src/Plugin/Block/OQApplicationHistoriesBlock.php

src/Plugin/Block/OQApplicationHistoriesBlock.php
<?php

namespace Drupal\openquestions\Plugin\Block;

use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\Core\Utility\LinkGeneratorInterface;
use Drupal\Core\Session\AccountProxy;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\openquestions\Service\OQUtilsInterface;

/**
 * OpenQuestions application histories block.
 *
 * Designed to show the history of an application ("Pending" to "Approved" etc).
 *
 * "entity:oq_application" is available due to OQApplicationRouteContext.
 *
 * @Block(
 *  id = "oq_application_histories_block",
 *  admin_label = @Translation("OQ application histories"),
 *  category = @Translation("OpenQuestions"),
 *  context_definitions = {
 *    "oq_application" = @ContextDefinition("entity:oq_application", label = @Translation("OQ Item"))
 *  }
 * )
 */
class OQApplicationHistoriesBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
   * To generate links.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface
   */
  protected $linkGenerator;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountProxy
   */
  protected $currentUser;

  /**
   * The date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Our utils.
   *
   * @var \Drupal\openquestions\Service\OQUtilsInterface
   */
  protected $oqUtils;

  /**
   * Our logger.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $logger;

  /**
   * Constructs a OQApplicationHistoriesBlock.
   */
  public function __construct(array $configuration,
                $plugin_id,
                $plugin_definition,
                LinkGeneratorInterface $linkGenerator,
                AccountProxy $currentUser,
                DateFormatterInterface $dateFormatter,
                OQUtilsInterface $oqUtils,
                LoggerChannelInterface $logger) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);

    $this->linkGenerator = $linkGenerator;
    $this->currentUser = $currentUser;
    $this->dateFormatter = $dateFormatter;
    $this->oqUtils = $oqUtils;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
      $container->get('link_generator'),
      $container->get('current_user'),
      $container->get('date.formatter'),
      $container->get('openquestions.oq_utils'),
      $container->get('openquestions.logger.channel.openquestions')
    );
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'expand_all' => 1,
      'expand_questions' => 1,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function blockForm($form, FormStateInterface $form_state) {
    $form['expand_all'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Expand all'),
      '#description' => $this->t('Expand all'),
      '#default_value' => $this->configuration['expand_all'],
    ];

    $form['expand_questions'] = [
      '#type' => 'checkbox',
      '#title' => $this->t('Expand questions'),
      '#description' => $this->t('Expand questions'),
      '#default_value' => $this->configuration['expand_questions'],
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function blockSubmit($form, FormStateInterface $form_state) {
    $this->configuration['expand_all'] = $form_state->getValue('expand_all');
    $this->configuration['expand_questions'] = $form_state->getValue('expand_questions');
  }

  /**
   * {@inheritdoc}
   */
  public function build() {
    $oqApplication = $this->getContextValue('oq_application');
    $histories = $oqApplication->get('histories')->referencedEntities();

    usort($histories, function ($a, $b) {
      return $b->get('created')->value <=> $a->get('created')->value;
    });

    $ret = [
      '#theme' => 'table__openquestions_application_histories',
      '#caption' => $this->t('The disposition history of this application (latest change first)'),
      '#rows' => [],
      '#header' => [
        $this->t('Date'),
        $this->t('Before'),
        $this->t('After'),
        $this->t('Explanation'),
      ],
    ];

    foreach ($histories as $history) {
      $before = $this->oqUtils->getFirstReference($history, 'disposition_before');
      $after = $this->oqUtils->getFirstReference($history, 'disposition_after');
      $ret['#rows'][] = [
        $this->dateFormatter->format($history->get('created')->value, 'short'),
        $before ? $before->getName() : '--',
        $after ? $after->getName() : '--',
        $history->get('description')->value,
      ];
    }

    return $ret;
  }

}

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

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