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"),
20 * "settings_tray" = "Drupal\system\Form\SystemBrandingOffCanvasForm",
24 class SystemBrandingBlock extends BlockBase implements ContainerFactoryPluginInterface {
27 * Stores the configuration factory.
29 * @var \Drupal\Core\Config\ConfigFactoryInterface
31 protected $configFactory;
34 * Creates a SystemBrandingBlock instance.
36 * @param array $configuration
37 * A configuration array containing information about the plugin instance.
38 * @param string $plugin_id
39 * The plugin_id for the plugin instance.
40 * @param mixed $plugin_definition
41 * The plugin implementation definition.
42 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
43 * The factory for configuration objects.
45 public function __construct(array $configuration, $plugin_id, $plugin_definition, ConfigFactoryInterface $config_factory) {
46 parent::__construct($configuration, $plugin_id, $plugin_definition);
47 $this->configFactory = $config_factory;
53 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
58 $container->get('config.factory')
65 public function defaultConfiguration() {
67 'use_site_logo' => TRUE,
68 'use_site_name' => TRUE,
69 'use_site_slogan' => TRUE,
70 'label_display' => FALSE,
77 public function blockForm($form, FormStateInterface $form_state) {
79 $theme = $form_state->get('block_theme');
82 $url_system_theme_settings = new Url('system.theme_settings');
83 $url_system_theme_settings_theme = new Url('system.theme_settings_theme', ['theme' => $theme]);
85 if ($url_system_theme_settings->access() && $url_system_theme_settings_theme->access()) {
86 // Provide links to the Appearance Settings and Theme Settings pages
87 // if the user has access to administer themes.
88 $site_logo_description = $this->t('Defined on the <a href=":appearance">Appearance Settings</a> or <a href=":theme">Theme Settings</a> page.', [
89 ':appearance' => $url_system_theme_settings->toString(),
90 ':theme' => $url_system_theme_settings_theme->toString(),
94 // Explain that the user does not have access to the Appearance and Theme
96 $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.');
98 $url_system_site_information_settings = new Url('system.site_information_settings');
99 if ($url_system_site_information_settings->access()) {
100 // Get paths to settings pages.
101 $site_information_url = $url_system_site_information_settings->toString();
103 // Provide link to Site Information page if the user has access to
104 // administer site configuration.
105 $site_name_description = $this->t('Defined on the <a href=":information">Site Information</a> page.', [':information' => $site_information_url]);
106 $site_slogan_description = $this->t('Defined on the <a href=":information">Site Information</a> page.', [':information' => $site_information_url]);
109 // Explain that the user does not have access to the Site Information
111 $site_name_description = $this->t('Defined on the Site Information page. You do not have the appropriate permissions to change the site logo.');
112 $site_slogan_description = $this->t('Defined on the Site Information page. You do not have the appropriate permissions to change the site logo.');
115 $form['block_branding'] = [
116 '#type' => 'fieldset',
117 '#title' => $this->t('Toggle branding elements'),
118 '#description' => $this->t('Choose which branding elements you want to show in this block instance.'),
120 $form['block_branding']['use_site_logo'] = [
121 '#type' => 'checkbox',
122 '#title' => $this->t('Site logo'),
123 '#description' => $site_logo_description,
124 '#default_value' => $this->configuration['use_site_logo'],
127 $form['block_branding']['use_site_name'] = [
128 '#type' => 'checkbox',
129 '#title' => $this->t('Site name'),
130 '#description' => $site_name_description,
131 '#default_value' => $this->configuration['use_site_name'],
133 $form['block_branding']['use_site_slogan'] = [
134 '#type' => 'checkbox',
135 '#title' => $this->t('Site slogan'),
136 '#description' => $site_slogan_description,
137 '#default_value' => $this->configuration['use_site_slogan'],
145 public function blockSubmit($form, FormStateInterface $form_state) {
146 $block_branding = $form_state->getValue('block_branding');
147 $this->configuration['use_site_logo'] = $block_branding['use_site_logo'];
148 $this->configuration['use_site_name'] = $block_branding['use_site_name'];
149 $this->configuration['use_site_slogan'] = $block_branding['use_site_slogan'];
155 public function build() {
157 $site_config = $this->configFactory->get('system.site');
159 $build['site_logo'] = [
161 '#uri' => theme_get_setting('logo.url'),
162 '#alt' => $this->t('Home'),
163 '#access' => $this->configuration['use_site_logo'],
166 $build['site_name'] = [
167 '#markup' => $site_config->get('name'),
168 '#access' => $this->configuration['use_site_name'],
171 $build['site_slogan'] = [
172 '#markup' => $site_config->get('slogan'),
173 '#access' => $this->configuration['use_site_slogan'],
182 public function getCacheTags() {
183 return Cache::mergeTags(
184 parent::getCacheTags(),
185 $this->configFactory->get('system.site')->getCacheTags()