wayfinding-2.1.x-dev/wayfinding.install
wayfinding.install
<?php
/**
* @file
* Install file for the wayfinding module.
*/
use Drupal\Core\Entity\ContentEntityTypeInterface;
/**
* Implements hook_install().
*/
function wayfinding_install(): void {
Drupal::service('wayfinding.entity_update')->updateExistingEntityTypes();
}
/**
* Remove field digital_signage from wayfinding entities.
*/
function wayfinding_update_8001(): void {
$definition_update_manager = Drupal::entityDefinitionUpdateManager();
if ($definition = $definition_update_manager->getFieldStorageDefinition('digital_signage', 'wayfinding')) {
$definition_update_manager->uninstallFieldStorageDefinition($definition);
}
}
/**
* Remove field wayfinding from unsupported entities.
*/
function wayfinding_update_8002(): void {
$definition_update_manager = Drupal::entityDefinitionUpdateManager();
foreach (Drupal::entityTypeManager()->getDefinitions() as $definition) {
if (
($definition instanceof ContentEntityTypeInterface) &&
($field_definition = $definition_update_manager->getFieldStorageDefinition('wayfinding', $definition->id())) &&
in_array($definition->id(), Drupal::service('wayfinding.entity_types')->allDisabledIds(), TRUE)
) {
$definition_update_manager->uninstallFieldStorageDefinition($field_definition);
}
}
}
/**
* Correct wayfinding links from content entities.
*/
function wayfinding_update_8003(): void {
$definition_update_manager = Drupal::entityDefinitionUpdateManager();
foreach (Drupal::entityTypeManager()->getDefinitions() as $definition) {
if ($definition instanceof ContentEntityTypeInterface) {
/* @noinspection MissingOrEmptyGroupStatementInspection */
if ($definition->id() === 'wayfinding') {
// Nothing to do.
}
elseif (!in_array($definition->id(), Drupal::service('wayfinding.entity_types')->allDisabledIds(), TRUE)) {
if ($field_definition = $definition_update_manager->getFieldStorageDefinition('wayfinding', $definition->id())) {
$definition_update_manager->updateFieldStorageDefinition($field_definition);
}
$data_table = $definition->getDataTable();
$id_field = $definition->getKey('id');
if (!empty($data_table) && !empty($id_field)) {
try {
Drupal::database()
->update($data_table)
->fields(['wayfinding' => 0])
->execute();
}
catch (Exception) {
// Ignore.
}
}
}
}
}
$rows = Drupal::database()->select('wayfinding', 'w')
->fields('w', ['id', 'parent_entity__target_type', 'parent_entity__target_id'])
->execute()
->fetchAll();
foreach ($rows as $row) {
try {
if ($entity_type = Drupal::entityTypeManager()->getDefinition($row->parent_entity__target_type, FALSE)) {
$data_table = $entity_type->getDataTable();
$id_field = $entity_type->getKey('id');
if (!empty($data_table) && !empty($id_field)) {
Drupal::database()
->update($data_table)
->fields(['wayfinding' => $row->id])
->condition($id_field, $row->parent_entity__target_id)
->execute();
}
}
}
catch (Exception) {
// Ignore.
}
}
}
