ump-8.x-1.0-alpha1/src/Form/UmpRuleCreateForm.php
src/Form/UmpRuleCreateForm.php
<?php
namespace Drupal\ump\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
/**
* UMP rule created/edit form..
*/
class UmpRuleCreateForm extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'ump_create_rule_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, $rule_key = NULL) {
if ($rule_key != NULL && \Drupal::config('ump.settings')->get($rule_key) != NULL) {
// Load rule settings.
$rule = \Drupal::config('ump.settings')->get($rule_key);
$form_state->setStorage(['rule_key' => $rule_key]);
$select_role = $rule["select_role"];
$allow_to_create_enable = $rule["allow_to_create_enable"];
$allow_to_create_role = $rule["allow_to_create_role"];
$allow_to_create_overtake = $rule["allow_to_create_overtake"];
$allow_to_create_overtake_field = $rule["allow_to_create_overtake_field"];
$allow_to_view_enable = $rule["allow_to_view_enable"];
$allow_to_view_role = $rule["allow_to_view_role"];
$allow_to_view_which = $rule["allow_to_view_which"];
$allow_to_view_common_field = $rule["allow_to_view_common_field"];
$allow_to_edit_enable = $rule["allow_to_edit_enable"];
$allow_to_edit_role = $rule["allow_to_edit_role"];
$allow_to_edit_which = $rule["allow_to_edit_which"];
$allow_to_edit_common_field = $rule["allow_to_edit_common_field"];
$allow_to_delete_enable = $rule["allow_to_delete_enable"];
$allow_to_delete_role = $rule["allow_to_delete_role"];
$allow_to_delete_which = $rule["allow_to_delete_which"];
$allow_to_delete_common_field = $rule["allow_to_delete_common_field"];
}
else {
$select_role = NULL;
$allow_to_create_enable = NULL;
$allow_to_create_role = NULL;
$allow_to_create_overtake = NULL;
$allow_to_create_overtake_field = NULL;
$allow_to_view_enable = NULL;
$allow_to_view_role = NULL;
$allow_to_view_which = NULL;
$allow_to_view_common_field = NULL;
$allow_to_edit_enable = NULL;
$allow_to_edit_role = NULL;
$allow_to_edit_which = NULL;
$allow_to_edit_common_field = NULL;
$allow_to_delete_enable = NULL;
$allow_to_delete_role = NULL;
$allow_to_delete_which = NULL;
$allow_to_delete_common_field = NULL;
}
// Get the users roles defined and the custom user fields defined.
$get_roles = ump_get_user_roles();
$user_fields = ump_get_user_user_fields();
$form['rule_key'] = [
'#type' => 'textfield',
'#title' => $this->t('Rule machine name'),
'#description' => $this->t('Only alphanumeric characters and undescore are allowed.'),
'#default_value' => $rule_key,
'#disabled' => $rule_key == NULL ? FALSE : TRUE,
];
$form['select_role'] = [
'#type' => 'select',
'#title' => $this->t('Select role'),
'#options' => $get_roles,
'#default_value' => $select_role,
];
$form['allow_to_create'] = [
'#type' => 'fieldset',
'#title' => $this->t('Allow to create'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$form['allow_to_create']['allow_to_create_enable'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable'),
'#default_value' => $allow_to_create_enable,
];
$form['allow_to_create']['allow_to_create_role'] = [
'#type' => 'select',
'#title' => $this->t('Select role'),
'#options' => $get_roles,
'#default_value' => $allow_to_create_role,
'#states' => [
'disabled' => [
':input[name="allow_to_create_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_create']['allow_to_create_overtake'] = [
'#type' => 'checkbox',
'#title' => $this->t('Overtake commond field'),
'#default_value' => $allow_to_create_overtake,
'#states' => [
'disabled' => [
':input[name="allow_to_create_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_create']['allow_to_create_overtake_field'] = [
'#type' => 'select',
'#title' => $this->t('Select field'),
'#options' => $user_fields,
'#default_value' => $allow_to_create_overtake_field,
'#states' => [
'visible' => [
':input[name="allow_to_create_overtake"]' => ['checked' => TRUE],
],
'disabled' => [
':input[name="allow_to_create_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_view'] = [
'#type' => 'fieldset',
'#title' => $this->t('Allow to view'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$form['allow_to_view']['allow_to_view_enable'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable'),
'#default_value' => $allow_to_view_enable,
];
$form['allow_to_view']['allow_to_view_role'] = [
'#type' => 'select',
'#title' => $this->t('Select role'),
'#options' => $get_roles,
'#default_value' => $allow_to_view_role,
'#states' => [
'disabled' => [
':input[name="allow_to_view_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_view']['allow_to_view_which'] = [
'#type' => 'select',
'#title' => $this->t('Select field'),
'#options' => [
'all' => 'All of them',
'created_by_user' => 'Created by the user',
'if_share_common_field' => 'If share common field',
],
'#default_value' => $allow_to_view_which,
'#states' => [
'disabled' => [
':input[name="allow_to_view_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_view']['allow_to_view_common_field'] = [
'#type' => 'select',
'#title' => $this->t('Select field'),
'#options' => $user_fields,
'#default_value' => $allow_to_view_common_field,
'#states' => [
'visible' => [
':input[name="allow_to_view_which"]' => ['value' => 'if_share_common_field'],
],
'disabled' => [
':input[name="allow_to_view_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_edit'] = [
'#type' => 'fieldset',
'#title' => $this->t('Allow to edit'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$form['allow_to_edit']['allow_to_edit_enable'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable'),
'#default_value' => $allow_to_edit_enable,
];
$form['allow_to_edit']['allow_to_edit_role'] = [
'#type' => 'select',
'#title' => $this->t('Select role'),
'#options' => $get_roles,
'#default_value' => $allow_to_edit_role,
'#states' => [
'disabled' => [
':input[name="allow_to_edit_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_edit']['allow_to_edit_which'] = [
'#type' => 'select',
'#title' => $this->t('Select field'),
'#options' => [
'all' => 'All of them',
'created_by_user' => 'Created by the user',
'if_share_common_field' => 'If share common field',
],
'#default_value' => $allow_to_edit_which,
'#states' => [
'disabled' => [
':input[name="allow_to_edit_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_edit']['allow_to_edit_common_field'] = [
'#type' => 'select',
'#title' => $this->t('Select field'),
'#options' => $user_fields,
'#default_value' => $allow_to_edit_common_field,
'#states' => [
'visible' => [
':input[name="allow_to_edit_which"]' => ['value' => 'if_share_common_field'],
],
'disabled' => [
':input[name="allow_to_edit_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_delete'] = [
'#type' => 'fieldset',
'#title' => $this->t('Allow to delete'),
'#collapsible' => FALSE,
'#collapsed' => FALSE,
];
$form['allow_to_delete']['allow_to_delete_enable'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable'),
'#default_value' => $allow_to_delete_enable,
];
$form['allow_to_delete']['allow_to_delete_role'] = [
'#type' => 'select',
'#title' => $this->t('Select role'),
'#options' => $get_roles,
'#default_value' => $allow_to_delete_role,
'#states' => [
'disabled' => [
':input[name="allow_to_delete_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_delete']['allow_to_delete_which'] = [
'#type' => 'select',
'#title' => $this->t('Select field'),
'#options' => [
'all' => 'All of them',
'created_by_user' => 'Created by the user',
'if_share_common_field' => 'If share common field',
],
'#default_value' => $allow_to_delete_which,
'#states' => [
'disabled' => [
':input[name="allow_to_delete_enable"]' => ['checked' => FALSE],
],
],
];
$form['allow_to_delete']['allow_to_delete_common_field'] = [
'#type' => 'select',
'#title' => $this->t('Select field'),
'#options' => $user_fields,
'#default_value' => $allow_to_delete_common_field,
'#states' => [
'visible' => [
':input[name="allow_to_delete_which"]' => ['value' => 'if_share_common_field'],
],
'disabled' => [
':input[name="allow_to_delete_enable"]' => ['checked' => FALSE],
],
],
];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save'),
];
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
// Check if the rule_key contains only alphanumeric characters.
if (!preg_match('/^[a-zA-Z0-9_]+$/', $form_state->getValue("rule_key"))) {
$form_state->setErrorByName('rule_key', $this->t('Only alphanumeric characters and undescore are allowed.'));
}
// Check if the rule_key is not existing already.
$storage = $form_state->getStorage();
if (!isset($storage['rule_key'])) {
if (\Drupal::config('ump.settings')->get('ump_CRUD_rule_' . $form_state->getValue("rule_key")) != NULL) {
$form_state->setErrorByName('rule_key', $this->t('Rule machine name already is use, please type another one.'));
}
}
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$rule = [
"select_role" => $form_state->getValue('select_role'),
"allow_to_create_enable" => $form_state->getValue('allow_to_create_enable'),
"allow_to_create_role" => $form_state->getValue('allow_to_create_role'),
"allow_to_create_overtake" => $form_state->getValue('allow_to_create_overtake') ,
"allow_to_create_overtake_field" => $form_state->getValue('allow_to_create_overtake_field'),
"allow_to_view_enable" => $form_state->getValue('allow_to_view_enable'),
"allow_to_view_role" => $form_state->getValue('allow_to_view_role'),
"allow_to_view_which" => $form_state->getValue('allow_to_view_which'),
"allow_to_view_common_field" => $form_state->getValue('allow_to_view_common_field'),
"allow_to_edit_enable" => $form_state->getValue('allow_to_edit_enable'),
"allow_to_edit_role" => $form_state->getValue('allow_to_edit_role'),
"allow_to_edit_which" => $form_state->getValue('allow_to_edit_which'),
"allow_to_edit_common_field" => $form_state->getValue('allow_to_edit_common_field'),
"allow_to_delete_enable" => $form_state->getValue('allow_to_delete_enable'),
"allow_to_delete_role" => $form_state->getValue('allow_to_delete_role'),
"allow_to_delete_which" => $form_state->getValue('allow_to_delete_which'),
"allow_to_delete_common_field" => $form_state->getValue('allow_to_delete_common_field'),
];
$storage = $form_state->getStorage();
if (isset($storage['rule_key'])) {
// Update the rule.
$rule_key = $storage['rule_key'];
}
else {
// Create a new rule.
$rule_key = 'ump_CRUD_rule_' . $form_state->getValue("rule_key");
}
\Drupal::service('config.factory')->getEditable('ump.settings')->set($rule_key, $rule)->save();
$form_state->setRedirect('ump.settings');
// Rebuild the router.
\Drupal::service('router.builder')->rebuild();
}
}
