inline_feedback-1.0.x-dev/src/Form/InlineFeedbackCustomSettingsForm.php

src/Form/InlineFeedbackCustomSettingsForm.php
<?php

declare(strict_types=1);

namespace Drupal\inline_feedback\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\user\Entity\Role;

class InlineFeedbackCustomSettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return ['inline_feedback.settings'];
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'inline_feedback_settings_form';
  }

  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this->config('inline_feedback.settings');

    // allowed roles
    $role_storage = \Drupal::entityTypeManager()->getStorage('user_role');
    $roles = $role_storage->loadMultiple();
    $role_options = [];
    foreach ($roles as $role_id => $role) {
      $role_options[$role_id] = $role->label();
    }

    $form['$allowed_roles_to_see'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Roles allowed to view inline feedbacks'),
      '#default_value' => $config->get('$allowed_roles_to_see') ?? [],
      '#options' => $role_options,
    ];

    $form['allowed_roles_to_delete'] = [
      '#type' => 'checkboxes',
      '#title' => $this->t('Roles allowed to delete inline feedbacks'),
      '#default_value' => $config->get('allowed_roles_to_delete') ?? [],
      '#options' => $role_options,
    ];

    // colors
    $form['style'] = [
      '#type' => 'details',
      '#title' => $this->t('Feedback visual style'),
      '#open' => TRUE,
    ];

    $form['style']['background_color'] = [
      '#type' => 'color',
      '#title' => $this->t('Feedback background color'),
      '#default_value' => $config->get('background_color') ?? '#2b3c82',
    ];

    $form['style']['text_color'] = [
      '#type' => 'color',
      '#title' => $this->t('Feedback text color'),
      '#default_value' => $config->get('text_color') ?? '#FFFFFF',
    ];

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

  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->config('inline_feedback.settings')
      ->set('$allowed_roles_to_see', array_filter($form_state->getValue('$allowed_roles_to_see')))
      ->set('allowed_roles_to_delete', array_filter($form_state->getValue('allowed_roles_to_delete')))
      ->set('background_color', $form_state->getValue('background_color'))
      ->set('text_color', $form_state->getValue('text_color'))
      ->save();

    parent::submitForm($form, $form_state);
  }
}

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

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