demo_umami_content.install

Same filename in other branches
  1. 9 core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
  2. 8.9.x core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install
  3. 10 core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install

Install, update and uninstall functions for the module.

File

core/profiles/demo_umami/modules/demo_umami_content/demo_umami_content.install

View source
<?php


/**
 * @file
 * Install, update and uninstall functions for the module.
 */
use Drupal\demo_umami_content\InstallHelper;

/**
 * Implements hook_install().
 */
function demo_umami_content_install($is_syncing) : void {
    if (!$is_syncing) {
        $query = \Drupal::entityQuery('node')->accessCheck(TRUE);
        $nids = $query->execute();
        if (empty($nids)) {
            return;
        }
        // Sort node IDs in descending order
        rsort($nids);
        $nodes = \Drupal::entityTypeManager()->getStorage('node')
            ->loadMultiple($nids);
        // Use a counter to determine the "days ago" offset
        $counter = 1;
        foreach ($nodes as $node) {
            // Get all translation languages for the node
            $languages = $node->getTranslationLanguages();
            foreach ($languages as $language) {
                // Load the translated version of the node.
                $translatedNode = $node->getTranslation($language->getId());
                try {
                    // Use the counter to calculate "days ago".
                    $timestamp = strtotime("-{$counter} days");
                    // Set the created and updated time for the translated node.
                    $translatedNode->setCreatedTime($timestamp);
                    $translatedNode->setChangedTime($timestamp);
                    // Check if the translation is the default language.
                    // If not, mark the translation as changed.
                    if (!$translatedNode->isDefaultTranslation()) {
                        $translatedNode->setNewRevision(FALSE);
                        $translatedNode->isDefaultRevision(FALSE);
                        $translatedNode->setRevisionTranslationAffected(TRUE);
                    }
                    $translatedNode->save();
                } catch (\Exception $e) {
                    \Drupal::logger('system')->error($e->getMessage());
                }
            }
            // Increment the counter for the next node
            $counter++;
        }
    }
}

/**
 * Implements hook_uninstall().
 */
function demo_umami_content_uninstall($is_syncing) : void {
    if (!$is_syncing) {
        \Drupal::classResolver(InstallHelper::class)->deleteImportedContent();
    }
}

Functions

Title Deprecated Summary
demo_umami_content_install Implements hook_install().
demo_umami_content_uninstall Implements hook_uninstall().

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.