You are here

varbase_update_helper.module in Varbase Core 8.8

File

modules/varbase_update_helper/varbase_update_helper.module
View source
<?php

/**
 * @file
 * Contains varbase_update_helper.module.
 */
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
use Drupal\update_helper_checklist\Entity\Update;

/**
 * Implements hook_form_alter().
 */
function varbase_update_helper_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == "checklistapi_checklist_form") {
    if ($form['#checklist']->id == 'update_helper_checklist') {
      if (!isset($form['actions']['save']['#submit'])) {
        $form['actions']['save']['#submit'] = [];
      }
      $form['actions']['save']['#submit'][] = 'varbase_update_helper_checklistapi_form_submit';
      $checklist = $form['#checklist'];
      $groups = $checklist->items;

      // Prevent the user from Clearing checklist progress.
      unset($form['actions']['clear']);
      foreach (Element::children($groups) as $group_key) {
        $group =& $groups[$group_key];
        foreach (Element::children($group) as $item_key) {
          $update_key = str_replace('.', '_', $item_key);
          $entity = Update::load($update_key);
          $entityStatus = $entity && $entity
            ->wasSuccessfulByHook() ? TRUE : FALSE;
          if ($entityStatus) {
            $form[$group_key][$item_key]['#disabled'] = TRUE;
          }
        }
      }
    }
  }
}

/**
 * Implements hook_checklistapti_form_submit().
 */
function varbase_update_helper_checklistapi_form_submit(array $form, FormStateInterface $form_state) {
  if ($form['#checklist']->id == 'update_helper_checklist') {
    $messenger = \Drupal::messenger();
    $checklistapi = $form_state
      ->getValue('checklistapi');
    foreach ($checklistapi as $key => $updateset) {
      if ($key == "checklistapi__active_tab") {
        continue;
      }
      if (is_array($updateset) && !empty($updateset)) {
        foreach ($updateset as $update => $status) {
          $update_key = str_replace('.', '_', $update);
          $entity = Update::load($update_key);
          $entityStatus = $entity && $entity
            ->wasSuccessfulByHook() ? TRUE : FALSE;
          if ($entityStatus) {
            continue;
          }
          if ($status) {
            $update_data = explode(":", $update);
            module_load_install($update_data[0]);
            if (function_exists($update_data[1])) {
              call_user_func($update_data[1], FALSE);
            }
            else {
              $checklistapi[$key][$update] = 0;
              $messenger
                ->addWarning(t("Couldn't find an update hook: %update_hook. Please verify the update hook name.", [
                "%update_hook" => $update_data[1],
              ]));
            }
          }
        }
      }
    }
    $checklist = $form['#checklist'];

    // Save progress.
    $checklist
      ->saveProgress($checklistapi);

    // Preserve the active tab after submission.
    $form_state
      ->setRedirect($checklist
      ->getRouteName(), [], [
      'fragment' => $checklistapi['checklistapi__active_tab'],
    ]);
  }
}

/**
 * Implements hook_help().
 */
function varbase_update_helper_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the varbase_update_helper module.
    case 'help.page.varbase_update_helper':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Varbase update helper') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_checklistapi_checklist_info_alter().
 */
function varbase_update_helper_checklistapi_checklist_info_alter(array &$definitions) {
  if (isset($definitions['update_helper_checklist']['#title'])) {
    $definitions['update_helper_checklist']['#title'] = t('Varbase update instructions');
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function varbase_update_helper_form_checklistapi_checklist_form_alter(&$form, &$form_state) {
  if ($form['#checklist']->id == 'update_helper_checklist') {
    $form['#attached']['library'][] = 'varbase_update_helper/varbase_update_helper';
  }
}