View source
<?php
namespace Drupal\socialfeed\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
abstract class SocialBlockBase extends BlockBase {
protected $currentUser;
public function blockForm($form, FormStateInterface $form_state) {
$form = parent::blockForm($form, $form_state);
$settings = $this
->getConfiguration();
$access = $this->currentUser
->hasPermission('administer socialfeed');
$form['override'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Customize Feed'),
'#default_value' => isset($settings['override']) ? $settings['override'] : FALSE,
'#access' => $access,
];
$form['overrides'] = [
'#type' => 'fieldset',
'#title' => $this
->t('Feed Configuration'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
'#access' => $access,
'#states' => [
'invisible' => [
':input[name="settings[override]"]' => [
'checked' => FALSE,
],
],
],
];
return $form;
}
public function blockSubmit($form, FormStateInterface $form_state) {
$values = $form_state
->getValues();
$this->configuration['override'] = $values['override'];
foreach ($values['overrides'] as $key => $value) {
$this->configuration[$key] = $value;
}
}
public function invalidConfigurationValidator($element, FormStateInterface $form_state) {
$form_state
->setErrorByName('configuration_warning', $this
->t('This block cannot be placed.'));
}
public function getSetting($setting_name) {
$block_settings = $this
->getConfiguration();
return $block_settings['override'] ? $block_settings[$setting_name] : $this->config
->get($setting_name);
}
protected function blockFormElementStates(array &$form) {
$global_config = $this->config;
$privileged_user = $this->currentUser
->hasPermission('administer socialfeed');
$config_is_incomplete = FALSE;
foreach ($form['overrides'] as $key => $val) {
if (strrpos($key, '#') === 0) {
continue;
}
if (isset($form['overrides'][$key]['#states']['required'])) {
continue;
}
if (isset($form['overrides'][$key]['#markup'])) {
continue;
}
$form['overrides'][$key]['#states']['required'] = [
':input[name="settings[override]"]' => [
'checked' => TRUE,
],
];
$config_is_incomplete = $config_is_incomplete || empty($global_config
->get($key));
}
if ($config_is_incomplete) {
$form['override']['#disabled'] = TRUE;
$form['override']['#default_value'] = 1;
$form['override']['#description'] = $this
->t('To disable this option, configure default values at @admin', [
'@admin' => Url::fromRoute('socialfeed.configuration')
->toString(),
]);
}
if ($config_is_incomplete && !$privileged_user) {
$form['configuration_warning'] = [
'#type' => 'status_messages',
'#weight' => -1000,
'#element_validate' => [
[
$this,
'invalidConfigurationValidator',
],
],
];
$this
->messenger()
->addWarning($this
->getInvalidConfigurationWarning());
}
}
public function getInvalidConfigurationWarning() {
return $this
->t('Social media feed configuration is missing or is incomplete. Please contact your site administrator.');
}
protected function defaultSettingValue($setting_name) {
$settings = $this
->getConfiguration();
return isset($settings[$setting_name]) ? $settings[$setting_name] : $this->config
->get($setting_name);
}
}