commands-1.x-dev/src/CommandTypeListBuilder.php

src/CommandTypeListBuilder.php
<?php

namespace Drupal\commands;

use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;

/**
 * Defines a class to build a listing of command type entities.
 *
 * @see \Drupal\commands\Entity\CommandType
 */
class CommandTypeListBuilder extends ConfigEntityListBuilder {

  /**
   * {@inheritdoc}
   */
  public function buildHeader() {
    $header['title'] = $this->t('Type');
    $header['command'] = $this->t('Command');
    $header['description'] = $this->t('Description');

    return $header + parent::buildHeader();
  }

  /**
   * {@inheritdoc}
   */
  public function buildRow(EntityInterface $entity) {
    $plugin = $entity->commandPlugin();
    $row['title'] = [
      'data' => $entity->label(),
      'class' => ['menu-label'],
    ];

    $row['command'] = [
      'data' =>  [
        '#type' => 'html_tag',
        '#tag' => 'pre',
        '#value' => is_array($plugin->command())? implode(PHP_EOL, $plugin->command()): $plugin->command(),
      ]
    ];
    $row['description'] = $entity->description() ?: $plugin->description();

    return $row + parent::buildRow($entity);
  }

  /**
   * {@inheritdoc}
   */
  public function loadPlugins() {
    $type = \Drupal::service('plugin.manager.commands');
    $plugins = $type->getDefinitions();
    foreach ($plugins as $plugin_id => &$plugin) {
      $plugin = $type->createInstance($plugin_id);
    }
    return $plugins;
  }

  /**
   * Render a row of the "Disabled commands"
   */
  public function buildPluginRow($plugin) {
    $row['title'] = [
      'data' => $plugin->label(),
      'class' => ['menu-label'],
    ];
    $row['command'] = [
      'data' =>  [
        '#type' => 'html_tag',
        '#tag' => 'pre',
        '#value' => is_array($plugin->command())? implode(PHP_EOL, $plugin->command()): $plugin->command(),
      ]
    ];
    $row['description'] = $plugin->description();
    $row['operations'] = Link::createFromRoute(t('Enable'), 'entity.command_type.type_add', [
      'plugin'=> $plugin->id(),
    ]);

    return $row;
  }

  /**
   * Render a list of command plugins that do not have bundles.
   * @return array
   */
  public function renderDisabled() {
    $build['title'] = [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $this->t('Disabled Commands'),
    ];
    $build['table'] = [
      '#type' => 'table',
      '#header' => $this->buildHeader(),
      '#title' => t('Disabled commands'),
      '#title_display' => 'before',
      '#rows' => [],
      '#empty' => $this->t('There are no disabled @label.', ['@label' => $this->entityType->getPluralLabel()]),
      '#cache' => [
        'contexts' => $this->entityType->getListCacheContexts(),
        'tags' => $this->entityType->getListCacheTags(),
      ],
    ];
    foreach ($this->loadPlugins() as $plugin_id => $plugin) {
      if (!in_array($plugin->id(), $this->getEntityIds()) && $row = $this->buildPluginRow($plugin)) {
        $build['table']['#rows'][$plugin->id()] = $row;
      }
    }

    return $build;
  }

  /**
   * {@inheritdoc}
   */
  public function render() {
    $build = parent::render();

    $build['disabled'] = $this->renderDisabled();


    return $build;
  }

}

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

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