installation_checklist-1.0.x-dev/src/Plugin/Block/InstallationChecklistBlock.php

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

declare(strict_types=1);

namespace Drupal\installation_checklist\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;

/**
 * Provides an installation checklist block.
 */
#[Block(
  id: 'installation_checklist_block',
  admin_label: new TranslatableMarkup('Installation Checklist Block'),
  category: new TranslatableMarkup('Dashboard'),
)]
final class InstallationChecklistBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build(): array {
    $checklist = checklistapi_checklist_load('installation_checklist');
    $setup_steps = $checklist->items['setup'];
    $setup_steps_indices = Element::children($setup_steps);
    $next_task_id = reset($setup_steps_indices);
    $next_task = $setup_steps[$next_task_id]['#title'];

    if ($checklist->hasSavedProgress()) {
      $progressed = $checklist->savedProgress['#items'];
      foreach ($setup_steps_indices as $setup_steps_index) {
        if (!isset($progressed[$setup_steps_index])) {
          $next_task_id = $setup_steps_index;
          $next_task = $setup_steps[$setup_steps_index]['#title'];
          break;
        }
      }
    }
    $info = checklistapi_get_checklist_info('installation_checklist');
    $build['progress'] = [
      '#theme' => 'installation_checklist_block',
      '#message' => $next_task,
      '#number_complete' => $checklist->getNumberCompleted(),
      '#number_of_items' => $checklist->getNumberOfItems(),
      '#percent_complete' => (int) round($checklist->getPercentComplete()),
      '#attached' => [
        'library' => [
          'core/drupal.progress',
        ],
      ],
    ];
    $build['link'] = [
      '#title' => $setup_steps[$next_task_id]['link']['#text'],
      '#type' => 'link',
      '#url' => $setup_steps[$next_task_id]['link']['#url'],
      '#suffix' => ' | ',
    ];
    $build['checklist'] = [
      '#title' => $this->t('See the installation checklist'),
      '#type' => 'link',
      '#url' => $checklist->getUrl(),
    ];
    CacheableMetadata::createFromRenderArray($build)->addCacheableDependency($this)->applyTo($build);
    return $build;
  }

  /**
   * {@inheritdoc}
   */
  protected function blockAccess(AccountInterface $account): AccessResult {
    $checklist = checklistapi_checklist_load('installation_checklist');
    return AccessResult::allowedIf($checklist->getPercentComplete() !== 100)
      ->andIf(AccessResult::allowedIf($checklist->userHasAccess('view')));
  }

}

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

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