You are here

AutoBlockSchedulerController.php in Auto Block Scheduler 8

Same filename and directory in other branches
  1. 8.2 src/Controller/AutoBlockSchedulerController.php

File

src/Controller/AutoBlockSchedulerController.php
View source
<?php

namespace Drupal\auto_block_scheduler\Controller;

use Drupal\block\Entity\Block;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Url;
use Drupal\Core\Datetime\DrupalDateTime;
class AutoBlockSchedulerController extends ControllerBase {
  public function scheduler_block_list() {
    $blocks = Block::loadMultiple();
    $current_url = Url::fromRoute('<current>');
    $destination = $current_url
      ->toString();
    $rows = [];
    $inc = 0;
    foreach ($blocks as $key => $block) {
      $settings = $block
        ->get('visibility');
      $url = Url::fromRoute('entity.block.edit_form', [
        'block' => $key,
      ], [
        'query' => [
          'destination' => $destination,
        ],
        'absolute' => TRUE,
      ]);
      if (isset($settings['auto_block_scheduler'])) {
        $rows[$inc]['id'] = $key;
        $rows[$inc]['label'] = $block
          ->get('settings')['label'];
        $rows[$inc]['published_on'] = !empty($settings['auto_block_scheduler']['published_on']) ? DrupalDateTime::createFromTimestamp($settings['auto_block_scheduler']['published_on']) : 'NA';
        $rows[$inc]['unpublished_on'] = !empty($settings['auto_block_scheduler']['unpublished_on']) ? DrupalDateTime::createFromTimestamp($settings['auto_block_scheduler']['unpublished_on']) : 'NA';
        $rows[$inc]['negate'] = !empty($settings['auto_block_scheduler']['negate']) ? '1' : '0';
        $rows[$inc]['configure'] = \Drupal::l('Configure', $url);
        $inc++;
      }
    }
    return [
      '#type' => 'table',
      '#header' => [
        'id' => 'Id',
        'label' => 'Label',
        'published_on' => 'Published On',
        'unpublished_on' => 'Unpublished On',
        'negate' => 'Negate',
        'operation' => 'Operation',
      ],
      '#rows' => $rows,
      "#sticky" => true,
      "#empty" => "No, Block scheduled with auto block scheduler",
      '#caption' => $this
        ->t("Auto Block Scheduler"),
    ];
  }

}

Classes