You are here

public function SitemapSyndicateBlock::build in Sitemap 2.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Block/SitemapSyndicateBlock.php \Drupal\sitemap\Plugin\Block\SitemapSyndicateBlock::build()
  2. 8 src/Plugin/Block/SitemapSyndicateBlock.php \Drupal\sitemap\Plugin\Block\SitemapSyndicateBlock::build()

Builds and returns the renderable array for this block plugin.

If a block should not be rendered because it has no content, then this method must also ensure to return no content: it must then only return an empty array, or an empty array with #cache set (with cacheability metadata indicating the circumstances for it being empty).

Return value

array A renderable array representing the content of the block.

Overrides BlockPluginInterface::build

See also

\Drupal\block\BlockViewBuilder

File

src/Plugin/Block/SitemapSyndicateBlock.php, line 43

Class

SitemapSyndicateBlock
Provides a 'Syndicate (sitemap)' block.

Namespace

Drupal\sitemap\Plugin\Block

Code

public function build() {
  $config = \Drupal::config('sitemap.settings');
  $route_name = \Drupal::routeMatch()
    ->getRouteName();
  if ($route_name == 'blog.user_rss') {
    $feedurl = Url::fromRoute('blog.user_rss', [
      'user' => \Drupal::routeMatch()
        ->getParameter('user'),
    ]);
  }
  elseif ($route_name == 'blog.blog_rss') {
    $feedurl = Url::fromRoute('blog.blog_rss');
  }
  else {
    $feedurl = $config
      ->get('rss_front');
  }
  $feed_icon = [
    '#theme' => 'feed_icon',
    '#url' => $feedurl,
    '#title' => t('Syndicate'),
  ];
  $output = \Drupal::service('renderer')
    ->render($feed_icon);

  // Re-use drupal core's render element.
  $more_link = [
    '#type' => 'more_link',
    '#url' => Url::fromRoute('sitemap.page'),
    '#attributes' => [
      'title' => t('View the sitemap to see more RSS feeds.'),
    ],
  ];
  $output .= \Drupal::service('renderer')
    ->render($more_link);
  return [
    '#type' => 'markup',
    '#markup' => $output,
  ];
}