You are here

public function XmlSitemapCustomListController::render in XML sitemap 8

Same name and namespace in other branches
  1. 2.x xmlsitemap_custom/src/Controller/XmlSitemapCustomListController.php \Drupal\xmlsitemap_custom\Controller\XmlSitemapCustomListController::render()

Renders a list with all custom links.

Return value

array The list to be rendered.

1 string reference to 'XmlSitemapCustomListController::render'
xmlsitemap_custom.routing.yml in xmlsitemap_custom/xmlsitemap_custom.routing.yml
xmlsitemap_custom/xmlsitemap_custom.routing.yml

File

xmlsitemap_custom/src/Controller/XmlSitemapCustomListController.php, line 51

Class

XmlSitemapCustomListController
Builds the list table for all custom links.

Namespace

Drupal\xmlsitemap_custom\Controller

Code

public function render() {
  $build['xmlsitemap_add_custom'] = [
    '#type' => 'link',
    '#title' => $this
      ->t('Add custom link'),
    '#href' => 'admin/config/search/xmlsitemap/custom/add',
  ];
  $header = [
    'loc' => [
      'data' => $this
        ->t('Location'),
      'field' => 'loc',
      'sort' => 'asc',
    ],
    'priority' => [
      'data' => $this
        ->t('Priority'),
      'field' => 'priority',
    ],
    'changefreq' => [
      'data' => $this
        ->t('Change frequency'),
      'field' => 'changefreq',
    ],
    'language' => [
      'data' => $this
        ->t('Language'),
      'field' => 'language',
    ],
    'operations' => [
      'data' => $this
        ->t('Operations'),
    ],
  ];
  $rows = [];
  $query = $this->connection
    ->select('xmlsitemap');
  $query
    ->fields('xmlsitemap');
  $query
    ->condition('type', 'custom');
  $query
    ->extend(PagerSelectExtender::class)
    ->limit(50);
  $query
    ->extend(TableSortExtender::class)
    ->orderByHeader($header);
  $result = $query
    ->execute();
  foreach ($result as $link) {
    $language = $this
      ->languageManager()
      ->getLanguage($link->language);
    $row = [];
    $row['loc'] = Link::fromTextAndUrl($link->loc, Url::fromUri('internal:' . $link->loc));
    $row['priority'] = number_format($link->priority, 1);
    $row['changefreq'] = $link->changefreq ? Unicode::ucfirst(xmlsitemap_get_changefreq($link->changefreq)) : $this
      ->t('None');
    if (isset($header['language'])) {
      $row['language'] = $language
        ->getName();
    }
    $operations['edit'] = [
      'title' => $this
        ->t('Edit'),
      'url' => Url::fromRoute('xmlsitemap_custom.edit', [
        'link' => $link->id,
      ]),
    ];
    $operations['delete'] = [
      'title' => $this
        ->t('Delete'),
      'url' => Url::fromRoute('xmlsitemap_custom.delete', [
        'link' => $link->id,
      ]),
    ];
    $row['operations'] = [
      'data' => [
        '#type' => 'operations',
        '#links' => $operations,
      ],
    ];
    $rows[] = $row;
  }

  // @todo Convert to tableselect
  $build['xmlsitemap_custom_table'] = [
    '#type' => 'table',
    '#header' => $header,
    '#rows' => $rows,
    '#empty' => $this
      ->t('No custom links available. <a href="@custom_link">Add custom link</a>', [
      '@custom_link' => Url::fromRoute('xmlsitemap_custom.add', [], [
        'query' => $this
          ->getDestinationArray(),
      ])
        ->toString(),
    ]),
  ];
  $build['xmlsitemap_custom_pager'] = [
    '#type' => 'pager',
  ];
  return $build;
}