You are here

protected function SocialBlockBase::blockFormElementStates in Social Feed 8

Apply state based on the override field.

Parameters

array $form: blockFormElementStates.

3 calls to SocialBlockBase::blockFormElementStates()
FacebookPostBlock::blockForm in src/Plugin/Block/FacebookPostBlock.php
Returns the configuration form elements specific to this block plugin.
InstagramPostBlock::blockForm in src/Plugin/Block/InstagramPostBlock.php
Returns the configuration form elements specific to this block plugin.
TwitterPostBlock::blockForm in src/Plugin/Block/TwitterPostBlock.php
Returns the configuration form elements specific to this block plugin.

File

src/Plugin/Block/SocialBlockBase.php, line 94

Class

SocialBlockBase
Abstract base class SocialBlockBase.

Namespace

Drupal\socialfeed\Plugin\Block

Code

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) {

    // When global config is invalid, and a non-privileged user still has
    // sufficient access to place this block, add a warning message and a
    // validator to ensure the form cannot be submitted in this state.
    $form['configuration_warning'] = [
      '#type' => 'status_messages',
      '#weight' => -1000,
      '#element_validate' => [
        [
          $this,
          'invalidConfigurationValidator',
        ],
      ],
    ];
    $this
      ->messenger()
      ->addWarning($this
      ->getInvalidConfigurationWarning());
  }
}