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

DDST-376: Fix/requests as content entities #112

Closed
wants to merge 17 commits into from
Closed
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: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ jobs:
uses: actions/checkout@v4
- name: Run CodeSniffer
uses: discoverygarden/CodeSniffer@v1
with:
phpcpd-exclude: islandora_spreadsheet_ingest.install
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: []
66 changes: 3 additions & 63 deletions config/schema/islandora_spreadsheet_ingest.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,68 +7,8 @@ islandora_spreadsheet_ingest.settings:
label: 'List of allowed source directories.'
sequence:
type: string

islandora_spreadsheet_ingest.request.*:
type: config_entity
mapping:
id:
type: string
label: ID
label:
type: label
label: Label
active:
type: boolean
label: Active
description: If this request should have migrations derived for it.
sheet:
type: mapping
mapping:
file:
# XXX: Really... kind of indicative that these should be pulled out
# to content entities instead of being config entities... the whole
# having-to-reference a file thing... because the "entity:file"
# thing does not appear to work...
#type: entity:file
type: ignore
label: The spreadsheet file to process.
sheet:
type: string
label: The worksheet of the file to process.
mappings:
schemes:
type: sequence
label: 'Allowed source URI schemes.'
sequence:
type: islandora_spreadsheet_ingest.migration_structure
owner:
# XXX: Further reinforcement that these should be pulled out to content
# entities instead of config, because we are relating to the user here.
#type: entity:user
type: ignore
label: The owner's ID; should be either an int or a string.
originalMapping:
type: string
label: |
Reference to the original mapping from which this request was derived.

islandora_spreadsheet_ingest.migration_structure:
type: mapping
mapping:
original_migration_id:
type: string
label: ID
mappings:
type: sequence
sequence:
type: mapping
mapping:
weight:
type: integer
pipeline:
type: sequence
sequence:
# XXX: The migrate plugin does not define schemas for its plugins,
# nor any obvious mechanism by which to make reference to them...
# migrate_plus _does_ roll a handful, describing the things from
# migrate; however, it is not complete, nor necessarily desirable.
# ... so let's just ignore, for now at least.
type: ignore
type: string
132 changes: 109 additions & 23 deletions islandora_spreadsheet_ingest.install
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,121 @@
* Installation hooks.
*/

use Drupal\Core\Entity\ContentEntityType;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\islandora_spreadsheet_ingest\Entity\Request;

