geolocation-8.x-3.x-dev/modules/geolocation_demo/geolocation_demo.install
modules/geolocation_demo/geolocation_demo.install
<?php
/**
* @file
* Geolocation demo setup.
*/
use Drupal\Component\Utility\Random;
use Drupal\Core\File\FileExists;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Implements hook_install().
*/
function geolocation_demo_install(): void {
/** @var \Drupal\Core\Extension\ModuleHandlerInterface $module_handler */
$module_handler = \Drupal::service('module_handler');
if (
$module_handler->moduleExists('taxonomy')
&& $module_handler->moduleExists('node')
&& $module_handler->moduleExists('field')
) {
/*
* Create 3 defined terms.
*/
$term_ids = [];
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$icon_path = Drupal::service('extension.list.module')->getPath('geolocation_demo') . '/icons/';
/** @var \Drupal\taxonomy\TermInterface $term_a */
$term_a = $taxonomy_storage->create([
'vid' => 'geolocation_demo_taxonomy',
'name' => 'Class A',
]);
/** @var \Drupal\file\FileRepositoryInterface $file_repository */
$file_repository = Drupal::service('file.repository');
$data = file_get_contents($icon_path . 'druplicon-deadpool.png');
$file = $file_repository->writeData($data, 'public://druplicon-deadpool.png', FileExists::Replace);
$term_a->set('field_geolocation_demo_term_icon', [
'target_id' => $file->id(),
]);
$term_a->save();
$term_ids[] = $term_a->id();
/** @var \Drupal\taxonomy\TermInterface $term_b */
$term_b = $taxonomy_storage->create([
'vid' => 'geolocation_demo_taxonomy',
'name' => 'Class B',
]);
$data = file_get_contents($icon_path . 'druplicon-wolverine.png');
$file = $file_repository->writeData($data, 'public://druplicon-wolverine.png', FileExists::Replace);
$term_b->set('field_geolocation_demo_term_icon', [
'target_id' => $file->id(),
]);
$term_b->save();
$term_ids[] = $term_b->id();
/** @var \Drupal\taxonomy\TermInterface $term_c */
$term_c = $taxonomy_storage->create([
'vid' => 'geolocation_demo_taxonomy',
'name' => 'Class C',
]);
$data = file_get_contents($icon_path . 'druplicon-wonder-woman.png');
$file = $file_repository->writeData($data, 'public://druplicon-wonder-woman.png', FileExists::Replace);
$term_c->set('field_geolocation_demo_term_icon', [
'target_id' => $file->id(),
]);
$term_c->save();
$term_ids[] = $term_c->id();
/*
* Create 100 random nodes.
*/
$random = new Random();
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
for ($i = 1; $i < 100; $i++) {
$node = $node_storage->create([
'type' => 'geolocation_default_article',
'title' => $random->sentences(3, TRUE),
]);
/** @var \Drupal\node\NodeInterface $node */
$node->get('field_geolocation_demo_single')->generateSampleItems();
$node->get('field_geolocation_demo_multiple')->generateSampleItems(3);
$node->get('field_geolocation_demo_taxonomy')->appendItem([
'target_id' => $term_ids[mt_rand(0, count($term_ids) - 1)],
]);
$node->save();
}
/*
* Created defined unique node term relationship.
*/
/** @var \Drupal\taxonomy\TermInterface $term_single */
$term_single = $taxonomy_storage->create([
'vid' => 'geolocation_demo_taxonomy',
'name' => 'Class Single',
]);
$data = file_get_contents($icon_path . 'druplicon-nick-fury.png');
$file = $file_repository->writeData($data, 'public://druplicon-nick-fury.png', FileExists::Replace);
$term_single->set('field_geolocation_demo_term_icon', [
'target_id' => $file->id(),
]);
$term_single->save();
$node = $node_storage->create([
'type' => 'geolocation_default_article',
'title' => 'Geolocation with unique associated term "Class Single"',
]);
/** @var \Drupal\node\NodeInterface $node */
$node->get('field_geolocation_demo_single')->appendItem([
'lat' => 52,
'lng' => 34,
]);
$node->get('field_geolocation_demo_taxonomy')->appendItem([
'target_id' => $term_single->id(),
]);
$node->save();
}
}
/**
* Implements hook_uninstall().
*/
function geolocation_demo_uninstall(): void {
$taxonomy_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$node_storage = \Drupal::entityTypeManager()->getStorage('node');
$taxonomy_storage->delete($taxonomy_storage->loadByProperties(['vid' => 'geolocation_demo_taxonomy']));
$node_storage->delete($node_storage->loadByProperties(['type' => 'geolocation_default_article']));
foreach ([
'field_geolocation_demo_single',
'field_geolocation_demo_multiple',
'field_geolocation_demo_taxonomy',
] as $field_storage_name) {
FieldStorageConfig::loadByName('node', $field_storage_name)?->delete();
}
field_purge_batch(100);
}
