tripal_chado/tripal_chado.module

Summary

Maintainability
Test Coverage
F
20%
<?php

/**
 * @file
 * Contains tripal_chado.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Config\FileStorage;

require_once 'src/api/tripal_chado.schema.api.inc';
require_once 'src/api/tripal_chado.custom_tables.api.inc';
require_once 'src/api/tripal_chado.mviews.api.inc';
require_once 'src/api/tripal_chado.query.api.inc';
require_once 'src/api/tripal_chado.variables.api.inc';
require_once 'src/api/tripal_chado.organism.api.inc';
require_once 'src/api/tripal_chado.db.api.inc';
require_once 'src/api/tripal_chado.cv.api.inc';
require_once 'src/legacyFunctions.php';

/**
 * Implements hook_help().
 */
function tripal_chado_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the tripal_chado module.
    case 'help.page.tripal_chado':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Chado integration for Tripal.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Used by the hook_rebuild() function to recreate views from default.
 *
 * If the user deletes one of the views that are created on install of the
 * Tripal Chado module then this will restore them when the cache is cleared.
 */
function tripal_chado_rebuild_views() {

  //
  // Make sure the Views are present.
  //
  $storage = \Drupal::entityTypeManager()->getStorage('view');
  $dir = drupal_get_path('module', 'tripal_chado');
  $fileStorage = new FileStorage($dir);

  // The chado_custom_tables view.
  $view = $storage->load('chado_custom_tables');
  if (!$view) {
    $config = $fileStorage->read('config/install/views.view.chado_custom_tables');
    $view = $storage->create($config);
    $view->save();
  }

  // The chado_materialized_views view.
  $view = $storage->load('chado_mviews');
  if (!$view) {
    $config = $fileStorage->read('config/install/views.view.chado_mviews');
    $view = $storage->create($config);
    $view->save();
  }
}

/**
 * Used by the hook_rebuild() to recreate chado term mappings from default.
 */
function tripal_chado_rebuild_chado_term_mappings() {

  $storage = \Drupal::entityTypeManager()->getStorage('chado_term_mapping');
  $dir = \Drupal::service('extension.list.module')->getPath('tripal_chado');
  $fileStorage = new FileStorage($dir);

  $mapping = $storage->load('core_mapping');
  if (!$mapping) {
    $config = $fileStorage->read('config/install/tripal_chado.chado_term_mapping.core_mapping');
    $mapping = $storage->create($config);
    $mapping->save();
  }

  $storage = \Drupal::entityTypeManager()->getStorage('tripal_content_terms');
  $mapping = $storage->load('chado_content_terms');
  if (!$mapping) {
    $config = $fileStorage->read('config/install/tripal.tripal_content_terms.chado_content_terms');
    $mapping = $storage->create($config);
    $mapping->save();
  }
}

/**
 * Implements hook_rebuild().
 */
function tripal_chado_rebuild() {
  tripal_chado_rebuild_views();
  tripal_chado_rebuild_chado_term_mappings();
}