3 namespace Drupal\system\Plugin\Block;
5 use Drupal\Core\Block\BlockBase;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\Core\Config\ConfigFactoryInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * Provides a block to display 'Site branding' elements.
17 * id = "system_branding_block",
18 * admin_label = @Translation("Site branding")
21 class SystemBrandingBlock extends BlockBase implements ContainerFactoryPluginInterface {
24 * Stores the configuration factory.
26 * @var \Drupal\Core\Config\ConfigFactoryInterface
28 protected $configFactory;
31 * Creates a SystemBrandingBlock instance.
33 * @param array $configuration
34 * A configuration array containing information about the plugin instance.
35 * @param string $plugin_id
36 * The plugin_id for the plugin instance.
37 * @param mixed $plugin_definition
38 * The plugin implementation definition.
39 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
40 * The factory for configuration objects.
42 public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
43 parent::__construct($configuration, $plugin_id, $plugin_definition);
44 $this->configFactory = $config_factory;
50 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
55 $container->get('config.factory')
62 public function defaultConfiguration() {
64 'use_site_logo' => TRUE,
65 'use_site_name' => TRUE,
66 'use_site_slogan' => TRUE,
67 'label_display' => FALSE,
74 public function blockForm($form, FormStateInterface $form_state) {
76 $theme = $form_state->get('block_theme');
79 $url_system_theme_settings = new Url('system.theme_settings');
80 $url_system_theme_settings_theme = new Url('system.theme_settings_theme', ['theme' => $theme]);
82 if ($url_system_theme_settings->access() && $url_system_theme_settings_theme->access()) {
83 // Provide links to the Appearance Settings and Theme Settings pages
84 // if the user has access to administer themes.
85 $site_logo_description = $this->t('Defined on the <a href=":appearance">Appearance Settings</a> or <a href=":theme">Theme Settings</a> page.', [
86 ':appearance' => $url_system_theme_settings->toString(),
87 ':theme' => $url_system_theme_settings_theme->toString(),
91 // Explain that the user does not have access to the Appearance and Theme
93 $site_logo_description = $this->t('Defined on the Appearance or Theme Settings page. You do not have the appropriate permissions to change the site logo.');
95 $url_system_site_information_settings = new Url('system.site_information_settings');
96 if ($url_system_site_information_settings->access()) {
97 // Get paths to settings pages.
98 $site_information_url = $url_system_site_information_settings->toString();
100 // Provide link to Site Information page if the user has access to
101 // administer site configuration.
102 $site_name_description = $this->t('Defined on the <a href=":information">Site Information</a> page.', [':information' => $site_information_url]);
103 $site_slogan_description = $this->t('Defined on the <a href=":information">Site Information</a> page.', [':information' => $site_information_url]);
106 // Explain that the user does not have access to the Site Information
108 $site_name_description = $this->t('Defined on the Site Information page. You do not have the appropriate permissions to change the site logo.');
109 $site_slogan_description = $this->t('Defined on the Site Information page. You do not have the appropriate permissions to change the site logo.');
112 $form['block_branding'] = [
113 '#type' => 'fieldset',
114 '#title' => $this->t('Toggle branding elements'),
115 '#description' => $this->t('Choose which branding elements you want to show in this block instance.'),
117 $form['block_branding']['use_site_logo'] = [
118 '#type' => 'checkbox',
119 '#title' => $this->t('Site logo'),
120 '#description' => $site_logo_description,
121 '#default_value' => $this->configuration['use_site_logo'],
124 $form['block_branding']['use_site_name'] = [
125 '#type' => 'checkbox',
126 '#title' => $this->t('Site name'),
127 '#description' => $site_name_description,
128 '#default_value' => $this->configuration['use_site_name'],
130 $form['block_branding']['use_site_slogan'] = [
131 '#type' => 'checkbox',
132 '#title' => $this->t('Site slogan'),
133 '#description' => $site_slogan_description,
134 '#default_value' => $this->configuration['use_site_slogan'],
142 public function blockSubmit($form, FormStateInterface $form_state) {
143 $block_branding = $form_state->getValue('block_branding');
144 $this->configuration['use_site_logo'] = $block_branding['use_site_logo'];
145 $this->configuration['use_site_name'] = $block_branding['use_site_name'];
146 $this->configuration['use_site_slogan'] = $block_branding['use_site_slogan'];
152 public function build() {
154 $site_config = $this->configFactory->get('system.site');
156 $build['site_logo'] = [
158 '#uri' => theme_get_setting('logo.url'),
159 '#alt' => $this->t('Home'),
160 '#access' => $this->configuration['use_site_logo'],
163 $build['site_name'] = [
164 '#markup' => $site_config->get('name'),
165 '#access' => $this->configuration['use_site_name'],
168 $build['site_slogan'] = [
169 '#markup' => $site_config->get('slogan'),
170 '#access' => $this->configuration['use_site_slogan'],
179 public function getCacheTags() {
180 return Cache::mergeTags(
181 parent::getCacheTags(),
182 $this->configFactory->get('system.site')->getCacheTags()