/**
* Drop old, disused tables.
* Implements hook_update_last_removed().
*/
function islandora_spreadsheet_ingest_update_8201() {
$schema = \Drupal::service('database')->schema();

$tables = [
'islandora_spreadsheet_ingest_templates',
'islandora_spreadsheet_ingest_ingests',
];
array_map([$schema, 'dropTable'], $tables);

return t('Dropped tables.');
function islandora_spreadsheet_ingest_update_last_removed() : int {
return 9001;
}

/**
* Ensure the Islandora Spreadsheet Request ConfigEntityType is installed.
* Replace config entity with content entity.
*/
function islandora_spreadsheet_ingest_update_9001() {
function islandora_spreadsheet_ingest_update_9002() : void {
$upm = \Drupal::entityDefinitionUpdateManager();
$ent = $upm->getEntityType('isi_request');
if (is_null($ent)) {
$upm->installEntityType(\Drupal::entityTypeManager()
->getDefinition('isi_request')
);

return t('The "isi_request" config entity has been installed.');
}
else {
return t('The "isi_request" config entity is already installed.');
}
$upm->uninstallEntityType($upm->getEntityType('isi_request'));

// Much like general schema updates, this needs to include the definition
// inline to be installed, including field definitions, instead of dynamically
// lifting the definitions from the entity type manager in order to be able to
// reliably apply future updates.
$definition = new ContentEntityType([
"id" => "isi_request",
"label" => \t("Islandora Spreadsheet Ingest Request"),
"handlers" => [
"storage_schema" => "Drupal\islandora_spreadsheet_ingest\RequestStorageSchema",
"list_builder" => "Drupal\islandora_spreadsheet_ingest\Controller\RequestListBuilder",
"form" => [
"process" => "Drupal\islandora_spreadsheet_ingest\Form\Ingest\Review",
"add" => "Drupal\islandora_spreadsheet_ingest\Form\Ingest\FileUpload",
"delete" => "Drupal\islandora_spreadsheet_ingest\Form\RequestDeleteForm",
"edit" => "Drupal\islandora_spreadsheet_ingest\Form\Ingest\FileUpload",
"view" => "Drupal\islandora_spreadsheet_ingest\Form\Ingest\Review",
],
"access" => "Drupal\islandora_spreadsheet_ingest\RequestAccessControlHandler",
"view_builder" => "Drupal\islandora_spreadsheet_ingest\RequestViewBuilder",
],
"admin_permission" => "administer islandora_spreadsheet_ingest requests",
"base_table" => "islandora_spreadsheet_ingest_request",
"entity_keys" => [
"id" => "id",
"uuid" => "uuid",
"label" => "label",
"owner" => "uid",
],
"links" => [
"canonical" => "/admin/content/islandora_spreadsheet_ingest/{isi_request}",
"process-form" => "/admin/content/islandora_spreadsheet_ingest/{isi_request}/process",
"edit-form" => "/admin/content/islandora_spreadsheet_ingest/{isi_request}/edit",
"map-form" => "/admin/content/islandora_spreadsheet_ingest/{isi_request}/mapping",
"delete-form" => "/admin/content/islandora_spreadsheet_ingest/{isi_request}/delete",
],
]);
$fields = [];
$fields['uid'] = BaseFieldDefinition::create('entity_reference')
->setLabel(\t('User ID'))
->setSetting('target_type', 'user')
->setTranslatable(FALSE)
->setDefaultValueCallback(Request::class . '::getDefaultEntityOwner');
$fields['label'] = BaseFieldDefinition::create('string')
->setLabel(\t('Title'))
->setRequired(TRUE)
->setTranslatable(FALSE)
->setRevisionable(FALSE)
->setSetting('max_length', 255)
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'string',
'weight' => -5,
])
->setDisplayOptions('form', [
'type' => 'string_textfield',
'weight' => -5,
])
->setDisplayConfigurable('form', TRUE)
->setCardinality(1);
$fields['machine_name'] = BaseFieldDefinition::create('string')
->setLabel(\t('Machine Name'))
->setRequired(TRUE)
->setTranslatable(FALSE)
->setRevisionable(FALSE)
->setCardinality(1)
->addConstraint('UniqueField', [
'message' => 'The machine_name %value is already in use.',
]);
$fields['active'] = BaseFieldDefinition::create('boolean')
->setLabel(\t('Label'))
->setRequired(TRUE)
->setCardinality(1)
->setTranslatable(FALSE)
->setRevisionable(FALSE);
$fields['sheet_file'] = BaseFieldDefinition::create('entity_reference')
->setLabel(\t('Spreadsheet file'))
->setRequired(TRUE)
->setTranslatable(FALSE)
->setRevisionable(FALSE)
->setSetting('target_type', 'file')
->setDisplayOptions('form', [
'type' => 'file_generic',
])
->setCardinality(1);
$fields['sheet_sheet'] = BaseFieldDefinition::create('string')
->setLabel(\t('Worksheet'))
->setDescription(\t('The specific worksheet if the file corresponds to an ODS/XLSX which is possible of containing multiple.'))
->setCardinality(1);
$fields['mappings'] = BaseFieldDefinition::create('map')
->setLabel(\t('Mappings'))
->setDescription(\t('Migration mappings'))
->setCardinality(1);
$fields['original_mapping'] = BaseFieldDefinition::create('string')
->setLabel(\t('Original mapping'))
->setDescription(\t('Original migration template'))
->setCardinality(1);

$upm->installFieldableEntityType($definition, $fields);
}
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
Loading