Im doing a module to generate form validation on field length, first i display a select of availble content types. When the target content type is selected an ajax request display the end of the form with an another select wich contain avaibles fields of the content type selected before. When the form is submited form_state values of the step 2 of the form are empty. When i display the var_dump in the submitform, only 'content_type' has a value.
The code here
class OpenLimitLengthSettingsForm extends ConfigFormBase{
const PLACEHOLDERVALUE = '_none';
const LIMITABLEFIELDTYPES = ['integer', 'string_long', 'string', 'text_with_summary', 'text_long', 'text'];
const SKIPPEDFIELDS = ['nid', 'vid'];
/**
* Gets the configuration names that will be editable.
*
* @return array
* An array of configuration object names that are editable if called in
* conjunction with the trait's config() method.
*/
protected function getEditableConfigNames()
{
return array("open_limit_length.config");
}
/**
* Returns a unique string identifying the form.
*
* The returned ID should be a unique string that can be a valid PHP function
* name, since it's used in hook implementation names such as
* hook_form_FORM_ID_alter().
*
* @return string
* The unique string identifying the form.
*/
public function getFormId()
{
return "open-sendmail-settings-form";
}
public function buildForm(array $form, FormStateInterface $form_state)
{
$config = $this->config('open_limit_length.config');
//Loading content types
$contentTypes = $this->getAllContentTypes();
$contentTypeOptions = [self::PLACEHOLDERVALUE => $this->t("-- Choisir un type de contenu --")];
foreach($contentTypes as $contentType){
$contentTypeOptions[$contentType->get('type')] = $contentType->get('name');
}
//Building form's render array
$form["title"] = [
"#markup" => "<h2>".$this->t("Limitation de taille de champ")."</h2>"
];
$form["add_limit"] = [
'#type' => 'fieldset',
'#title' => $this->t("Ajouter une limitation"),
'#prefix' => '<div id="open_limit_length_mutistep_form">',
'#suffix' => '</div>'
];
$form["add_limit"]['content_type'] = [
'#type' => 'select',
'#title' => $this->t("Type de contenu"),
'#options' => $contentTypeOptions,
'#ajax' => [
'callback' => '::getFieldsOfContentType',
'wrapper' => 'open_limit_length_mutistep_form'
]
];
$form['add_limit']['available_fields'] = [
'#type' => 'select',
'#title' => $this->t("Champ à limiter"),
'#options' => '',
'#access' => false
];
$form['add_limit']['field_limit'] = [
'#type' => 'textfield',
'#title' => $this->t('Taille maximum'),
'#attributes' => [
' type' => 'number'
],
'#access' => false
];
$form['add_limit']['text_limit'] = [
'#type' => 'textarea',
'#title' => $this->t("Message d'erreur en cas de dépassement de la limite"),
'#suffix' => '<div><i>'.$this->t("Exemple : Le champ {{field_name}} est limité à {{nb_max_car}}, taille actuelle {{current_nb_car}}").'</i></div>',
'#access' => false,
];
$form['add_limit']['submit'] = [
'#type' => 'submit',
'#value' => $this->t("Enregistrer"),
'#access' => false,
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state)
{
var_dump($form_state->getValues());
exit();
}
private function getAllContentTypes(){
$types = \Drupal::entityTypeManager()
->getStorage('node_type')
->loadMultiple();
return $types;
}
$selectedContentType = $form_state->getValues()['content_type'];
$entityManager = \Drupal::service('entity_field.manager');
if($selectedContentType != self::PLACEHOLDERVALUE){
$fields = $entityManager->getFieldDefinitions('node', $selectedContentType);
$optionsFields = [self::PLACEHOLDERVALUE => '-- Choisir un champ --'];
/** @var \Drupal\Core\Field\BaseFieldDefinition $field */
foreach($fields as $field){
if(in_array($field->getType(), self::LIMITABLEFIELDTYPES) && !in_array($field->getName(), self::SKIPPEDFIELDS)){
$optionsFields[$field->getName()] = $field->getLabel();
}
}
$form['add_limit']['available_fields']['#options'] = $optionsFields;
$form['add_limit']['available_fields']['#access'] = true;
$form['add_limit']['field_limit']['#access'] = true;
$form['add_limit']['text_limit']['#access'] = true;
$form['add_limit']['submit']['#access'] = true;
$form_state->setRebuild();
$response->addCommand(new ReplaceCommand('#open_limit_length_mutistep_form', $form['add_limit']));
}else{
$response->addCommand(new AlertCommand("Veuillez selectionner un type de contenu valide"));
}
return $response;}}
I'm searching an issue since this morning but nothing solved my problem. Thanks for your help