You are here

public function DsController::listDisplays in Display Suite 8.3

Same name and namespace in other branches
  1. 8.4 src/Controller/DsController.php \Drupal\ds\Controller\DsController::listDisplays()
  2. 8.2 src/Controller/DsController.php \Drupal\ds\Controller\DsController::listDisplays()

Lists all bundles per entity type.

Return value

array The Views fields report page.

1 string reference to 'DsController::listDisplays'
ds.routing.yml in ./ds.routing.yml
ds.routing.yml

File

src/Controller/DsController.php, line 24

Class

DsController
Returns responses for Display Suite UI routes.

Namespace

Drupal\ds\Controller

Code

public function listDisplays() {
  $build = [];

  // All entities.
  $entity_info = $this
    ->entityTypeManager()
    ->getDefinitions();

  // Move node to the top.
  if (isset($entity_info['node'])) {
    $node_entity = $entity_info['node'];
    unset($entity_info['node']);
    $entity_info = array_merge([
      'node' => $node_entity,
    ], $entity_info);
  }
  $field_ui_enabled = $this
    ->moduleHandler()
    ->moduleExists('field_ui');
  if (!$field_ui_enabled) {
    $build['no_field_ui'] = [
      '#markup' => '<p>' . $this
        ->t('You need to enable Field UI to manage the displays of entities.') . '</p>',
      '#weight' => -10,
    ];
  }
  if (isset($entity_info['comment'])) {
    $comment_entity = $entity_info['comment'];
    unset($entity_info['comment']);
    $entity_info['comment'] = $comment_entity;
  }
  foreach ($entity_info as $entity_type => $info) {
    $base_table = $info
      ->getBaseTable();
    if ($info
      ->get('field_ui_base_route') && !empty($base_table)) {
      $rows = [];
      $bundles = \Drupal::service('entity_type.bundle.info')
        ->getBundleInfo($entity_type);
      foreach ($bundles as $bundle_type => $bundle) {
        $row = [];
        $operations = '';
        $row[] = [
          'data' => [
            '#plain_text' => $bundle['label'],
          ],
        ];
        if ($field_ui_enabled) {

          // Get the manage display URI.
          $route = FieldUI::getOverviewRouteInfo($entity_type, $bundle_type);
          if ($route) {
            try {
              $operations = Link::fromTextAndUrl($this
                ->t('Manage display'), Url::fromRoute("entity.entity_view_display.{$entity_type}.default", $route
                ->getRouteParameters()))
                ->toString();
            } catch (\Exception $ignored) {
            }
          }
        }

        // Add operation links.
        if (!empty($operations)) {

          // Simulate a drop button. Not using operations because we are
          // catching exceptions from the route generation, sometimes they
          // simply fatal.
          // @see https://www.drupal.org/project/ds/issues/3036765
          $row[] = [
            'data' => [
              '#type' => 'markup',
              '#markup' => '<div class="dropbutton-wrapper dropbutton-single"><div class="dropbutton-widget"><ul class="dropbutton"><li class="manage-display dropbutton-action">' . $operations . '</li></ul></div></div>',
            ],
          ];
        }
        else {
          $row[] = [
            'data' => '',
          ];
        }
        $rows[] = $row;
      }
      if (!empty($rows)) {
        $header = [
          [
            'data' => $info
              ->getLabel(),
          ],
          [
            'data' => $field_ui_enabled ? $this
              ->t('operations') : '',
            'class' => 'ds-display-list-options',
          ],
        ];
        $build['list_' . $entity_type] = [
          '#theme' => 'table',
          '#header' => $header,
          '#rows' => $rows,
        ];
      }
    }
  }
  $build['#attached']['library'][] = 'ds/admin';
  return $build;
}