function EntityDisplayModeListBuilder::render

Same name in other branches
  1. 9 core/modules/field_ui/src/EntityDisplayModeListBuilder.php \Drupal\field_ui\EntityDisplayModeListBuilder::render()
  2. 8.9.x core/modules/field_ui/src/EntityDisplayModeListBuilder.php \Drupal\field_ui\EntityDisplayModeListBuilder::render()
  3. 11.x core/modules/field_ui/src/EntityDisplayModeListBuilder.php \Drupal\field_ui\EntityDisplayModeListBuilder::render()

Overrides EntityListBuilder::render

File

core/modules/field_ui/src/EntityDisplayModeListBuilder.php, line 115

Class

EntityDisplayModeListBuilder
Defines a class to build a listing of view mode entities.

Namespace

Drupal\field_ui

Code

public function render() {
    $build = [];
    foreach ($this->load() as $entity_type => $entities) {
        if (!isset($this->entityTypes[$entity_type])) {
            continue;
        }
        // Filter entities.
        if (!$this->isValidEntity($entity_type)) {
            continue;
        }
        $table = [
            '#prefix' => '<h2>' . $this->entityTypes[$entity_type]
                ->getLabel() . '</h2>',
            '#type' => 'table',
            '#header' => $this->buildHeader(),
            '#rows' => [],
            '#attributes' => [
                'class' => [
                    'display-mode-table',
                ],
            ],
        ];
        foreach ($entities as $entity) {
            if ($row = $this->buildRow($entity)) {
                $table['#rows'][$entity->id()] = $row;
            }
        }
        // Move content at the top.
        if ($entity_type == 'node') {
            $table['#weight'] = -10;
        }
        $short_type = str_replace([
            'entity_',
            '_mode',
        ], '', $this->entityTypeId);
        $table['#rows']['_add_new'][] = [
            'data' => [
                '#type' => 'link',
                '#url' => Url::fromRoute($short_type == 'view' ? 'entity.entity_view_mode.add_form' : 'entity.entity_form_mode.add_form', [
                    'entity_type_id' => $entity_type,
                ]),
                '#title' => $this->t('Add %label for @entity-type', [
                    '@entity-type' => $this->entityTypes[$entity_type]
                        ->getLabel(),
                    '%label' => $this->entityType
                        ->getSingularLabel(),
                ]),
                '#button_type' => 'primary',
                '#attributes' => [
                    'class' => [
                        'button',
                        'use-ajax',
                        'button--small',
                    ],
                    'data-dialog-type' => 'modal',
                    'data-dialog-options' => Json::encode([
                        'width' => '880',
                    ]),
                ],
                '#attached' => [
                    'library' => [
                        'core/drupal.dialog.ajax',
                        'field_ui/drupal.field_ui_table',
                    ],
                ],
            ],
            'colspan' => count($table['#header']),
        ];
        $build[$entity_type] = $table;
    }
    return $build;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.