-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DIGITAL-233: Allow setting the homepage to a specific node on every i…
…nstall.
- Loading branch information
Showing
4 changed files
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
df0e2d62-c293-42be-8165-84542c35129d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
79 changes: 79 additions & 0 deletions
79
web/modules/custom/default_content_config/src/Commands/CustomCommands.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |