dhis2-8.x-1.x-dev/dhis.install

dhis.install
<?php

use Symfony\Component\Yaml\Yaml;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
use Drupal\dhis\Entity\OrganisationUnit;
use Drupal\dhis\Entity\DataElement;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * @file
 * Contains dhis.install
 */

/**
 * Implements hook_install.
 */
function dhis_install()
{
    $module_path = drupal_get_path('module', 'dhis');
    $file_contents = file_get_contents($module_path . '/dhis.periods.yml');
    $periods = Yaml::parse($file_contents);

    $vid = 'periods';

    FieldStorageConfig::create(
        array(
            'field_name' => 'activate',
            'entity_type' => 'taxonomy_term',
            'type' => 'boolean',
            'settings' => [
                'max_length' => '1',
            ],
            'cardinality' => 1,
        )
    )->save();

    FieldConfig::create([
        'field_name' => 'activate',
        'entity_type' => 'taxonomy_term',
        'bundle' => $vid,
        'label' => 'Activate',
        'field_type' => 'boolean',
        'widget' => [

        ]
    ])->save();

    foreach ($periods['dhis.periods'] as $key => $value) {
        Term::create([
            'name' => $key,
            'vid' => $vid,
            'description' => $value
        ])->save();
    }
}

/**
 * Implements hook_uninstall.
 */
function dhis_uninstall()
{
    print('Starting uninstall process');
    $entities = ['data_element', 'organisation_unit', 'periods'];
    foreach ($entities as $entity) {
        $vids = [];
        try {
            if ($entity == 'data_element') {
                $vids = DataElement::loadMultiple();
                deleteCustomEntity($entity, $vids);
            }
            if ($entity == 'organisation_unit') {
                $vids = OrganisationUnit::loadMultiple();
                deleteCustomEntity($entity, $vids);
            }
        } catch (\Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException $e) {
            print($e->getMessage());
        }

        if ($entity == 'periods') {
            deleteVocabulary($entity);
        }
    }
    $entity_settings = ['OrganisationUnit_settings', 'DataElement_settings',
        'field.storage.node.value', 'field.field.node.dhis_data.value',
        'field.storage.node.data_source', 'field.field.node.dhis_data.data_source'];

    foreach ($entity_settings as $entity_setting) {
        \Drupal::configFactory()->getEditable($entity_setting)->delete();
    }
}

function deleteVocabulary($vid)
{
    $vocab = Vocabulary::load($vid);
    if ($vocab) {
        $vocab->delete();
    }
}

function deleteCustomEntity($entity, $vids)
{
    if ($vids) {
        \Drupal::entityTypeManager()->getStorage($entity)->delete($vids);
    }
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc