Skip to content

Commit

Permalink
DIGITAL-233: Allow setting the homepage to a specific node on every i…
Browse files Browse the repository at this point in the history
…nstall.
  • Loading branch information
mattsqd committed Jan 6, 2025
1 parent 5e72207 commit 7ef1cc6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 0 deletions.
1 change: 1 addition & 0 deletions .homepage-uuid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
df0e2d62-c293-42be-8165-84542c35129d
3 changes: 3 additions & 0 deletions orch/deploy_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,7 @@ fi
# Clear cache after installation
drush cr

# Set the homepage. Custom functionality for Digital.gov.
drush set-hp -y

./orch/show_file.sh $0 end
6 changes: 6 additions & 0 deletions web/modules/custom/default_content_config/drush.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
init.commands:
class: \Drupal\default_content_config\Commands\CustomCommands
arguments: ["@config.factory", "@entity_type.manager"]
tags:
- { name: drush.command }
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Drupal\default_content_config\Commands;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drush\Commands\DrushCommands;

/**
* Drush command file for Default Content Config.
*/
class CustomCommands extends DrushCommands {

/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;

/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManager
*/
protected $entityTypeManager;

/**
* Custom Commands constructor.
*/
public function __construct(ConfigFactoryInterface $configFactory, EntityTypeManager $entityTypeManager) {
parent::__construct();
$this->configFactory = $configFactory;
$this->entityTypeManager = $entityTypeManager;
}

/**
* A custom Drush command to set the homepage URL post site install.
*
* @command drush-command:set-homepage
*
* @aliases set-hp
*/
public function setHomepage(array $options = []): void {
$no_interaction = FALSE;
if ($options['no-interaction'] || $options['yes']) {
$no_interaction = TRUE;
}
$homepage_uuid_file = '../.homepage-uuid';
$homepage_uuid = file_get_contents($homepage_uuid_file);
if (!$homepage_uuid) {
if ($no_interaction) {
$this->io()->warning(dt('Your homepage has not been set yet, do so by calling drush set-hp'));
return;
}
else {
$homepage_uuid = $this->ask('What is the UUID of your homepage node?');
if (!$homepage_uuid) {
throw new \Exception(dt('A homepage is required'));
}
file_put_contents($homepage_uuid_file, $homepage_uuid);
}
}

$nodes = $this->entityTypeManager->getStorage('node')->loadByProperties(['uuid' => $homepage_uuid]);
if (empty($nodes)) {
file_put_contents($homepage_uuid_file, '');
throw new \Exception(dt('Unable to find a node for homepage by UUID ') . $homepage_uuid);
}
/** @var \Drupal\node\NodeInterface $node */
$node = reset($nodes);
$homepage_url = '/node/' . $node->id();
$this->configFactory->getEditable('system.site')
->set('page.front', $homepage_url)
->save();
$this->io()->success(dt('Homepage has been set to ') . $homepage_url);
}

}

0 comments on commit 7ef1cc6

Please sign in to comment.