Loading src/ConfigFormBuilder.php +18 −15 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldConfigInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Class ConfigFormAlter. Loading @@ -15,6 +16,8 @@ use Drupal\Core\Session\AccountProxyInterface; */ class ConfigFormBuilder { use StringTranslationTrait; /** * The current user. * Loading Loading @@ -76,26 +79,26 @@ class ConfigFormBuilder { // Prepare group with fields for settings. $form['disable_field'] = [ '#type' => 'details', '#title' => t('Disable Field Settings'), '#title' => $this->t('Disable Field Settings'), '#open' => TRUE, '#tree' => TRUE, '#weight' => 20, 'add' => [ '#type' => 'fieldset', '#title' => t('Disable this field on add content form?'), '#title' => $this->t('Disable this field on add content form?'), ], 'edit' => [ '#type' => 'fieldset', '#title' => t('Disable this field on edit content form?'), '#title' => $this->t('Disable this field on edit content form?'), ], ]; $form['disable_field']['add']['disable'] = [ '#type' => 'select', '#options' => [ 'none' => t('Enable for all users'), 'all' => t('Disable for all users'), 'roles' => t('Disable for certain roles'), 'roles_enable' => t('Enable for certain roles'), 'none' => $this->t('Enable for all users'), 'all' => $this->t('Disable for all users'), 'roles' => $this->t('Disable for certain roles'), 'roles_enable' => $this->t('Enable for certain roles'), ], '#default_value' => !empty($settings['add_disable']) ? $settings['add_disable'] : 'none', '#required' => TRUE, Loading @@ -103,7 +106,7 @@ class ConfigFormBuilder { $form['disable_field']['add']['roles'] = [ '#type' => 'select', '#options' => $role_options, '#title' => t('Enable field on the add content form for next roles:'), '#title' => $this->t('Enable field on the add content form for next roles:'), '#multiple' => TRUE, '#states' => [ 'visible' => [ Loading @@ -124,10 +127,10 @@ class ConfigFormBuilder { $form['disable_field']['edit']['disable'] = [ '#type' => 'select', '#options' => [ 'none' => t('Enable for all users'), 'all' => t('Disable for all users'), 'roles' => t('Disable for certain roles'), 'roles_enable' => t('Enable for certain roles'), 'none' => $this->t('Enable for all users'), 'all' => $this->t('Disable for all users'), 'roles' => $this->t('Disable for certain roles'), 'roles_enable' => $this->t('Enable for certain roles'), ], '#default_value' => !empty($settings['edit_disable']) ? $settings['edit_disable'] : 'none', '#required' => TRUE, Loading @@ -135,7 +138,7 @@ class ConfigFormBuilder { $form['disable_field']['edit']['roles'] = [ '#type' => 'select', '#options' => $role_options, '#title' => t('Disable field on the edit content form for next roles:'), '#title' => $this->t('Disable field on the edit content form for next roles:'), '#multiple' => TRUE, '#states' => [ 'visible' => [ Loading Loading @@ -174,14 +177,14 @@ class ConfigFormBuilder { $add_roles = $form_state->getValue(['disable_field', 'add', 'roles']); $add_option = $form_state->getValue(['disable_field', 'add', 'disable']); if (empty($add_roles) && in_array($add_option, ['roles', 'roles_enable'])) { $form_state->setErrorByName('disable_field][add][roles', t('Please, choose at least one role.')); $form_state->setErrorByName('disable_field][add][roles', $this->t('Please, choose at least one role.')); } // Check if the edit roles field contains values when required. $edit_roles = $form_state->getValue(['disable_field', 'edit', 'roles']); $edit_option = $form_state->getValue(['disable_field', 'edit', 'disable']); if (empty($edit_roles) && in_array($edit_option, ['roles', 'roles_enable'])) { $form_state->setErrorByName('disable_field][edit][roles', t('Please, choose at least one role.')); $form_state->setErrorByName('disable_field][edit][roles', $this->t('Please, choose at least one role.')); } } Loading Loading
src/ConfigFormBuilder.php +18 −15 Original line number Diff line number Diff line Loading @@ -7,6 +7,7 @@ use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Field\FieldConfigInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Session\AccountProxyInterface; use Drupal\Core\StringTranslation\StringTranslationTrait; /** * Class ConfigFormAlter. Loading @@ -15,6 +16,8 @@ use Drupal\Core\Session\AccountProxyInterface; */ class ConfigFormBuilder { use StringTranslationTrait; /** * The current user. * Loading Loading @@ -76,26 +79,26 @@ class ConfigFormBuilder { // Prepare group with fields for settings. $form['disable_field'] = [ '#type' => 'details', '#title' => t('Disable Field Settings'), '#title' => $this->t('Disable Field Settings'), '#open' => TRUE, '#tree' => TRUE, '#weight' => 20, 'add' => [ '#type' => 'fieldset', '#title' => t('Disable this field on add content form?'), '#title' => $this->t('Disable this field on add content form?'), ], 'edit' => [ '#type' => 'fieldset', '#title' => t('Disable this field on edit content form?'), '#title' => $this->t('Disable this field on edit content form?'), ], ]; $form['disable_field']['add']['disable'] = [ '#type' => 'select', '#options' => [ 'none' => t('Enable for all users'), 'all' => t('Disable for all users'), 'roles' => t('Disable for certain roles'), 'roles_enable' => t('Enable for certain roles'), 'none' => $this->t('Enable for all users'), 'all' => $this->t('Disable for all users'), 'roles' => $this->t('Disable for certain roles'), 'roles_enable' => $this->t('Enable for certain roles'), ], '#default_value' => !empty($settings['add_disable']) ? $settings['add_disable'] : 'none', '#required' => TRUE, Loading @@ -103,7 +106,7 @@ class ConfigFormBuilder { $form['disable_field']['add']['roles'] = [ '#type' => 'select', '#options' => $role_options, '#title' => t('Enable field on the add content form for next roles:'), '#title' => $this->t('Enable field on the add content form for next roles:'), '#multiple' => TRUE, '#states' => [ 'visible' => [ Loading @@ -124,10 +127,10 @@ class ConfigFormBuilder { $form['disable_field']['edit']['disable'] = [ '#type' => 'select', '#options' => [ 'none' => t('Enable for all users'), 'all' => t('Disable for all users'), 'roles' => t('Disable for certain roles'), 'roles_enable' => t('Enable for certain roles'), 'none' => $this->t('Enable for all users'), 'all' => $this->t('Disable for all users'), 'roles' => $this->t('Disable for certain roles'), 'roles_enable' => $this->t('Enable for certain roles'), ], '#default_value' => !empty($settings['edit_disable']) ? $settings['edit_disable'] : 'none', '#required' => TRUE, Loading @@ -135,7 +138,7 @@ class ConfigFormBuilder { $form['disable_field']['edit']['roles'] = [ '#type' => 'select', '#options' => $role_options, '#title' => t('Disable field on the edit content form for next roles:'), '#title' => $this->t('Disable field on the edit content form for next roles:'), '#multiple' => TRUE, '#states' => [ 'visible' => [ Loading Loading @@ -174,14 +177,14 @@ class ConfigFormBuilder { $add_roles = $form_state->getValue(['disable_field', 'add', 'roles']); $add_option = $form_state->getValue(['disable_field', 'add', 'disable']); if (empty($add_roles) && in_array($add_option, ['roles', 'roles_enable'])) { $form_state->setErrorByName('disable_field][add][roles', t('Please, choose at least one role.')); $form_state->setErrorByName('disable_field][add][roles', $this->t('Please, choose at least one role.')); } // Check if the edit roles field contains values when required. $edit_roles = $form_state->getValue(['disable_field', 'edit', 'roles']); $edit_option = $form_state->getValue(['disable_field', 'edit', 'disable']); if (empty($edit_roles) && in_array($edit_option, ['roles', 'roles_enable'])) { $form_state->setErrorByName('disable_field][edit][roles', t('Please, choose at least one role.')); $form_state->setErrorByName('disable_field][edit][roles', $this->t('Please, choose at least one role.')); } } Loading