You are here

opigno_forum.install in Opigno forum 8

Same filename and directory in other branches
  1. 3.x opigno_forum.install

Install, update and uninstall functions for the Opigno Forum module.

File

opigno_forum.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for the Opigno Forum module.
 */
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\opigno_forum\ForumTopicHandler;

/**
 * Creates field for a forum reference in a training groups.
 */
function opigno_forum_install($is_syncing) {
  if (!$is_syncing) {
    $config_path = drupal_get_path('module', 'opigno_forum') . '/config/install';
    $storage = new FileStorage($config_path);
    $data = $storage
      ->read('field.storage.group.field_learning_path_forum');
    if (!FieldStorageConfig::loadByName($data['entity_type'], $data['field_name'])) {
      FieldStorageConfig::create($data)
        ->save();
    }
    $data = $storage
      ->read('field.field.group.learning_path.field_learning_path_forum');
    if (!FieldConfig::loadByName($data['entity_type'], $data['bundle'], $data['field_name'])) {
      FieldConfig::create($data)
        ->save();
    }
  }
}

/**
 * Implements hook_requirements()
 */
function opigno_forum_requirements($phase) {
  $requirements = [];
  if ($phase === 'runtime') {
    $non_configured_types = [];
    $forum_topic_handler = ForumTopicHandler::get();
    $node_types = \Drupal::service('entity_type.bundle.info')
      ->getBundleInfo('node');
    foreach ($forum_topic_handler
      ->getForumTopicTypeIds() as $node_type_id) {
      if ($forum_topic_handler
        ->isForumTopicType($node_type_id) && !$forum_topic_handler
        ->isLearningPathContent($node_type_id)) {
        $route_parameters = [
          'group_type' => 'learning_path',
          'plugin_id' => 'group_node:' . $node_type_id,
        ];
        $non_configured_types[$node_type_id] = Link::createFromRoute($node_types[$node_type_id]['label'], 'entity.group_content_type.add_form', $route_parameters)
          ->toString();
      }
    }
    if ($non_configured_types) {
      $args = [
        ':url' => Url::fromRoute('entity.group_type.content_plugins', [
          'group_type' => 'learning_path',
        ])
          ->toString(),
      ];
      $requirements['opigno_forum'] = [
        'title' => t('Opigno forum access'),
        'description' => [
          'text' => [
            '#markup' => t('Forum topics having these content types are not configured as <a href=":url">Learning path content</a> and will be visible to every user allowed to see regular content:', $args),
          ],
          'types' => [
            '#theme' => 'item_list',
            '#items' => $non_configured_types,
          ],
        ],
        'severity' => REQUIREMENT_WARNING,
      ];
    }
  }
  return $requirements;
}

/**
 * Implements hook_update_N()
 *
 * Fix issue with access to the forum node type.
 *
 */
function opigno_forum_update_8001() {

  // Install module.
  \Drupal::service('module_installer')
    ->install([
    'gnode',
  ]);
  $config_path = drupal_get_path('module', 'opigno_forum') . '/config/install';

  /* @var Drupal\Core\Config\CachedStorage $config_storage */
  $storage = new FileStorage($config_path);
  $config_storage = \Drupal::service('config.storage');
  $data = $storage
    ->read('group.content_type.learning_path-group_node-forum');
  $config_storage
    ->write('group.content_type.learning_path-group_node-forum', $data);
}

Functions

Namesort descending Description
opigno_forum_install Creates field for a forum reference in a training groups.
opigno_forum_requirements Implements hook_requirements()
opigno_forum_update_8001 Implements hook_update_N()