Skip to content

Commit

Permalink
Merge pull request #113 from discoverygarden/fix/suppressed-config
Browse files Browse the repository at this point in the history
DDST-376: Fix/suppress config
  • Loading branch information
chrismacdonaldw authored Jul 29, 2024
2 parents f0a0fc5 + 4d0730c commit bdfc107
Show file tree
Hide file tree
Showing 11 changed files with 202 additions and 40 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
"type": "drupal-module",
"license": "GPL-3.0-only",
"require": {
"openspout/openspout": "^3.4 || ^4",
"discoverygarden/dgi_migrate": "^1 || ^2 || ^3",
"drupal/migrate_plus": "^4.2 || ^5.1 || ^6",
"phpoffice/phpspreadsheet": "^1"
"openspout/openspout": "^3.4 || ^4",
"discoverygarden/dgi_migrate": "^1 || ^2 || ^3",
"drupal/migrate_plus": "^4.2 || ^5.1 || ^6",
"phpoffice/phpspreadsheet": "^1"
}
}
1 change: 1 addition & 0 deletions config/install/islandora_spreadsheet_ingest.settings.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
---
binary_directory_whitelist: []
schemes: []
5 changes: 5 additions & 0 deletions config/schema/islandora_spreadsheet_ingest.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ islandora_spreadsheet_ingest.settings:
label: 'List of allowed source directories.'
sequence:
type: string
schemes:
type: sequence
label: 'Allowed source URI schemes.'
sequence:
type: string

islandora_spreadsheet_ingest.request.*:
type: config_entity
Expand Down
30 changes: 16 additions & 14 deletions islandora_spreadsheet_ingest.module
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
*/

use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;

use Drupal\islandora_spreadsheet_ingest\RequestInterface;

