Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
langcode: en
status: true
dependencies:
config:
- field.storage.node.localgov_services_parent
- node.type.localgov_election
- node.type.localgov_services_landing
- node.type.localgov_services_sublanding
id: node.localgov_election.localgov_services_parent
field_name: localgov_services_parent
entity_type: node
bundle: localgov_election
label: 'Parent page'
description: 'Optionally add the election to a Service.'
required: false
translatable: true
default_value: { }
default_value_callback: ''
settings:
handler: localgov_services
handler_settings:
target_bundles:
localgov_services_landing: localgov_services_landing
localgov_services_sublanding: localgov_services_sublanding
field_type: entity_reference
9 changes: 9 additions & 0 deletions localgov_elections.install
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ use Drupal\Core\Entity\EntityStorageException;
* Implements hook_install().
*/
function localgov_elections_install($is_syncing): void {
if ($is_syncing) {
return;
}

$module_path = \Drupal::getContainer()
->get('module_handler')
->getModule('localgov_elections')
Expand All @@ -25,6 +29,11 @@ function localgov_elections_install($is_syncing): void {
$config = \Drupal::configFactory()->getEditable($id);
$config->setData($config_data)->save();
}

$services = \Drupal::moduleHandler()->moduleExists('localgov_services_navigation');
if ($services) {
localgov_elections_optional_fields_settings($services);
}
}

/**
Expand Down
57 changes: 57 additions & 0 deletions localgov_elections.module
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\Display\EntityFormDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Template\Attribute;
Expand Down Expand Up @@ -42,6 +43,62 @@ function localgov_elections_theme($existing, $type, $theme, $path): mixed {
];
}

/**
* Implements hook_modules_installed().
*/
function localgov_elections_modules_installed($modules, $is_syncing): void {
if ($is_syncing) {
return;
}

$services = in_array('localgov_services_navigation', $modules, TRUE);
if ($services) {
\Drupal::service('config.installer')->installOptionalConfig(NULL, [
'config' => 'field.storage.node.localgov_services_parent',
]);
localgov_elections_optional_fields_settings($services);
}
}

/**
* Set form settings for optional services fields on installation.
*
* @param bool $services
* If localgov_services is (being) installed.
*/
function localgov_elections_optional_fields_settings($services): void {
$properties = [
'targetEntityType' => 'node',
'bundle' => 'localgov_election',
];
if ($form_displays = \Drupal::entityTypeManager()->getStorage('entity_form_display')->loadByProperties($properties)) {
foreach ($form_displays as $form_display) {
assert($form_display instanceof EntityFormDisplayInterface);
if ($services && !$form_display->getComponent('localgov_services_parent')) {
$form_display->setComponent('localgov_services_parent', [
'type' => 'entity_reference_autocomplete',
'region' => 'content',
'settings' => [
'match_operator' => 'CONTAINS',
'size' => '60',
'placeholder' => '',
'match_limit' => 10,
],
'weight' => -1,
])->save();
if (
($field_groups = $form_display->getThirdPartySettings('field_group')) &&
($group = $field_groups['group_description'] ?? FALSE)
) {
$group['children'][] = 'localgov_services_parent';
$form_display->setThirdPartySetting('field_group', 'group_description', $group)
->save();
}
}
}
}
}

/**
* Implements hook_entity_presave().
*/
Expand Down
100 changes: 100 additions & 0 deletions tests/src/Functional/PagesIntegrationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\localgov_elections\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait;
use Drupal\node\NodeInterface;

/**
* Tests pages working together with services.
*
* @group localgov_elections
*/
class PagesIntegrationTest extends BrowserTestBase {

use NodeCreationTrait;
use AssertBreadcrumbTrait;

protected $strictConfigSchema = FALSE;

/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';

/**
* A user with permission to bypass content access checks.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;

/**
* The node storage.
*
* @var \Drupal\node\NodeStorageInterface
*/
protected $nodeStorage;

/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'localgov_core',
'localgov_services_landing',
'localgov_services_sublanding',
'localgov_services_navigation',
'localgov_elections',
];

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->adminUser = $this->drupalCreateUser([
'bypass node access',
'administer nodes',
]);
$this->nodeStorage = $this->container->get('entity_type.manager')->getStorage('node');
}

/**
* Post overview into a service.
*/
public function testServicesIntegration() {
$landing = $this->createNode([
'title' => 'Landing Page 1',
'type' => 'localgov_services_landing',
'status' => NodeInterface::PUBLISHED,
]);
$sublanding = $this->createNode([
'title' => 'Sublanding 1',
'type' => 'localgov_services_sublanding',
'status' => NodeInterface::PUBLISHED,
'localgov_services_parent' => ['target_id' => $landing->id()],
]);

$this->drupalLogin($this->adminUser);
$this->drupalGet('node/add/localgov_election');
$form = $this->getSession()->getPage();
$form->fillField('edit-title-0-value', 'Election page 1');
// Required name="localgov_election_service_type[0][uri]" should be autopopulated.
$form->fillField('edit-localgov-services-parent-0-target-id', "Sublanding 1 ({$sublanding->id()})");
$form->checkField('edit-status-value');
$form->unCheckField('edit-localgov-election-display-map-value');
$form->pressButton('edit-submit');
$this->assertSession()->pageTextContains('Election page 1');
$trail = ['' => 'Home'];
$trail += ['landing-page-1' => 'Landing Page 1'];
$trail += ['landing-page-1/sublanding-1' => 'Sublanding 1'];
$this->assertBreadcrumb(NULL, $trail);
}

}
Loading