You are here

public function SiteSettingEntityListBuilder::render in Site Settings and Labels 8

Builds the entity listing as renderable array for table.html.twig.

@todo Add a link to add a new item to the #empty text.

Overrides EntityListBuilder::render

File

src/SiteSettingEntityListBuilder.php, line 166

Class

SiteSettingEntityListBuilder
Defines a class to build a listing of Site Setting entities.

Namespace

Drupal\site_settings

Code

public function render() {
  $entity_type = $this->entityType
    ->getBundleEntityType();
  $this->bundles = $this->entityTypeManager
    ->getStorage($entity_type)
    ->loadMultiple();
  $missing_bundles = array_keys($this->bundles);
  $variables['settings'] = $this->siteSettingsLoader
    ->loadAll(TRUE);
  $build['table'] = [
    '#type' => 'table',
    '#header' => $this
      ->buildHeader(),
    '#title' => $this
      ->getTitle(),
    '#rows' => [],
    '#empty' => $this
      ->t('There is no @label yet.', [
      '@label' => $this->entityType
        ->getLabel(),
    ]),
    '#cache' => [
      'contexts' => $this->entityType
        ->getListCacheContexts(),
      'tags' => $this->entityType
        ->getListCacheTags(),
    ],
  ];

  // Storage for entities the user is allowed to create but is not allowed
  // to update. Used when site_settings_type_permissions submodule is enabled.
  $creatable_non_viewable_entities = [];
  $last_fieldset = FALSE;
  foreach ($this
    ->load() as $entity) {

    // Get bundle type.
    $bundle_type = $entity
      ->getType();
    $search = array_search($bundle_type, $missing_bundles);
    if ($search !== FALSE) {
      unset($missing_bundles[$search]);
    }

    // Add each site setting if the user has access.
    if ($entity
      ->access('update') || $entity
      ->access('delete')) {

      // Set fieldset separator.
      $fieldset = $entity->fieldset
        ->getValue()[0]['value'];
      if ($fieldset != $last_fieldset) {
        $heading = [
          '#markup' => '<strong>' . $fieldset . '</strong>',
        ];
        $build['table']['#rows'][$fieldset] = [
          'name' => $this->renderer
            ->render($heading),
          'fieldset' => '',
          'value' => '',
          'operations' => '',
        ];
        $last_fieldset = $fieldset;
      }

      // Add table rows.
      if ($row = $this
        ->buildRow($entity)) {
        $build['table']['#rows'][$entity
          ->id()] = $row;
      }
    }
    elseif ($entity
      ->access('create')) {

      // User is allowed to create this site setting type but cannot view.
      $creatable_non_viewable_entities[$bundle_type] = $entity;
    }
  }

  // If we have site settings the user can create but not update.
  if ($creatable_non_viewable_entities) {

    // Add site settings that can be created.
    foreach ($creatable_non_viewable_entities as $bundle => $entity) {
      $url = new Url('entity.site_setting_entity.add_form', [
        'site_setting_entity_type' => $bundle,
      ]);
      if ($url
        ->access()) {

        // Add link if user has access.
        $link = [
          '#type' => 'link',
          '#title' => $this
            ->t('Create setting'),
          '#url' => $url,
          '#attributes' => [
            'class' => [
              'button',
            ],
          ],
        ];
        array_unshift($build['table']['#rows'], [
          'name' => $this->linkGeneration
            ->generate($entity
            ->label(), $url),
          'fieldset' => $entity->fieldset->value,
          'value' => '',
          'operations' => $this->renderer
            ->render($link),
        ]);
      }
    }

    // Add heading.
    $heading = [
      '#markup' => '<strong>' . $this
        ->t('Settings where more can be created') . '</strong>',
    ];
    array_unshift($build['table']['#rows'], [
      'name' => $this->renderer
        ->render($heading),
      'fieldset' => '',
      'value' => '',
      'operations' => '',
    ]);
  }

  // If we have site settings not yet created.
  if ($missing_bundles) {

    // Sort missing bundles alphabetically by fieldset and label.
    usort($missing_bundles, function ($a, $b) {
      if ($this->bundles[$a]->fieldset == $this->bundles[$b]->fieldset) {
        return $this->bundles[$a]
          ->label() >= $this->bundles[$b]
          ->label() ? -1 : 1;
      }
      return $this->bundles[$a]->fieldset >= $this->bundles[$b]->fieldset ? -1 : 1;
    });

    // Boolean to determine whether the 'Settings not yet created' title
    // should be shown.
    $has_access_to_not_yet_created = FALSE;
    foreach ($missing_bundles as $missing) {

      // Settings that have not yet been created rows.
      $url = new Url('entity.site_setting_entity.add_form', [
        'site_setting_entity_type' => $missing,
      ]);
      if ($url
        ->access()) {
        $has_access_to_not_yet_created = TRUE;

        // Add link if user has access.
        $link = [
          '#type' => 'link',
          '#title' => $this
            ->t('Create setting'),
          '#url' => $url,
          '#attributes' => [
            'class' => [
              'button',
            ],
          ],
        ];
        array_unshift($build['table']['#rows'], [
          'name' => $this->linkGeneration
            ->generate($this->bundles[$missing]
            ->label(), $url),
          'fieldset' => $this->bundles[$missing]->fieldset,
          'value' => '',
          'operations' => $this->renderer
            ->render($link),
        ]);
      }
    }

    // Not yet created title.
    if ($has_access_to_not_yet_created) {
      $heading = [
        '#markup' => '<strong>' . $this
          ->t('Settings not yet created') . '</strong>',
      ];
      array_unshift($build['table']['#rows'], [
        'name' => $this->renderer
          ->render($heading),
        'fieldset' => '',
        'value' => '',
        'operations' => '',
      ]);
    }
  }
  return $build;
}