/**
Expand All @@ -18,7 +19,7 @@ use Drupal\islandora_spreadsheet_ingest\RequestInterface;
* @param \Drupal\islandora_spreadsheet_ingest\RequestInterface $request
* The request of which to add the usage.
*/
function _islandora_spreadsheet_ingest_add_file_usage(RequestInterface $request) {
function _islandora_spreadsheet_ingest_add_file_usage(RequestInterface $request) : void {
$file_usage_service = \Drupal::service('file.usage');
$file_service = \Drupal::service('entity_type.manager')->getStorage('file');
$fids = $request->getSheet()['file'] ?? NULL;
Expand All @@ -42,7 +43,7 @@ function _islandora_spreadsheet_ingest_add_file_usage(RequestInterface $request)
* @param \Drupal\islandora_spreadsheet_ingest\RequestInterface $request
* The request of which to delete the usage.
*/
function _islandora_spreadsheet_ingest_delete_file_usage(RequestInterface $request) {
function _islandora_spreadsheet_ingest_delete_file_usage(RequestInterface $request) : void {
$file_usage_service = \Drupal::service('file.usage');
$file_service = \Drupal::service('entity_type.manager')->getStorage('file');
$fids = $request->getSheet()['file'];
Expand All @@ -63,7 +64,7 @@ function _islandora_spreadsheet_ingest_delete_file_usage(RequestInterface $reque
/**
* Implements hook_ENTITY_TYPE_insert().
*/
function islandora_spreadsheet_ingest_isi_request_insert(RequestInterface $request) {
function islandora_spreadsheet_ingest_isi_request_insert(RequestInterface $request) : void {
_islandora_spreadsheet_ingest_add_file_usage($request);

// If active, generate the migration group...
Expand All @@ -79,7 +80,7 @@ function islandora_spreadsheet_ingest_isi_request_insert(RequestInterface $reque
/**
* Implements hook_ENTITY_TYPE_update().
*/
function islandora_spreadsheet_ingest_isi_request_update(RequestInterface $request) {
function islandora_spreadsheet_ingest_isi_request_update(RequestInterface $request) : void {
_islandora_spreadsheet_ingest_delete_file_usage($request->original);
_islandora_spreadsheet_ingest_add_file_usage($request);

Expand All @@ -98,7 +99,7 @@ function islandora_spreadsheet_ingest_isi_request_update(RequestInterface $reque
/**
* Implements hook_ENTITY_TYPE_delete().
*/
function islandora_spreadsheet_ingest_isi_request_delete(RequestInterface $request) {
function islandora_spreadsheet_ingest_isi_request_delete(RequestInterface $request) : void {
_islandora_spreadsheet_ingest_delete_file_usage($request);

\Drupal::service('islandora_spreadsheet_ingest.migration_deriver')->deleteAll($request);
Expand All @@ -108,7 +109,7 @@ function islandora_spreadsheet_ingest_isi_request_delete(RequestInterface $reque
/**
* Implements hook_entity_operation().
*/
function islandora_spreadsheet_ingest_entity_operation(EntityInterface $entity) {
function islandora_spreadsheet_ingest_entity_operation(EntityInterface $entity) : array {
$ops = [];

if ($entity->getEntityType()->id() == 'isi_request') {
Expand Down Expand Up @@ -138,7 +139,7 @@ function islandora_spreadsheet_ingest_entity_operation(EntityInterface $entity)
* @see islandora_spreadsheet_ingest_migration_group_entity_access()
* @see islandora_spreadsheet_ingest_migration_entity_access()
*/
function _islandora_spreadsheet_ingest_migration_entity_helper(EntityInterface $entity, AccountInterface $account, $uid, $tags) {
function _islandora_spreadsheet_ingest_migration_entity_helper(EntityInterface $entity, AccountInterface $account, $uid, $tags) : AccessResultInterface {
return AccessResult::allowedIf(in_array('isi_derived_migration', $tags))
->addCacheableDependency($entity)
->andIf(
Expand All @@ -150,8 +151,8 @@ function _islandora_spreadsheet_ingest_migration_entity_helper(EntityInterface $
/**
* Implements hook_ENTITY_TYPE_entity_access() for migration_group entities.
*/
function islandora_spreadsheet_ingest_migration_group_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
$shared = $entity->shared_configuration ?? [];
function islandora_spreadsheet_ingest_migration_group_entity_access(EntityInterface $entity, $operation, AccountInterface $account) : AccessResultInterface {
$shared = $entity->get('shared_configuration') ?? [];
$tags = NestedArray::getValue($shared, ['migration_tags']) ?? [];
$uid = NestedArray::getValue($shared, ['source', 'isi', 'uid']) ?? FALSE;
return _islandora_spreadsheet_ingest_migration_entity_helper($entity, $account, $uid, $tags);
Expand All @@ -160,16 +161,17 @@ function islandora_spreadsheet_ingest_migration_group_entity_access(EntityInterf
/**
* Implements hook_ENTITY_TYPE_entity_access() for migration entities.
*/
function islandora_spreadsheet_ingest_migration_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
$tags = $entity->migration_tags ?? [];
$uid = NestedArray::getValue($entity->source, ['isi', 'uid']) ?? FALSE;
function islandora_spreadsheet_ingest_migration_entity_access(EntityInterface $entity, $operation, AccountInterface $account) : AccessResultInterface {
$tags = $entity->get('migration_tags') ?? [];
$source = $entity->get('source');
$uid = NestedArray::getValue($source, ['isi', 'uid']) ?? FALSE;
return _islandora_spreadsheet_ingest_migration_entity_helper($entity, $account, $uid, $tags);
}

/**
* Implements hook_migration_plugins_alter().
*/
function islandora_spreadsheet_ingest_migration_plugins_alter(&$definitions) {
function islandora_spreadsheet_ingest_migration_plugins_alter(&$definitions) : void {
$logger = \Drupal::logger('islandora_spreadsheet_ingest');
$logger->debug('Altering...');

Expand Down
18 changes: 18 additions & 0 deletions islandora_spreadsheet_ingest.post_update.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/**
* @file
* Post-update hooks.
*/

/**
* Set a value for islandora_spreadsheet_ingest.settings:schemes, if unset.
*/
function islandora_spreadsheet_ingest_post_update_set_default_schemes(&$sandbox) {
$config = \Drupal::configFactory()->getEditable('islandora_spreadsheet_ingest.settings');
if ($config->get('schemes') === NULL) {
$config->set('schemes', [])->save();
return \t('Set default value of an empty array for islandora_spreadsheet_ingest.settings:schemes.');
}
return \t('A value is already present for islandora_spreadsheet_ingest.settings:schemes.');
}
7 changes: 7 additions & 0 deletions islandora_spreadsheet_ingest.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ services:
arguments:
- isi_deferred_ingest
- true
islandora_spreadsheet_ingest.event_subscriber.config_transform:
class: Drupal\islandora_spreadsheet_ingest\EventSubscriber\ConfigTransformationEventSubscriber
factory: [null, 'create']
arguments:
- '@service_container'
tags:
- { name: event_subscriber }
2 changes: 2 additions & 0 deletions src/Controller/RequestListBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Link;
use Drupal\islandora_spreadsheet_ingest\Entity\Request;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
Expand Down Expand Up @@ -38,6 +39,7 @@ public function buildHeader() {
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
assert($entity instanceof Request);
$row['label'] = $entity->toLink(NULL, 'edit-form');
if (!$row['label']->getUrl()->access()) {
$row['label'] = $entity->label();
Expand Down
99 changes: 99 additions & 0 deletions src/EventSubscriber/ConfigTransformationEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Drupal\islandora_spreadsheet_ingest\EventSubscriber;

use Drupal\Core\Config\ConfigEvents;
use Drupal\Core\Config\StorageInterface;
use Drupal\Core\Config\StorageTransformEvent;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Config transformation event subscriber.
*
* Inspired by https://www.drupal.org/sandbox/ekes/3187856, which deals instead
* with "webform" entities.
*/
class ConfigTransformationEventSubscriber implements EventSubscriberInterface, ContainerInjectionInterface {

/**
* Constructor.
*/
public function __construct(
protected StorageInterface $activeStorage,
) {
// No-op.
}

/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.storage'),
);
}

/**
* {@inheritDoc}
*/
public static function getSubscribedEvents() : array {
return [
ConfigEvents::STORAGE_TRANSFORM_EXPORT => 'onExportTransform',
ConfigEvents::STORAGE_TRANSFORM_IMPORT => 'onImportTransform',
];
}

/**
* Config export event handler.
*
* @param \Drupal\Core\Config\StorageTransformEvent $event
* The event to which to respond.
*/
public function onExportTransform(StorageTransformEvent $event) : void {
$storage = $event->getStorage();
foreach ($this->toIgnore($storage) as $name) {
$storage->delete($name);
}
}

/**
* Config import event handler.
*
* @param \Drupal\Core\Config\StorageTransformEvent $event
* The event to which to respond.
*/
public function onImportTransform(StorageTransformEvent $event) : void {
$storage = $event->getStorage();

$inbound = iterator_to_array(static::toIgnore($storage), FALSE);
$current = iterator_to_array(static::toIgnore($this->activeStorage), FALSE);

// In case a config object escaped let's deal with it.
foreach (array_diff($inbound, $current) as $to_delete) {
$storage->delete($to_delete);
}

// Keep the current config as the current config.
foreach ($current as $to_maintain) {
$storage->write($to_maintain, $this->activeStorage->read($to_maintain));
}
}

/**
* Helper; yield all the configs that should not change on imports/exports.
*
* @param \Drupal\Core\Config\StorageInterface $storage
* The storage from which to enumerate configs.
*
* @return \Generator
* The names of the configs that should never be changed on imports/exports.
*/
protected static function toIgnore(StorageInterface $storage) : \Generator {
yield from $storage->listAll('migrate_plus.migration.isi__');
yield from $storage->listAll('migrate_plus.migration_group.isi__');
yield from $storage->listAll('islandora_spreadsheet_ingest.request.');
}

}
51 changes: 38 additions & 13 deletions src/Form/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Drupal\islandora_spreadsheet_ingest\Form;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
Expand All @@ -15,28 +15,54 @@
class Admin extends ConfigFormBase {

/**
* The StreamWrapperManager.
* Drupal's stream wrapper manager service..
*
* @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
*/
protected $streamWrapperManager;
protected StreamWrapperManagerInterface $streamWrapperManager;

/**
* {@inheritdoc}
* Drupal's module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
public function __construct(ConfigFactoryInterface $config_factory, StreamWrapperManagerInterface $stream_wrapper_manager) {
parent::__construct($config_factory);
$this->streamWrapperManager = $stream_wrapper_manager;
}
protected ModuleHandlerInterface $moduleHandler;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('config.factory'),
$container->get('stream_wrapper_manager')
);
return parent::create($container)
->setStreamWrapperManager($container->get('stream_wrapper_manager'))
->setModuleHandler($container->get('module_handler'));
}

/**
* Setter for the stream wrapper manager service.
*
* @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $streamWrapperManager
* The stream wrapper manager service to set.
*
* @return $this
* Fluent interface.
*/
public function setStreamWrapperManager(StreamWrapperManagerInterface $streamWrapperManager) : static {
$this->streamWrapperManager = $streamWrapperManager;
return $this;
}

/**
* Setter for the module handler service.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
* The module handler service to set.
*
* @return $this
* Fluent interface.
*/
public function setModuleHandler(ModuleHandlerInterface $moduleHandler) : static {
$this->moduleHandler = $moduleHandler;
return $this;
}

/**
Expand All @@ -50,7 +76,6 @@ public function getFormId() {
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form = [];
$config = $this->config('islandora_spreadsheet_ingest.settings');
$current_whitelist = $config->get('binary_directory_whitelist');
$form['schemes'] = [
Expand Down
Loading

0 comments on commit bdfc107

Please sign in to comment.