You are here

public function AdministerTaxonomy::build in Total Control Admin Dashboard 8.2

Same name and namespace in other branches
  1. 3.0.x src/Plugin/Block/AdministerTaxonomy.php \Drupal\total_control\Plugin\Block\AdministerTaxonomy::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/AdministerTaxonomy.php, line 137

Class

AdministerTaxonomy
Provides a 'Administer Taxonomy'.

Namespace

Drupal\total_control\Plugin\Block

Code

public function build() {
  if (!$this->moduleHandler
    ->moduleExists('taxonomy')) {
    $markup_data = $this
      ->t('You have to enable') . ' <strong>' . $this
      ->t('Taxonomy') . '</strong> ' . $this
      ->t('module to see this block.');
    return [
      '#type' => 'markup',
      '#markup' => $markup_data,
    ];
  }
  $vocabs = $this->entityTypeManager
    ->getStorage('taxonomy_vocabulary')
    ->loadMultiple();
  $config = $this
    ->getConfiguration();
  $vids = $config['total_control_admin_taxonomy'];
  $header = [
    [
      'data' => $this
        ->t('Vocabulary'),
    ],
    [
      'data' => $this
        ->t('Operations'),
      'colspan' => 3,
    ],
  ];
  $destination = $this->redirectDestination
    ->getAsArray();
  $options = [
    $destination,
  ];
  $rows = [];
  if (!empty($vocabs)) {
    foreach ($vocabs as $key => $vocab) {
      if (in_array($vocab
        ->get('vid'), $vids) && isset($vids[$key]) && $vids[$key] === $vocab
        ->id() || !array_key_exists($vocab
        ->get('vid'), $config['total_control_admin_taxonomy'])) {
        $term_query = $this->connection
          ->query("SELECT count(*) FROM {taxonomy_term_data} WHERE vid = :vid", [
          ':vid' => $vocab
            ->get('vid'),
        ]);
        $term_count = $term_query
          ->fetchField();
        if ($this->currentUser
          ->hasPermission('administer taxonomy') || $this->currentUser
          ->hasPermission('edit terms in ' . $vocab
          ->get('vid'))) {
          $terms = $this->stringTranslation
            ->formatPlural($term_count, '1 categories', '@count categories');
          $rows[] = [
            'data' => [
              $vocab
                ->get('name') . ': ' . $terms,
              Link::fromTextAndUrl($this
                ->t('Configure'), new Url('entity.taxonomy_vocabulary.edit_form', [
                'taxonomy_vocabulary' => $vocab
                  ->get('vid'),
                'options' => $options,
              ]))
                ->toString(),
              Link::fromTextAndUrl($this
                ->t('Manage categories'), new Url('entity.taxonomy_vocabulary.overview_form', [
                'taxonomy_vocabulary' => $vocab
                  ->get('vid'),
                'options' => $options,
              ]))
                ->toString(),
              Link::fromTextAndUrl($this
                ->t('Add new category'), new Url('entity.taxonomy_term.add_form', [
                'taxonomy_vocabulary' => $vocab
                  ->get('vid'),
                'options' => $options,
              ]))
                ->toString(),
            ],
          ];
        }
      }
    }
  }
  if (empty($rows)) {
    $rows[] = [
      'data' => $this
        ->t('There are no vocabularies to display.'),
      'colspan' => 4,
    ];
  }
  $link = NULL;
  if ($this->currentUser
    ->hasPermission('administer taxonomy')) {
    $link = Link::fromTextAndUrl($this
      ->t('Taxonomy administration'), new Url('entity.taxonomy_vocabulary.collection', $options));
  }
  $body_data = [
    '#theme' => 'table',
    '#header' => $header,
    '#rows' => $rows,
  ];
  $markup_data = $this->renderer
    ->render($body_data);
  if ($link instanceof RenderableInterface) {
    $markup_data .= $link
      ->toString();
  }
  return [
    '#type' => 'markup',
    '#markup' => $markup_data,
  ];
}