SyndicateBlock.php

Same filename and directory in other branches
  1. 8.9.x core/modules/node/src/Plugin/Block/SyndicateBlock.php
  2. 10 core/modules/node/src/Plugin/Block/SyndicateBlock.php
  3. 11.x core/modules/node/src/Plugin/Block/SyndicateBlock.php

Namespace

Drupal\node\Plugin\Block

File

core/modules/node/src/Plugin/Block/SyndicateBlock.php

View source
<?php

namespace Drupal\node\Plugin\Block;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;

/**
 * Provides a 'Syndicate' block that links to the site's RSS feed.
 *
 * @Block(
 *   id = "node_syndicate_block",
 *   admin_label = @Translation("Syndicate"),
 *   category = @Translation("System")
 * )
 */
class SyndicateBlock extends BlockBase implements ContainerFactoryPluginInterface {
    
    /**
     * The config factory.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * Constructs a SyndicateBlock object.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin_id for the plugin instance.
     * @param mixed $plugin_definition
     *   The plugin implementation definition.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
     *   The config factory.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $configFactory) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->configFactory = $configFactory;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static($configuration, $plugin_id, $plugin_definition, $container->get('config.factory'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function defaultConfiguration() {
        return [
            'block_count' => 10,
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    protected function blockAccess(AccountInterface $account) {
        return AccessResult::allowedIfHasPermission($account, 'access content');
    }
    
    /**
     * {@inheritdoc}
     */
    public function build() {
        $title = $this->configuration['label'];
        return [
            '#theme' => 'feed_icon',
            '#url' => Url::fromUri('internal:/rss.xml'),
            '#title' => $title,
        ];
    }

}

Classes

Title Deprecated Summary
SyndicateBlock Provides a 'Syndicate' block that links to the site's RSS feed.

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