function opigno_module_form_alter in Opigno module 8
Same name and namespace in other branches
- 3.x opigno_module.module \opigno_module_form_alter()
Implements hook_form_alter().
File
- ./
opigno_module.module, line 249 - Contains opigno_module.module.
Code
function opigno_module_form_alter(&$form, FormStateInterface &$form_state, $form_id) {
$build_info = $form_state
->getBuildInfo();
$route_name = \Drupal::routeMatch()
->getRouteName();
$moduleHandler = \Drupal::service('module_handler');
// Alter taxonomy term form for vocabulary 'Skills'.
if ($moduleHandler
->moduleExists('opigno_skills_system') && $form_id == 'taxonomy_term_skills_form' && method_exists($build_info['callback_object'], 'getEntity')) {
$entity = $build_info['callback_object']
->getEntity();
$form['relations']['#open'] = TRUE;
$form['relations']['#type'] = 'fieldset';
$form['relations']['parent']['#multiple'] = FALSE;
$form['relations']['#title'] = t('Choose parent tree or parent skill');
$form['relations']['#weight'] = 2;
unset($form['relations']['parent']['#title']);
unset($form["relations"]["weight"]);
$form['field_minimum_score']['widget'][0]['value']['#default_value'] = 100;
$form['field_minimum_count_of_answers']['widget'][0]['value']['#default_value'] = 2;
// Do custom validation first.
array_unshift($form['#validate'], '_skills_term_validation');
// Check if it is a target skill.
$target_skill = $entity
->get('parent')
->getValue()[0]['target_id'] == 0 ? TRUE : FALSE;
if ($entity
->isNew()) {
$description = t('Check this option if you are creating a new skill tree, with a target skill on top of it.
You will then be able to add several skills to this tree and organize them with a hierarchy.
This target skill can be used in modules using skill system,
so that the module will check that users master all the skills in the related skill tree.
But you won’t be able to assign this skill to activities (only activities that are not target skills can be assigned to activities).');
$form['target_skill'] = [
'#title' => t('This is target skill'),
'#description' => $description,
'#type' => 'checkbox',
'#default_value' => 0,
];
}
if ($target_skill && !$entity
->isNew()) {
// Hide fields with minimum score and minimum count of answers for target skill.
if (isset($form['field_minimum_count_of_answers'])) {
$form['field_minimum_count_of_answers']['#access'] = FALSE;
}
if (isset($form['field_minimum_score'])) {
$form['field_minimum_score']['#access'] = FALSE;
}
if (isset($form['field_level_names'])) {
$form['field_level_names']['#access'] = FALSE;
}
$form['target_skill'] = [
'#title' => t('This is target skill'),
'#type' => 'checkbox',
'#default_value' => 1,
'#disabled' => TRUE,
];
}
else {
$values = $form_state
->getValues();
$ajax_wrapper = 'skill-level-names';
unset($form['field_level_names']['widget']['add_more']);
$form['field_level_names']['#prefix'] = '<div id="skill-level-names">';
$form['field_level_names']['#suffix'] = '</div>';
$form['field_minimum_score']['#states'] = [
'visible' => [
':input[name="target_skill"]' => [
'checked' => FALSE,
],
],
];
$form['field_level_names']['#states'] = [
'visible' => [
':input[name="target_skill"]' => [
'checked' => FALSE,
],
],
];
$form['field_minimum_count_of_answers']['#states'] = [
'visible' => [
':input[name="target_skill"]' => [
'checked' => FALSE,
],
],
];
$form['relations']['#states'] = [
'visible' => [
':input[name="target_skill"]' => [
'checked' => FALSE,
],
],
];
if (!empty($values)) {
$max_delta = $values['count_of_levels'] - 1;
$default_option = $form['field_level_names']['widget'][0];
for ($i = 0; $i <= $max_delta; $i++) {
if (empty($values['field_level_names'][$i])) {
$form['field_level_names']['widget'][$i] = $default_option;
$form['field_level_names']['widget'][$i]['value']['#default_value'] = 'Level ' . ($i + 1);
$form['field_level_names']['widget'][$i]['#delta'] = $i;
$form['field_level_names']['widget'][$i]['#weight'] = $i;
$form['field_level_names']['widget'][$i]['_weight']['#default_value'] = $i;
}
}
foreach ($form['field_level_names']['widget'] as $key => $option) {
if (is_numeric($key) && $key > $max_delta) {
unset($form['field_level_names']['widget'][$key]);
}
}
}
else {
$max_delta = $form['field_level_names']['widget']['#max_delta'];
if ($max_delta == 0) {
$form['field_level_names']['widget'][0]['value']['#default_value'] = 'Level 1';
}
else {
unset($form['field_level_names']['widget'][$max_delta]);
}
}
$options = [];
for ($i = 1; $i <= 10; $i++) {
$options[$i] = $i;
}
$form['count_of_levels'] = [
'#type' => 'select',
'#title' => t('Number of levels for this skill'),
'#default_value' => $max_delta,
'#required' => TRUE,
'#options' => $options,
'#weight' => 3,
'#ajax' => [
'event' => 'change',
'callback' => '_skill_level_names_ajax',
'wrapper' => $ajax_wrapper,
],
'#states' => [
'visible' => [
':input[name="target_skill"]' => [
'checked' => FALSE,
],
],
],
];
$form['field_level_names']['widget']['#prefix'] = '<div class="skill-levels-description">' . t('You can adapt the names of levels, listed from the highest to the lowest') . '</div>';
$form['#attached']['library'][] = 'opigno_skills_system/opigno_skills_entity';
}
}
if ($route_name == 'entity.opigno_activity.delete_form' && method_exists($build_info['callback_object'], 'getEntity')) {
// Attach js with autosubmit.
$form['#attached']['library'][] = 'opigno_module/activity';
$entity = $build_info['callback_object']
->getEntity();
$answers = $entity
->getAnswers();
$modules = $entity
->getModules();
$messages = [];
if ($answers) {
// Disallow activity deleting if it has users answers.
$messages[] = t('There are some results for this activity, and it cannot consequently be deleted in order to keep the users\' achievements');
}
if (!empty($modules)) {
$m_list = [];
foreach ($modules as $module) {
$m_list[] = $module
->getName();
}
// Disallow activity deleting if it has modules.
$messages[] = \Drupal::translation()
->formatPlural(count($modules), 'This activity is linked to the following module: @modules. Please unlink first the activity from this module and try again to delete it', 'This activity is linked to the following modules: @modules. Please unlink first the activity from these modules and try again to delete it', [
'@modules' => implode(', ', $m_list),
]);
}
if (!empty($messages)) {
$form['message'] = [
'#theme' => 'item_list',
'#list_type' => 'ul',
'#attributes' => [
'class' => [
'alert',
'alert-danger',
],
],
'#items' => $messages,
];
$form['description'] = [];
$form_state
->set('activity_answers', TRUE);
$form_state
->set('activity_message', implode(', ', $messages));
$form['actions']['submit']['#attributes']['disabled'] = 'disabled';
}
}
// Change button value from "Save" to "Next".
if (in_array($route_name, [
'opigno_module.manager.get_item_form',
'opigno_module.group.answer_form',
])) {
$form['actions']['submit']['#value'] = t('Next');
}
// Improve module form display.
if ($route_name == 'opigno_module.manager.get_item_form') {
$form['uid']['#access'] = FALSE;
$form['created']['#access'] = FALSE;
if (isset($form['actions']['delete'])) {
unset($form['actions']['delete']);
}
}
// If the form has an entity, ajaxify it.
if ($route_name == 'opigno_module.manager.get_item_form' && method_exists($build_info['callback_object'], 'getEntity')) {
// Get the entity.
/** @var \Drupal\Core\Entity\EntityInterface $entity */
$entity = $build_info['callback_object']
->getEntity();
$entity_type = $entity
->getEntityTypeId();
$bundle = $entity
->bundle();
$id = $entity
->id();
// Add form class for ajaxification.
// In case of add form, append "new" instead of the entity ID.
$ajax_id = "ajax-form-entity-{$entity_type}-{$bundle}-" . ($id ?: 'new');
$form['#attributes']['class'][] = $ajax_id;
// Ajaxification settings of the buttons.
$ajax_settings = [
'callback' => 'Drupal\\opigno_module\\Controller\\OpignoModuleManagerController::ajaxFormEntityCallback',
'wrapper' => $ajax_id,
'effect' => 'fade',
'event' => 'click',
];
$form['#attached']['library'][] = 'opigno_module/ajax_form';
$form['actions']['submit']['#ajax'] = $ajax_settings;
$form['actions']['publish']['#ajax'] = $ajax_settings;
$form['actions']['unpublish']['#ajax'] = $ajax_settings;
$form['actions']['preview']['#access'] = FALSE;
unset($form['actions']['publish']['#dropbutton']);
unset($form['actions']['unpublish']['#dropbutton']);
// Ajaxify the buttons.
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = 'Drupal\\opigno_module\\Controller\\OpignoModuleManagerController::ajaxFormEntityFormSubmit';
}
}
// Handle case of entity edition : define the options.
if ($id) {
$current_path = \Drupal::service('path.current')
->getPath();
$path_args = explode('/', $current_path);
if ($path_args[1] == 'ajax-form-entity') {
// Case of edit link.
$view_mode = $path_args[5];
$reload = FALSE;
}
else {
// Case of form inside the display of the entity.
$view_mode = 'default';
$reload = 'reload_entity';
}
}
else {
// Case of creation.
$view_mode = 'default';
$reload = TRUE;
}
// Add all configurations to the form to make it available everywhere.
$form['ajax_form_entity'] = [
'#type' => 'hidden',
'#value' => [
'view_mode' => $view_mode,
'reload' => $reload,
'content_selector' => ".{$ajax_id}",
'form_selector' => ".{$ajax_id}",
],
];
}
if ($form_id == 'opigno_module_form') {
$form['#attached']['library'][] = 'opigno_module/opigno_badges';
// Make field "name" required.
$form["name"]["widget"][0]["value"]["#required"] = TRUE;
}
if ($form_id == 'views_form_opigno_activities_bank_lp_interface_default') {
// Hide duplicate button and unnecessary fields.
$form['header']['#attributes']['hidden'] = 'hidden';
// Rename submit button.
$form['actions']['submit']['#value'] = t('Add selected activities to the module');
$form['#attached']['library'][] = 'opigno_module/activities_bank';
$route_name = \Drupal::routeMatch()
->getRouteName();
if ($route_name == 'opigno_module.activities_bank_lpm') {
$form['#attached']['drupalSettings']['activities_bank']['page'] = 'group';
if (!empty($_SESSION['activities_bank']['checkboxes_ids'])) {
$form['#attached']['drupalSettings']['activities_bank']['checkboxes_ids'] = $_SESSION['activities_bank']['checkboxes_ids'];
}
$form_state
->set('activities_bank_page', 'group');
if (!empty($_COOKIE['activities_bank_activities'])) {
unset($_COOKIE['activities_bank_activities']);
setcookie('activities_bank_activities', '', time() - 3600);
}
if (!empty($_COOKIE['activities_bank_checked'])) {
unset($_COOKIE['activities_bank_checked']);
setcookie('activities_bank_checked', '', time() - 3600);
}
}
elseif ($route_name == 'opigno_module.activities_bank') {
$form['#attached']['drupalSettings']['activities_bank']['page'] = 'module';
}
$form['#submit'][] = 'opigno_module_activities_bank_lp_form_submit';
$form["#validate"] = [
'opigno_module_activities_bank_lp_form_validate',
];
}
}