Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dennis 8.x 1.x #1

Open
wants to merge 6 commits into
base: 8.x-1.x
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion domain/domain.module
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ function domain_confirm_fields($entity_type, $bundle, $handler = 'default:domain
'id' => $storage_id,
'field_name' => DOMAIN_ADMIN_FIELD,
'type' => 'entity_reference',
'dependencies' => 'domain' , 'user',
'dependencies' => array('domain' , 'user'),
'entity_type' => 'user',
'cardinality' => -1,
'module' => 'entity_reference',
Expand Down
1 change: 1 addition & 0 deletions domain_access/domain_access.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ package: Domain
version: VERSION
core: 8.x
dependencies:
- node
- domain
9 changes: 9 additions & 0 deletions domain_config_ui/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Domain Config UI
================

This module allows configuration to be saved for a selected domain.

Dependencies
============

- Domain Config
8 changes: 8 additions & 0 deletions domain_config_ui/domain_config_ui.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
name: Domain Configuration UI
description: 'Allows saving of domain specific configuration through the UI.'
type: module
package: Domain
version: VERSION
core: 8.x
dependencies:
- domain_config
166 changes: 166 additions & 0 deletions domain_config_ui/domain_config_ui.module
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
<?php
/**
* @file
* Allows saving of domain specific configuration through the UI.
*/

use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Component\Render\FormattableMarkup;

/**
* Implements hook_preprocess_page().
*/
function domain_config_ui_preprocess_page(&$variables) {
if (!domain_config_ui_route_is_admin() || !domain_config_ui_route_is_allowed()) {
return;
}

// Add domain switch form that will reload the page to top of content region.
$form = \Drupal::formBuilder()->getForm('Drupal\domain_config_ui\Form\SwitchForm');
$content = [
'domain_config_ui_switch' => $form,
];

$variables['page']['content'] = array_merge($content, $variables['page']['content']);

// Add a message below the form to remind the administrator which domain they are currently configuring.
if ($warning_message = domain_config_ui_save_warning_message()) {
$variables['page']['content']['domain_config_ui_switch_warning'] = $warning_message;
}
}

/**
* Implements hook_form_alter().
*
* @param array $form
* @param FormStateInterface $form_state
*/
function domain_config_ui_form_alter(&$form, FormStateInterface $form_state) {
// Only alter config forms that can have a config factory and are on an admin path.
if (!domain_config_ui_route_is_admin() || !domain_config_ui_form_is_allowed($form)) {
return;
}

// Create fieldset to group domain fields.
$form['domain_config_ui'] = [
'#type' => 'fieldset',
'#title' => 'Domain Configuration',
'#weight' => -10,
];

// Add domain switch select field.
$selected_domain = \Drupal::service('domain_config_ui.manager')->getSelectedDomain();
$form['domain_config_ui']['config_save_domain'] = [
'#type' => 'select',
'#title' => 'Domain',
'#options' => array_merge(['' => 'All Domains'], \Drupal::service('domain.loader')->loadOptionsList()),
'#default_value' => $selected_domain ? $selected_domain->id() : '',
'#ajax' => [
'callback' => 'domain_config_ui_domain_switch_form_callback',
],
];

// Add language select field.
$selected_language = \Drupal::service('domain_config_ui.manager')->getSelectedLanguage();
$language_options = ['' => 'Default'];
foreach (\Drupal::languageManager()->getLanguages() as $id => $language) {
$language_options[$id] = $language->getName();
}
$form['domain_config_ui']['config_save_language'] = [
'#type' => 'select',
'#title' => 'Language',
'#options' => $language_options,
'#default_value' => $selected_language ? $selected_language->getId() : '',
'#ajax' => [
'callback' => 'domain_config_ui_domain_switch_form_callback',
],
];

// Add a message below the form to remind the administrator which domain they are currently configuring.
if ($warning_message = domain_config_ui_save_warning_message()) {
$form['domain_message'] = $warning_message;
}
}

/**
* Helper to generate the markup for the domain save warning message.
*/
function domain_config_ui_save_warning_message() {
$selected_domain = \Drupal::service('domain_config_ui.manager')->getSelectedDomain();
if ($selected_domain) {
$selected_language = \Drupal::service('domain_config_ui.manager')->getSelectedLanguage();
$message = new TranslatableMarkup('Configuration will be saved for @domain @language', [
'@domain' => $selected_domain->label(),
'@language' => $selected_language ? '(' . $selected_language->getName() . ')' : '',
]);
return [
'#markup' => new FormattableMarkup('<div class="messages messages--warning">@message</div>', [
'@message' => $message,
]),
'#weight' => 1000,
];
}
}

/**
* Checks if provided form can be used to save domain specic configuration.
*
* @param array $form
* @return boolean
*/
function domain_config_ui_form_is_allowed($form) {
$allowed = [
'system_site_information_settings',
'system_theme_settings',
];
\Drupal::moduleHandler()->alter('domain_config_form_allowed', $allowed);
return in_array($form['#form_id'], $allowed);
}

/**
* Checks if provided path should have a domain switch form added to the top of the page.
*
* @return boolean
*/
function domain_config_ui_route_is_allowed() {
$allowed = [
'/admin/appearance',
];
\Drupal::moduleHandler()->alter('domain_config_route_allowed', $allowed);
$route = \Drupal::routeMatch()->getRouteObject();
return in_array($route->getPath(), $allowed);
}

/**
* Checks if route is admin.
*
* @return boolean
*/
function domain_config_ui_route_is_admin() {
$route = \Drupal::routeMatch()->getRouteObject();
return \Drupal::service('router.admin_context')->isAdminRoute($route);
}

/**
* AJAX callback to set the current domain.
*
* @param array $form
* @param FormStateInterface $form_state
*/
function domain_config_ui_domain_switch_form_callback($form, FormStateInterface $form_state) {
// Switch the current domain.
\Drupal::service('domain_config_ui.manager')->setSelectedDomain($form_state->getValue('config_save_domain'));

// Switch the current language.
\Drupal::service('domain_config_ui.manager')->setSelectedLanguage($form_state->getValue('config_save_language'));

// Reset form with selected domain configuration.
$form_state->setUserInput([]);
$new_form = \Drupal::formBuilder()->rebuildForm($form['#form_id'], $form_state, $form);
$response = new AjaxResponse();
$response->addCommand(new ReplaceCommand('.' . str_replace('_', '-', $form['#form_id']), $new_form));
return $response;
}
16 changes: 16 additions & 0 deletions domain_config_ui/domain_config_ui.services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
domain_config_ui.manager:
class: Drupal\domain_config_ui\DomainConfigUIManager
arguments: ['@config.storage', '@domain.loader', '@language_manager']

domain_config_ui.factory:
class: Drupal\domain_config_ui\Config\ConfigFactory
tags:
- { name: event_subscriber }
- { name: service_collector, tag: 'config.factory.override', call: addOverride }
arguments: ['@config.storage', '@event_dispatcher', '@config.typed']
calls:
- [setDomainConfigUIManager, ['@domain_config_ui.manager']]

config.factory:
alias: domain_config_ui.factory
68 changes: 68 additions & 0 deletions domain_config_ui/src/Config/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace Drupal\domain_config_ui\Config;

use Drupal\Core\Config\Config as CoreConfig;
use Drupal\domain_config_ui\DomainConfigUIManager;

/**
* Extend core Config class to save domain specific configuration.
*/
class Config extends CoreConfig {
/**
* The Domain config UI manager.
*
* @var DomainConfigUIManager
*/
protected $domainConfigUIManager;

/**
* Set the Domain config UI manager.
* @param DomainConfigUIManager $domain_config_ui_manager
*/
public function setDomainConfigUIManager($domain_config_ui_manager) {
$this->domainConfigUIManager = $domain_config_ui_manager;
}

/**
* {@inheritdoc}
*/
public function save($has_trusted_data = FALSE) {
// Remember original config name.
$originalName = $this->name;

try {
// Get domain config name for saving.
$domainConfigName = $this->getDomainConfigName();

// If config is new and we are currently saving domain specific configuration,
// save with original name first so that there is always a default configuration.
if ($this->isNew && $domainConfigName != $originalName) {
parent::save($has_trusted_data);
}

// Switch to use domain config name and save.
$this->name = $domainConfigName;
parent::save($has_trusted_data);
}
catch (\Exception $e) {
// Reset back to original config name if save fails and re-throw.
$this->name = $originalName;
throw $e;
}

// Reset back to original config name after saving.
$this->name = $originalName;

return $this;
}

/**
* Get the domain config name.
*/
protected function getDomainConfigName() {
// Return selected config name.
return $this->domainConfigUIManager->getSelectedConfigName($this->name);
}

}
Loading