330528528d67808d8ec94da33f59f201c3e51448
[yaffs-website] / web / core / modules / system / src / Plugin / Block / SystemBrandingBlock.php
1 <?php
2
3 namespace Drupal\system\Plugin\Block;
4
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;
10 use Drupal\Core\Url;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * Provides a block to display 'Site branding' elements.
15  *
16  * @Block(
17  *   id = "system_branding_block",
18  *   admin_label = @Translation("Site branding")
19  * )
20  */
21 class SystemBrandingBlock extends BlockBase implements ContainerFactoryPluginInterface {
22
23   /**
24    * Stores the configuration factory.
25    *
26    * @var \Drupal\Core\Config\ConfigFactoryInterface
27    */
28   protected $configFactory;
29
30   /**
31    * Creates a SystemBrandingBlock instance.
32    *
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.
41    */
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;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
51     return new static(
52       $configuration,
53       $plugin_id,
54       $plugin_definition,
55       $container->get('config.factory')
56     );
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function defaultConfiguration() {
63     return [
64       'use_site_logo' => TRUE,
65       'use_site_name' => TRUE,
66       'use_site_slogan' => TRUE,
67       'label_display' => FALSE,
68     ];
69   }
70
71   /**
72    * {@inheritdoc}
73    */
74   public function blockForm($form, FormStateInterface $form_state) {
75     // Get the theme.
76     $theme = $form_state->get('block_theme');
77
78     // Get permissions.
79     $url_system_theme_settings = new Url('system.theme_settings');
80     $url_system_theme_settings_theme = new Url('system.theme_settings_theme', ['theme' => $theme]);
81
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(),
88       ]);
89     }
90     else {
91       // Explain that the user does not have access to the Appearance and Theme
92       // Settings pages.
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.');
94     }
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();
99
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]);
104     }
105     else {
106       // Explain that the user does not have access to the Site Information
107       // page.
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.');
110     }
111
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.'),
116     ];
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'],
122     ];
123
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'],
129     ];
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'],
135     ];
136     return $form;
137   }
138
139   /**
140    * {@inheritdoc}
141    */
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'];
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function build() {
153     $build = [];
154     $site_config = $this->configFactory->get('system.site');
155
156     $build['site_logo'] = [
157       '#theme' => 'image',
158       '#uri' => theme_get_setting('logo.url'),
159       '#alt' => $this->t('Home'),
160       '#access' => $this->configuration['use_site_logo'],
161     ];
162
163     $build['site_name'] = [
164       '#markup' => $site_config->get('name'),
165       '#access' => $this->configuration['use_site_name'],
166     ];
167
168     $build['site_slogan'] = [
169       '#markup' => $site_config->get('slogan'),
170       '#access' => $this->configuration['use_site_slogan'],
171     ];
172
173     return $build;
174   }
175
176   /**
177    * {@inheritdoc}
178    */
179   public function getCacheTags() {
180     return Cache::mergeTags(
181       parent::getCacheTags(),
182       $this->configFactory->get('system.site')->getCacheTags()
183     );
184   }
185
186 }