Updated to Drupal 8.6.4, which is PHP 7.3 friendly. Also updated HTMLaw library....
[yaffs-website] / web / core / modules / system / src / Form / SystemBrandingOffCanvasForm.php
1 <?php
2
3 namespace Drupal\system\Form;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Config\ConfigFactoryInterface;
7 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
8 use Drupal\Core\Form\FormStateInterface;
9 use Drupal\Core\Plugin\PluginFormBase;
10 use Drupal\Core\Session\AccountInterface;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
12
13 /**
14  * The settings_tray form handler for the SystemBrandingBlock.
15  *
16  * @internal
17  */
18 class SystemBrandingOffCanvasForm extends PluginFormBase implements ContainerInjectionInterface {
19
20   /**
21    * The block plugin.
22    *
23    * @var \Drupal\Core\Block\BlockPluginInterface
24    */
25   protected $plugin;
26
27   /**
28    * The config factory.
29    *
30    * @var \Drupal\Core\Config\ConfigFactoryInterface
31    */
32   protected $configFactory;
33
34   /**
35    * The current user.
36    *
37    * @var \Drupal\Core\Session\AccountInterface
38    */
39   protected $currentUser;
40
41   /**
42    * SystemBrandingOffCanvasForm constructor.
43    *
44    * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
45    *   The config factory.
46    * @param \Drupal\Core\Session\AccountInterface $current_user
47    *   The current user.
48    */
49   public function __construct(ConfigFactoryInterface $config_factory, AccountInterface $current_user) {
50     $this->configFactory = $config_factory;
51     $this->currentUser = $current_user;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function create(ContainerInterface $container) {
58     return new static(
59       $container->get('config.factory'),
60       $container->get('current_user')
61     );
62   }
63
64   /**
65    * {@inheritdoc}
66    */
67   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
68     $form = $this->plugin->buildConfigurationForm($form, $form_state);
69
70     $form['block_branding']['#type'] = 'details';
71     $form['block_branding']['#weight'] = 10;
72
73     // Unset links to Site Information form, we can make these changes here.
74     unset($form['block_branding']['use_site_name']['#description'], $form['block_branding']['use_site_slogan']['#description']);
75
76     $site_config = $this->configFactory->getEditable('system.site');
77     // Load the immutable config to load the overrides.
78     $site_config_immutable = $this->configFactory->get('system.site');
79     $form['site_information'] = [
80       '#type' => 'details',
81       '#title' => t('Site details'),
82       '#open' => TRUE,
83       '#access' => $this->currentUser->hasPermission('administer site configuration') && !$site_config_immutable->hasOverrides('name') && !$site_config_immutable->hasOverrides('slogan'),
84     ];
85     $form['site_information']['site_name'] = [
86       '#type' => 'textfield',
87       '#title' => t('Site name'),
88       '#default_value' => $site_config->get('name'),
89       '#required' => TRUE,
90     ];
91     $form['site_information']['site_slogan'] = [
92       '#type' => 'textfield',
93       '#title' => t('Slogan'),
94       '#default_value' => $site_config->get('slogan'),
95       '#description' => t("How this is used depends on your site's theme."),
96     ];
97
98     return $form;
99   }
100
101   /**
102    * {@inheritdoc}
103    */
104   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
105     $this->plugin->validateConfigurationForm($form, $form_state);
106   }
107
108   /**
109    * {@inheritdoc}
110    */
111   public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
112     $site_config = $this->configFactory->get('system.site');
113     if (AccessResult::allowedIf(!$site_config->hasOverrides('name') && !$site_config->hasOverrides('slogan'))->isAllowed()) {
114       $site_info = $form_state->getValue('site_information');
115       $this->configFactory->getEditable('system.site')
116         ->set('name', $site_info['site_name'])
117         ->set('slogan', $site_info['site_slogan'])
118         ->save();
119     }
120
121     $this->plugin->submitConfigurationForm($form, $form_state);
122   }
123
124 }