entity_mesh-1.1.1/entity_mesh.install
entity_mesh.install
<?php
/**
* @file
* Install, update and uninstall functions for the Entity Mesh module.
*/
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
/**
* Implements hook_install().
*/
function entity_mesh_install() {
$config = \Drupal::configFactory()->getEditable('entity_mesh.settings');
// Set default source types.
$config->set('source_types', [
'node' => [
'enabled' => TRUE,
'bundles' => [],
],
]);
// Set up target types configuration.
$target_types = [];
// Set up external targets.
$target_types['external'] = [
'scheme' => [
'http' => TRUE,
'tel' => TRUE,
'mailto' => TRUE,
],
'categories' => [
'iframe' => TRUE,
],
];
// Set up internal targets.
$target_types['internal'] = [];
// Enable view target type by default.
$target_types['internal']['view'] = [
'enabled' => TRUE,
];
// Get and enable available content entity types.
$entity_type_manager = \Drupal::entityTypeManager();
$entity_types = $entity_type_manager->getDefinitions();
foreach ($entity_types as $entity_type_id => $entity_type) {
if ($entity_type instanceof ContentEntityTypeInterface
|| $entity_type instanceof ConfigEntityTypeInterface) {
$target_types['internal'][$entity_type_id] = [
'enabled' => TRUE,
'bundles' => [],
];
}
}
$config->set('target_types', $target_types);
$config->save();
\Drupal::messenger()->addStatus(__FUNCTION__);
}
/**
* Implements hook_uninstall().
*/
function entity_mesh_uninstall() {
\Drupal::messenger()->addStatus(__FUNCTION__);
}
/**
* Implements hook_schema().
*/
function entity_mesh_schema() {
$schema = [];
$schema['entity_mesh'] = [
'description' => 'Entity Mesh.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique Mesh ID.',
],
'type' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Mesh Type',
],
'category' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Mesh Category',
],
'subcategory' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Mesh Subcategory',
],
'source_hash_id' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Source Hash ID',
],
'source_entity_type' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'Source Entity Type',
],
'source_entity_bundle' => [
'type' => 'varchar',
'length' => 64,
'default' => NULL,
'description' => 'Source Entity bundle',
],
'source_entity_id' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'Source Entity ID.',
],
'source_entity_langcode' => [
'type' => 'varchar',
'length' => 12,
'not null' => TRUE,
'default' => '',
'description' => 'Langcode.',
],
'source_title' => [
'type' => 'varchar',
'length' => 256,
'default' => NULL,
'description' => 'Source Title',
],
'target_href' => [
'type' => 'varchar',
'length' => 256,
'default' => NULL,
'description' => 'Target Href property',
],
'target_path' => [
'type' => 'varchar',
'length' => 256,
'default' => NULL,
'description' => 'Target Path property',
],
'target_scheme' => [
'type' => 'varchar',
'length' => 256,
'default' => NULL,
'description' => 'Target Schema property',
],
'target_link_type' => [
'type' => 'varchar',
'length' => 64,
'default' => NULL,
'description' => 'Target Link type property',
],
'target_hash_id' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'Target Hash ID',
],
'target_entity_type' => [
'type' => 'varchar',
'length' => 64,
'default' => NULL,
'description' => 'TargeT Entity Type.',
],
'target_entity_bundle' => [
'type' => 'varchar',
'length' => 64,
'default' => NULL,
'description' => 'Target Entity bundle.',
],
'target_entity_id' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'default' => '',
'description' => 'Target Entity ID.',
],
'target_entity_langcode' => [
'type' => 'varchar',
'length' => 12,
'default' => NULL,
'description' => 'Target Langcode.',
],
'target_title' => [
'type' => 'varchar',
'length' => 256,
'default' => NULL,
'description' => 'Target Title',
],
'target_host' => [
'type' => 'varchar',
'length' => 256,
'default' => NULL,
'description' => 'Target Host property',
],
],
'primary key' => ['id'],
'indexes' => [
'source_entity' => [
'source_entity_id',
'source_entity_type',
'source_entity_langcode',
],
],
];
$schema['entity_mesh_tracker'] = [
'description' => 'Tracks entities that need to be processed by entity_mesh.',
'fields' => [
'id' => [
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique tracker ID.',
],
'entity_type' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'The entity type (e.g., node, taxonomy_term).',
],
'entity_id' => [
'type' => 'varchar',
'length' => 64,
'not null' => TRUE,
'description' => 'The entity ID.',
],
'operation' => [
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'description' => 'The operation to perform (1 = process, 2 = delete).',
],
'status' => [
'type' => 'int',
'size' => 'tiny',
'not null' => TRUE,
'default' => 1,
'description' => 'Processing status (1 = pending, 2 = processing, 3 = processed, 4 = failed).',
],
'timestamp' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Timestamp when the entity was added or last updated.',
],
'retry_count' => [
'type' => 'int',
'not null' => TRUE,
'default' => 0,
'description' => 'Number of processing retry attempts.',
],
],
'primary key' => ['id'],
'indexes' => [
'entity_lookup' => ['entity_type', 'entity_id'],
'status' => ['status'],
'timestamp' => ['timestamp'],
],
'unique keys' => [
'entity_unique' => ['entity_type', 'entity_id'],
],
];
return $schema;
}
/**
* Implements hook_requirements().
*/
function entity_mesh_requirements($phase) {
$requirements = [];
if ($phase !== 'runtime') {
return $requirements;
}
$requirements = array_merge($requirements, entity_mesh_get_insights_for_drupal_report());
return $requirements;
}
/**
* Insight for the Drupal status report.
*
* @return array
* Array of status report entries keyed by machine name.
*/
function entity_mesh_get_insights_for_drupal_report() {
$database = \Drupal::database();
$insights = [];
// Subcategories to report.
$subcategories = [
'broken-link' => t('Broken links'),
'access-denied-link' => t('Access denied links'),
'redirected-link' => t('Redirected links'),
];
// Build insight for each subcategory.
foreach ($subcategories as $key => $label) {
// Count entries for this subcategory.
$count = (int) $database->select('entity_mesh', 'em')
->condition('subcategory', $key)
->countQuery()
->execute()
->fetchField();
$url = Url::fromUri('internal:/admin/reports/entity-mesh/table', [
'query' => ['subcategory[]' => $key],
])->toString();
if ($count > 0) {
$severity = REQUIREMENT_WARNING;
$value = t('<a href=":url">@count @label.</a>', [
'@count' => $count,
'@label' => $label,
':url' => $url,
]);
}
else {
$severity = REQUIREMENT_OK;
$value = t('<a href=":url">No @label detected.</a>', [
'@label' => strtolower($label),
':url' => Url::fromUri('internal:/admin/reports/entity-mesh/table')->toString(),
]);
}
$insights["entity_mesh_{$key}"] = [
'title' => t('Entity Mesh: @label', ['@label' => $label]),
'value' => $value,
'severity' => $severity,
];
}
return $insights;
}
/**
* Adding new field subcategory.
*/
function entity_mesh_update_10004() {
// Update data base schema.
$schema = \Drupal::database()->schema();
$schema_definition = entity_mesh_schema();
$schema->dropTable('entity_mesh');
$schema->createTable('entity_mesh', $schema_definition['entity_mesh']);
}
/**
* Set new field configuration "process_mesh_using_queue".
*/
function entity_mesh_update_10005() {
$config = \Drupal::configFactory()->getEditable('entity_mesh.settings');
$config->set('process_mesh_using_queue', FALSE);
$config->save();
}
/**
* Create display views_data_export.
*/
function entity_mesh_update_10006() {
\Drupal::service('module_installer')->install(['views_data_export']);
return (string) new TranslatableMarkup("Views data export installed and configured on Entity Mesh report.");
}
/**
* Add field and filter subcategory in entity mesh node views.
*/
function entity_mesh_update_10007() {
// Update views.
\Drupal::configFactory()->getEditable('views.view.entity_mesh')->delete();
\Drupal::configFactory()->getEditable('views.view.entity_mesh_node')->delete();
$path_to_module = \Drupal::service('extension.path.resolver')->getPath('module', 'entity_mesh');
$config_path = $path_to_module . '/config/optional';
$config_source = new FileStorage($config_path);
\Drupal::service('config.installer')->installOptionalConfig($config_source);
return (string) new TranslatableMarkup("Views data export installed and configured on Entity Mesh report.");
}
/**
* Solve config schema views schema errors.
*/
function entity_mesh_update_10008() {
// Update views.
\Drupal::configFactory()->getEditable('views.view.entity_mesh')->delete();
\Drupal::configFactory()->getEditable('views.view.entity_mesh_node')->delete();
$path_to_module = \Drupal::service('extension.path.resolver')->getPath('module', 'entity_mesh');
$config_path = $path_to_module . '/config/optional';
$config_source = new FileStorage($config_path);
\Drupal::service('config.installer')->installOptionalConfig($config_source);
return (string) new TranslatableMarkup("Views data export installed and configured on Entity Mesh report.");
}
/**
* Empty update to clear caches for service Entity render definition.
*/
function entity_mesh_update_10009() {
}
/**
* Update node views to add langcode column.
*/
function entity_mesh_update_10011() {
$config_factory = \Drupal::configFactory();
$existing_config = $config_factory->getEditable('views.view.entity_mesh_node');
$uuid = $existing_config->get('uuid');
$existing_config->delete();
$path_to_module = \Drupal::service('extension.path.resolver')->getPath('module', 'entity_mesh');
$config_path = $path_to_module . '/config/optional';
$config_source = new FileStorage($config_path);
\Drupal::service('config.installer')->installOptionalConfig($config_source);
$new_config = $config_factory->getEditable('views.view.entity_mesh_node');
$new_config->set('uuid', $uuid);
$new_config->save();
}
/**
* Add debug configuration field.
*/
function entity_mesh_update_10012() {
$config = \Drupal::configFactory()->getEditable('entity_mesh.settings');
$config->set('debug', FALSE);
$config->save();
return (string) new TranslatableMarkup("Added debug configuration field with default value of false.");
}
/**
* Migrate process_mesh_using_queue to processing_mode configuration.
*/
function entity_mesh_update_10013() {
$config = \Drupal::configFactory()->getEditable('entity_mesh.settings');
// Get the current process_mesh_using_queue value.
$process_mesh_using_queue = $config->get('process_mesh_using_queue');
// If process_mesh_using_queue exists, migrate it.
if ($process_mesh_using_queue !== NULL) {
// Set processing_mode based on process_mesh_using_queue value.
$processing_mode = $process_mesh_using_queue ? 'asynchronous' : 'synchronous';
$config->set('processing_mode', $processing_mode);
// Set default synchronous_limit if synchronous mode.
if ($processing_mode === 'synchronous') {
$config->set('synchronous_limit', 25);
}
// Remove the old configuration key.
$config->clear('process_mesh_using_queue');
// Save the configuration.
$config->save();
return (string) new TranslatableMarkup('Migrated process_mesh_using_queue to processing_mode configuration. Processing mode set to @mode.', [
'@mode' => $processing_mode,
]);
}
return (string) new TranslatableMarkup('No migration needed. process_mesh_using_queue configuration not found.');
}
/**
* Set default value for source_types and target_types if not configured.
*/
function entity_mesh_update_10014() {
$config = \Drupal::configFactory()->getEditable('entity_mesh.settings');
// Set default source types if not configured.
$config->set('source_types', [
'node' => [
'enabled' => TRUE,
'bundles' => [],
],
]);
// Set default target types if not configured.
$target_types = [];
// Set up external targets.
$target_types['external'] = [
'scheme' => [
'http' => TRUE,
'tel' => TRUE,
'mailto' => TRUE,
],
'categories' => [
'iframe' => TRUE,
],
];
// Set up internal targets.
$target_types['internal'] = [];
// Enable view target type by default.
$target_types['internal']['view'] = [
'enabled' => TRUE,
];
// Get and enable available content entity types.
$entity_type_manager = \Drupal::entityTypeManager();
$entity_types = $entity_type_manager->getDefinitions();
foreach ($entity_types as $entity_type_id => $entity_type) {
if ($entity_type instanceof ContentEntityTypeInterface
|| $entity_type instanceof ConfigEntityTypeInterface) {
$target_types['internal'][$entity_type_id] = [
'enabled' => TRUE,
'bundles' => [],
];
}
}
$config->set('target_types', $target_types);
// Clear old config values.
$config->clear('external');
$config->clear('internal');
$config->save();
}
/**
* Add self_domain_internal configuration field.
*/
function entity_mesh_update_10015() {
$config = \Drupal::configFactory()->getEditable('entity_mesh.settings');
$config->set('self_domain_internal', TRUE);
$config->save();
return (string) new TranslatableMarkup("Added self domain internal configuration field with default value of true.");
}
/**
* Add target_host column to entity_mesh table.
*/
function entity_mesh_update_10016() {
$schema = \Drupal::database()->schema();
$spec = [
'type' => 'varchar',
'length' => 256,
'default' => NULL,
'description' => 'Target Host property',
];
$schema->addField('entity_mesh', 'target_host', $spec);
return (string) new TranslatableMarkup("Added target_host column to entity_mesh table.");
}
/**
* Update config schema views, add host field.
*/
function entity_mesh_update_10017() {
// Update views.
\Drupal::configFactory()->getEditable('views.view.entity_mesh')->delete();
\Drupal::configFactory()->getEditable('views.view.entity_mesh_node')->delete();
$path_to_module = \Drupal::service('extension.path.resolver')->getPath('module', 'entity_mesh');
$config_path = $path_to_module . '/config/optional';
$config_source = new FileStorage($config_path);
\Drupal::service('config.installer')->installOptionalConfig($config_source);
return (string) new TranslatableMarkup("Views data export installed and configured on Entity Mesh report.");
}
/**
* Create entity_mesh_tracker table for tracking entities to be processed.
*/
function entity_mesh_update_10018() {
$schema = \Drupal::database()->schema();
$schema_definition = entity_mesh_schema();
if (!$schema->tableExists('entity_mesh_tracker')) {
$schema->createTable('entity_mesh_tracker', $schema_definition['entity_mesh_tracker']);
return (string) new TranslatableMarkup('Created entity_mesh_tracker table for tracking entities to be processed.');
}
return (string) new TranslatableMarkup('entity_mesh_tracker table already exists.');
}
/**
* Set default values for cron_enabled and cron_limit configuration.
*/
function entity_mesh_update_10019() {
$config = \Drupal::configFactory()->getEditable('entity_mesh.settings');
// Set default cron_enabled to TRUE if not already set.
if ($config->get('cron_enabled') === NULL) {
$config->set('cron_enabled', TRUE);
}
// Set default cron_limit to 50 if not already set.
if ($config->get('cron_limit') === NULL) {
$config->set('cron_limit', 50);
}
$config->save();
return (string) new TranslatableMarkup('Set default values for cron configuration: cron_enabled=@enabled, cron_limit=@limit.', [
'@enabled' => $config->get('cron_enabled') ? 'TRUE' : 'FALSE',
'@limit' => $config->get('cron_limit'),
]);
}
