Error: Cannot use object of type Drupal\Core\Form\FormState as array in Drupal

Главные вкладки

Аватар пользователя loup54 loup54 20 января 2019 в 11:58

Доброго времени суток, при создании конфигурации по умолчанию в кастомном модуле получаю ошибку описанную в заголовке, только естественно с указанием на файл и строку, в частности 85.
Вот код:

<?php

/*
 * @file  
 * Contains \Drupal\user_warning\Form\UserWarningConfigForm
 * 
 * Provides a configuration form for our User Warning module. By
 * extending the ConfigFormBase class we automatically have access to
 * Drupal's Configuration Management System.
 */

namespace Drupal\user_warning\Form;

use 

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

/**
 * Configure the settings for User Warn
 */
class UserWarningConfigForm extends ConfigFormBase {
  
  
/**
   * Specify the form ID.
   */
  
public function getFormId() {
    return 
'user_warning_config_form';
  }

  

/**
   * Build the actual form.
   */
  
public function buildForm(array $formFormStateInterface $form_state) {
    
// Retrieve the stored configuration settings
    
$config $this->config('user_warning.settings');

    

// Text field for the e-mail subject.
    
$form['user_warning_e-mail_subject'] = array(
      
'#type' => 'textfield',
      
'#title' => t('Warning e-mail subject'),
      
'#description' => t('The subject of the e-mail which will be sent to users.'),
      
'#size' => 40,
      
'#maxlength' => 120,
      
'#required' => TRUE,
      
'#default_value'  =>  $config->get('email.subject')
    );

    

// Textarea for the body of the e-mail.
    
$form['user_warning_e-mail_text'] = array(
      
'#type' => 'textarea',
      
'#rows' => 10,
      
'#columns' => 40,
      
'#title' => t('Warning e-mail text'),
      
'#required' => TRUE,
      
'#description' => t('The text of the e-mail which will be sent to users.'),
      
'#default_value'  =>  $config->get('email.body')
    );
    
// Checkbox to indicate if admin should be sent a Bcc on e-mails.
    
$form['user_warning_bcc'] = array(
      
'#type' => 'checkbox',
      
'#title' => t('BCC admin on all e-mails'),
      
'#default_value'  =>  $config->get('bcc'),
      
'#description' => t("Indicates whether the admin user (as set in site configuration) should be sent on all warning e-mails."),
    );
    
    return 
parent::buildForm($form$form_state);
  }
  
  
/**
   * Validates the form - here you can specify your custom validation
   * rules.
   */
  
public function validateForm(array &$formFormStateInterface $form_state) {
    
parent::validateForm($form$form_state);    
  }
  
  
/**
   * Submits the form and saves the configuration settings.
   */
  
public function submitForm(array &$formFormStateInterface $form_state) {
    
    
parent::submitForm($form$form_state);
    
    
// Save the configuration settings
    
$this->config('user_warning.settings')
      ->
set('email.subject'$form_state['values']['user_warning_e-mail_subject'])
      ->
set('email.body'$form_state['values']['user_warning_e-mail_text'])
      ->
set('bcc'$form_state['values']['user_warning_bcc'])
      ->
save();  
  }

  protected function 

getEditableConfigNames() {
    return 
'user_warning.settings';
  }
}
?>

Ошибка:
Error: Cannot use object of type Drupal\Core\Form\FormState as array in Drupal\user_warning\Form\UserWarningConfigForm->submitForm() (line 85 of /home/loup54/public_html/dru.loc/modules/custom/user_warning/src/Form/UserWarningConfigForm.php)
Помогите найти ошибку

Лучший ответ

Комментарии

Аватар пользователя loup54 loup54 20 января 2019 в 14:29

Спасибо за наводку, да действительно нужно использовать getValue, вот как я решил, может кому то пригодится:

<?php

/*
 * @file  
 * Contains \Drupal\user_warning\Form\UserWarningConfigForm
 * 
 * Provides a configuration form for our User Warn module. By
 * extending the ConfigFormBase class we automatically have access to
 * Drupal's Configuration Management System.
 */

namespace Drupal\user_warning\Form;

use 

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

/**
 * Configure the settings for User Warning
 */
class UserWarningConfigForm extends ConfigFormBase {
  
  
/**
   * Specify the form ID.
   */
  
public function getFormId() {
    return 
'user_warning_config_form';
  }

  

/**
   * Build the actual form.
   */
  
public function buildForm(array $formFormStateInterface $form_state) {
    
// Retrieve the stored configuration settings
    
$config $this->config('user_warning.settings');

    

// Text field for the e-mail subject.
    
$form['user_warning_e-mail_subject'] = array(
      
'#type' => 'textfield',
      
'#title' => t('Warning e-mail subject'),
      
'#description' => t('The subject of the e-mail which will be sent to users.'),
      
'#size' => 40,
      
'#maxlength' => 120,
      
'#required' => TRUE,
      
'#default_value'  =>  $config->get('email.subject')
    );

    

// Textarea for the body of the e-mail.
    
$form['user_warning_e-mail_text'] = array(
      
'#type' => 'textarea',
      
'#rows' => 10,
      
'#columns' => 40,
      
'#title' => t('Warning e-mail text'),
      
'#required' => TRUE,
      
'#description' => t('The text of the e-mail which will be sent to users.'),
      
'#default_value'  =>  $config->get('email.body')
    );
    
// Checkbox to indicate if admin should be sent a Bcc on e-mails.
    
$form['user_warning_bcc'] = array(
      
'#type' => 'checkbox',
      
'#title' => t('BCC admin on all e-mails'),
      
'#default_value'  =>  $config->get('bcc'),
      
'#description' => t("Indicates whether the admin user (as set in site configuration) should be sent on all warning e-mails."),
    );
    
    return 
parent::buildForm($form$form_state);
  }
  
  
/**
   * Validates the form - here you can specify your custom validation
   * rules.
   */
  
public function validateForm(array &$formFormStateInterface $form_state) {
    
parent::validateForm($form$form_state);    
  }
  
  
/**
   * Submits the form and saves the configuration settings.
   */
  
public function submitForm(array &$formFormStateInterface $form_state) {
    
    
    
    
// Save the configuration settings
    
$this->configFactory->getEditable('user_warning.settings')
      ->
set('email.subject'$form_state->getValue('user_warning_e-mail_subject'))
      ->
set('email.body'$form_state->getValue('user_warning_e-mail_text'))
      ->
set('bcc'$form_state->getValue('user_warning_bcc'))
      ->
save();  
      
      
parent::submitForm($form$form_state);
  }

  protected function 

getEditableConfigNames() {
    return array(
'user_warning.settings');;
  }
}
?>

А вот ссылка на актуальную информацию по созданию форм конфигурации:
https://www.drupal.org/docs/8/api/configuration-api/working-with-configuration-forms