easy_social-8.x-3.x-dev/src/Form/EasySocialSettingsForm.php

src/Form/EasySocialSettingsForm.php
<?php

namespace Drupal\easy_social\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Configure Easy Social global settings for this site.
 */
final class EasySocialSettingsForm extends ConfigFormBase {

  /**
   * Config settings.
   *
   * @var string
   */
  private const SETTINGS = 'easy_social.settings';

  /**
   * {@inheritdoc}
   */
  public function getFormId(): string {
    return 'easy_social_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames(): array {
    return [self::SETTINGS];
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $type = 'new'): array {
    $config = $this->config(self::SETTINGS);

    if ($widgets = easy_social_get_widgets()) {
      $widget_options = array_map(static fn(array $widget) => $widget['name'], $widgets);

      $form['widgets'] = [
        '#type' => 'checkboxes',
        '#title' => t('Enabled widgets'),
        // @todo decide whether we're going to enable overrides for specific
        // entities and, if so, update the description.
        '#description' => t('Select the social sharing widgets you would like to enable globally..'),
        '#default_value' => (array) $config->get('global.widgets'),
        '#options' => $widget_options,
      ];

      $form['advanced'] = [
        '#type' => 'fieldset',
        '#title' => t('Advanced settings'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
      ];

      $form['advanced']['async'] = [
        '#type' => 'checkbox',
        '#title' => t('Load javascript asynchronously'),
        '#description' => t('This is recommended for performance purposes.'),
        '#default_value' => $config->get('global.async'),
      ];
    }
    else {
      $form['widgets_empty'] = [
        '#prefix' => '<div class="empty">',
        '#suffix' => '</div>',
        '#markup' => t('There are no widgets defined.'),
      ];
    }

    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state): void {
    parent::submitForm($form, $form_state);

    $config = $this->config(self::SETTINGS);

    $config
      ->set('global.widgets', $form_state->getValue('widgets'))
      ->set('global.async', $form_state->getValue('async'))
      ->save();
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc